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.1 by greg, Thu Feb 2 10:41:34 1989 UTC vs.
Revision 2.3 by greg, Tue Jan 26 09:28:00 1993 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1986 Regents of the University of California */
1 > /* Copyright (c) 1991 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 16 | Line 16 | static char SCCSid[] = "$SunId$ LBL";
16  
17   #include  "otypes.h"
18  
19 + #include  "otspecial.h"
20 +
21 + #define  MAXCSET        ((MAXSET+1)*2-1)        /* maximum check set size */
22 +
23   extern CUBE  thescene;                  /* our scene */
24   extern int  maxdepth;                   /* maximum recursion depth */
25   extern double  minweight;               /* minimum ray weight */
26 + extern int  do_irrad;                   /* compute irradiance? */
27  
28 < long  nrays = 0L;                       /* number of rays traced */
28 > long  raynum = 0L;                      /* next unique ray number */
29 > long  nrays = 0L;                       /* number of calls to localhit */
30  
31 < #define  MAXLOOP        32              /* modifier loop detection */
31 > static FLOAT  Lambfa[5] = {PI, PI, PI, 0.0, 0.0};
32 > OBJREC  Lamb = {
33 >        OVOID, MAT_PLASTIC, "Lambertian",
34 >        {0, 5, NULL, Lambfa}, NULL,
35 > };                                      /* a Lambertian surface */
36  
37 + #define  MAXLOOP        128             /* modifier loop detection */
38 +
39   #define  RAYHIT         (-1)            /* return value for intercepted ray */
40  
41  
# Line 38 | Line 50 | double  rw;
50                  r->crtype = r->rtype = rt;
51                  r->rsrc = -1;
52                  r->clipset = NULL;
53 +                r->revf = raytrace;
54          } else {                                /* spawned ray */
55                  r->rlvl = ro->rlvl;
56                  if (rt & RAYREFL) {
# Line 48 | Line 61 | double  rw;
61                          r->rsrc = ro->rsrc;
62                          r->clipset = ro->newcset;
63                  }
64 +                r->revf = ro->revf;
65                  r->rweight = ro->rweight * rw;
66                  r->crtype = ro->crtype | (r->rtype = rt);
67                  VCOPY(r->rorg, ro->rop);
68          }
69 <        r->rno = nrays;
69 >        rayclear(r);
70 >        return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1);
71 > }
72 >
73 >
74 > rayclear(r)                     /* clear a ray for (re)evaluation */
75 > register RAY  *r;
76 > {
77 >        r->rno = raynum++;
78          r->newcset = r->clipset;
79          r->ro = NULL;
80          r->rot = FHUGE;
59        r->ros = 1.0; setident4(r->rox);
81          r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
82          setcolor(r->pcol, 1.0, 1.0, 1.0);
83          setcolor(r->rcol, 0.0, 0.0, 0.0);
84 <        return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1);
84 >        r->rt = 0.0;
85   }
86  
87  
88 < rayvalue(r)                     /* compute a ray's value */
89 < register RAY  *r;
88 > raytrace(r)                     /* trace a ray and compute its value */
89 > RAY  *r;
90   {
91          extern int  (*trace)();
92  
93          if (localhit(r, &thescene))
94 <                if (r->clipset != NULL && inset(r->clipset, r->ro->omod))
74 <                        raytrans(r);            /* object is clipped */
75 <                else
76 <                        rayshade(r, r->ro->omod);
94 >                raycont(r);
95          else if (sourcehit(r))
96                  rayshade(r, r->ro->omod);
97  
# Line 82 | Line 100 | register RAY  *r;
100   }
101  
102  
103 + raycont(r)                      /* check for clipped object and continue */
104 + register RAY  *r;
105 + {
106 +        if (r->clipset != NULL && inset(r->clipset, r->ro->omod))
107 +                raytrans(r);
108 +        else
109 +                rayshade(r, r->ro->omod);
110 + }
111 +
112 +
113   raytrans(r)                     /* transmit ray as is */
114 < RAY  *r;
114 > register RAY  *r;
115   {
116          RAY  tr;
117  
# Line 91 | Line 119 | RAY  *r;
119                  VCOPY(tr.rdir, r->rdir);
120                  rayvalue(&tr);
121                  copycolor(r->rcol, tr.rcol);
122 +                r->rt = r->rot + tr.rt;
123          }
124   }
125  
# Line 103 | Line 132 | int  mod;
132          register OBJREC  *m;
133                                          /* check for infinite loop */
134          if (depth++ >= MAXLOOP)
135 <                objerror(r->ro, USER, "material loop");
135 >                objerror(r->ro, USER, "possible modifier loop");
136 >        r->rt = r->rot;                 /* set effective ray length */
137          for ( ; mod != OVOID; mod = m->omod) {
138                  m = objptr(mod);
139 +                /****** unnecessary test since modifier() is always called
140                  if (!ismodifier(m->otype)) {
141                          sprintf(errmsg, "illegal modifier \"%s\"", m->oname);
142                          error(USER, errmsg);
143                  }
144 +                ******/
145 +                                        /* hack for irradiance calculation */
146 +                if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS))) {
147 +                        if (irr_ignore(m->otype)) {
148 +                                depth--;
149 +                                raytrans(r);
150 +                                return;
151 +                        }
152 +                        if (!islight(m->otype))
153 +                                m = &Lamb;
154 +                }
155                  (*ofun[m->otype].funp)(m, r);   /* execute function */
114                m->lastrno = r->rno;
156                  if (ismaterial(m->otype)) {     /* materials call raytexture */
157                          depth--;
158                          return;         /* we're done */
# Line 138 | Line 179 | int  mod;
179                          error(USER, errmsg);
180                  }
181                  (*ofun[m->otype].funp)(m, r);
141                m->lastrno = r->rno;
182          }
183          depth--;                        /* end here */
184   }
# Line 204 | Line 244 | register RAY  *r;
244           *  still fraught with problems since reflected rays and similar
245           *  directions calculated from the surface normal may spawn rays behind
246           *  the surface.  The only solution is to curb textures at high
247 <         *  incidence (Rdot << 1).
247 >         *  incidence (namely, keep DOT(rdir,pert) < Rdot).
248           */
249  
250          for (i = 0; i < 3; i++)
# Line 225 | Line 265 | register RAY  *r;
265   }
266  
267  
268 + newrayxf(r)                     /* get new tranformation matrix for ray */
269 + RAY  *r;
270 + {
271 +        static struct xfn {
272 +                struct xfn  *next;
273 +                FULLXF  xf;
274 +        }  xfseed = { &xfseed }, *xflast = &xfseed;
275 +        register struct xfn  *xp;
276 +        register RAY  *rp;
277 +
278 +        /*
279 +         * Search for transform in circular list that
280 +         * has no associated ray in the tree.
281 +         */
282 +        xp = xflast;
283 +        for (rp = r->parent; rp != NULL; rp = rp->parent)
284 +                if (rp->rox == &xp->xf) {               /* xp in use */
285 +                        xp = xp->next;                  /* move to next */
286 +                        if (xp == xflast) {             /* need new one */
287 +                                xp = (struct xfn *)bmalloc(sizeof(struct xfn));
288 +                                if (xp == NULL)
289 +                                        error(SYSTEM,
290 +                                                "out of memory in newrayxf");
291 +                                                        /* insert in list */
292 +                                xp->next = xflast->next;
293 +                                xflast->next = xp;
294 +                                break;                  /* we're done */
295 +                        }
296 +                        rp = r;                 /* start check over */
297 +                }
298 +                                        /* got it */
299 +        r->rox = &xp->xf;
300 +        xflast = xp;
301 + }
302 +
303 +
304   flipsurface(r)                  /* reverse surface orientation */
305   register RAY  *r;
306   {
# Line 242 | Line 318 | localhit(r, scene)             /* check for hit in the octree */
318   register RAY  *r;
319   register CUBE  *scene;
320   {
321 +        OBJECT  cxset[MAXCSET+1];       /* set of checked objects */
322          FVECT  curpos;                  /* current cube position */
323 <        int  mpos, mneg;                /* sign flags */
323 >        int  sflags;                    /* sign flags */
324          double  t, dt;
325          register int  i;
326  
327          nrays++;                        /* increment trace counter */
328 <
252 <        mpos = mneg = 0;
328 >        sflags = 0;
329          for (i = 0; i < 3; i++) {
330                  curpos[i] = r->rorg[i];
331                  if (r->rdir[i] > FTINY)
332 <                        mpos |= 1 << i;
332 >                        sflags |= 1 << i;
333                  else if (r->rdir[i] < -FTINY)
334 <                        mneg |= 1 << i;
334 >                        sflags |= 0x10 << i;
335          }
336 +        if (sflags == 0)
337 +                error(CONSISTENCY, "zero ray direction in localhit");
338          t = 0.0;
339          if (!incube(scene, curpos)) {
340                                          /* find distance to entry */
341                  for (i = 0; i < 3; i++) {
342                                          /* plane in our direction */
343 <                        if (mpos & 1<<i)
343 >                        if (sflags & 1<<i)
344                                  dt = scene->cuorg[i];
345 <                        else if (mneg & 1<<i)
345 >                        else if (sflags & 0x10<<i)
346                                  dt = scene->cuorg[i] + scene->cusize;
347                          else
348                                  continue;
# Line 281 | Line 359 | register CUBE  *scene;
359                  if (!incube(scene, curpos))     /* non-intersecting ray */
360                          return(0);
361          }
362 <        return(raymove(curpos, mpos, mneg, r, scene) == RAYHIT);
362 >        cxset[0] = 0;
363 >        return(raymove(curpos, cxset, sflags, r, scene) == RAYHIT);
364   }
365  
366  
367   static int
368 < raymove(pos, plus, minus, r, cu)        /* check for hit as we move */
369 < FVECT  pos;                     /* modified */
370 < int  plus, minus;               /* direction indicators to speed tests */
368 > raymove(pos, cxs, dirf, r, cu)          /* check for hit as we move */
369 > FVECT  pos;                     /* current position, modified herein */
370 > OBJECT  *cxs;                   /* checked objects, modified by checkhit */
371 > int  dirf;                      /* direction indicators to speed tests */
372   register RAY  *r;
373   register CUBE  *cu;
374   {
375          int  ax;
376          double  dt, t;
297        register int  sgn;
377  
378          if (istree(cu->cutree)) {               /* recurse on subcubes */
379                  CUBE  cukid;
380 <                register int  br;
380 >                register int  br, sgn;
381  
382                  cukid.cusize = cu->cusize * 0.5;        /* find subcube */
383                  VCOPY(cukid.cuorg, cu->cuorg);
# Line 317 | Line 396 | register CUBE  *cu;
396                  }
397                  for ( ; ; ) {
398                          cukid.cutree = octkid(cu->cutree, br);
399 <                        if ((ax = raymove(pos,plus,minus,r,&cukid)) == RAYHIT)
399 >                        if ((ax = raymove(pos,cxs,dirf,r,&cukid)) == RAYHIT)
400                                  return(RAYHIT);
401                          sgn = 1 << ax;
402 <                        if (sgn & minus)                /* negative axis? */
324 <                                if (sgn & br) {
325 <                                        cukid.cuorg[ax] -= cukid.cusize;
326 <                                        br &= ~sgn;
327 <                                } else
328 <                                        return(ax);     /* underflow */
329 <                        else
402 >                        if (sgn & dirf)                 /* positive axis? */
403                                  if (sgn & br)
404                                          return(ax);     /* overflow */
405                                  else {
406                                          cukid.cuorg[ax] += cukid.cusize;
407                                          br |= sgn;
408                                  }
409 +                        else
410 +                                if (sgn & br) {
411 +                                        cukid.cuorg[ax] -= cukid.cusize;
412 +                                        br &= ~sgn;
413 +                                } else
414 +                                        return(ax);     /* underflow */
415                  }
416                  /*NOTREACHED*/
417          }
418 <        if (isfull(cu->cutree) && checkhit(r, cu))
418 >        if (isfull(cu->cutree) && checkhit(r, cu, cxs))
419                  return(RAYHIT);
420                                          /* advance to next cube */
421 <        sgn = plus | minus;
422 <        if (sgn&1) {
344 <                dt = plus&1 ? cu->cuorg[0] + cu->cusize : cu->cuorg[0];
421 >        if (dirf&0x11) {
422 >                dt = dirf&1 ? cu->cuorg[0] + cu->cusize : cu->cuorg[0];
423                  t = (dt - pos[0])/r->rdir[0];
424                  ax = 0;
425          } else
426                  t = FHUGE;
427 <        if (sgn&2) {
428 <                dt = plus&2 ? cu->cuorg[1] + cu->cusize : cu->cuorg[1];
427 >        if (dirf&0x22) {
428 >                dt = dirf&2 ? cu->cuorg[1] + cu->cusize : cu->cuorg[1];
429                  dt = (dt - pos[1])/r->rdir[1];
430                  if (dt < t) {
431                          t = dt;
432                          ax = 1;
433                  }
434          }
435 <        if (sgn&4) {
436 <                dt = plus&4 ? cu->cuorg[2] + cu->cusize : cu->cuorg[2];
435 >        if (dirf&0x44) {
436 >                dt = dirf&4 ? cu->cuorg[2] + cu->cusize : cu->cuorg[2];
437                  dt = (dt - pos[2])/r->rdir[2];
438                  if (dt < t) {
439                          t = dt;
# Line 370 | Line 448 | register CUBE  *cu;
448  
449  
450   static
451 < checkhit(r, cu)                 /* check for hit in full cube */
451 > checkhit(r, cu, cxs)            /* check for hit in full cube */
452   register RAY  *r;
453   CUBE  *cu;
454 + OBJECT  *cxs;
455   {
456          OBJECT  oset[MAXSET+1];
457          register OBJREC  *o;
458          register int  i;
459  
460          objset(oset, cu->cutree);
461 +        checkset(oset, cxs);                    /* eliminate double-checking */
462          for (i = oset[0]; i > 0; i--) {
463                  o = objptr(oset[i]);
384                if (o->lastrno == r->rno)               /* checked already? */
385                        continue;
464                  (*ofun[o->otype].funp)(o, r);
387                o->lastrno = r->rno;
465          }
466          if (r->ro == NULL)
467                  return(0);                      /* no scores yet */
468  
469          return(incube(cu, r->rop));             /* hit OK if in current cube */
470 + }
471 +
472 +
473 + static
474 + checkset(os, cs)                /* modify checked set and set to check */
475 + register OBJECT  *os;                   /* os' = os - cs */
476 + register OBJECT  *cs;                   /* cs' = cs + os */
477 + {
478 +        OBJECT  cset[MAXCSET+MAXSET+1];
479 +        register int  i, j;
480 +        int  k;
481 +                                        /* copy os in place, cset <- cs */
482 +        cset[0] = 0;
483 +        k = 0;
484 +        for (i = j = 1; i <= os[0]; i++) {
485 +                while (j <= cs[0] && cs[j] < os[i])
486 +                        cset[++cset[0]] = cs[j++];
487 +                if (j > cs[0] || os[i] != cs[j]) {      /* object to check */
488 +                        os[++k] = os[i];
489 +                        cset[++cset[0]] = os[i];
490 +                }
491 +        }
492 +        if (!(os[0] = k))               /* new "to check" set size */
493 +                return;                 /* special case */
494 +        while (j <= cs[0])              /* get the rest of cs */
495 +                cset[++cset[0]] = cs[j++];
496 +        if (cset[0] > MAXCSET)          /* truncate "checked" set if nec. */
497 +                cset[0] = MAXCSET;
498 +        /* setcopy(cs, cset); */        /* copy cset back to cs */
499 +        os = cset;
500 +        for (i = os[0]; i-- >= 0; )
501 +                *cs++ = *os++;
502   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines