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.17 by greg, Fri May 10 08:51:06 1991 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   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 102 | Line 120 | int  mod;
120          register OBJREC  *m;
121                                          /* check for infinite loop */
122          if (depth++ >= MAXLOOP)
123 <                objerror(r->ro, USER, "material loop");
123 >                objerror(r->ro, USER, "possible modifier loop");
124          for ( ; mod != OVOID; mod = m->omod) {
125                  m = objptr(mod);
126 +                /****** unnecessary test since modifier() is always called
127                  if (!ismodifier(m->otype)) {
128                          sprintf(errmsg, "illegal modifier \"%s\"", m->oname);
129                          error(USER, errmsg);
130                  }
131 +                ******/
132 +                                        /* hack for irradiance calculation */
133 +                if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS))) {
134 +                        if (irr_ignore(m->otype)) {
135 +                                depth--;
136 +                                raytrans(r);
137 +                                return;
138 +                        }
139 +                        if (m->otype != MAT_ILLUM)
140 +                                m = &Lamb;
141 +                }
142                  (*ofun[m->otype].funp)(m, r);   /* execute function */
143                  m->lastrno = r->rno;
144                  if (ismaterial(m->otype)) {     /* materials call raytexture */
# Line 203 | Line 233 | register RAY  *r;
233           *  still fraught with problems since reflected rays and similar
234           *  directions calculated from the surface normal may spawn rays behind
235           *  the surface.  The only solution is to curb textures at high
236 <         *  incidence (Rdot << 1).
236 >         *  incidence (namely, keep DOT(rdir,pert) < Rdot).
237           */
238  
239          for (i = 0; i < 3; i++)
# Line 224 | Line 254 | register RAY  *r;
254   }
255  
256  
257 + newrayxf(r)                     /* get new tranformation matrix for ray */
258 + RAY  *r;
259 + {
260 +        static struct xfn {
261 +                struct xfn  *next;
262 +                FULLXF  xf;
263 +        }  xfseed = { &xfseed }, *xflast = &xfseed;
264 +        register struct xfn  *xp;
265 +        register RAY  *rp;
266 +
267 +        /*
268 +         * Search for transform in circular list that
269 +         * has no associated ray in the tree.
270 +         */
271 +        xp = xflast;
272 +        for (rp = r->parent; rp != NULL; rp = rp->parent)
273 +                if (rp->rox == &xp->xf) {               /* xp in use */
274 +                        xp = xp->next;                  /* move to next */
275 +                        if (xp == xflast) {             /* need new one */
276 +                                xp = (struct xfn *)bmalloc(sizeof(struct xfn));
277 +                                if (xp == NULL)
278 +                                        error(SYSTEM,
279 +                                                "out of memory in newrayxf");
280 +                                                        /* insert in list */
281 +                                xp->next = xflast->next;
282 +                                xflast->next = xp;
283 +                                break;                  /* we're done */
284 +                        }
285 +                        rp = r;                 /* start check over */
286 +                }
287 +                                        /* got it */
288 +        r->rox = &xp->xf;
289 +        xflast = xp;
290 + }
291 +
292 +
293   flipsurface(r)                  /* reverse surface orientation */
294   register RAY  *r;
295   {
# Line 242 | Line 308 | register RAY  *r;
308   register CUBE  *scene;
309   {
310          FVECT  curpos;                  /* current cube position */
311 <        int  mpos, mneg;                /* sign flags */
311 >        int  sflags;                    /* sign flags */
312          double  t, dt;
313          register int  i;
314  
315          nrays++;                        /* increment trace counter */
316  
317 <        mpos = mneg = 0;
317 >        sflags = 0;
318          for (i = 0; i < 3; i++) {
319                  curpos[i] = r->rorg[i];
320                  if (r->rdir[i] > FTINY)
321 <                        mpos |= 1 << i;
321 >                        sflags |= 1 << i;
322                  else if (r->rdir[i] < -FTINY)
323 <                        mneg |= 1 << i;
323 >                        sflags |= 0x10 << i;
324          }
325 +        if (sflags == 0)
326 +                error(CONSISTENCY, "zero ray direction in localhit");
327          t = 0.0;
328          if (!incube(scene, curpos)) {
329                                          /* find distance to entry */
330                  for (i = 0; i < 3; i++) {
331                                          /* plane in our direction */
332 <                        if (mpos & 1<<i)
332 >                        if (sflags & 1<<i)
333                                  dt = scene->cuorg[i];
334 <                        else if (mneg & 1<<i)
334 >                        else if (sflags & 0x10<<i)
335                                  dt = scene->cuorg[i] + scene->cusize;
336                          else
337                                  continue;
# Line 280 | Line 348 | register CUBE  *scene;
348                  if (!incube(scene, curpos))     /* non-intersecting ray */
349                          return(0);
350          }
351 <        return(raymove(curpos, mpos, mneg, r, scene) == RAYHIT);
351 >        return(raymove(curpos, sflags, r, scene) == RAYHIT);
352   }
353  
354  
355   static int
356 < raymove(pos, plus, minus, r, cu)        /* check for hit as we move */
356 > raymove(pos, dirf, r, cu)               /* check for hit as we move */
357   FVECT  pos;                     /* modified */
358 < int  plus, minus;               /* direction indicators to speed tests */
358 > int  dirf;                      /* direction indicators to speed tests */
359   register RAY  *r;
360   register CUBE  *cu;
361   {
362          int  ax;
363          double  dt, t;
296        register int  sgn;
364  
365          if (istree(cu->cutree)) {               /* recurse on subcubes */
366                  CUBE  cukid;
367 <                register int  br;
367 >                register int  br, sgn;
368  
369                  cukid.cusize = cu->cusize * 0.5;        /* find subcube */
370                  VCOPY(cukid.cuorg, cu->cuorg);
# Line 316 | Line 383 | register CUBE  *cu;
383                  }
384                  for ( ; ; ) {
385                          cukid.cutree = octkid(cu->cutree, br);
386 <                        if ((ax = raymove(pos,plus,minus,r,&cukid)) == RAYHIT)
386 >                        if ((ax = raymove(pos,dirf,r,&cukid)) == RAYHIT)
387                                  return(RAYHIT);
388                          sgn = 1 << ax;
389 <                        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
389 >                        if (sgn & dirf)                 /* positive axis? */
390                                  if (sgn & br)
391                                          return(ax);     /* overflow */
392                                  else {
393                                          cukid.cuorg[ax] += cukid.cusize;
394                                          br |= sgn;
395                                  }
396 +                        else
397 +                                if (sgn & br) {
398 +                                        cukid.cuorg[ax] -= cukid.cusize;
399 +                                        br &= ~sgn;
400 +                                } else
401 +                                        return(ax);     /* underflow */
402                  }
403                  /*NOTREACHED*/
404          }
405          if (isfull(cu->cutree) && checkhit(r, cu))
406                  return(RAYHIT);
407                                          /* advance to next cube */
408 <        sgn = plus | minus;
409 <        if (sgn&1) {
343 <                dt = plus&1 ? cu->cuorg[0] + cu->cusize : cu->cuorg[0];
408 >        if (dirf&0x11) {
409 >                dt = dirf&1 ? cu->cuorg[0] + cu->cusize : cu->cuorg[0];
410                  t = (dt - pos[0])/r->rdir[0];
411                  ax = 0;
412          } else
413                  t = FHUGE;
414 <        if (sgn&2) {
415 <                dt = plus&2 ? cu->cuorg[1] + cu->cusize : cu->cuorg[1];
414 >        if (dirf&0x22) {
415 >                dt = dirf&2 ? cu->cuorg[1] + cu->cusize : cu->cuorg[1];
416                  dt = (dt - pos[1])/r->rdir[1];
417                  if (dt < t) {
418                          t = dt;
419                          ax = 1;
420                  }
421          }
422 <        if (sgn&4) {
423 <                dt = plus&4 ? cu->cuorg[2] + cu->cusize : cu->cuorg[2];
422 >        if (dirf&0x44) {
423 >                dt = dirf&4 ? cu->cuorg[2] + cu->cusize : cu->cuorg[2];
424                  dt = (dt - pos[2])/r->rdir[2];
425                  if (dt < t) {
426                          t = dt;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines