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.11 by greg, Sat Aug 18 04:25:59 1990 UTC vs.
Revision 2.7 by greg, Wed Jul 21 15:36:13 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 > unsigned long  raynum = 0;              /* next unique ray number */
29 > unsigned long  nrays = 0;               /* 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;
# Line 60 | Line 84 | double  rw;
84          setcolor(r->pcol, 1.0, 1.0, 1.0);
85          setcolor(r->rcol, 0.0, 0.0, 0.0);
86          r->rt = 0.0;
63        return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1);
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 80 | Line 105 | RAY  *r;
105   raycont(r)                      /* check for clipped object and continue */
106   register RAY  *r;
107   {
108 <        if (r->clipset != NULL && inset(r->clipset, r->ro->omod))
108 >        if ((r->clipset != NULL && inset(r->clipset, r->ro->omod)) ||
109 >                        r->ro->omod == OVOID)
110                  raytrans(r);
111          else
112                  rayshade(r, r->ro->omod);
# Line 110 | Line 136 | int  mod;
136                                          /* check for infinite loop */
137          if (depth++ >= MAXLOOP)
138                  objerror(r->ro, USER, "possible modifier loop");
139 +        r->rt = r->rot;                 /* set effective ray length */
140          for ( ; mod != OVOID; mod = m->omod) {
141                  m = objptr(mod);
142                  /****** unnecessary test since modifier() is always called
# Line 118 | Line 145 | int  mod;
145                          error(USER, errmsg);
146                  }
147                  ******/
148 +                                        /* hack for irradiance calculation */
149 +                if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS))) {
150 +                        if (irr_ignore(m->otype)) {
151 +                                depth--;
152 +                                raytrans(r);
153 +                                return;
154 +                        }
155 +                        if (!islight(m->otype))
156 +                                m = &Lamb;
157 +                }
158                  (*ofun[m->otype].funp)(m, r);   /* execute function */
122                m->lastrno = r->rno;
159                  if (ismaterial(m->otype)) {     /* materials call raytexture */
160                          depth--;
161                          return;         /* we're done */
# Line 146 | Line 182 | int  mod;
182                          error(USER, errmsg);
183                  }
184                  (*ofun[m->otype].funp)(m, r);
149                m->lastrno = r->rno;
185          }
186          depth--;                        /* end here */
187   }
# Line 233 | Line 268 | register RAY  *r;
268   }
269  
270  
271 + newrayxf(r)                     /* get new tranformation matrix for ray */
272 + RAY  *r;
273 + {
274 +        static struct xfn {
275 +                struct xfn  *next;
276 +                FULLXF  xf;
277 +        }  xfseed = { &xfseed }, *xflast = &xfseed;
278 +        register struct xfn  *xp;
279 +        register RAY  *rp;
280 +
281 +        /*
282 +         * Search for transform in circular list that
283 +         * has no associated ray in the tree.
284 +         */
285 +        xp = xflast;
286 +        for (rp = r->parent; rp != NULL; rp = rp->parent)
287 +                if (rp->rox == &xp->xf) {               /* xp in use */
288 +                        xp = xp->next;                  /* move to next */
289 +                        if (xp == xflast) {             /* need new one */
290 +                                xp = (struct xfn *)bmalloc(sizeof(struct xfn));
291 +                                if (xp == NULL)
292 +                                        error(SYSTEM,
293 +                                                "out of memory in newrayxf");
294 +                                                        /* insert in list */
295 +                                xp->next = xflast->next;
296 +                                xflast->next = xp;
297 +                                break;                  /* we're done */
298 +                        }
299 +                        rp = r;                 /* start check over */
300 +                }
301 +                                        /* got it */
302 +        r->rox = &xp->xf;
303 +        xflast = xp;
304 + }
305 +
306 +
307   flipsurface(r)                  /* reverse surface orientation */
308   register RAY  *r;
309   {
# Line 250 | Line 321 | localhit(r, scene)             /* check for hit in the octree */
321   register RAY  *r;
322   register CUBE  *scene;
323   {
324 +        OBJECT  cxset[MAXCSET+1];       /* set of checked objects */
325          FVECT  curpos;                  /* current cube position */
326          int  sflags;                    /* sign flags */
327          double  t, dt;
328          register int  i;
329  
330          nrays++;                        /* increment trace counter */
259
331          sflags = 0;
332          for (i = 0; i < 3; i++) {
333                  curpos[i] = r->rorg[i];
# Line 265 | Line 336 | register CUBE  *scene;
336                  else if (r->rdir[i] < -FTINY)
337                          sflags |= 0x10 << i;
338          }
339 +        if (sflags == 0)
340 +                error(CONSISTENCY, "zero ray direction in localhit");
341          t = 0.0;
342          if (!incube(scene, curpos)) {
343                                          /* find distance to entry */
# Line 289 | Line 362 | register CUBE  *scene;
362                  if (!incube(scene, curpos))     /* non-intersecting ray */
363                          return(0);
364          }
365 <        return(raymove(curpos, sflags, r, scene) == RAYHIT);
365 >        cxset[0] = 0;
366 >        return(raymove(curpos, cxset, sflags, r, scene) == RAYHIT);
367   }
368  
369  
370   static int
371 < raymove(pos, dirf, r, cu)               /* check for hit as we move */
372 < FVECT  pos;                     /* modified */
371 > raymove(pos, cxs, dirf, r, cu)          /* check for hit as we move */
372 > FVECT  pos;                     /* current position, modified herein */
373 > OBJECT  *cxs;                   /* checked objects, modified by checkhit */
374   int  dirf;                      /* direction indicators to speed tests */
375   register RAY  *r;
376   register CUBE  *cu;
# Line 324 | Line 399 | register CUBE  *cu;
399                  }
400                  for ( ; ; ) {
401                          cukid.cutree = octkid(cu->cutree, br);
402 <                        if ((ax = raymove(pos,dirf,r,&cukid)) == RAYHIT)
402 >                        if ((ax = raymove(pos,cxs,dirf,r,&cukid)) == RAYHIT)
403                                  return(RAYHIT);
404                          sgn = 1 << ax;
405                          if (sgn & dirf)                 /* positive axis? */
# Line 343 | Line 418 | register CUBE  *cu;
418                  }
419                  /*NOTREACHED*/
420          }
421 <        if (isfull(cu->cutree) && checkhit(r, cu))
421 >        if (isfull(cu->cutree) && checkhit(r, cu, cxs))
422                  return(RAYHIT);
423                                          /* advance to next cube */
424          if (dirf&0x11) {
# Line 376 | Line 451 | register CUBE  *cu;
451  
452  
453   static
454 < checkhit(r, cu)                 /* check for hit in full cube */
454 > checkhit(r, cu, cxs)            /* check for hit in full cube */
455   register RAY  *r;
456   CUBE  *cu;
457 + OBJECT  *cxs;
458   {
459          OBJECT  oset[MAXSET+1];
460          register OBJREC  *o;
461          register int  i;
462  
463          objset(oset, cu->cutree);
464 +        checkset(oset, cxs);                    /* eliminate double-checking */
465          for (i = oset[0]; i > 0; i--) {
466                  o = objptr(oset[i]);
390                if (o->lastrno == r->rno)               /* checked already? */
391                        continue;
467                  (*ofun[o->otype].funp)(o, r);
393                o->lastrno = r->rno;
468          }
469          if (r->ro == NULL)
470                  return(0);                      /* no scores yet */
471  
472          return(incube(cu, r->rop));             /* hit OK if in current cube */
473 + }
474 +
475 +
476 + static
477 + checkset(os, cs)                /* modify checked set and set to check */
478 + register OBJECT  *os;                   /* os' = os - cs */
479 + register OBJECT  *cs;                   /* cs' = cs + os */
480 + {
481 +        OBJECT  cset[MAXCSET+MAXSET+1];
482 +        register int  i, j;
483 +        int  k;
484 +                                        /* copy os in place, cset <- cs */
485 +        cset[0] = 0;
486 +        k = 0;
487 +        for (i = j = 1; i <= os[0]; i++) {
488 +                while (j <= cs[0] && cs[j] < os[i])
489 +                        cset[++cset[0]] = cs[j++];
490 +                if (j > cs[0] || os[i] != cs[j]) {      /* object to check */
491 +                        os[++k] = os[i];
492 +                        cset[++cset[0]] = os[i];
493 +                }
494 +        }
495 +        if (!(os[0] = k))               /* new "to check" set size */
496 +                return;                 /* special case */
497 +        while (j <= cs[0])              /* get the rest of cs */
498 +                cset[++cset[0]] = cs[j++];
499 +        if (cset[0] > MAXCSET)          /* truncate "checked" set if nec. */
500 +                cset[0] = MAXCSET;
501 +        /* setcopy(cs, cset); */        /* copy cset back to cs */
502 +        os = cset;
503 +        for (i = os[0]; i-- >= 0; )
504 +                *cs++ = *os++;
505   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines