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 2.49 by greg, Tue Apr 19 01:15:06 2005 UTC vs.
Revision 2.65 by greg, Wed Feb 19 14:12:48 2014 UTC

# Line 13 | Line 13 | static const char RCSid[] = "$Id$";
13   #include  "source.h"
14   #include  "otypes.h"
15   #include  "otspecial.h"
16 + #include  "random.h"
17  
18   #define  MAXCSET        ((MAXSET+1)*2-1)        /* maximum check set size */
19  
20 < unsigned long  raynum = 0;              /* next unique ray number */
21 < unsigned long  nrays = 0;               /* number of calls to localhit */
20 > RNUMBER  raynum = 0;            /* next unique ray number */
21 > RNUMBER  nrays = 0;             /* number of calls to localhit */
22  
23   static RREAL  Lambfa[5] = {PI, PI, PI, 0.0, 0.0};
24   OBJREC  Lamb = {
25          OVOID, MAT_PLASTIC, "Lambertian",
26 <        {0, 5, NULL, Lambfa}, NULL,
26 >        {NULL, Lambfa, 0, 5}, NULL
27   };                                      /* a Lambertian surface */
28  
29   OBJREC  Aftplane;                       /* aft clipping plane object */
# Line 34 | Line 35 | static int checkhit(RAY  *r, CUBE  *cu, OBJECT  *cxs);
35   static void checkset(OBJECT  *os, OBJECT  *cs);
36  
37  
38 < extern int
38 > int
39   rayorigin(              /* start new ray from old one */
40          RAY  *r,
41          int  rt,
# Line 58 | Line 59 | rayorigin(             /* start new ray from old one */
59                  r->crtype = r->rtype = rt;
60                  r->rsrc = -1;
61                  r->clipset = NULL;
62 +                r->revf = raytrace;
63                  copycolor(r->cext, cextinction);
64                  copycolor(r->albedo, salbedo);
65                  r->gecc = seccg;
# Line 78 | Line 80 | rayorigin(             /* start new ray from old one */
80                          r->clipset = ro->newcset;
81                          r->rmax = ro->rmax <= FTINY ? 0.0 : ro->rmax - ro->rot;
82                  }
83 +                r->revf = ro->revf;
84                  copycolor(r->cext, ro->cext);
85                  copycolor(r->albedo, ro->albedo);
86                  r->gecc = ro->gecc;
# Line 90 | Line 93 | rayorigin(             /* start new ray from old one */
93                                  colval(ro->cext,RED) : colval(ro->cext,GRN);
94                  if (colval(ro->cext,BLU) < re) re = colval(ro->cext,BLU);
95                  re *= ro->rot;
96 <                if (re > .1)
97 <                        r->rweight *= exp(-re);
96 >                if (re > 0.1) {
97 >                        if (re > 92.) {
98 >                                r->rweight = 0.0;
99 >                        } else {
100 >                                r->rweight *= exp(-re);
101 >                        }
102 >                }
103          }
104          rayclear(r);
105 <        return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1);
105 >        if (r->rweight <= 0.0)                  /* check for expiration */
106 >                return(-1);
107 >        if (r->crtype & SHADOW)                 /* shadow commitment */
108 >                return(0);
109 >        if (maxdepth <= 0 && rc != NULL) {      /* Russian roulette */
110 >                if (minweight <= 0.0)
111 >                        error(USER, "zero ray weight in Russian roulette");
112 >                if (maxdepth < 0 && r->rlvl > -maxdepth)
113 >                        return(-1);             /* upper reflection limit */
114 >                if (r->rweight >= minweight)
115 >                        return(0);
116 >                if (frandom() > r->rweight/minweight)
117 >                        return(-1);
118 >                rw = minweight/r->rweight;      /* promote survivor */
119 >                scalecolor(r->rcoef, rw);
120 >                r->rweight = minweight;
121 >                return(0);
122 >        }
123 >        return(r->rweight >= minweight && r->rlvl <= abs(maxdepth) ? 0 : -1);
124   }
125  
126  
127 < extern void
127 > void
128   rayclear(                       /* clear a ray for (re)evaluation */
129 <        register RAY  *r
129 >        RAY  *r
130   )
131   {
132          r->rno = raynum++;
# Line 117 | Line 143 | rayclear(                      /* clear a ray for (re)evaluation */
143   }
144  
145  
146 < extern void
147 < rayvalue(                       /* trace a ray and compute its value */
146 > void
147 > raytrace(                       /* trace a ray and compute its value */
148          RAY  *r
149   )
150   {
# Line 137 | Line 163 | rayvalue(                      /* trace a ray and compute its value */
163   }
164  
165  
166 < extern void
166 > void
167   raycont(                        /* check for clipped object and continue */
168 <        register RAY  *r
168 >        RAY  *r
169   )
170   {
171          if ((r->clipset != NULL && inset(r->clipset, r->ro->omod)) ||
# Line 148 | Line 174 | raycont(                       /* check for clipped object and continue */
174   }
175  
176  
177 < extern void
177 > void
178   raytrans(                       /* transmit ray as is */
179 <        register RAY  *r
179 >        RAY  *r
180   )
181   {
182          RAY  tr;
# Line 164 | Line 190 | raytrans(                      /* transmit ray as is */
190   }
191  
192  
193 < extern int
193 > int
194   rayshade(               /* shade ray r with material mod */
195 <        register RAY  *r,
195 >        RAY  *r,
196          int  mod
197   )
198   {
199 <        register OBJREC  *m;
199 >        OBJREC  *m;
200  
201          r->rt = r->rot;                 /* set effective ray length */
202          for ( ; mod != OVOID; mod = m->omod) {
# Line 199 | Line 225 | rayshade(              /* shade ray r with material mod */
225   }
226  
227  
228 < extern void
228 > void
229   rayparticipate(                 /* compute ray medium participation */
230 <        register RAY  *r
230 >        RAY  *r
231   )
232   {
233          COLOR   ce, ca;
# Line 232 | Line 258 | rayparticipate(                        /* compute ray medium participation
258   }
259  
260  
261 < extern void
261 > void
262   raytexture(                     /* get material modifiers */
263          RAY  *r,
264          OBJECT  mod
265   )
266   {
267 <        register OBJREC  *m;
267 >        OBJREC  *m;
268                                          /* execute textures and patterns */
269          for ( ; mod != OVOID; mod = m->omod) {
270                  m = objptr(mod);
# Line 257 | Line 283 | raytexture(                    /* get material modifiers */
283   }
284  
285  
286 < extern int
286 > int
287   raymixture(             /* mix modifiers */
288 <        register RAY  *r,
288 >        RAY  *r,
289          OBJECT  fore,
290          OBJECT  back,
291          double  coef
# Line 267 | Line 293 | raymixture(            /* mix modifiers */
293   {
294          RAY  fr, br;
295          int  foremat, backmat;
296 <        register int  i;
296 >        int  i;
297                                          /* bound coefficient */
298          if (coef > 1.0)
299                  coef = 1.0;
# Line 277 | Line 303 | raymixture(            /* mix modifiers */
303          foremat = backmat = 0;
304                                          /* foreground */
305          fr = *r;
306 <        if (coef > FTINY)
306 >        if (coef > FTINY) {
307 >                fr.rweight *= coef;
308 >                scalecolor(fr.rcoef, coef);
309                  foremat = rayshade(&fr, fore);
310 +        }
311                                          /* background */
312          br = *r;
313 <        if (coef < 1.0-FTINY)
313 >        if (coef < 1.0-FTINY) {
314 >                br.rweight *= 1.0-coef;
315 >                scalecolor(br.rcoef, 1.0-coef);
316                  backmat = rayshade(&br, back);
317 +        }
318                                          /* check for transparency */
319          if (backmat ^ foremat) {
320                  if (backmat && coef > FTINY)
# Line 311 | Line 343 | raymixture(            /* mix modifiers */
343   }
344  
345  
346 < extern double
346 > double
347   raydist(                /* compute (cumulative) ray distance */
348 <        register const RAY  *r,
349 <        register int  flags
348 >        const RAY  *r,
349 >        int  flags
350   )
351   {
352          double  sum = 0.0;
# Line 327 | Line 359 | raydist(               /* compute (cumulative) ray distance */
359   }
360  
361  
362 < extern void
362 > void
363   raycontrib(             /* compute (cumulative) ray contribution */
364 <        COLOR  rc,
364 >        RREAL  rc[3],
365          const RAY  *r,
366          int  flags
367   )
368   {
369 <        COLOR   eext, ext1;
370 <        
339 <        setcolor(eext, 0., 0., 0.);
340 <        setcolor(rc, 1., 1., 1.);
369 >        double  eext[3];
370 >        int     i;
371  
372 +        eext[0] = eext[1] = eext[2] = 0.;
373 +        rc[0] = rc[1] = rc[2] = 1.;
374 +
375          while (r != NULL && r->crtype&flags) {
376 <                multcolor(rc, r->rcoef);
377 <                copycolor(ext1, r->cext);
378 <                scalecolor(ext1, r->rot);
379 <                addcolor(eext, ext1);
376 >                for (i = 3; i--; ) {
377 >                        rc[i] *= colval(r->rcoef,i);
378 >                        eext[i] += r->rot * colval(r->cext,i);
379 >                }
380                  r = r->parent;
381          }
382 <        if (intens(eext) > FTINY) {
383 <                setcolor(ext1,  exp(-colval(eext,RED)),
384 <                                exp(-colval(eext,GRN)),
352 <                                exp(-colval(eext,BLU)));
353 <                multcolor(rc, ext1);
354 <        }
382 >        for (i = 3; i--; )
383 >                rc[i] *= (eext[i] <= FTINY) ? 1. :
384 >                                (eext[i] > 92.) ? 0. : exp(-eext[i]);
385   }
386  
387  
388 < extern double
388 > double
389   raynormal(              /* compute perturbed normal for ray */
390          FVECT  norm,
391 <        register RAY  *r
391 >        RAY  *r
392   )
393   {
394          double  newdot;
395 <        register int  i;
395 >        int  i;
396  
397          /*      The perturbation is added to the surface normal to obtain
398           *  the new normal.  If the new normal would affect the surface
# Line 391 | Line 421 | raynormal(             /* compute perturbed normal for ray */
421   }
422  
423  
424 < extern void
424 > void
425   newrayxf(                       /* get new tranformation matrix for ray */
426          RAY  *r
427   )
# Line 400 | Line 430 | newrayxf(                      /* get new tranformation matrix for ray */
430                  struct xfn  *next;
431                  FULLXF  xf;
432          }  xfseed = { &xfseed }, *xflast = &xfseed;
433 <        register struct xfn  *xp;
434 <        register const RAY  *rp;
433 >        struct xfn  *xp;
434 >        const RAY  *rp;
435  
436          /*
437           * Search for transform in circular list that
# Line 412 | Line 442 | newrayxf(                      /* get new tranformation matrix for ray */
442                  if (rp->rox == &xp->xf) {               /* xp in use */
443                          xp = xp->next;                  /* move to next */
444                          if (xp == xflast) {             /* need new one */
445 <                                xp = (struct xfn *)malloc(sizeof(struct xfn));
445 >                                xp = (struct xfn *)bmalloc(sizeof(struct xfn));
446                                  if (xp == NULL)
447                                          error(SYSTEM,
448                                                  "out of memory in newrayxf");
# Line 429 | Line 459 | newrayxf(                      /* get new tranformation matrix for ray */
459   }
460  
461  
462 < extern void
462 > void
463   flipsurface(                    /* reverse surface orientation */
464 <        register RAY  *r
464 >        RAY  *r
465   )
466   {
467          r->rod = -r->rod;
# Line 444 | Line 474 | flipsurface(                   /* reverse surface orientation */
474   }
475  
476  
477 < extern void
477 > void
478   rayhit(                 /* standard ray hit test */
479          OBJECT  *oset,
480          RAY  *r
# Line 461 | Line 491 | rayhit(                        /* standard ray hit test */
491   }
492  
493  
494 < extern int
494 > int
495   localhit(               /* check for hit in the octree */
496 <        register RAY  *r,
497 <        register CUBE  *scene
496 >        RAY  *r,
497 >        CUBE  *scene
498   )
499   {
500          OBJECT  cxset[MAXCSET+1];       /* set of checked objects */
501          FVECT  curpos;                  /* current cube position */
502          int  sflags;                    /* sign flags */
503          double  t, dt;
504 <        register int  i;
504 >        int  i;
505  
506          nrays++;                        /* increment trace counter */
507          sflags = 0;
# Line 482 | Line 512 | localhit(              /* check for hit in the octree */
512                  else if (r->rdir[i] < -1e-7)
513                          sflags |= 0x10 << i;
514          }
515 <        if (sflags == 0)
516 <                error(CONSISTENCY, "zero ray direction in localhit");
515 >        if (!sflags) {
516 >                error(WARNING, "zero ray direction in localhit");
517 >                return(0);
518 >        }
519                                          /* start off assuming nothing hit */
520          if (r->rmax > FTINY) {          /* except aft plane if one */
521                  r->ro = &Aftplane;
522                  r->rot = r->rmax;
523 <                for (i = 0; i < 3; i++)
492 <                        r->rop[i] = r->rorg[i] + r->rot*r->rdir[i];
523 >                VSUM(r->rop, r->rorg, r->rdir, r->rot);
524          }
525                                          /* find global cube entrance point */
526          t = 0.0;
# Line 512 | Line 543 | localhit(              /* check for hit in the octree */
543                  if (t >= r->rot)        /* clipped already */
544                          return(0);
545                                          /* advance position */
546 <                for (i = 0; i < 3; i++)
516 <                        curpos[i] += r->rdir[i]*t;
546 >                VSUM(curpos, curpos, r->rdir, t);
547  
548                  if (!incube(scene, curpos))     /* non-intersecting ray */
549                          return(0);
# Line 529 | Line 559 | raymove(               /* check for hit as we move */
559          FVECT  pos,                     /* current position, modified herein */
560          OBJECT  *cxs,                   /* checked objects, modified by checkhit */
561          int  dirf,                      /* direction indicators to speed tests */
562 <        register RAY  *r,
563 <        register CUBE  *cu
562 >        RAY  *r,
563 >        CUBE  *cu
564   )
565   {
566          int  ax;
# Line 538 | Line 568 | raymove(               /* check for hit as we move */
568  
569          if (istree(cu->cutree)) {               /* recurse on subcubes */
570                  CUBE  cukid;
571 <                register int  br, sgn;
571 >                int  br, sgn;
572  
573                  cukid.cusize = cu->cusize * 0.5;        /* find subcube */
574                  VCOPY(cukid.cuorg, cu->cuorg);
# Line 604 | Line 634 | raymove(               /* check for hit as we move */
634                          ax = 2;
635                  }
636          }
637 <        pos[0] += r->rdir[0]*t;
608 <        pos[1] += r->rdir[1]*t;
609 <        pos[2] += r->rdir[2]*t;
637 >        VSUM(pos, pos, r->rdir, t);
638          return(ax);
639   }
640  
641  
642   static int
643   checkhit(               /* check for hit in full cube */
644 <        register RAY  *r,
644 >        RAY  *r,
645          CUBE  *cu,
646          OBJECT  *cxs
647   )
# Line 634 | Line 662 | checkhit(              /* check for hit in full cube */
662  
663   static void
664   checkset(               /* modify checked set and set to check */
665 <        register OBJECT  *os,                   /* os' = os - cs */
666 <        register OBJECT  *cs                    /* cs' = cs + os */
665 >        OBJECT  *os,                    /* os' = os - cs */
666 >        OBJECT  *cs                     /* cs' = cs + os */
667   )
668   {
669          OBJECT  cset[MAXCSET+MAXSET+1];
670 <        register int  i, j;
670 >        int  i, j;
671          int  k;
672                                          /* copy os in place, cset <- cs */
673          cset[0] = 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines