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.6 by greg, Mon Oct 16 18:57:02 1989 UTC vs.
Revision 2.11 by greg, Thu Jan 13 09:45:11 1994 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1986 Regents of the University of California */
1 > /* Copyright (c) 1994 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;
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 */
91 < register RAY  *r;
90 > raytrace(r)                     /* trace a ray and compute its value */
91 > RAY  *r;
92   {
93          extern int  (*trace)();
94 +        int  gotmat;
95  
96 <        if (localhit(r, &thescene) || sourcehit(r))
97 <                rayshade(r, r->ro->omod);
96 >        if (localhit(r, &thescene))
97 >                gotmat = raycont(r);
98 >        else if (sourcehit(r))
99 >                gotmat = rayshade(r, r->ro->omod);
100  
101 +        if (!gotmat)
102 +                objerror(r->ro, USER, "material not found");
103 +
104          if (trace != NULL)
105                  (*trace)(r);            /* trace execution */
106   }
107  
108  
109 + raycont(r)                      /* check for clipped object and continue */
110 + register RAY  *r;
111 + {
112 +        if ((r->clipset != NULL && inset(r->clipset, r->ro->omod)) ||
113 +                        r->ro->omod == OVOID) {
114 +                raytrans(r);
115 +                return(1);
116 +        }
117 +        return(rayshade(r, r->ro->omod));
118 + }
119 +
120 +
121   raytrans(r)                     /* transmit ray as is */
122 < RAY  *r;
122 > register RAY  *r;
123   {
124          RAY  tr;
125  
# Line 85 | Line 127 | RAY  *r;
127                  VCOPY(tr.rdir, r->rdir);
128                  rayvalue(&tr);
129                  copycolor(r->rcol, tr.rcol);
130 +                r->rt = r->rot + tr.rt;
131          }
132   }
133  
# Line 94 | Line 137 | register RAY  *r;
137   int  mod;
138   {
139          static int  depth = 0;
140 +        int  gotmat;
141          register OBJREC  *m;
98                                        /* check for clipped surface */
99        if (r->clipset != NULL && r->rot < FHUGE &&
100                        inset(r->clipset, mod)) {
101                raytrans(r);
102                return;
103        }
142                                          /* check for infinite loop */
143          if (depth++ >= MAXLOOP)
144                  objerror(r->ro, USER, "possible modifier loop");
145 <        for ( ; mod != OVOID; mod = m->omod) {
145 >        r->rt = r->rot;                 /* set effective ray length */
146 >        for (gotmat = 0; !gotmat && mod != OVOID; mod = m->omod) {
147                  m = objptr(mod);
148                  /****** unnecessary test since modifier() is always called
149                  if (!ismodifier(m->otype)) {
# Line 112 | Line 151 | int  mod;
151                          error(USER, errmsg);
152                  }
153                  ******/
154 <                (*ofun[m->otype].funp)(m, r);   /* execute function */
155 <                m->lastrno = r->rno;
156 <                if (ismaterial(m->otype)) {     /* materials call raytexture */
157 <                        depth--;
158 <                        return;         /* we're done */
154 >                                        /* hack for irradiance calculation */
155 >                if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS))) {
156 >                        if (irr_ignore(m->otype)) {
157 >                                depth--;
158 >                                raytrans(r);
159 >                                return;
160 >                        }
161 >                        if (!islight(m->otype))
162 >                                m = &Lamb;
163                  }
164 +                                        /* materials call raytexture */
165 +                gotmat = (*ofun[m->otype].funp)(m, r);
166          }
167 <        objerror(r->ro, USER, "material not found");
167 >        depth--;
168 >        return(gotmat);
169   }
170  
171  
# Line 135 | Line 181 | int  mod;
181                                          /* execute textures and patterns */
182          for ( ; mod != OVOID; mod = m->omod) {
183                  m = objptr(mod);
184 <                if (!istexture(m->otype)) {
184 >                /****** unnecessary test since modifier() is always called
185 >                if (!ismodifier(m->otype)) {
186                          sprintf(errmsg, "illegal modifier \"%s\"", m->oname);
187                          error(USER, errmsg);
188                  }
189 <                (*ofun[m->otype].funp)(m, r);
190 <                m->lastrno = r->rno;
189 >                ******/
190 >                if ((*ofun[m->otype].funp)(m, r))
191 >                        objerror(r->ro, USER, "conflicting materials");
192          }
193          depth--;                        /* end here */
194   }
# Line 151 | Line 199 | register RAY  *r;
199   OBJECT  fore, back;
200   double  coef;
201   {
202 <        FVECT  curpert, forepert, backpert;
203 <        COLOR  curpcol, forepcol, backpcol;
202 >        RAY  fr, br;
203 >        COLOR  ctmp;
204 >        int  foremat, backmat;
205          register int  i;
206                                          /* clip coefficient */
207          if (coef > 1.0)
208                  coef = 1.0;
209          else if (coef < 0.0)
210                  coef = 0.0;
211 <                                        /* save current mods */
212 <        VCOPY(curpert, r->pert);
213 <        copycolor(curpcol, r->pcol);
214 <                                        /* compute new mods */
215 <                                                /* foreground */
167 <        r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
168 <        setcolor(r->pcol, 1.0, 1.0, 1.0);
211 >                                        /* foreground */
212 >        copystruct(&fr, r);
213 >        fr.pert[0] = fr.pert[1] = fr.pert[2] = 0.0;
214 >        setcolor(fr.pcol, 1.0, 1.0, 1.0);
215 >        setcolor(fr.rcol, 0.0, 0.0, 0.0);
216          if (fore != OVOID && coef > FTINY)
217 <                raytexture(r, fore);
218 <        VCOPY(forepert, r->pert);
219 <        copycolor(forepcol, r->pcol);
220 <                                                /* background */
221 <        r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
222 <        setcolor(r->pcol, 1.0, 1.0, 1.0);
217 >                foremat = rayshade(&fr, fore);
218 >        else
219 >                foremat = 0;
220 >                                        /* background */
221 >        copystruct(&br, r);
222 >        br.pert[0] = br.pert[1] = br.pert[2] = 0.0;
223 >        setcolor(br.pcol, 1.0, 1.0, 1.0);
224 >        setcolor(br.rcol, 0.0, 0.0, 0.0);
225          if (back != OVOID && coef < 1.0-FTINY)
226 <                raytexture(r, back);
227 <        VCOPY(backpert, r->pert);
228 <        copycolor(backpcol, r->pcol);
226 >                backmat = rayshade(&br, back);
227 >        else
228 >                backmat = foremat;
229 >                                        /* check */
230 >        if ((backmat==0) != (foremat==0))
231 >                objerror(r->ro, USER, "mixing material with non-material");
232                                          /* sum perturbations */
233          for (i = 0; i < 3; i++)
234 <                r->pert[i] = curpert[i] + coef*forepert[i] +
235 <                                (1.0-coef)*backpert[i];
236 <                                        /* multiply colors */
237 <        setcolor(r->pcol, coef*colval(forepcol,RED) +
238 <                                (1.0-coef)*colval(backpcol,RED),
239 <                        coef*colval(forepcol,GRN) +
240 <                                (1.0-coef)*colval(backpcol,GRN),
241 <                        coef*colval(forepcol,BLU) +
242 <                                (1.0-coef)*colval(backpcol,BLU));
243 <        multcolor(r->pcol, curpcol);
234 >                r->pert[i] += coef*fr.pert[i] + (1.0-coef)*br.pert[i];
235 >                                        /* multiply pattern colors */
236 >        scalecolor(fr.pcol, coef);
237 >        scalecolor(br.pcol, 1.0-coef);
238 >        copycolor(ctmp, fr.pcol);
239 >        addcolor(ctmp, br.pcol);
240 >        multcolor(r->pcol, ctmp);
241 >                                        /* sum returned ray values */
242 >        scalecolor(fr.rcol, coef);
243 >        scalecolor(br.rcol, 1.0-coef);
244 >        addcolor(r->rcol, fr.rcol);
245 >        addcolor(r->rcol, br.rcol);
246 >        if (foremat)
247 >                r->rt = bright(fr.rcol) > bright(br.rcol) ? fr.rt : br.rt;
248 >                                        /* return value tells if material */
249 >        return(foremat);
250   }
251  
252  
# Line 206 | Line 264 | register RAY  *r;
264           *  still fraught with problems since reflected rays and similar
265           *  directions calculated from the surface normal may spawn rays behind
266           *  the surface.  The only solution is to curb textures at high
267 <         *  incidence (Rdot << 1).
267 >         *  incidence (namely, keep DOT(rdir,pert) < Rdot).
268           */
269  
270          for (i = 0; i < 3; i++)
# Line 227 | Line 285 | register RAY  *r;
285   }
286  
287  
288 + newrayxf(r)                     /* get new tranformation matrix for ray */
289 + RAY  *r;
290 + {
291 +        static struct xfn {
292 +                struct xfn  *next;
293 +                FULLXF  xf;
294 +        }  xfseed = { &xfseed }, *xflast = &xfseed;
295 +        register struct xfn  *xp;
296 +        register RAY  *rp;
297 +
298 +        /*
299 +         * Search for transform in circular list that
300 +         * has no associated ray in the tree.
301 +         */
302 +        xp = xflast;
303 +        for (rp = r->parent; rp != NULL; rp = rp->parent)
304 +                if (rp->rox == &xp->xf) {               /* xp in use */
305 +                        xp = xp->next;                  /* move to next */
306 +                        if (xp == xflast) {             /* need new one */
307 +                                xp = (struct xfn *)bmalloc(sizeof(struct xfn));
308 +                                if (xp == NULL)
309 +                                        error(SYSTEM,
310 +                                                "out of memory in newrayxf");
311 +                                                        /* insert in list */
312 +                                xp->next = xflast->next;
313 +                                xflast->next = xp;
314 +                                break;                  /* we're done */
315 +                        }
316 +                        rp = r;                 /* start check over */
317 +                }
318 +                                        /* got it */
319 +        r->rox = &xp->xf;
320 +        xflast = xp;
321 + }
322 +
323 +
324   flipsurface(r)                  /* reverse surface orientation */
325   register RAY  *r;
326   {
# Line 244 | Line 338 | localhit(r, scene)             /* check for hit in the octree */
338   register RAY  *r;
339   register CUBE  *scene;
340   {
341 +        OBJECT  cxset[MAXCSET+1];       /* set of checked objects */
342          FVECT  curpos;                  /* current cube position */
343 <        int  mpos, mneg;                /* sign flags */
343 >        int  sflags;                    /* sign flags */
344          double  t, dt;
345          register int  i;
346  
347          nrays++;                        /* increment trace counter */
348 <
254 <        mpos = mneg = 0;
348 >        sflags = 0;
349          for (i = 0; i < 3; i++) {
350                  curpos[i] = r->rorg[i];
351 <                if (r->rdir[i] > FTINY)
352 <                        mpos |= 1 << i;
353 <                else if (r->rdir[i] < -FTINY)
354 <                        mneg |= 1 << i;
351 >                if (r->rdir[i] > 1e-7)
352 >                        sflags |= 1 << i;
353 >                else if (r->rdir[i] < -1e-7)
354 >                        sflags |= 0x10 << i;
355          }
356 +        if (sflags == 0)
357 +                error(CONSISTENCY, "zero ray direction in localhit");
358          t = 0.0;
359          if (!incube(scene, curpos)) {
360                                          /* find distance to entry */
361                  for (i = 0; i < 3; i++) {
362                                          /* plane in our direction */
363 <                        if (mpos & 1<<i)
363 >                        if (sflags & 1<<i)
364                                  dt = scene->cuorg[i];
365 <                        else if (mneg & 1<<i)
365 >                        else if (sflags & 0x10<<i)
366                                  dt = scene->cuorg[i] + scene->cusize;
367                          else
368                                  continue;
# Line 283 | Line 379 | register CUBE  *scene;
379                  if (!incube(scene, curpos))     /* non-intersecting ray */
380                          return(0);
381          }
382 <        return(raymove(curpos, mpos, mneg, r, scene) == RAYHIT);
382 >        cxset[0] = 0;
383 >        return(raymove(curpos, cxset, sflags, r, scene) == RAYHIT);
384   }
385  
386  
387   static int
388 < raymove(pos, plus, minus, r, cu)        /* check for hit as we move */
389 < FVECT  pos;                     /* modified */
390 < int  plus, minus;               /* direction indicators to speed tests */
388 > raymove(pos, cxs, dirf, r, cu)          /* check for hit as we move */
389 > FVECT  pos;                     /* current position, modified herein */
390 > OBJECT  *cxs;                   /* checked objects, modified by checkhit */
391 > int  dirf;                      /* direction indicators to speed tests */
392   register RAY  *r;
393   register CUBE  *cu;
394   {
395          int  ax;
396          double  dt, t;
299        register int  sgn;
397  
398          if (istree(cu->cutree)) {               /* recurse on subcubes */
399                  CUBE  cukid;
400 <                register int  br;
400 >                register int  br, sgn;
401  
402                  cukid.cusize = cu->cusize * 0.5;        /* find subcube */
403                  VCOPY(cukid.cuorg, cu->cuorg);
# Line 319 | Line 416 | register CUBE  *cu;
416                  }
417                  for ( ; ; ) {
418                          cukid.cutree = octkid(cu->cutree, br);
419 <                        if ((ax = raymove(pos,plus,minus,r,&cukid)) == RAYHIT)
419 >                        if ((ax = raymove(pos,cxs,dirf,r,&cukid)) == RAYHIT)
420                                  return(RAYHIT);
421                          sgn = 1 << ax;
422 <                        if (sgn & minus)                /* negative axis? */
326 <                                if (sgn & br) {
327 <                                        cukid.cuorg[ax] -= cukid.cusize;
328 <                                        br &= ~sgn;
329 <                                } else
330 <                                        return(ax);     /* underflow */
331 <                        else
422 >                        if (sgn & dirf)                 /* positive axis? */
423                                  if (sgn & br)
424                                          return(ax);     /* overflow */
425                                  else {
426                                          cukid.cuorg[ax] += cukid.cusize;
427                                          br |= sgn;
428                                  }
429 +                        else
430 +                                if (sgn & br) {
431 +                                        cukid.cuorg[ax] -= cukid.cusize;
432 +                                        br &= ~sgn;
433 +                                } else
434 +                                        return(ax);     /* underflow */
435                  }
436                  /*NOTREACHED*/
437          }
438 <        if (isfull(cu->cutree) && checkhit(r, cu))
438 >        if (isfull(cu->cutree) && checkhit(r, cu, cxs))
439                  return(RAYHIT);
440                                          /* advance to next cube */
441 <        sgn = plus | minus;
442 <        if (sgn&1) {
346 <                dt = plus&1 ? cu->cuorg[0] + cu->cusize : cu->cuorg[0];
441 >        if (dirf&0x11) {
442 >                dt = dirf&1 ? cu->cuorg[0] + cu->cusize : cu->cuorg[0];
443                  t = (dt - pos[0])/r->rdir[0];
444                  ax = 0;
445          } else
446                  t = FHUGE;
447 <        if (sgn&2) {
448 <                dt = plus&2 ? cu->cuorg[1] + cu->cusize : cu->cuorg[1];
447 >        if (dirf&0x22) {
448 >                dt = dirf&2 ? cu->cuorg[1] + cu->cusize : cu->cuorg[1];
449                  dt = (dt - pos[1])/r->rdir[1];
450                  if (dt < t) {
451                          t = dt;
452                          ax = 1;
453                  }
454          }
455 <        if (sgn&4) {
456 <                dt = plus&4 ? cu->cuorg[2] + cu->cusize : cu->cuorg[2];
455 >        if (dirf&0x44) {
456 >                dt = dirf&4 ? cu->cuorg[2] + cu->cusize : cu->cuorg[2];
457                  dt = (dt - pos[2])/r->rdir[2];
458                  if (dt < t) {
459                          t = dt;
# Line 372 | Line 468 | register CUBE  *cu;
468  
469  
470   static
471 < checkhit(r, cu)                 /* check for hit in full cube */
471 > checkhit(r, cu, cxs)            /* check for hit in full cube */
472   register RAY  *r;
473   CUBE  *cu;
474 + OBJECT  *cxs;
475   {
476          OBJECT  oset[MAXSET+1];
477          register OBJREC  *o;
478          register int  i;
479  
480          objset(oset, cu->cutree);
481 +        checkset(oset, cxs);                    /* eliminate double-checking */
482          for (i = oset[0]; i > 0; i--) {
483                  o = objptr(oset[i]);
386                if (o->lastrno == r->rno)               /* checked already? */
387                        continue;
484                  (*ofun[o->otype].funp)(o, r);
389                o->lastrno = r->rno;
485          }
486          if (r->ro == NULL)
487                  return(0);                      /* no scores yet */
488  
489          return(incube(cu, r->rop));             /* hit OK if in current cube */
490 + }
491 +
492 +
493 + static
494 + checkset(os, cs)                /* modify checked set and set to check */
495 + register OBJECT  *os;                   /* os' = os - cs */
496 + register OBJECT  *cs;                   /* cs' = cs + os */
497 + {
498 +        OBJECT  cset[MAXCSET+MAXSET+1];
499 +        register int  i, j;
500 +        int  k;
501 +                                        /* copy os in place, cset <- cs */
502 +        cset[0] = 0;
503 +        k = 0;
504 +        for (i = j = 1; i <= os[0]; i++) {
505 +                while (j <= cs[0] && cs[j] < os[i])
506 +                        cset[++cset[0]] = cs[j++];
507 +                if (j > cs[0] || os[i] != cs[j]) {      /* object to check */
508 +                        os[++k] = os[i];
509 +                        cset[++cset[0]] = os[i];
510 +                }
511 +        }
512 +        if (!(os[0] = k))               /* new "to check" set size */
513 +                return;                 /* special case */
514 +        while (j <= cs[0])              /* get the rest of cs */
515 +                cset[++cset[0]] = cs[j++];
516 +        if (cset[0] > MAXCSET)          /* truncate "checked" set if nec. */
517 +                cset[0] = MAXCSET;
518 +        /* setcopy(cs, cset); */        /* copy cset back to cs */
519 +        os = cset;
520 +        for (i = os[0]; i-- >= 0; )
521 +                *cs++ = *os++;
522   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines