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.8 by greg, Tue Jan 2 15:16:14 1990 UTC vs.
Revision 1.21 by greg, Thu Jun 20 13:29:32 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 */
26 > long  raynum = 0L;                      /* next unique ray number */
27 > long  nrays = 0L;                       /* number of calls to localhit */
28  
29 + static double  Lambfa[5] = {PI, PI, PI, 0.0, 0.0};
30 + OBJREC  Lamb = {
31 +        OVOID, MAT_PLASTIC, "Lambertian",
32 +        {0, 5, NULL, Lambfa}, NULL, -1,
33 + };                                      /* a Lambertian surface */
34 +
35   #define  MAXLOOP        128             /* modifier loop detection */
36  
37   #define  RAYHIT         (-1)            /* return value for intercepted ray */
# Line 38 | Line 48 | double  rw;
48                  r->crtype = r->rtype = rt;
49                  r->rsrc = -1;
50                  r->clipset = NULL;
51 +                r->revf = raytrace;
52          } else {                                /* spawned ray */
53                  r->rlvl = ro->rlvl;
54                  if (rt & RAYREFL) {
# Line 48 | Line 59 | double  rw;
59                          r->rsrc = ro->rsrc;
60                          r->clipset = ro->newcset;
61                  }
62 +                r->revf = ro->revf;
63                  r->rweight = ro->rweight * rw;
64                  r->crtype = ro->crtype | (r->rtype = rt);
65                  VCOPY(r->rorg, ro->rop);
66          }
67 <        r->rno = nrays;
67 >        r->rno = raynum++;
68          r->newcset = r->clipset;
69          r->ro = NULL;
70          r->rot = FHUGE;
71          r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
72          setcolor(r->pcol, 1.0, 1.0, 1.0);
73          setcolor(r->rcol, 0.0, 0.0, 0.0);
74 +        r->rt = 0.0;
75          return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1);
76   }
77  
78  
79 < rayvalue(r)                     /* compute a ray's value */
79 > raytrace(r)                     /* trace a ray and compute its value */
80   RAY  *r;
81   {
82          extern int  (*trace)();
83  
84 <        if (localhit(r, &thescene) || sourcehit(r))
84 >        if (localhit(r, &thescene))
85                  raycont(r);
86 +        else if (sourcehit(r))
87 +                rayshade(r, r->ro->omod);
88  
89          if (trace != NULL)
90                  (*trace)(r);            /* trace execution */
# Line 95 | Line 110 | register RAY  *r;
110                  VCOPY(tr.rdir, r->rdir);
111                  rayvalue(&tr);
112                  copycolor(r->rcol, tr.rcol);
113 +                r->rt = r->rot + tr.rt;
114          }
115   }
116  
# Line 108 | Line 124 | int  mod;
124                                          /* check for infinite loop */
125          if (depth++ >= MAXLOOP)
126                  objerror(r->ro, USER, "possible modifier loop");
127 +        r->rt = r->rot;                 /* set effective ray length */
128          for ( ; mod != OVOID; mod = m->omod) {
129                  m = objptr(mod);
130                  /****** unnecessary test since modifier() is always called
# Line 116 | Line 133 | int  mod;
133                          error(USER, errmsg);
134                  }
135                  ******/
136 +                                        /* hack for irradiance calculation */
137 +                if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS))) {
138 +                        if (irr_ignore(m->otype)) {
139 +                                depth--;
140 +                                raytrans(r);
141 +                                return;
142 +                        }
143 +                        if (!islight(m->otype))
144 +                                m = &Lamb;
145 +                }
146                  (*ofun[m->otype].funp)(m, r);   /* execute function */
147                  m->lastrno = r->rno;
148                  if (ismaterial(m->otype)) {     /* materials call raytexture */
# Line 210 | Line 237 | register RAY  *r;
237           *  still fraught with problems since reflected rays and similar
238           *  directions calculated from the surface normal may spawn rays behind
239           *  the surface.  The only solution is to curb textures at high
240 <         *  incidence (Rdot << 1).
240 >         *  incidence (namely, keep DOT(rdir,pert) < Rdot).
241           */
242  
243          for (i = 0; i < 3; i++)
# Line 231 | Line 258 | register RAY  *r;
258   }
259  
260  
261 + newrayxf(r)                     /* get new tranformation matrix for ray */
262 + RAY  *r;
263 + {
264 +        static struct xfn {
265 +                struct xfn  *next;
266 +                FULLXF  xf;
267 +        }  xfseed = { &xfseed }, *xflast = &xfseed;
268 +        register struct xfn  *xp;
269 +        register RAY  *rp;
270 +
271 +        /*
272 +         * Search for transform in circular list that
273 +         * has no associated ray in the tree.
274 +         */
275 +        xp = xflast;
276 +        for (rp = r->parent; rp != NULL; rp = rp->parent)
277 +                if (rp->rox == &xp->xf) {               /* xp in use */
278 +                        xp = xp->next;                  /* move to next */
279 +                        if (xp == xflast) {             /* need new one */
280 +                                xp = (struct xfn *)bmalloc(sizeof(struct xfn));
281 +                                if (xp == NULL)
282 +                                        error(SYSTEM,
283 +                                                "out of memory in newrayxf");
284 +                                                        /* insert in list */
285 +                                xp->next = xflast->next;
286 +                                xflast->next = xp;
287 +                                break;                  /* we're done */
288 +                        }
289 +                        rp = r;                 /* start check over */
290 +                }
291 +                                        /* got it */
292 +        r->rox = &xp->xf;
293 +        xflast = xp;
294 + }
295 +
296 +
297   flipsurface(r)                  /* reverse surface orientation */
298   register RAY  *r;
299   {
# Line 249 | Line 312 | register RAY  *r;
312   register CUBE  *scene;
313   {
314          FVECT  curpos;                  /* current cube position */
315 <        int  mpos, mneg;                /* sign flags */
315 >        int  sflags;                    /* sign flags */
316          double  t, dt;
317          register int  i;
318  
319          nrays++;                        /* increment trace counter */
320 <
258 <        mpos = mneg = 0;
320 >        sflags = 0;
321          for (i = 0; i < 3; i++) {
322                  curpos[i] = r->rorg[i];
323                  if (r->rdir[i] > FTINY)
324 <                        mpos |= 1 << i;
324 >                        sflags |= 1 << i;
325                  else if (r->rdir[i] < -FTINY)
326 <                        mneg |= 1 << i;
326 >                        sflags |= 0x10 << i;
327          }
328 +        if (sflags == 0)
329 +                error(CONSISTENCY, "zero ray direction in localhit");
330          t = 0.0;
331          if (!incube(scene, curpos)) {
332                                          /* find distance to entry */
333                  for (i = 0; i < 3; i++) {
334                                          /* plane in our direction */
335 <                        if (mpos & 1<<i)
335 >                        if (sflags & 1<<i)
336                                  dt = scene->cuorg[i];
337 <                        else if (mneg & 1<<i)
337 >                        else if (sflags & 0x10<<i)
338                                  dt = scene->cuorg[i] + scene->cusize;
339                          else
340                                  continue;
# Line 287 | Line 351 | register CUBE  *scene;
351                  if (!incube(scene, curpos))     /* non-intersecting ray */
352                          return(0);
353          }
354 <        return(raymove(curpos, mpos, mneg, r, scene) == RAYHIT);
354 >        return(raymove(curpos, sflags, r, scene) == RAYHIT);
355   }
356  
357  
358   static int
359 < raymove(pos, plus, minus, r, cu)        /* check for hit as we move */
359 > raymove(pos, dirf, r, cu)               /* check for hit as we move */
360   FVECT  pos;                     /* modified */
361 < int  plus, minus;               /* direction indicators to speed tests */
361 > int  dirf;                      /* direction indicators to speed tests */
362   register RAY  *r;
363   register CUBE  *cu;
364   {
365          int  ax;
366          double  dt, t;
303        register int  sgn;
367  
368          if (istree(cu->cutree)) {               /* recurse on subcubes */
369                  CUBE  cukid;
370 <                register int  br;
370 >                register int  br, sgn;
371  
372                  cukid.cusize = cu->cusize * 0.5;        /* find subcube */
373                  VCOPY(cukid.cuorg, cu->cuorg);
# Line 323 | Line 386 | register CUBE  *cu;
386                  }
387                  for ( ; ; ) {
388                          cukid.cutree = octkid(cu->cutree, br);
389 <                        if ((ax = raymove(pos,plus,minus,r,&cukid)) == RAYHIT)
389 >                        if ((ax = raymove(pos,dirf,r,&cukid)) == RAYHIT)
390                                  return(RAYHIT);
391                          sgn = 1 << ax;
392 <                        if (sgn & minus)                /* negative axis? */
330 <                                if (sgn & br) {
331 <                                        cukid.cuorg[ax] -= cukid.cusize;
332 <                                        br &= ~sgn;
333 <                                } else
334 <                                        return(ax);     /* underflow */
335 <                        else
392 >                        if (sgn & dirf)                 /* positive axis? */
393                                  if (sgn & br)
394                                          return(ax);     /* overflow */
395                                  else {
396                                          cukid.cuorg[ax] += cukid.cusize;
397                                          br |= sgn;
398                                  }
399 +                        else
400 +                                if (sgn & br) {
401 +                                        cukid.cuorg[ax] -= cukid.cusize;
402 +                                        br &= ~sgn;
403 +                                } else
404 +                                        return(ax);     /* underflow */
405                  }
406                  /*NOTREACHED*/
407          }
408          if (isfull(cu->cutree) && checkhit(r, cu))
409                  return(RAYHIT);
410                                          /* advance to next cube */
411 <        sgn = plus | minus;
412 <        if (sgn&1) {
350 <                dt = plus&1 ? cu->cuorg[0] + cu->cusize : cu->cuorg[0];
411 >        if (dirf&0x11) {
412 >                dt = dirf&1 ? cu->cuorg[0] + cu->cusize : cu->cuorg[0];
413                  t = (dt - pos[0])/r->rdir[0];
414                  ax = 0;
415          } else
416                  t = FHUGE;
417 <        if (sgn&2) {
418 <                dt = plus&2 ? cu->cuorg[1] + cu->cusize : cu->cuorg[1];
417 >        if (dirf&0x22) {
418 >                dt = dirf&2 ? cu->cuorg[1] + cu->cusize : cu->cuorg[1];
419                  dt = (dt - pos[1])/r->rdir[1];
420                  if (dt < t) {
421                          t = dt;
422                          ax = 1;
423                  }
424          }
425 <        if (sgn&4) {
426 <                dt = plus&4 ? cu->cuorg[2] + cu->cusize : cu->cuorg[2];
425 >        if (dirf&0x44) {
426 >                dt = dirf&4 ? cu->cuorg[2] + cu->cusize : cu->cuorg[2];
427                  dt = (dt - pos[2])/r->rdir[2];
428                  if (dt < t) {
429                          t = dt;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines