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.8 by greg, Tue Jan 2 15:16:14 1990 UTC vs.
Revision 2.5 by greg, Mon Mar 8 12:37:30 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 + 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 + static int  raymove(), checkset(), checkhit();
38 +
39   #define  MAXLOOP        128             /* modifier loop detection */
40  
41   #define  RAYHIT         (-1)            /* return value for intercepted ray */
# Line 38 | Line 52 | double  rw;
52                  r->crtype = r->rtype = rt;
53                  r->rsrc = -1;
54                  r->clipset = NULL;
55 +                r->revf = raytrace;
56          } else {                                /* spawned ray */
57                  r->rlvl = ro->rlvl;
58                  if (rt & RAYREFL) {
# Line 48 | Line 63 | double  rw;
63                          r->rsrc = ro->rsrc;
64                          r->clipset = ro->newcset;
65                  }
66 +                r->revf = ro->revf;
67                  r->rweight = ro->rweight * rw;
68                  r->crtype = ro->crtype | (r->rtype = rt);
69                  VCOPY(r->rorg, ro->rop);
70          }
71 <        r->rno = nrays;
71 >        rayclear(r);
72 >        return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1);
73 > }
74 >
75 >
76 > rayclear(r)                     /* clear a ray for (re)evaluation */
77 > register RAY  *r;
78 > {
79 >        r->rno = raynum++;
80          r->newcset = r->clipset;
81          r->ro = NULL;
82          r->rot = FHUGE;
83          r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
84          setcolor(r->pcol, 1.0, 1.0, 1.0);
85          setcolor(r->rcol, 0.0, 0.0, 0.0);
86 <        return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1);
86 >        r->rt = 0.0;
87   }
88  
89  
90 < rayvalue(r)                     /* compute a ray's value */
90 > raytrace(r)                     /* trace a ray and compute its value */
91   RAY  *r;
92   {
93          extern int  (*trace)();
94  
95 <        if (localhit(r, &thescene) || sourcehit(r))
95 >        if (localhit(r, &thescene))
96                  raycont(r);
97 +        else if (sourcehit(r))
98 +                rayshade(r, r->ro->omod);
99  
100          if (trace != NULL)
101                  (*trace)(r);            /* trace execution */
# Line 95 | Line 121 | register RAY  *r;
121                  VCOPY(tr.rdir, r->rdir);
122                  rayvalue(&tr);
123                  copycolor(r->rcol, tr.rcol);
124 +                r->rt = r->rot + tr.rt;
125          }
126   }
127  
# Line 108 | Line 135 | int  mod;
135                                          /* check for infinite loop */
136          if (depth++ >= MAXLOOP)
137                  objerror(r->ro, USER, "possible modifier loop");
138 +        r->rt = r->rot;                 /* set effective ray length */
139          for ( ; mod != OVOID; mod = m->omod) {
140                  m = objptr(mod);
141                  /****** unnecessary test since modifier() is always called
# Line 116 | Line 144 | int  mod;
144                          error(USER, errmsg);
145                  }
146                  ******/
147 +                                        /* hack for irradiance calculation */
148 +                if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS))) {
149 +                        if (irr_ignore(m->otype)) {
150 +                                depth--;
151 +                                raytrans(r);
152 +                                return;
153 +                        }
154 +                        if (!islight(m->otype))
155 +                                m = &Lamb;
156 +                }
157                  (*ofun[m->otype].funp)(m, r);   /* execute function */
120                m->lastrno = r->rno;
158                  if (ismaterial(m->otype)) {     /* materials call raytexture */
159                          depth--;
160                          return;         /* we're done */
# Line 144 | Line 181 | int  mod;
181                          error(USER, errmsg);
182                  }
183                  (*ofun[m->otype].funp)(m, r);
147                m->lastrno = r->rno;
184          }
185          depth--;                        /* end here */
186   }
# Line 210 | Line 246 | register RAY  *r;
246           *  still fraught with problems since reflected rays and similar
247           *  directions calculated from the surface normal may spawn rays behind
248           *  the surface.  The only solution is to curb textures at high
249 <         *  incidence (Rdot << 1).
249 >         *  incidence (namely, keep DOT(rdir,pert) < Rdot).
250           */
251  
252          for (i = 0; i < 3; i++)
# Line 231 | Line 267 | register RAY  *r;
267   }
268  
269  
270 + newrayxf(r)                     /* get new tranformation matrix for ray */
271 + RAY  *r;
272 + {
273 +        static struct xfn {
274 +                struct xfn  *next;
275 +                FULLXF  xf;
276 +        }  xfseed = { &xfseed }, *xflast = &xfseed;
277 +        register struct xfn  *xp;
278 +        register RAY  *rp;
279 +
280 +        /*
281 +         * Search for transform in circular list that
282 +         * has no associated ray in the tree.
283 +         */
284 +        xp = xflast;
285 +        for (rp = r->parent; rp != NULL; rp = rp->parent)
286 +                if (rp->rox == &xp->xf) {               /* xp in use */
287 +                        xp = xp->next;                  /* move to next */
288 +                        if (xp == xflast) {             /* need new one */
289 +                                xp = (struct xfn *)bmalloc(sizeof(struct xfn));
290 +                                if (xp == NULL)
291 +                                        error(SYSTEM,
292 +                                                "out of memory in newrayxf");
293 +                                                        /* insert in list */
294 +                                xp->next = xflast->next;
295 +                                xflast->next = xp;
296 +                                break;                  /* we're done */
297 +                        }
298 +                        rp = r;                 /* start check over */
299 +                }
300 +                                        /* got it */
301 +        r->rox = &xp->xf;
302 +        xflast = xp;
303 + }
304 +
305 +
306   flipsurface(r)                  /* reverse surface orientation */
307   register RAY  *r;
308   {
# Line 248 | Line 320 | localhit(r, scene)             /* check for hit in the octree */
320   register RAY  *r;
321   register CUBE  *scene;
322   {
323 +        OBJECT  cxset[MAXCSET+1];       /* set of checked objects */
324          FVECT  curpos;                  /* current cube position */
325 <        int  mpos, mneg;                /* sign flags */
325 >        int  sflags;                    /* sign flags */
326          double  t, dt;
327          register int  i;
328  
329          nrays++;                        /* increment trace counter */
330 <
258 <        mpos = mneg = 0;
330 >        sflags = 0;
331          for (i = 0; i < 3; i++) {
332                  curpos[i] = r->rorg[i];
333                  if (r->rdir[i] > FTINY)
334 <                        mpos |= 1 << i;
334 >                        sflags |= 1 << i;
335                  else if (r->rdir[i] < -FTINY)
336 <                        mneg |= 1 << i;
336 >                        sflags |= 0x10 << i;
337          }
338 +        if (sflags == 0)
339 +                error(CONSISTENCY, "zero ray direction in localhit");
340          t = 0.0;
341          if (!incube(scene, curpos)) {
342                                          /* find distance to entry */
343                  for (i = 0; i < 3; i++) {
344                                          /* plane in our direction */
345 <                        if (mpos & 1<<i)
345 >                        if (sflags & 1<<i)
346                                  dt = scene->cuorg[i];
347 <                        else if (mneg & 1<<i)
347 >                        else if (sflags & 0x10<<i)
348                                  dt = scene->cuorg[i] + scene->cusize;
349                          else
350                                  continue;
# Line 287 | Line 361 | register CUBE  *scene;
361                  if (!incube(scene, curpos))     /* non-intersecting ray */
362                          return(0);
363          }
364 <        return(raymove(curpos, mpos, mneg, r, scene) == RAYHIT);
364 >        cxset[0] = 0;
365 >        return(raymove(curpos, cxset, sflags, r, scene) == RAYHIT);
366   }
367  
368  
369   static int
370 < raymove(pos, plus, minus, r, cu)        /* check for hit as we move */
371 < FVECT  pos;                     /* modified */
372 < int  plus, minus;               /* direction indicators to speed tests */
370 > raymove(pos, cxs, dirf, r, cu)          /* check for hit as we move */
371 > FVECT  pos;                     /* current position, modified herein */
372 > OBJECT  *cxs;                   /* checked objects, modified by checkhit */
373 > int  dirf;                      /* direction indicators to speed tests */
374   register RAY  *r;
375   register CUBE  *cu;
376   {
377          int  ax;
378          double  dt, t;
303        register int  sgn;
379  
380          if (istree(cu->cutree)) {               /* recurse on subcubes */
381                  CUBE  cukid;
382 <                register int  br;
382 >                register int  br, sgn;
383  
384                  cukid.cusize = cu->cusize * 0.5;        /* find subcube */
385                  VCOPY(cukid.cuorg, cu->cuorg);
# Line 323 | Line 398 | register CUBE  *cu;
398                  }
399                  for ( ; ; ) {
400                          cukid.cutree = octkid(cu->cutree, br);
401 <                        if ((ax = raymove(pos,plus,minus,r,&cukid)) == RAYHIT)
401 >                        if ((ax = raymove(pos,cxs,dirf,r,&cukid)) == RAYHIT)
402                                  return(RAYHIT);
403                          sgn = 1 << ax;
404 <                        if (sgn & minus)                /* negative axis? */
330 <                                if (sgn & br) {
331 <                                        cukid.cuorg[ax] -= cukid.cusize;
332 <                                        br &= ~sgn;
333 <                                } else
334 <                                        return(ax);     /* underflow */
335 <                        else
404 >                        if (sgn & dirf)                 /* positive axis? */
405                                  if (sgn & br)
406                                          return(ax);     /* overflow */
407                                  else {
408                                          cukid.cuorg[ax] += cukid.cusize;
409                                          br |= sgn;
410                                  }
411 +                        else
412 +                                if (sgn & br) {
413 +                                        cukid.cuorg[ax] -= cukid.cusize;
414 +                                        br &= ~sgn;
415 +                                } else
416 +                                        return(ax);     /* underflow */
417                  }
418                  /*NOTREACHED*/
419          }
420 <        if (isfull(cu->cutree) && checkhit(r, cu))
420 >        if (isfull(cu->cutree) && checkhit(r, cu, cxs))
421                  return(RAYHIT);
422                                          /* advance to next cube */
423 <        sgn = plus | minus;
424 <        if (sgn&1) {
350 <                dt = plus&1 ? cu->cuorg[0] + cu->cusize : cu->cuorg[0];
423 >        if (dirf&0x11) {
424 >                dt = dirf&1 ? cu->cuorg[0] + cu->cusize : cu->cuorg[0];
425                  t = (dt - pos[0])/r->rdir[0];
426                  ax = 0;
427          } else
428                  t = FHUGE;
429 <        if (sgn&2) {
430 <                dt = plus&2 ? cu->cuorg[1] + cu->cusize : cu->cuorg[1];
429 >        if (dirf&0x22) {
430 >                dt = dirf&2 ? cu->cuorg[1] + cu->cusize : cu->cuorg[1];
431                  dt = (dt - pos[1])/r->rdir[1];
432                  if (dt < t) {
433                          t = dt;
434                          ax = 1;
435                  }
436          }
437 <        if (sgn&4) {
438 <                dt = plus&4 ? cu->cuorg[2] + cu->cusize : cu->cuorg[2];
437 >        if (dirf&0x44) {
438 >                dt = dirf&4 ? cu->cuorg[2] + cu->cusize : cu->cuorg[2];
439                  dt = (dt - pos[2])/r->rdir[2];
440                  if (dt < t) {
441                          t = dt;
# Line 376 | Line 450 | register CUBE  *cu;
450  
451  
452   static
453 < checkhit(r, cu)                 /* check for hit in full cube */
453 > checkhit(r, cu, cxs)            /* check for hit in full cube */
454   register RAY  *r;
455   CUBE  *cu;
456 + OBJECT  *cxs;
457   {
458          OBJECT  oset[MAXSET+1];
459          register OBJREC  *o;
460          register int  i;
461  
462          objset(oset, cu->cutree);
463 +        checkset(oset, cxs);                    /* eliminate double-checking */
464          for (i = oset[0]; i > 0; i--) {
465                  o = objptr(oset[i]);
466 <                if (o->lastrno == r->rno)               /* checked already? */
467 <                        continue;
466 >                if (o->omod == OVOID && issurface(o->otype))
467 >                        continue;               /* ignore void surfaces */
468                  (*ofun[o->otype].funp)(o, r);
393                o->lastrno = r->rno;
469          }
470          if (r->ro == NULL)
471                  return(0);                      /* no scores yet */
472  
473          return(incube(cu, r->rop));             /* hit OK if in current cube */
474 + }
475 +
476 +
477 + static
478 + checkset(os, cs)                /* modify checked set and set to check */
479 + register OBJECT  *os;                   /* os' = os - cs */
480 + register OBJECT  *cs;                   /* cs' = cs + os */
481 + {
482 +        OBJECT  cset[MAXCSET+MAXSET+1];
483 +        register int  i, j;
484 +        int  k;
485 +                                        /* copy os in place, cset <- cs */
486 +        cset[0] = 0;
487 +        k = 0;
488 +        for (i = j = 1; i <= os[0]; i++) {
489 +                while (j <= cs[0] && cs[j] < os[i])
490 +                        cset[++cset[0]] = cs[j++];
491 +                if (j > cs[0] || os[i] != cs[j]) {      /* object to check */
492 +                        os[++k] = os[i];
493 +                        cset[++cset[0]] = os[i];
494 +                }
495 +        }
496 +        if (!(os[0] = k))               /* new "to check" set size */
497 +                return;                 /* special case */
498 +        while (j <= cs[0])              /* get the rest of cs */
499 +                cset[++cset[0]] = cs[j++];
500 +        if (cset[0] > MAXCSET)          /* truncate "checked" set if nec. */
501 +                cset[0] = MAXCSET;
502 +        /* setcopy(cs, cset); */        /* copy cset back to cs */
503 +        os = cset;
504 +        for (i = os[0]; i-- >= 0; )
505 +                *cs++ = *os++;
506   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines