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.3 by greg, Wed Apr 19 22:24:28 1989 UTC vs.
Revision 2.20 by greg, Fri Sep 29 10:46:12 1995 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1986 Regents of the University of California */
1 > /* Copyright (c) 1995 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 < #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 + OBJREC  Aftplane;                       /* aft clipping plane object */
38 +
39 + static int  raymove(), checkset(), checkhit();
40 +
41 + #define  MAXLOOP        128             /* modifier loop detection */
42 +
43   #define  RAYHIT         (-1)            /* return value for intercepted ray */
44  
45  
# Line 38 | Line 54 | double  rw;
54                  r->crtype = r->rtype = rt;
55                  r->rsrc = -1;
56                  r->clipset = NULL;
57 +                r->revf = raytrace;
58          } else {                                /* spawned ray */
59                  r->rlvl = ro->rlvl;
60                  if (rt & RAYREFL) {
# Line 48 | Line 65 | double  rw;
65                          r->rsrc = ro->rsrc;
66                          r->clipset = ro->newcset;
67                  }
68 +                r->revf = ro->revf;
69                  r->rweight = ro->rweight * rw;
70                  r->crtype = ro->crtype | (r->rtype = rt);
71                  VCOPY(r->rorg, ro->rop);
72 +                r->rmax = 0.0;
73          }
74 <        r->rno = nrays;
74 >        rayclear(r);
75 >        return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1);
76 > }
77 >
78 >
79 > rayclear(r)                     /* clear a ray for (re)evaluation */
80 > register RAY  *r;
81 > {
82 >        r->rno = raynum++;
83          r->newcset = r->clipset;
84          r->ro = NULL;
85          r->rot = FHUGE;
86          r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
87          setcolor(r->pcol, 1.0, 1.0, 1.0);
88          setcolor(r->rcol, 0.0, 0.0, 0.0);
89 <        return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1);
89 >        r->rt = 0.0;
90   }
91  
92  
93 < rayvalue(r)                     /* compute a ray's value */
94 < register RAY  *r;
93 > raytrace(r)                     /* trace a ray and compute its value */
94 > RAY  *r;
95   {
96          extern int  (*trace)();
97 +        int  gotmat;
98  
99          if (localhit(r, &thescene))
100 <                if (r->clipset != NULL && inset(r->clipset, r->ro->omod))
101 <                        raytrans(r);            /* object is clipped */
102 <                else
103 <                        rayshade(r, r->ro->omod);
104 <        else if (sourcehit(r))
105 <                rayshade(r, r->ro->omod);
100 >                gotmat = raycont(r);
101 >        else if (r->ro == &Aftplane) {
102 >                r->ro = NULL;
103 >                r->rot = FHUGE;
104 >        } else if (sourcehit(r))
105 >                gotmat = rayshade(r, r->ro->omod);
106  
107 +        if (r->ro != NULL && !gotmat)
108 +                objerror(r->ro, USER, "material not found");
109 +
110          if (trace != NULL)
111                  (*trace)(r);            /* trace execution */
112   }
113  
114  
115 + raycont(r)                      /* check for clipped object and continue */
116 + register RAY  *r;
117 + {
118 +        if ((r->clipset != NULL && inset(r->clipset, r->ro->omod)) ||
119 +                        r->ro->omod == OVOID) {
120 +                raytrans(r);
121 +                return(1);
122 +        }
123 +        return(rayshade(r, r->ro->omod));
124 + }
125 +
126 +
127   raytrans(r)                     /* transmit ray as is */
128 < RAY  *r;
128 > register RAY  *r;
129   {
130          RAY  tr;
131  
132          if (rayorigin(&tr, r, TRANS, 1.0) == 0) {
133                  VCOPY(tr.rdir, r->rdir);
134 +                if (r->rmax > FTINY)
135 +                        tr.rmax = r->rmax - r->rot;
136                  rayvalue(&tr);
137                  copycolor(r->rcol, tr.rcol);
138 +                r->rt = r->rot + tr.rt;
139          }
140   }
141  
# Line 99 | Line 145 | register RAY  *r;
145   int  mod;
146   {
147          static int  depth = 0;
148 +        int  gotmat;
149          register OBJREC  *m;
150                                          /* check for infinite loop */
151          if (depth++ >= MAXLOOP)
152 <                objerror(r->ro, USER, "material loop");
153 <        for ( ; mod != OVOID; mod = m->omod) {
152 >                objerror(r->ro, USER, "possible modifier loop");
153 >        r->rt = r->rot;                 /* set effective ray length */
154 >        for (gotmat = 0; !gotmat && mod != OVOID; mod = m->omod) {
155                  m = objptr(mod);
156 +                /****** unnecessary test since modifier() is always called
157                  if (!ismodifier(m->otype)) {
158                          sprintf(errmsg, "illegal modifier \"%s\"", m->oname);
159                          error(USER, errmsg);
160                  }
161 <                (*ofun[m->otype].funp)(m, r);   /* execute function */
162 <                m->lastrno = r->rno;
163 <                if (ismaterial(m->otype)) {     /* materials call raytexture */
164 <                        depth--;
165 <                        return;         /* we're done */
161 >                ******/
162 >                                        /* hack for irradiance calculation */
163 >                if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS))) {
164 >                        if (irr_ignore(m->otype)) {
165 >                                depth--;
166 >                                raytrans(r);
167 >                                return(1);
168 >                        }
169 >                        if (!islight(m->otype))
170 >                                m = &Lamb;
171                  }
172 +                                        /* materials call raytexture */
173 +                gotmat = (*ofun[m->otype].funp)(m, r);
174          }
175 <        objerror(r->ro, USER, "material not found");
175 >        depth--;
176 >        return(gotmat);
177   }
178  
179  
# Line 132 | Line 189 | int  mod;
189                                          /* execute textures and patterns */
190          for ( ; mod != OVOID; mod = m->omod) {
191                  m = objptr(mod);
192 <                if (!istexture(m->otype)) {
192 >                /****** unnecessary test since modifier() is always called
193 >                if (!ismodifier(m->otype)) {
194                          sprintf(errmsg, "illegal modifier \"%s\"", m->oname);
195                          error(USER, errmsg);
196                  }
197 <                (*ofun[m->otype].funp)(m, r);
198 <                m->lastrno = r->rno;
197 >                ******/
198 >                if ((*ofun[m->otype].funp)(m, r)) {
199 >                        sprintf(errmsg, "conflicting material \"%s\"",
200 >                                        m->oname);
201 >                        objerror(r->ro, USER, errmsg);
202 >                }
203          }
204          depth--;                        /* end here */
205   }
# Line 148 | Line 210 | register RAY  *r;
210   OBJECT  fore, back;
211   double  coef;
212   {
213 <        FVECT  curpert, forepert, backpert;
214 <        COLOR  curpcol, forepcol, backpcol;
213 >        RAY  fr, br;
214 >        int  foremat, backmat;
215          register int  i;
216                                          /* clip coefficient */
217          if (coef > 1.0)
218                  coef = 1.0;
219          else if (coef < 0.0)
220                  coef = 0.0;
221 <                                        /* save current mods */
222 <        VCOPY(curpert, r->pert);
223 <        copycolor(curpcol, r->pcol);
224 <                                        /* compute new mods */
163 <                                                /* foreground */
164 <        r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
165 <        setcolor(r->pcol, 1.0, 1.0, 1.0);
221 >                                        /* compute foreground and background */
222 >        foremat = backmat = -1;
223 >                                        /* foreground */
224 >        copystruct(&fr, r);
225          if (fore != OVOID && coef > FTINY)
226 <                raytexture(r, fore);
227 <        VCOPY(forepert, r->pert);
228 <        copycolor(forepcol, r->pcol);
170 <                                                /* background */
171 <        r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
172 <        setcolor(r->pcol, 1.0, 1.0, 1.0);
226 >                foremat = rayshade(&fr, fore);
227 >                                        /* background */
228 >        copystruct(&br, r);
229          if (back != OVOID && coef < 1.0-FTINY)
230 <                raytexture(r, back);
231 <        VCOPY(backpert, r->pert);
232 <        copycolor(backpcol, r->pcol);
233 <                                        /* sum perturbations */
230 >                backmat = rayshade(&br, back);
231 >                                        /* check */
232 >        if (foremat < 0)
233 >                if (backmat < 0)
234 >                        foremat = backmat = 0;
235 >                else
236 >                        foremat = backmat;
237 >        else if (backmat < 0)
238 >                backmat = foremat;
239 >        if ((foremat==0) != (backmat==0))
240 >                objerror(r->ro, USER, "mixing material with non-material");
241 >                                        /* mix perturbations */
242          for (i = 0; i < 3; i++)
243 <                r->pert[i] = curpert[i] + coef*forepert[i] +
244 <                                (1.0-coef)*backpert[i];
245 <                                        /* multiply colors */
246 <        setcolor(r->pcol, coef*colval(forepcol,RED) +
247 <                                (1.0-coef)*colval(backpcol,RED),
248 <                        coef*colval(forepcol,GRN) +
249 <                                (1.0-coef)*colval(backpcol,GRN),
250 <                        coef*colval(forepcol,BLU) +
251 <                                (1.0-coef)*colval(backpcol,BLU));
252 <        multcolor(r->pcol, curpcol);
243 >                r->pert[i] = coef*fr.pert[i] + (1.0-coef)*br.pert[i];
244 >                                        /* mix pattern colors */
245 >        scalecolor(fr.pcol, coef);
246 >        scalecolor(br.pcol, 1.0-coef);
247 >        copycolor(r->pcol, fr.pcol);
248 >        addcolor(r->pcol, br.pcol);
249 >                                        /* mix returned ray values */
250 >        if (foremat) {
251 >                scalecolor(fr.rcol, coef);
252 >                scalecolor(br.rcol, 1.0-coef);
253 >                copycolor(r->rcol, fr.rcol);
254 >                addcolor(r->rcol, br.rcol);
255 >                r->rt = bright(fr.rcol) > bright(br.rcol) ? fr.rt : br.rt;
256 >        }
257 >                                        /* return value tells if material */
258 >        return(foremat);
259   }
260  
261  
# Line 203 | Line 273 | register RAY  *r;
273           *  still fraught with problems since reflected rays and similar
274           *  directions calculated from the surface normal may spawn rays behind
275           *  the surface.  The only solution is to curb textures at high
276 <         *  incidence (Rdot << 1).
276 >         *  incidence (namely, keep DOT(rdir,pert) < Rdot).
277           */
278  
279          for (i = 0; i < 3; i++)
# Line 224 | Line 294 | register RAY  *r;
294   }
295  
296  
297 + newrayxf(r)                     /* get new tranformation matrix for ray */
298 + RAY  *r;
299 + {
300 +        static struct xfn {
301 +                struct xfn  *next;
302 +                FULLXF  xf;
303 +        }  xfseed = { &xfseed }, *xflast = &xfseed;
304 +        register struct xfn  *xp;
305 +        register RAY  *rp;
306 +
307 +        /*
308 +         * Search for transform in circular list that
309 +         * has no associated ray in the tree.
310 +         */
311 +        xp = xflast;
312 +        for (rp = r->parent; rp != NULL; rp = rp->parent)
313 +                if (rp->rox == &xp->xf) {               /* xp in use */
314 +                        xp = xp->next;                  /* move to next */
315 +                        if (xp == xflast) {             /* need new one */
316 +                                xp = (struct xfn *)bmalloc(sizeof(struct xfn));
317 +                                if (xp == NULL)
318 +                                        error(SYSTEM,
319 +                                                "out of memory in newrayxf");
320 +                                                        /* insert in list */
321 +                                xp->next = xflast->next;
322 +                                xflast->next = xp;
323 +                                break;                  /* we're done */
324 +                        }
325 +                        rp = r;                 /* start check over */
326 +                }
327 +                                        /* got it */
328 +        r->rox = &xp->xf;
329 +        xflast = xp;
330 + }
331 +
332 +
333   flipsurface(r)                  /* reverse surface orientation */
334   register RAY  *r;
335   {
# Line 241 | Line 347 | localhit(r, scene)             /* check for hit in the octree */
347   register RAY  *r;
348   register CUBE  *scene;
349   {
350 +        OBJECT  cxset[MAXCSET+1];       /* set of checked objects */
351          FVECT  curpos;                  /* current cube position */
352 <        int  mpos, mneg;                /* sign flags */
352 >        int  sflags;                    /* sign flags */
353          double  t, dt;
354          register int  i;
355  
356          nrays++;                        /* increment trace counter */
357 <
251 <        mpos = mneg = 0;
357 >        sflags = 0;
358          for (i = 0; i < 3; i++) {
359                  curpos[i] = r->rorg[i];
360 <                if (r->rdir[i] > FTINY)
361 <                        mpos |= 1 << i;
362 <                else if (r->rdir[i] < -FTINY)
363 <                        mneg |= 1 << i;
360 >                if (r->rdir[i] > 1e-7)
361 >                        sflags |= 1 << i;
362 >                else if (r->rdir[i] < -1e-7)
363 >                        sflags |= 0x10 << i;
364          }
365 +        if (sflags == 0)
366 +                error(CONSISTENCY, "zero ray direction in localhit");
367 +                                        /* start off assuming nothing hit */
368 +        if (r->rmax > FTINY) {          /* except aft plane if one */
369 +                r->ro = &Aftplane;
370 +                r->rot = r->rmax;
371 +                for (i = 0; i < 3; i++)
372 +                        r->rop[i] = r->rorg[i] + r->rot*r->rdir[i];
373 +        }
374 +                                        /* find global cube entrance point */
375          t = 0.0;
376          if (!incube(scene, curpos)) {
377                                          /* find distance to entry */
378                  for (i = 0; i < 3; i++) {
379                                          /* plane in our direction */
380 <                        if (mpos & 1<<i)
380 >                        if (sflags & 1<<i)
381                                  dt = scene->cuorg[i];
382 <                        else if (mneg & 1<<i)
382 >                        else if (sflags & 0x10<<i)
383                                  dt = scene->cuorg[i] + scene->cusize;
384                          else
385                                  continue;
# Line 273 | Line 389 | register CUBE  *scene;
389                                  t = dt; /* farthest face is the one */
390                  }
391                  t += FTINY;             /* fudge to get inside cube */
392 +                if (t >= r->rot)        /* clipped already */
393 +                        return(0);
394                                          /* advance position */
395                  for (i = 0; i < 3; i++)
396                          curpos[i] += r->rdir[i]*t;
# Line 280 | Line 398 | register CUBE  *scene;
398                  if (!incube(scene, curpos))     /* non-intersecting ray */
399                          return(0);
400          }
401 <        return(raymove(curpos, mpos, mneg, r, scene) == RAYHIT);
401 >        cxset[0] = 0;
402 >        raymove(curpos, cxset, sflags, r, scene);
403 >        return(r->ro != NULL & r->ro != &Aftplane);
404   }
405  
406  
407   static int
408 < raymove(pos, plus, minus, r, cu)        /* check for hit as we move */
409 < FVECT  pos;                     /* modified */
410 < int  plus, minus;               /* direction indicators to speed tests */
408 > raymove(pos, cxs, dirf, r, cu)          /* check for hit as we move */
409 > FVECT  pos;                     /* current position, modified herein */
410 > OBJECT  *cxs;                   /* checked objects, modified by checkhit */
411 > int  dirf;                      /* direction indicators to speed tests */
412   register RAY  *r;
413   register CUBE  *cu;
414   {
415          int  ax;
416          double  dt, t;
296        register int  sgn;
417  
418          if (istree(cu->cutree)) {               /* recurse on subcubes */
419                  CUBE  cukid;
420 <                register int  br;
420 >                register int  br, sgn;
421  
422                  cukid.cusize = cu->cusize * 0.5;        /* find subcube */
423                  VCOPY(cukid.cuorg, cu->cuorg);
# Line 316 | Line 436 | register CUBE  *cu;
436                  }
437                  for ( ; ; ) {
438                          cukid.cutree = octkid(cu->cutree, br);
439 <                        if ((ax = raymove(pos,plus,minus,r,&cukid)) == RAYHIT)
439 >                        if ((ax = raymove(pos,cxs,dirf,r,&cukid)) == RAYHIT)
440                                  return(RAYHIT);
441                          sgn = 1 << ax;
442 <                        if (sgn & minus)                /* negative axis? */
323 <                                if (sgn & br) {
324 <                                        cukid.cuorg[ax] -= cukid.cusize;
325 <                                        br &= ~sgn;
326 <                                } else
327 <                                        return(ax);     /* underflow */
328 <                        else
442 >                        if (sgn & dirf)                 /* positive axis? */
443                                  if (sgn & br)
444                                          return(ax);     /* overflow */
445                                  else {
446                                          cukid.cuorg[ax] += cukid.cusize;
447                                          br |= sgn;
448                                  }
449 +                        else
450 +                                if (sgn & br) {
451 +                                        cukid.cuorg[ax] -= cukid.cusize;
452 +                                        br &= ~sgn;
453 +                                } else
454 +                                        return(ax);     /* underflow */
455                  }
456                  /*NOTREACHED*/
457          }
458 <        if (isfull(cu->cutree) && checkhit(r, cu))
458 >        if (isfull(cu->cutree)) {
459 >                if (checkhit(r, cu, cxs))
460 >                        return(RAYHIT);
461 >        } else if (r->ro == &Aftplane && incube(cu, r->rop))
462                  return(RAYHIT);
463                                          /* advance to next cube */
464 <        sgn = plus | minus;
465 <        if (sgn&1) {
343 <                dt = plus&1 ? cu->cuorg[0] + cu->cusize : cu->cuorg[0];
464 >        if (dirf&0x11) {
465 >                dt = dirf&1 ? cu->cuorg[0] + cu->cusize : cu->cuorg[0];
466                  t = (dt - pos[0])/r->rdir[0];
467                  ax = 0;
468          } else
469                  t = FHUGE;
470 <        if (sgn&2) {
471 <                dt = plus&2 ? cu->cuorg[1] + cu->cusize : cu->cuorg[1];
470 >        if (dirf&0x22) {
471 >                dt = dirf&2 ? cu->cuorg[1] + cu->cusize : cu->cuorg[1];
472                  dt = (dt - pos[1])/r->rdir[1];
473                  if (dt < t) {
474                          t = dt;
475                          ax = 1;
476                  }
477          }
478 <        if (sgn&4) {
479 <                dt = plus&4 ? cu->cuorg[2] + cu->cusize : cu->cuorg[2];
478 >        if (dirf&0x44) {
479 >                dt = dirf&4 ? cu->cuorg[2] + cu->cusize : cu->cuorg[2];
480                  dt = (dt - pos[2])/r->rdir[2];
481                  if (dt < t) {
482                          t = dt;
# Line 369 | Line 491 | register CUBE  *cu;
491  
492  
493   static
494 < checkhit(r, cu)                 /* check for hit in full cube */
494 > checkhit(r, cu, cxs)            /* check for hit in full cube */
495   register RAY  *r;
496   CUBE  *cu;
497 + OBJECT  *cxs;
498   {
499          OBJECT  oset[MAXSET+1];
500          register OBJREC  *o;
501          register int  i;
502  
503          objset(oset, cu->cutree);
504 +        checkset(oset, cxs);                    /* eliminate double-checking */
505          for (i = oset[0]; i > 0; i--) {
506                  o = objptr(oset[i]);
383                if (o->lastrno == r->rno)               /* checked already? */
384                        continue;
507                  (*ofun[o->otype].funp)(o, r);
386                o->lastrno = r->rno;
508          }
509          if (r->ro == NULL)
510                  return(0);                      /* no scores yet */
511  
512          return(incube(cu, r->rop));             /* hit OK if in current cube */
513 + }
514 +
515 +
516 + static
517 + checkset(os, cs)                /* modify checked set and set to check */
518 + register OBJECT  *os;                   /* os' = os - cs */
519 + register OBJECT  *cs;                   /* cs' = cs + os */
520 + {
521 +        OBJECT  cset[MAXCSET+MAXSET+1];
522 +        register int  i, j;
523 +        int  k;
524 +                                        /* copy os in place, cset <- cs */
525 +        cset[0] = 0;
526 +        k = 0;
527 +        for (i = j = 1; i <= os[0]; i++) {
528 +                while (j <= cs[0] && cs[j] < os[i])
529 +                        cset[++cset[0]] = cs[j++];
530 +                if (j > cs[0] || os[i] != cs[j]) {      /* object to check */
531 +                        os[++k] = os[i];
532 +                        cset[++cset[0]] = os[i];
533 +                }
534 +        }
535 +        if (!(os[0] = k))               /* new "to check" set size */
536 +                return;                 /* special case */
537 +        while (j <= cs[0])              /* get the rest of cs */
538 +                cset[++cset[0]] = cs[j++];
539 +        if (cset[0] > MAXCSET)          /* truncate "checked" set if nec. */
540 +                cset[0] = MAXCSET;
541 +        /* setcopy(cs, cset); */        /* copy cset back to cs */
542 +        os = cset;
543 +        for (i = os[0]; i-- >= 0; )
544 +                *cs++ = *os++;
545   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines