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.46 by greg, Thu Mar 10 22:37:00 2005 UTC vs.
Revision 2.66 by greg, Tue Feb 24 19:39:27 2015 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 + #include  "pmap.h"
18  
19   #define  MAXCSET        ((MAXSET+1)*2-1)        /* maximum check set size */
20  
21 < unsigned long  raynum = 0;              /* next unique ray number */
22 < unsigned long  nrays = 0;               /* number of calls to localhit */
21 > RNUMBER  raynum = 0;            /* next unique ray number */
22 > RNUMBER  nrays = 0;             /* number of calls to localhit */
23  
24   static RREAL  Lambfa[5] = {PI, PI, PI, 0.0, 0.0};
25   OBJREC  Lamb = {
26          OVOID, MAT_PLASTIC, "Lambertian",
27 <        {0, 5, NULL, Lambfa}, NULL,
27 >        {NULL, Lambfa, 0, 5}, NULL
28   };                                      /* a Lambertian surface */
29  
30   OBJREC  Aftplane;                       /* aft clipping plane object */
# Line 34 | Line 36 | static int checkhit(RAY  *r, CUBE  *cu, OBJECT  *cxs);
36   static void checkset(OBJECT  *os, OBJECT  *cs);
37  
38  
39 < extern int
39 > int
40   rayorigin(              /* start new ray from old one */
41 <        register RAY  *r,
40 <        register RAY  *ro,
41 >        RAY  *r,
42          int  rt,
43 <        double  rw
43 >        const RAY  *ro,
44 >        const COLOR rc
45   )
46   {
47 <        double  re;
48 <
47 >        double  rw, re;
48 >                                                /* assign coefficient/weight */
49 >        if (rc == NULL) {
50 >                rw = 1.0;
51 >                setcolor(r->rcoef, 1., 1., 1.);
52 >        } else {
53 >                rw = intens(rc);
54 >                if (rc != r->rcoef)
55 >                        copycolor(r->rcoef, rc);
56 >        }
57          if ((r->parent = ro) == NULL) {         /* primary ray */
58                  r->rlvl = 0;
59                  r->rweight = rw;
# Line 55 | Line 65 | rayorigin(             /* start new ray from old one */
65                  copycolor(r->albedo, salbedo);
66                  r->gecc = seccg;
67                  r->slights = NULL;
58        } else if (ro->rot >= FHUGE) {          /* illegal continuation */
59                memset(r, 0, sizeof(RAY));
60                return(-1);
68          } else {                                /* spawned ray */
69 +                if (ro->rot >= FHUGE) {
70 +                        memset(r, 0, sizeof(RAY));
71 +                        return(-1);             /* illegal continuation */
72 +                }
73                  r->rlvl = ro->rlvl;
74                  if (rt & RAYREFL) {
75                          r->rlvl++;
# Line 78 | Line 89 | rayorigin(             /* start new ray from old one */
89                  r->crtype = ro->crtype | (r->rtype = rt);
90                  VCOPY(r->rorg, ro->rop);
91                  r->rweight = ro->rweight * rw;
92 <                                                /* estimate absorption */
92 >                                                /* estimate extinction */
93                  re = colval(ro->cext,RED) < colval(ro->cext,GRN) ?
94                                  colval(ro->cext,RED) : colval(ro->cext,GRN);
95                  if (colval(ro->cext,BLU) < re) re = colval(ro->cext,BLU);
96 <                if (re > 0.)
97 <                        r->rweight *= exp(-re*ro->rot);
96 >                re *= ro->rot;
97 >                if (re > 0.1) {
98 >                        if (re > 92.) {
99 >                                r->rweight = 0.0;
100 >                        } else {
101 >                                r->rweight *= exp(-re);
102 >                        }
103 >                }
104          }
105          rayclear(r);
106 <        return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1);
106 >        if (r->rweight <= 0.0)                  /* check for expiration */
107 >                return(-1);
108 >        if (r->crtype & SHADOW)                 /* shadow commitment */
109 >                return(0);
110 >        if (maxdepth <= 0 && rc != NULL) {      /* Russian roulette */
111 >                if (minweight <= 0.0)
112 >                        error(USER, "zero ray weight in Russian roulette");
113 >                if (maxdepth < 0 && r->rlvl > -maxdepth)
114 >                        return(-1);             /* upper reflection limit */
115 >                if (r->rweight >= minweight)
116 >                        return(0);
117 >                if (frandom() > r->rweight/minweight)
118 >                        return(-1);
119 >                rw = minweight/r->rweight;      /* promote survivor */
120 >                scalecolor(r->rcoef, rw);
121 >                r->rweight = minweight;
122 >                return(0);
123 >        }
124 >        return(r->rweight >= minweight && r->rlvl <= abs(maxdepth) ? 0 : -1);
125   }
126  
127  
128 < extern void
128 > void
129   rayclear(                       /* clear a ray for (re)evaluation */
130 <        register RAY  *r
130 >        RAY  *r
131   )
132   {
133          r->rno = raynum++;
# Line 109 | Line 144 | rayclear(                      /* clear a ray for (re)evaluation */
144   }
145  
146  
147 < extern void
147 > void
148   raytrace(                       /* trace a ray and compute its value */
149          RAY  *r
150   )
# Line 122 | Line 157 | raytrace(                      /* trace a ray and compute its value */
157          } else if (sourcehit(r))
158                  rayshade(r, r->ro->omod);       /* distant source */
159  
125        rayparticipate(r);              /* for participating medium */
126
160          if (trace != NULL)
161                  (*trace)(r);            /* trace execution */
162 +
163 +        rayparticipate(r);              /* for participating medium */
164   }
165  
166  
167 < extern void
167 > void
168   raycont(                        /* check for clipped object and continue */
169 <        register RAY  *r
169 >        RAY  *r
170   )
171   {
172          if ((r->clipset != NULL && inset(r->clipset, r->ro->omod)) ||
# Line 140 | Line 175 | raycont(                       /* check for clipped object and continue */
175   }
176  
177  
178 < extern void
178 > void
179   raytrans(                       /* transmit ray as is */
180 <        register RAY  *r
180 >        RAY  *r
181   )
182   {
183          RAY  tr;
184  
185 <        if (rayorigin(&tr, r, TRANS, 1.0) == 0) {
185 >        if (rayorigin(&tr, TRANS, r, NULL) == 0) {
186                  VCOPY(tr.rdir, r->rdir);
187                  rayvalue(&tr);
188                  copycolor(r->rcol, tr.rcol);
# Line 156 | Line 191 | raytrans(                      /* transmit ray as is */
191   }
192  
193  
194 < extern int
194 > int
195   rayshade(               /* shade ray r with material mod */
196 <        register RAY  *r,
196 >        RAY  *r,
197          int  mod
198   )
199   {
200 <        int  gotmat;
201 <        register OBJREC  *m;
200 >        OBJREC  *m;
201 >
202          r->rt = r->rot;                 /* set effective ray length */
203 <        for (gotmat = 0; !gotmat && mod != OVOID; mod = m->omod) {
203 >        for ( ; mod != OVOID; mod = m->omod) {
204                  m = objptr(mod);
205                  /****** unnecessary test since modifier() is always called
206                  if (!ismodifier(m->otype)) {
# Line 184 | Line 219 | rayshade(              /* shade ray r with material mod */
219                          if (!islight(m->otype))
220                                  m = &Lamb;
221                  }
222 <                                        /* materials call raytexture */
223 <                gotmat = (*ofun[m->otype].funp)(m, r);
222 >                if ((*ofun[m->otype].funp)(m, r))
223 >                        return(1);      /* materials call raytexture() */
224          }
225 <        return(gotmat);
225 >        return(0);                      /* no material! */
226   }
227  
228  
229 < extern void
229 > void
230   rayparticipate(                 /* compute ray medium participation */
231 <        register RAY  *r
231 >        RAY  *r
232   )
233   {
234          COLOR   ce, ca;
# Line 209 | Line 244 | rayparticipate(                        /* compute ray medium participation
244                  ge *= 1. - colval(r->albedo,GRN);
245                  be *= 1. - colval(r->albedo,BLU);
246          }
247 <        setcolor(ce,    re<=0. ? 1. : re>92. ? 0. : exp(-re),
248 <                        ge<=0. ? 1. : ge>92. ? 0. : exp(-ge),
249 <                        be<=0. ? 1. : be>92. ? 0. : exp(-be));
250 <        multcolor(r->rcol, ce);                 /* path absorption */
247 >        setcolor(ce,    re<=FTINY ? 1. : re>92. ? 0. : exp(-re),
248 >                        ge<=FTINY ? 1. : ge>92. ? 0. : exp(-ge),
249 >                        be<=FTINY ? 1. : be>92. ? 0. : exp(-be));
250 >        multcolor(r->rcol, ce);                 /* path extinction */
251          if (r->crtype & SHADOW || intens(r->albedo) <= FTINY)
252                  return;                         /* no scattering */
253 <        setcolor(ca,
254 <                colval(r->albedo,RED)*colval(ambval,RED)*(1.-colval(ce,RED)),
255 <                colval(r->albedo,GRN)*colval(ambval,GRN)*(1.-colval(ce,GRN)),
256 <                colval(r->albedo,BLU)*colval(ambval,BLU)*(1.-colval(ce,BLU)));
257 <        addcolor(r->rcol, ca);                  /* ambient in scattering */
253 >        
254 >        /* PMAP: indirect inscattering accounted for by volume photons? */
255 >        if (!volumePhotonMapping) {
256 >                setcolor(ca,
257 >                        colval(r->albedo,RED)*colval(ambval,RED)*(1.-colval(ce,RED)),
258 >                        colval(r->albedo,GRN)*colval(ambval,GRN)*(1.-colval(ce,GRN)),
259 >                        colval(r->albedo,BLU)*colval(ambval,BLU)*(1.-colval(ce,BLU)));
260 >                addcolor(r->rcol, ca);                  /* ambient in scattering */
261 >        }
262 >        
263          srcscatter(r);                          /* source in scattering */
264   }
265  
266  
267 < extern void
267 > void
268   raytexture(                     /* get material modifiers */
269          RAY  *r,
270          OBJECT  mod
271   )
272   {
273 <        register OBJREC  *m;
273 >        OBJREC  *m;
274                                          /* execute textures and patterns */
275          for ( ; mod != OVOID; mod = m->omod) {
276                  m = objptr(mod);
# Line 249 | Line 289 | raytexture(                    /* get material modifiers */
289   }
290  
291  
292 < extern int
292 > int
293   raymixture(             /* mix modifiers */
294 <        register RAY  *r,
294 >        RAY  *r,
295          OBJECT  fore,
296          OBJECT  back,
297          double  coef
# Line 259 | Line 299 | raymixture(            /* mix modifiers */
299   {
300          RAY  fr, br;
301          int  foremat, backmat;
302 <        register int  i;
302 >        int  i;
303                                          /* bound coefficient */
304          if (coef > 1.0)
305                  coef = 1.0;
# Line 269 | Line 309 | raymixture(            /* mix modifiers */
309          foremat = backmat = 0;
310                                          /* foreground */
311          fr = *r;
312 <        if (coef > FTINY)
312 >        if (coef > FTINY) {
313 >                fr.rweight *= coef;
314 >                scalecolor(fr.rcoef, coef);
315                  foremat = rayshade(&fr, fore);
316 +        }
317                                          /* background */
318          br = *r;
319 <        if (coef < 1.0-FTINY)
319 >        if (coef < 1.0-FTINY) {
320 >                br.rweight *= 1.0-coef;
321 >                scalecolor(br.rcoef, 1.0-coef);
322                  backmat = rayshade(&br, back);
323 +        }
324                                          /* check for transparency */
325          if (backmat ^ foremat) {
326                  if (backmat && coef > FTINY)
# Line 303 | Line 349 | raymixture(            /* mix modifiers */
349   }
350  
351  
352 < extern double
352 > double
353   raydist(                /* compute (cumulative) ray distance */
354 <        register RAY  *r,
355 <        register int  flags
354 >        const RAY  *r,
355 >        int  flags
356   )
357   {
358          double  sum = 0.0;
# Line 319 | Line 365 | raydist(               /* compute (cumulative) ray distance */
365   }
366  
367  
368 < extern double
368 > void
369 > raycontrib(             /* compute (cumulative) ray contribution */
370 >        RREAL  rc[3],
371 >        const RAY  *r,
372 >        int  flags
373 > )
374 > {
375 >        double  eext[3];
376 >        int     i;
377 >
378 >        eext[0] = eext[1] = eext[2] = 0.;
379 >        rc[0] = rc[1] = rc[2] = 1.;
380 >
381 >        while (r != NULL && r->crtype&flags) {
382 >                for (i = 3; i--; ) {
383 >                        rc[i] *= colval(r->rcoef,i);
384 >                        eext[i] += r->rot * colval(r->cext,i);
385 >                }
386 >                r = r->parent;
387 >        }
388 >        for (i = 3; i--; )
389 >                rc[i] *= (eext[i] <= FTINY) ? 1. :
390 >                                (eext[i] > 92.) ? 0. : exp(-eext[i]);
391 > }
392 >
393 >
394 > double
395   raynormal(              /* compute perturbed normal for ray */
396          FVECT  norm,
397 <        register RAY  *r
397 >        RAY  *r
398   )
399   {
400          double  newdot;
401 <        register int  i;
401 >        int  i;
402  
403          /*      The perturbation is added to the surface normal to obtain
404           *  the new normal.  If the new normal would affect the surface
# Line 355 | Line 427 | raynormal(             /* compute perturbed normal for ray */
427   }
428  
429  
430 < extern void
430 > void
431   newrayxf(                       /* get new tranformation matrix for ray */
432          RAY  *r
433   )
# Line 364 | Line 436 | newrayxf(                      /* get new tranformation matrix for ray */
436                  struct xfn  *next;
437                  FULLXF  xf;
438          }  xfseed = { &xfseed }, *xflast = &xfseed;
439 <        register struct xfn  *xp;
440 <        register RAY  *rp;
439 >        struct xfn  *xp;
440 >        const RAY  *rp;
441  
442          /*
443           * Search for transform in circular list that
# Line 376 | Line 448 | newrayxf(                      /* get new tranformation matrix for ray */
448                  if (rp->rox == &xp->xf) {               /* xp in use */
449                          xp = xp->next;                  /* move to next */
450                          if (xp == xflast) {             /* need new one */
451 <                                xp = (struct xfn *)malloc(sizeof(struct xfn));
451 >                                xp = (struct xfn *)bmalloc(sizeof(struct xfn));
452                                  if (xp == NULL)
453                                          error(SYSTEM,
454                                                  "out of memory in newrayxf");
# Line 393 | Line 465 | newrayxf(                      /* get new tranformation matrix for ray */
465   }
466  
467  
468 < extern void
468 > void
469   flipsurface(                    /* reverse surface orientation */
470 <        register RAY  *r
470 >        RAY  *r
471   )
472   {
473          r->rod = -r->rod;
# Line 408 | Line 480 | flipsurface(                   /* reverse surface orientation */
480   }
481  
482  
483 < extern void
483 > void
484   rayhit(                 /* standard ray hit test */
485          OBJECT  *oset,
486          RAY  *r
# Line 425 | Line 497 | rayhit(                        /* standard ray hit test */
497   }
498  
499  
500 < extern int
500 > int
501   localhit(               /* check for hit in the octree */
502 <        register RAY  *r,
503 <        register CUBE  *scene
502 >        RAY  *r,
503 >        CUBE  *scene
504   )
505   {
506          OBJECT  cxset[MAXCSET+1];       /* set of checked objects */
507          FVECT  curpos;                  /* current cube position */
508          int  sflags;                    /* sign flags */
509          double  t, dt;
510 <        register int  i;
510 >        int  i;
511  
512          nrays++;                        /* increment trace counter */
513          sflags = 0;
# Line 446 | Line 518 | localhit(              /* check for hit in the octree */
518                  else if (r->rdir[i] < -1e-7)
519                          sflags |= 0x10 << i;
520          }
521 <        if (sflags == 0)
522 <                error(CONSISTENCY, "zero ray direction in localhit");
521 >        if (!sflags) {
522 >                error(WARNING, "zero ray direction in localhit");
523 >                return(0);
524 >        }
525                                          /* start off assuming nothing hit */
526          if (r->rmax > FTINY) {          /* except aft plane if one */
527                  r->ro = &Aftplane;
528                  r->rot = r->rmax;
529 <                for (i = 0; i < 3; i++)
456 <                        r->rop[i] = r->rorg[i] + r->rot*r->rdir[i];
529 >                VSUM(r->rop, r->rorg, r->rdir, r->rot);
530          }
531                                          /* find global cube entrance point */
532          t = 0.0;
# Line 476 | Line 549 | localhit(              /* check for hit in the octree */
549                  if (t >= r->rot)        /* clipped already */
550                          return(0);
551                                          /* advance position */
552 <                for (i = 0; i < 3; i++)
480 <                        curpos[i] += r->rdir[i]*t;
552 >                VSUM(curpos, curpos, r->rdir, t);
553  
554                  if (!incube(scene, curpos))     /* non-intersecting ray */
555                          return(0);
# Line 493 | Line 565 | raymove(               /* check for hit as we move */
565          FVECT  pos,                     /* current position, modified herein */
566          OBJECT  *cxs,                   /* checked objects, modified by checkhit */
567          int  dirf,                      /* direction indicators to speed tests */
568 <        register RAY  *r,
569 <        register CUBE  *cu
568 >        RAY  *r,
569 >        CUBE  *cu
570   )
571   {
572          int  ax;
# Line 502 | Line 574 | raymove(               /* check for hit as we move */
574  
575          if (istree(cu->cutree)) {               /* recurse on subcubes */
576                  CUBE  cukid;
577 <                register int  br, sgn;
577 >                int  br, sgn;
578  
579                  cukid.cusize = cu->cusize * 0.5;        /* find subcube */
580                  VCOPY(cukid.cuorg, cu->cuorg);
# Line 568 | Line 640 | raymove(               /* check for hit as we move */
640                          ax = 2;
641                  }
642          }
643 <        pos[0] += r->rdir[0]*t;
572 <        pos[1] += r->rdir[1]*t;
573 <        pos[2] += r->rdir[2]*t;
643 >        VSUM(pos, pos, r->rdir, t);
644          return(ax);
645   }
646  
647  
648   static int
649   checkhit(               /* check for hit in full cube */
650 <        register RAY  *r,
650 >        RAY  *r,
651          CUBE  *cu,
652          OBJECT  *cxs
653   )
# Line 598 | Line 668 | checkhit(              /* check for hit in full cube */
668  
669   static void
670   checkset(               /* modify checked set and set to check */
671 <        register OBJECT  *os,                   /* os' = os - cs */
672 <        register OBJECT  *cs                    /* cs' = cs + os */
671 >        OBJECT  *os,                    /* os' = os - cs */
672 >        OBJECT  *cs                     /* cs' = cs + os */
673   )
674   {
675          OBJECT  cset[MAXCSET+MAXSET+1];
676 <        register int  i, j;
676 >        int  i, j;
677          int  k;
678                                          /* copy os in place, cset <- cs */
679          cset[0] = 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines