ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/raytrace.c
(Generate patch)

Comparing ray/src/rt/raytrace.c (file contents):
Revision 1.3 by greg, Wed Apr 19 22:24:28 1989 UTC vs.
Revision 1.12 by greg, Sat Dec 15 15:03:32 1990 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1986 Regents of the University of California */
1 > /* Copyright (c) 1990 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 22 | Line 22 | extern double  minweight;              /* minimum ray weight */
22  
23   long  nrays = 0L;                       /* number of rays traced */
24  
25 < #define  MAXLOOP        32              /* modifier loop detection */
25 > #define  MAXLOOP        128             /* modifier loop detection */
26  
27   #define  RAYHIT         (-1)            /* return value for intercepted ray */
28  
# Line 56 | Line 56 | double  rw;
56          r->newcset = r->clipset;
57          r->ro = NULL;
58          r->rot = FHUGE;
59 +        r->rox = NULL;
60          r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
61          setcolor(r->pcol, 1.0, 1.0, 1.0);
62          setcolor(r->rcol, 0.0, 0.0, 0.0);
63 +        r->rt = 0.0;
64          return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1);
65   }
66  
67  
68   rayvalue(r)                     /* compute a ray's value */
69 < register RAY  *r;
69 > RAY  *r;
70   {
71          extern int  (*trace)();
72  
73 <        if (localhit(r, &thescene))
74 <                if (r->clipset != NULL && inset(r->clipset, r->ro->omod))
73 <                        raytrans(r);            /* object is clipped */
74 <                else
75 <                        rayshade(r, r->ro->omod);
76 <        else if (sourcehit(r))
77 <                rayshade(r, r->ro->omod);
73 >        if (localhit(r, &thescene) || sourcehit(r))
74 >                raycont(r);
75  
76          if (trace != NULL)
77                  (*trace)(r);            /* trace execution */
78   }
79  
80  
81 + raycont(r)                      /* check for clipped object and continue */
82 + register RAY  *r;
83 + {
84 +        if (r->clipset != NULL && inset(r->clipset, r->ro->omod))
85 +                raytrans(r);
86 +        else
87 +                rayshade(r, r->ro->omod);
88 + }
89 +
90 +
91   raytrans(r)                     /* transmit ray as is */
92 < RAY  *r;
92 > register RAY  *r;
93   {
94          RAY  tr;
95  
# Line 90 | Line 97 | RAY  *r;
97                  VCOPY(tr.rdir, r->rdir);
98                  rayvalue(&tr);
99                  copycolor(r->rcol, tr.rcol);
100 +                r->rt = r->rot + tr.rt;
101          }
102   }
103  
# Line 102 | Line 110 | int  mod;
110          register OBJREC  *m;
111                                          /* check for infinite loop */
112          if (depth++ >= MAXLOOP)
113 <                objerror(r->ro, USER, "material loop");
113 >                objerror(r->ro, USER, "possible modifier loop");
114          for ( ; mod != OVOID; mod = m->omod) {
115                  m = objptr(mod);
116 +                /****** unnecessary test since modifier() is always called
117                  if (!ismodifier(m->otype)) {
118                          sprintf(errmsg, "illegal modifier \"%s\"", m->oname);
119                          error(USER, errmsg);
120                  }
121 +                ******/
122                  (*ofun[m->otype].funp)(m, r);   /* execute function */
123                  m->lastrno = r->rno;
124                  if (ismaterial(m->otype)) {     /* materials call raytexture */
# Line 203 | Line 213 | register RAY  *r;
213           *  still fraught with problems since reflected rays and similar
214           *  directions calculated from the surface normal may spawn rays behind
215           *  the surface.  The only solution is to curb textures at high
216 <         *  incidence (Rdot << 1).
216 >         *  incidence (namely, keep DOT(rdir,pert) < Rdot).
217           */
218  
219          for (i = 0; i < 3; i++)
# Line 224 | Line 234 | register RAY  *r;
234   }
235  
236  
237 + newrayxf(r)                     /* get new tranformation matrix for ray */
238 + RAY  *r;
239 + {
240 +        static struct xfn {
241 +                struct xfn  *next;
242 +                FULLXF  xf;
243 +        }  xfseed = { &xfseed }, *xflast = &xfseed;
244 +        register struct xfn  *xp;
245 +        register RAY  *rp;
246 +
247 +        /*
248 +         * Search for transform in circular list that
249 +         * has no associated ray in the tree.
250 +         */
251 +        xp = xflast;
252 +        for (rp = r->parent; rp != NULL; rp = rp->parent)
253 +                if (rp->rox == &xp->xf) {               /* xp in use */
254 +                        xp = xp->next;                  /* move to next */
255 +                        if (xp == xflast) {             /* need new one */
256 +                                xp = (struct xfn *)malloc(sizeof(struct xfn));
257 +                                if (xp == NULL)
258 +                                        error(SYSTEM,
259 +                                                "out of memory in newrayxf");
260 +                                                        /* insert in list */
261 +                                xp->next = xflast->next;
262 +                                xflast->next = xp;
263 +                                break;                  /* we're done */
264 +                        }
265 +                        rp = r;                 /* start check over */
266 +                }
267 +                                        /* got it */
268 +        r->rox = &xp->xf;
269 +        xflast = xp;
270 + }
271 +
272 +
273   flipsurface(r)                  /* reverse surface orientation */
274   register RAY  *r;
275   {
# Line 242 | Line 288 | register RAY  *r;
288   register CUBE  *scene;
289   {
290          FVECT  curpos;                  /* current cube position */
291 <        int  mpos, mneg;                /* sign flags */
291 >        int  sflags;                    /* sign flags */
292          double  t, dt;
293          register int  i;
294  
295          nrays++;                        /* increment trace counter */
296  
297 <        mpos = mneg = 0;
297 >        sflags = 0;
298          for (i = 0; i < 3; i++) {
299                  curpos[i] = r->rorg[i];
300                  if (r->rdir[i] > FTINY)
301 <                        mpos |= 1 << i;
301 >                        sflags |= 1 << i;
302                  else if (r->rdir[i] < -FTINY)
303 <                        mneg |= 1 << i;
303 >                        sflags |= 0x10 << i;
304          }
305          t = 0.0;
306          if (!incube(scene, curpos)) {
307                                          /* find distance to entry */
308                  for (i = 0; i < 3; i++) {
309                                          /* plane in our direction */
310 <                        if (mpos & 1<<i)
310 >                        if (sflags & 1<<i)
311                                  dt = scene->cuorg[i];
312 <                        else if (mneg & 1<<i)
312 >                        else if (sflags & 0x10<<i)
313                                  dt = scene->cuorg[i] + scene->cusize;
314                          else
315                                  continue;
# Line 280 | Line 326 | register CUBE  *scene;
326                  if (!incube(scene, curpos))     /* non-intersecting ray */
327                          return(0);
328          }
329 <        return(raymove(curpos, mpos, mneg, r, scene) == RAYHIT);
329 >        return(raymove(curpos, sflags, r, scene) == RAYHIT);
330   }
331  
332  
333   static int
334 < raymove(pos, plus, minus, r, cu)        /* check for hit as we move */
334 > raymove(pos, dirf, r, cu)               /* check for hit as we move */
335   FVECT  pos;                     /* modified */
336 < int  plus, minus;               /* direction indicators to speed tests */
336 > int  dirf;                      /* direction indicators to speed tests */
337   register RAY  *r;
338   register CUBE  *cu;
339   {
340          int  ax;
341          double  dt, t;
296        register int  sgn;
342  
343          if (istree(cu->cutree)) {               /* recurse on subcubes */
344                  CUBE  cukid;
345 <                register int  br;
345 >                register int  br, sgn;
346  
347                  cukid.cusize = cu->cusize * 0.5;        /* find subcube */
348                  VCOPY(cukid.cuorg, cu->cuorg);
# Line 316 | Line 361 | register CUBE  *cu;
361                  }
362                  for ( ; ; ) {
363                          cukid.cutree = octkid(cu->cutree, br);
364 <                        if ((ax = raymove(pos,plus,minus,r,&cukid)) == RAYHIT)
364 >                        if ((ax = raymove(pos,dirf,r,&cukid)) == RAYHIT)
365                                  return(RAYHIT);
366                          sgn = 1 << ax;
367 <                        if (sgn & minus)                /* negative axis? */
323 <                                if (sgn & br) {
324 <                                        cukid.cuorg[ax] -= cukid.cusize;
325 <                                        br &= ~sgn;
326 <                                } else
327 <                                        return(ax);     /* underflow */
328 <                        else
367 >                        if (sgn & dirf)                 /* positive axis? */
368                                  if (sgn & br)
369                                          return(ax);     /* overflow */
370                                  else {
371                                          cukid.cuorg[ax] += cukid.cusize;
372                                          br |= sgn;
373                                  }
374 +                        else
375 +                                if (sgn & br) {
376 +                                        cukid.cuorg[ax] -= cukid.cusize;
377 +                                        br &= ~sgn;
378 +                                } else
379 +                                        return(ax);     /* underflow */
380                  }
381                  /*NOTREACHED*/
382          }
383          if (isfull(cu->cutree) && checkhit(r, cu))
384                  return(RAYHIT);
385                                          /* advance to next cube */
386 <        sgn = plus | minus;
387 <        if (sgn&1) {
343 <                dt = plus&1 ? cu->cuorg[0] + cu->cusize : cu->cuorg[0];
386 >        if (dirf&0x11) {
387 >                dt = dirf&1 ? cu->cuorg[0] + cu->cusize : cu->cuorg[0];
388                  t = (dt - pos[0])/r->rdir[0];
389                  ax = 0;
390          } else
391                  t = FHUGE;
392 <        if (sgn&2) {
393 <                dt = plus&2 ? cu->cuorg[1] + cu->cusize : cu->cuorg[1];
392 >        if (dirf&0x22) {
393 >                dt = dirf&2 ? cu->cuorg[1] + cu->cusize : cu->cuorg[1];
394                  dt = (dt - pos[1])/r->rdir[1];
395                  if (dt < t) {
396                          t = dt;
397                          ax = 1;
398                  }
399          }
400 <        if (sgn&4) {
401 <                dt = plus&4 ? cu->cuorg[2] + cu->cusize : cu->cuorg[2];
400 >        if (dirf&0x44) {
401 >                dt = dirf&4 ? cu->cuorg[2] + cu->cusize : cu->cuorg[2];
402                  dt = (dt - pos[2])/r->rdir[2];
403                  if (dt < t) {
404                          t = dt;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines