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.7 by greg, Wed Nov 29 14:35:25 1989 UTC vs.
Revision 2.12 by greg, Thu Jan 13 10:43:34 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 <                                                /* check for clipped object */
98 <                if (r->clipset != NULL && inset(r->clipset, r->ro->omod))
99 <                        raytrans(r);
75 <                else
76 <                        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 89 | 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 98 | Line 137 | register RAY  *r;
137   int  mod;
138   {
139          static int  depth = 0;
140 +        int  gotmat;
141          register OBJREC  *m;
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 110 | 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 133 | 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 149 | 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 >        int  foremat, backmat;
204          register int  i;
205                                          /* clip coefficient */
206          if (coef > 1.0)
207                  coef = 1.0;
208          else if (coef < 0.0)
209                  coef = 0.0;
210 <                                        /* save current mods */
211 <        VCOPY(curpert, r->pert);
162 <        copycolor(curpcol, r->pcol);
163 <                                        /* compute new mods */
164 <                                                /* foreground */
165 <        r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
166 <        setcolor(r->pcol, 1.0, 1.0, 1.0);
210 >                                        /* foreground */
211 >        copystruct(&fr, r);
212          if (fore != OVOID && coef > FTINY)
213 <                raytexture(r, fore);
214 <        VCOPY(forepert, r->pert);
215 <        copycolor(forepcol, r->pcol);
216 <                                                /* background */
217 <        r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
173 <        setcolor(r->pcol, 1.0, 1.0, 1.0);
213 >                foremat = rayshade(&fr, fore);
214 >        else
215 >                foremat = 0;
216 >                                        /* background */
217 >        copystruct(&br, r);
218          if (back != OVOID && coef < 1.0-FTINY)
219 <                raytexture(r, back);
220 <        VCOPY(backpert, r->pert);
221 <        copycolor(backpcol, r->pcol);
222 <                                        /* sum perturbations */
219 >                backmat = rayshade(&br, back);
220 >        else
221 >                backmat = foremat;
222 >                                        /* check */
223 >        if ((foremat==0) != (backmat==0))
224 >                objerror(r->ro, USER, "mixing material with non-material");
225 >                                        /* mix perturbations */
226          for (i = 0; i < 3; i++)
227 <                r->pert[i] = curpert[i] + coef*forepert[i] +
228 <                                (1.0-coef)*backpert[i];
229 <                                        /* multiply colors */
230 <        setcolor(r->pcol, coef*colval(forepcol,RED) +
231 <                                (1.0-coef)*colval(backpcol,RED),
232 <                        coef*colval(forepcol,GRN) +
233 <                                (1.0-coef)*colval(backpcol,GRN),
234 <                        coef*colval(forepcol,BLU) +
235 <                                (1.0-coef)*colval(backpcol,BLU));
236 <        multcolor(r->pcol, curpcol);
227 >                r->pert[i] = coef*fr.pert[i] + (1.0-coef)*br.pert[i];
228 >                                        /* mix pattern colors */
229 >        scalecolor(fr.pcol, coef);
230 >        scalecolor(br.pcol, 1.0-coef);
231 >        copycolor(r->pcol, fr.pcol);
232 >        addcolor(r->pcol, br.pcol);
233 >                                        /* mix returned ray values */
234 >        if (foremat) {
235 >                scalecolor(fr.rcol, coef);
236 >                scalecolor(br.rcol, 1.0-coef);
237 >                copycolor(r->rcol, fr.rcol);
238 >                addcolor(r->rcol, br.rcol);
239 >                r->rt = bright(fr.rcol) > bright(br.rcol) ? fr.rt : br.rt;
240 >        }
241 >                                        /* return value tells if material */
242 >        return(foremat);
243   }
244  
245  
# Line 204 | Line 257 | register RAY  *r;
257           *  still fraught with problems since reflected rays and similar
258           *  directions calculated from the surface normal may spawn rays behind
259           *  the surface.  The only solution is to curb textures at high
260 <         *  incidence (Rdot << 1).
260 >         *  incidence (namely, keep DOT(rdir,pert) < Rdot).
261           */
262  
263          for (i = 0; i < 3; i++)
# Line 225 | Line 278 | register RAY  *r;
278   }
279  
280  
281 + newrayxf(r)                     /* get new tranformation matrix for ray */
282 + RAY  *r;
283 + {
284 +        static struct xfn {
285 +                struct xfn  *next;
286 +                FULLXF  xf;
287 +        }  xfseed = { &xfseed }, *xflast = &xfseed;
288 +        register struct xfn  *xp;
289 +        register RAY  *rp;
290 +
291 +        /*
292 +         * Search for transform in circular list that
293 +         * has no associated ray in the tree.
294 +         */
295 +        xp = xflast;
296 +        for (rp = r->parent; rp != NULL; rp = rp->parent)
297 +                if (rp->rox == &xp->xf) {               /* xp in use */
298 +                        xp = xp->next;                  /* move to next */
299 +                        if (xp == xflast) {             /* need new one */
300 +                                xp = (struct xfn *)bmalloc(sizeof(struct xfn));
301 +                                if (xp == NULL)
302 +                                        error(SYSTEM,
303 +                                                "out of memory in newrayxf");
304 +                                                        /* insert in list */
305 +                                xp->next = xflast->next;
306 +                                xflast->next = xp;
307 +                                break;                  /* we're done */
308 +                        }
309 +                        rp = r;                 /* start check over */
310 +                }
311 +                                        /* got it */
312 +        r->rox = &xp->xf;
313 +        xflast = xp;
314 + }
315 +
316 +
317   flipsurface(r)                  /* reverse surface orientation */
318   register RAY  *r;
319   {
# Line 242 | Line 331 | localhit(r, scene)             /* check for hit in the octree */
331   register RAY  *r;
332   register CUBE  *scene;
333   {
334 +        OBJECT  cxset[MAXCSET+1];       /* set of checked objects */
335          FVECT  curpos;                  /* current cube position */
336 <        int  mpos, mneg;                /* sign flags */
336 >        int  sflags;                    /* sign flags */
337          double  t, dt;
338          register int  i;
339  
340          nrays++;                        /* increment trace counter */
341 <
252 <        mpos = mneg = 0;
341 >        sflags = 0;
342          for (i = 0; i < 3; i++) {
343                  curpos[i] = r->rorg[i];
344 <                if (r->rdir[i] > FTINY)
345 <                        mpos |= 1 << i;
346 <                else if (r->rdir[i] < -FTINY)
347 <                        mneg |= 1 << i;
344 >                if (r->rdir[i] > 1e-7)
345 >                        sflags |= 1 << i;
346 >                else if (r->rdir[i] < -1e-7)
347 >                        sflags |= 0x10 << i;
348          }
349 +        if (sflags == 0)
350 +                error(CONSISTENCY, "zero ray direction in localhit");
351          t = 0.0;
352          if (!incube(scene, curpos)) {
353                                          /* find distance to entry */
354                  for (i = 0; i < 3; i++) {
355                                          /* plane in our direction */
356 <                        if (mpos & 1<<i)
356 >                        if (sflags & 1<<i)
357                                  dt = scene->cuorg[i];
358 <                        else if (mneg & 1<<i)
358 >                        else if (sflags & 0x10<<i)
359                                  dt = scene->cuorg[i] + scene->cusize;
360                          else
361                                  continue;
# Line 281 | Line 372 | register CUBE  *scene;
372                  if (!incube(scene, curpos))     /* non-intersecting ray */
373                          return(0);
374          }
375 <        return(raymove(curpos, mpos, mneg, r, scene) == RAYHIT);
375 >        cxset[0] = 0;
376 >        return(raymove(curpos, cxset, sflags, r, scene) == RAYHIT);
377   }
378  
379  
380   static int
381 < raymove(pos, plus, minus, r, cu)        /* check for hit as we move */
382 < FVECT  pos;                     /* modified */
383 < int  plus, minus;               /* direction indicators to speed tests */
381 > raymove(pos, cxs, dirf, r, cu)          /* check for hit as we move */
382 > FVECT  pos;                     /* current position, modified herein */
383 > OBJECT  *cxs;                   /* checked objects, modified by checkhit */
384 > int  dirf;                      /* direction indicators to speed tests */
385   register RAY  *r;
386   register CUBE  *cu;
387   {
388          int  ax;
389          double  dt, t;
297        register int  sgn;
390  
391          if (istree(cu->cutree)) {               /* recurse on subcubes */
392                  CUBE  cukid;
393 <                register int  br;
393 >                register int  br, sgn;
394  
395                  cukid.cusize = cu->cusize * 0.5;        /* find subcube */
396                  VCOPY(cukid.cuorg, cu->cuorg);
# Line 317 | Line 409 | register CUBE  *cu;
409                  }
410                  for ( ; ; ) {
411                          cukid.cutree = octkid(cu->cutree, br);
412 <                        if ((ax = raymove(pos,plus,minus,r,&cukid)) == RAYHIT)
412 >                        if ((ax = raymove(pos,cxs,dirf,r,&cukid)) == RAYHIT)
413                                  return(RAYHIT);
414                          sgn = 1 << ax;
415 <                        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
415 >                        if (sgn & dirf)                 /* positive axis? */
416                                  if (sgn & br)
417                                          return(ax);     /* overflow */
418                                  else {
419                                          cukid.cuorg[ax] += cukid.cusize;
420                                          br |= sgn;
421                                  }
422 +                        else
423 +                                if (sgn & br) {
424 +                                        cukid.cuorg[ax] -= cukid.cusize;
425 +                                        br &= ~sgn;
426 +                                } else
427 +                                        return(ax);     /* underflow */
428                  }
429                  /*NOTREACHED*/
430          }
431 <        if (isfull(cu->cutree) && checkhit(r, cu))
431 >        if (isfull(cu->cutree) && checkhit(r, cu, cxs))
432                  return(RAYHIT);
433                                          /* advance to next cube */
434 <        sgn = plus | minus;
435 <        if (sgn&1) {
344 <                dt = plus&1 ? cu->cuorg[0] + cu->cusize : cu->cuorg[0];
434 >        if (dirf&0x11) {
435 >                dt = dirf&1 ? cu->cuorg[0] + cu->cusize : cu->cuorg[0];
436                  t = (dt - pos[0])/r->rdir[0];
437                  ax = 0;
438          } else
439                  t = FHUGE;
440 <        if (sgn&2) {
441 <                dt = plus&2 ? cu->cuorg[1] + cu->cusize : cu->cuorg[1];
440 >        if (dirf&0x22) {
441 >                dt = dirf&2 ? cu->cuorg[1] + cu->cusize : cu->cuorg[1];
442                  dt = (dt - pos[1])/r->rdir[1];
443                  if (dt < t) {
444                          t = dt;
445                          ax = 1;
446                  }
447          }
448 <        if (sgn&4) {
449 <                dt = plus&4 ? cu->cuorg[2] + cu->cusize : cu->cuorg[2];
448 >        if (dirf&0x44) {
449 >                dt = dirf&4 ? cu->cuorg[2] + cu->cusize : cu->cuorg[2];
450                  dt = (dt - pos[2])/r->rdir[2];
451                  if (dt < t) {
452                          t = dt;
# Line 370 | Line 461 | register CUBE  *cu;
461  
462  
463   static
464 < checkhit(r, cu)                 /* check for hit in full cube */
464 > checkhit(r, cu, cxs)            /* check for hit in full cube */
465   register RAY  *r;
466   CUBE  *cu;
467 + OBJECT  *cxs;
468   {
469          OBJECT  oset[MAXSET+1];
470          register OBJREC  *o;
471          register int  i;
472  
473          objset(oset, cu->cutree);
474 +        checkset(oset, cxs);                    /* eliminate double-checking */
475          for (i = oset[0]; i > 0; i--) {
476                  o = objptr(oset[i]);
384                if (o->lastrno == r->rno)               /* checked already? */
385                        continue;
477                  (*ofun[o->otype].funp)(o, r);
387                o->lastrno = r->rno;
478          }
479          if (r->ro == NULL)
480                  return(0);                      /* no scores yet */
481  
482          return(incube(cu, r->rop));             /* hit OK if in current cube */
483 + }
484 +
485 +
486 + static
487 + checkset(os, cs)                /* modify checked set and set to check */
488 + register OBJECT  *os;                   /* os' = os - cs */
489 + register OBJECT  *cs;                   /* cs' = cs + os */
490 + {
491 +        OBJECT  cset[MAXCSET+MAXSET+1];
492 +        register int  i, j;
493 +        int  k;
494 +                                        /* copy os in place, cset <- cs */
495 +        cset[0] = 0;
496 +        k = 0;
497 +        for (i = j = 1; i <= os[0]; i++) {
498 +                while (j <= cs[0] && cs[j] < os[i])
499 +                        cset[++cset[0]] = cs[j++];
500 +                if (j > cs[0] || os[i] != cs[j]) {      /* object to check */
501 +                        os[++k] = os[i];
502 +                        cset[++cset[0]] = os[i];
503 +                }
504 +        }
505 +        if (!(os[0] = k))               /* new "to check" set size */
506 +                return;                 /* special case */
507 +        while (j <= cs[0])              /* get the rest of cs */
508 +                cset[++cset[0]] = cs[j++];
509 +        if (cset[0] > MAXCSET)          /* truncate "checked" set if nec. */
510 +                cset[0] = MAXCSET;
511 +        /* setcopy(cs, cset); */        /* copy cset back to cs */
512 +        os = cset;
513 +        for (i = os[0]; i-- >= 0; )
514 +                *cs++ = *os++;
515   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines