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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines