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 1.15 by greg, Wed May 1 11:17:01 1991 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1986 Regents of the University of California */
1 > /* Copyright (c) 1990 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   extern CUBE  thescene;                  /* our scene */
22   extern int  maxdepth;                   /* maximum recursion depth */
23   extern double  minweight;               /* minimum ray weight */
24 + extern int  do_irrad;                   /* compute irradiance? */
25  
26   long  nrays = 0L;                       /* number of rays traced */
27  
28 < #define  MAXLOOP        32              /* modifier loop detection */
28 > static double  Lambfa[5] = {PI, PI, PI, 0.0, 0.0};
29 > OBJREC  Lamb = {
30 >        OVOID, MAT_PLASTIC, "Lambertian",
31 >        {0, 5, NULL, Lambfa}, NULL, -1,
32 > };                                      /* a Lambertian surface */
33  
34 + #define  MAXLOOP        128             /* modifier loop detection */
35 +
36   #define  RAYHIT         (-1)            /* return value for intercepted ray */
37  
38  
# Line 59 | Line 68 | double  rw;
68          r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
69          setcolor(r->pcol, 1.0, 1.0, 1.0);
70          setcolor(r->rcol, 0.0, 0.0, 0.0);
71 +        r->rt = 0.0;
72          return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1);
73   }
74  
75  
76   rayvalue(r)                     /* compute a ray's value */
77 < register RAY  *r;
77 > RAY  *r;
78   {
79          extern int  (*trace)();
80  
81          if (localhit(r, &thescene))
82 <                if (r->clipset != NULL && inset(r->clipset, r->ro->omod))
73 <                        raytrans(r);            /* object is clipped */
74 <                else
75 <                        rayshade(r, r->ro->omod);
82 >                raycont(r);
83          else if (sourcehit(r))
84                  rayshade(r, r->ro->omod);
85  
# Line 81 | Line 88 | register RAY  *r;
88   }
89  
90  
91 + raycont(r)                      /* check for clipped object and continue */
92 + register RAY  *r;
93 + {
94 +        if (r->clipset != NULL && inset(r->clipset, r->ro->omod))
95 +                raytrans(r);
96 +        else
97 +                rayshade(r, r->ro->omod);
98 + }
99 +
100 +
101   raytrans(r)                     /* transmit ray as is */
102 < RAY  *r;
102 > register RAY  *r;
103   {
104          RAY  tr;
105  
# Line 90 | Line 107 | RAY  *r;
107                  VCOPY(tr.rdir, r->rdir);
108                  rayvalue(&tr);
109                  copycolor(r->rcol, tr.rcol);
110 +                r->rt = r->rot + tr.rt;
111          }
112   }
113  
# Line 100 | Line 118 | int  mod;
118   {
119          static int  depth = 0;
120          register OBJREC  *m;
121 +                                        /* check for irradiance calc. */
122 +        if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS))) {
123 +                if (irr_ignore(objptr(mod)->otype))
124 +                        raytrans(r);
125 +                else
126 +                        (*ofun[Lamb.otype].funp)(&Lamb, r);
127 +                return;
128 +        }
129                                          /* check for infinite loop */
130          if (depth++ >= MAXLOOP)
131 <                objerror(r->ro, USER, "material loop");
131 >                objerror(r->ro, USER, "possible modifier loop");
132          for ( ; mod != OVOID; mod = m->omod) {
133                  m = objptr(mod);
134 +                /****** unnecessary test since modifier() is always called
135                  if (!ismodifier(m->otype)) {
136                          sprintf(errmsg, "illegal modifier \"%s\"", m->oname);
137                          error(USER, errmsg);
138                  }
139 +                ******/
140                  (*ofun[m->otype].funp)(m, r);   /* execute function */
141                  m->lastrno = r->rno;
142                  if (ismaterial(m->otype)) {     /* materials call raytexture */
# Line 203 | Line 231 | register RAY  *r;
231           *  still fraught with problems since reflected rays and similar
232           *  directions calculated from the surface normal may spawn rays behind
233           *  the surface.  The only solution is to curb textures at high
234 <         *  incidence (Rdot << 1).
234 >         *  incidence (namely, keep DOT(rdir,pert) < Rdot).
235           */
236  
237          for (i = 0; i < 3; i++)
# Line 224 | Line 252 | register RAY  *r;
252   }
253  
254  
255 + newrayxf(r)                     /* get new tranformation matrix for ray */
256 + RAY  *r;
257 + {
258 +        static struct xfn {
259 +                struct xfn  *next;
260 +                FULLXF  xf;
261 +        }  xfseed = { &xfseed }, *xflast = &xfseed;
262 +        register struct xfn  *xp;
263 +        register RAY  *rp;
264 +
265 +        /*
266 +         * Search for transform in circular list that
267 +         * has no associated ray in the tree.
268 +         */
269 +        xp = xflast;
270 +        for (rp = r->parent; rp != NULL; rp = rp->parent)
271 +                if (rp->rox == &xp->xf) {               /* xp in use */
272 +                        xp = xp->next;                  /* move to next */
273 +                        if (xp == xflast) {             /* need new one */
274 +                                xp = (struct xfn *)bmalloc(sizeof(struct xfn));
275 +                                if (xp == NULL)
276 +                                        error(SYSTEM,
277 +                                                "out of memory in newrayxf");
278 +                                                        /* insert in list */
279 +                                xp->next = xflast->next;
280 +                                xflast->next = xp;
281 +                                break;                  /* we're done */
282 +                        }
283 +                        rp = r;                 /* start check over */
284 +                }
285 +                                        /* got it */
286 +        r->rox = &xp->xf;
287 +        xflast = xp;
288 + }
289 +
290 +
291   flipsurface(r)                  /* reverse surface orientation */
292   register RAY  *r;
293   {
# Line 242 | Line 306 | register RAY  *r;
306   register CUBE  *scene;
307   {
308          FVECT  curpos;                  /* current cube position */
309 <        int  mpos, mneg;                /* sign flags */
309 >        int  sflags;                    /* sign flags */
310          double  t, dt;
311          register int  i;
312  
313          nrays++;                        /* increment trace counter */
314  
315 <        mpos = mneg = 0;
315 >        sflags = 0;
316          for (i = 0; i < 3; i++) {
317                  curpos[i] = r->rorg[i];
318                  if (r->rdir[i] > FTINY)
319 <                        mpos |= 1 << i;
319 >                        sflags |= 1 << i;
320                  else if (r->rdir[i] < -FTINY)
321 <                        mneg |= 1 << i;
321 >                        sflags |= 0x10 << i;
322          }
323          t = 0.0;
324          if (!incube(scene, curpos)) {
325                                          /* find distance to entry */
326                  for (i = 0; i < 3; i++) {
327                                          /* plane in our direction */
328 <                        if (mpos & 1<<i)
328 >                        if (sflags & 1<<i)
329                                  dt = scene->cuorg[i];
330 <                        else if (mneg & 1<<i)
330 >                        else if (sflags & 0x10<<i)
331                                  dt = scene->cuorg[i] + scene->cusize;
332                          else
333                                  continue;
# Line 280 | Line 344 | register CUBE  *scene;
344                  if (!incube(scene, curpos))     /* non-intersecting ray */
345                          return(0);
346          }
347 <        return(raymove(curpos, mpos, mneg, r, scene) == RAYHIT);
347 >        return(raymove(curpos, sflags, r, scene) == RAYHIT);
348   }
349  
350  
351   static int
352 < raymove(pos, plus, minus, r, cu)        /* check for hit as we move */
352 > raymove(pos, dirf, r, cu)               /* check for hit as we move */
353   FVECT  pos;                     /* modified */
354 < int  plus, minus;               /* direction indicators to speed tests */
354 > int  dirf;                      /* direction indicators to speed tests */
355   register RAY  *r;
356   register CUBE  *cu;
357   {
358          int  ax;
359          double  dt, t;
296        register int  sgn;
360  
361          if (istree(cu->cutree)) {               /* recurse on subcubes */
362                  CUBE  cukid;
363 <                register int  br;
363 >                register int  br, sgn;
364  
365                  cukid.cusize = cu->cusize * 0.5;        /* find subcube */
366                  VCOPY(cukid.cuorg, cu->cuorg);
# Line 316 | Line 379 | register CUBE  *cu;
379                  }
380                  for ( ; ; ) {
381                          cukid.cutree = octkid(cu->cutree, br);
382 <                        if ((ax = raymove(pos,plus,minus,r,&cukid)) == RAYHIT)
382 >                        if ((ax = raymove(pos,dirf,r,&cukid)) == RAYHIT)
383                                  return(RAYHIT);
384                          sgn = 1 << ax;
385 <                        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
385 >                        if (sgn & dirf)                 /* positive axis? */
386                                  if (sgn & br)
387                                          return(ax);     /* overflow */
388                                  else {
389                                          cukid.cuorg[ax] += cukid.cusize;
390                                          br |= sgn;
391                                  }
392 +                        else
393 +                                if (sgn & br) {
394 +                                        cukid.cuorg[ax] -= cukid.cusize;
395 +                                        br &= ~sgn;
396 +                                } else
397 +                                        return(ax);     /* underflow */
398                  }
399                  /*NOTREACHED*/
400          }
401          if (isfull(cu->cutree) && checkhit(r, cu))
402                  return(RAYHIT);
403                                          /* advance to next cube */
404 <        sgn = plus | minus;
405 <        if (sgn&1) {
343 <                dt = plus&1 ? cu->cuorg[0] + cu->cusize : cu->cuorg[0];
404 >        if (dirf&0x11) {
405 >                dt = dirf&1 ? cu->cuorg[0] + cu->cusize : cu->cuorg[0];
406                  t = (dt - pos[0])/r->rdir[0];
407                  ax = 0;
408          } else
409                  t = FHUGE;
410 <        if (sgn&2) {
411 <                dt = plus&2 ? cu->cuorg[1] + cu->cusize : cu->cuorg[1];
410 >        if (dirf&0x22) {
411 >                dt = dirf&2 ? cu->cuorg[1] + cu->cusize : cu->cuorg[1];
412                  dt = (dt - pos[1])/r->rdir[1];
413                  if (dt < t) {
414                          t = dt;
415                          ax = 1;
416                  }
417          }
418 <        if (sgn&4) {
419 <                dt = plus&4 ? cu->cuorg[2] + cu->cusize : cu->cuorg[2];
418 >        if (dirf&0x44) {
419 >                dt = dirf&4 ? cu->cuorg[2] + cu->cusize : cu->cuorg[2];
420                  dt = (dt - pos[2])/r->rdir[2];
421                  if (dt < t) {
422                          t = dt;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines