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.20 by greg, Fri Sep 29 10:46:12 1995 UTC vs.
Revision 2.58 by schorsch, Wed Jun 7 17:52:04 2006 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1995 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char RCSid[] = "$Id$";
3   #endif
6
4   /*
5   *  raytrace.c - routines for tracing and shading rays.
6   *
7 < *     8/7/85
7 > *  External symbols declared in ray.h
8   */
9  
10 < #include  "ray.h"
10 > #include "copyright.h"
11  
12 < #include  "octree.h"
13 <
12 > #include  "ray.h"
13 > #include  "source.h"
14   #include  "otypes.h"
18
15   #include  "otspecial.h"
16 + #include  "random.h"
17  
18   #define  MAXCSET        ((MAXSET+1)*2-1)        /* maximum check set size */
19  
23 extern CUBE  thescene;                  /* our scene */
24 extern int  maxdepth;                   /* maximum recursion depth */
25 extern double  minweight;               /* minimum ray weight */
26 extern int  do_irrad;                   /* compute irradiance? */
27
20   unsigned long  raynum = 0;              /* next unique ray number */
21   unsigned long  nrays = 0;               /* number of calls to localhit */
22  
23 < static FLOAT  Lambfa[5] = {PI, PI, PI, 0.0, 0.0};
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 */
30  
39 static int  raymove(), checkset(), checkhit();
40
41 #define  MAXLOOP        128             /* modifier loop detection */
42
31   #define  RAYHIT         (-1)            /* return value for intercepted ray */
32  
33 + static int raymove(FVECT  pos, OBJECT  *cxs, int  dirf, RAY  *r, CUBE  *cu);
34 + static int checkhit(RAY  *r, CUBE  *cu, OBJECT  *cxs);
35 + static void checkset(OBJECT  *os, OBJECT  *cs);
36  
37 < rayorigin(r, ro, rt, rw)                /* start new ray from old one */
38 < register RAY  *r, *ro;
39 < int  rt;
40 < double  rw;
37 >
38 > extern int
39 > rayorigin(              /* start new ray from old one */
40 >        RAY  *r,
41 >        int  rt,
42 >        const RAY  *ro,
43 >        const COLOR rc
44 > )
45   {
46 +        double  rw, re;
47 +                                                /* assign coefficient/weight */
48 +        if (rc == NULL) {
49 +                rw = 1.0;
50 +                setcolor(r->rcoef, 1., 1., 1.);
51 +        } else {
52 +                rw = intens(rc);
53 +                if (rc != r->rcoef)
54 +                        copycolor(r->rcoef, rc);
55 +        }
56          if ((r->parent = ro) == NULL) {         /* primary ray */
57                  r->rlvl = 0;
58                  r->rweight = rw;
# Line 55 | Line 60 | double  rw;
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;
66 +                r->slights = NULL;
67          } else {                                /* spawned ray */
68 +                if (ro->rot >= FHUGE) {
69 +                        memset(r, 0, sizeof(RAY));
70 +                        return(-1);             /* illegal continuation */
71 +                }
72                  r->rlvl = ro->rlvl;
73                  if (rt & RAYREFL) {
74                          r->rlvl++;
75                          r->rsrc = -1;
76                          r->clipset = ro->clipset;
77 +                        r->rmax = 0.0;
78                  } else {
79                          r->rsrc = ro->rsrc;
80                          r->clipset = ro->newcset;
81 +                        r->rmax = ro->rmax <= FTINY ? 0.0 : ro->rmax - ro->rot;
82                  }
83                  r->revf = ro->revf;
84 <                r->rweight = ro->rweight * rw;
84 >                copycolor(r->cext, ro->cext);
85 >                copycolor(r->albedo, ro->albedo);
86 >                r->gecc = ro->gecc;
87 >                r->slights = ro->slights;
88                  r->crtype = ro->crtype | (r->rtype = rt);
89                  VCOPY(r->rorg, ro->rop);
90 <                r->rmax = 0.0;
90 >                r->rweight = ro->rweight * rw;
91 >                                                /* estimate extinction */
92 >                re = colval(ro->cext,RED) < colval(ro->cext,GRN) ?
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 > 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->rlvl <= abs(maxdepth) && r->rweight >= minweight ? 0 : -1);
124   }
125  
126  
127 < rayclear(r)                     /* clear a ray for (re)evaluation */
128 < register RAY  *r;
127 > extern void
128 > rayclear(                       /* clear a ray for (re)evaluation */
129 >        register RAY  *r
130 > )
131   {
132          r->rno = raynum++;
133          r->newcset = r->clipset;
134 +        r->hitf = rayhit;
135 +        r->robj = OVOID;
136          r->ro = NULL;
137 <        r->rot = FHUGE;
137 >        r->rox = NULL;
138 >        r->rt = r->rot = FHUGE;
139          r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
140 +        r->uv[0] = r->uv[1] = 0.0;
141          setcolor(r->pcol, 1.0, 1.0, 1.0);
142          setcolor(r->rcol, 0.0, 0.0, 0.0);
89        r->rt = 0.0;
143   }
144  
145  
146 < raytrace(r)                     /* trace a ray and compute its value */
147 < RAY  *r;
146 > extern void
147 > raytrace(                       /* trace a ray and compute its value */
148 >        RAY  *r
149 > )
150   {
96        extern int  (*trace)();
97        int  gotmat;
98
151          if (localhit(r, &thescene))
152 <                gotmat = raycont(r);
152 >                raycont(r);             /* hit local surface, evaluate */
153          else if (r->ro == &Aftplane) {
154 <                r->ro = NULL;
154 >                r->ro = NULL;           /* hit aft clipping plane */
155                  r->rot = FHUGE;
156          } else if (sourcehit(r))
157 <                gotmat = rayshade(r, r->ro->omod);
157 >                rayshade(r, r->ro->omod);       /* distant source */
158  
107        if (r->ro != NULL && !gotmat)
108                objerror(r->ro, USER, "material not found");
109
159          if (trace != NULL)
160                  (*trace)(r);            /* trace execution */
161 +
162 +        rayparticipate(r);              /* for participating medium */
163   }
164  
165  
166 < raycont(r)                      /* check for clipped object and continue */
167 < register RAY  *r;
166 > extern void
167 > raycont(                        /* check for clipped object and continue */
168 >        register RAY  *r
169 > )
170   {
171          if ((r->clipset != NULL && inset(r->clipset, r->ro->omod)) ||
172 <                        r->ro->omod == OVOID) {
172 >                        !rayshade(r, r->ro->omod))
173                  raytrans(r);
121                return(1);
122        }
123        return(rayshade(r, r->ro->omod));
174   }
175  
176  
177 < raytrans(r)                     /* transmit ray as is */
178 < register RAY  *r;
177 > extern void
178 > raytrans(                       /* transmit ray as is */
179 >        register RAY  *r
180 > )
181   {
182          RAY  tr;
183  
184 <        if (rayorigin(&tr, r, TRANS, 1.0) == 0) {
184 >        if (rayorigin(&tr, TRANS, r, NULL) == 0) {
185                  VCOPY(tr.rdir, r->rdir);
134                if (r->rmax > FTINY)
135                        tr.rmax = r->rmax - r->rot;
186                  rayvalue(&tr);
187                  copycolor(r->rcol, tr.rcol);
188                  r->rt = r->rot + tr.rt;
# Line 140 | Line 190 | register RAY  *r;
190   }
191  
192  
193 < rayshade(r, mod)                /* shade ray r with material mod */
194 < register RAY  *r;
195 < int  mod;
193 > extern int
194 > rayshade(               /* shade ray r with material mod */
195 >        register RAY  *r,
196 >        int  mod
197 > )
198   {
147        static int  depth = 0;
148        int  gotmat;
199          register OBJREC  *m;
200 <                                        /* check for infinite loop */
151 <        if (depth++ >= MAXLOOP)
152 <                objerror(r->ro, USER, "possible modifier loop");
200 >
201          r->rt = r->rot;                 /* set effective ray length */
202 <        for (gotmat = 0; !gotmat && mod != OVOID; mod = m->omod) {
202 >        for ( ; mod != OVOID; mod = m->omod) {
203                  m = objptr(mod);
204                  /****** unnecessary test since modifier() is always called
205                  if (!ismodifier(m->otype)) {
# Line 160 | Line 208 | int  mod;
208                  }
209                  ******/
210                                          /* hack for irradiance calculation */
211 <                if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS))) {
211 >                if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS)) &&
212 >                                m->otype != MAT_CLIP &&
213 >                                (ofun[m->otype].flags & (T_M|T_X))) {
214                          if (irr_ignore(m->otype)) {
165                                depth--;
215                                  raytrans(r);
216                                  return(1);
217                          }
218                          if (!islight(m->otype))
219                                  m = &Lamb;
220                  }
221 <                                        /* materials call raytexture */
222 <                gotmat = (*ofun[m->otype].funp)(m, r);
221 >                if ((*ofun[m->otype].funp)(m, r))
222 >                        return(1);      /* materials call raytexture() */
223          }
224 <        depth--;
176 <        return(gotmat);
224 >        return(0);                      /* no material! */
225   }
226  
227  
228 < raytexture(r, mod)                      /* get material modifiers */
229 < RAY  *r;
230 < int  mod;
228 > extern void
229 > rayparticipate(                 /* compute ray medium participation */
230 >        register RAY  *r
231 > )
232   {
233 <        static int  depth = 0;
233 >        COLOR   ce, ca;
234 >        double  re, ge, be;
235 >
236 >        if (intens(r->cext) <= 1./FHUGE)
237 >                return;                         /* no medium */
238 >        re = r->rot*colval(r->cext,RED);
239 >        ge = r->rot*colval(r->cext,GRN);
240 >        be = r->rot*colval(r->cext,BLU);
241 >        if (r->crtype & SHADOW) {               /* no scattering for sources */
242 >                re *= 1. - colval(r->albedo,RED);
243 >                ge *= 1. - colval(r->albedo,GRN);
244 >                be *= 1. - colval(r->albedo,BLU);
245 >        }
246 >        setcolor(ce,    re<=FTINY ? 1. : re>92. ? 0. : exp(-re),
247 >                        ge<=FTINY ? 1. : ge>92. ? 0. : exp(-ge),
248 >                        be<=FTINY ? 1. : be>92. ? 0. : exp(-be));
249 >        multcolor(r->rcol, ce);                 /* path extinction */
250 >        if (r->crtype & SHADOW || intens(r->albedo) <= FTINY)
251 >                return;                         /* no scattering */
252 >        setcolor(ca,
253 >                colval(r->albedo,RED)*colval(ambval,RED)*(1.-colval(ce,RED)),
254 >                colval(r->albedo,GRN)*colval(ambval,GRN)*(1.-colval(ce,GRN)),
255 >                colval(r->albedo,BLU)*colval(ambval,BLU)*(1.-colval(ce,BLU)));
256 >        addcolor(r->rcol, ca);                  /* ambient in scattering */
257 >        srcscatter(r);                          /* source in scattering */
258 > }
259 >
260 >
261 > extern void
262 > raytexture(                     /* get material modifiers */
263 >        RAY  *r,
264 >        OBJECT  mod
265 > )
266 > {
267          register OBJREC  *m;
186                                        /* check for infinite loop */
187        if (depth++ >= MAXLOOP)
188                objerror(r->ro, USER, "modifier loop");
268                                          /* execute textures and patterns */
269          for ( ; mod != OVOID; mod = m->omod) {
270                  m = objptr(mod);
# Line 201 | Line 280 | int  mod;
280                          objerror(r->ro, USER, errmsg);
281                  }
282          }
204        depth--;                        /* end here */
283   }
284  
285  
286 < raymixture(r, fore, back, coef)         /* mix modifiers */
287 < register RAY  *r;
288 < OBJECT  fore, back;
289 < double  coef;
286 > extern int
287 > raymixture(             /* mix modifiers */
288 >        register RAY  *r,
289 >        OBJECT  fore,
290 >        OBJECT  back,
291 >        double  coef
292 > )
293   {
294          RAY  fr, br;
295          int  foremat, backmat;
296          register int  i;
297 <                                        /* clip coefficient */
297 >                                        /* bound coefficient */
298          if (coef > 1.0)
299                  coef = 1.0;
300          else if (coef < 0.0)
301                  coef = 0.0;
302                                          /* compute foreground and background */
303 <        foremat = backmat = -1;
303 >        foremat = backmat = 0;
304                                          /* foreground */
305 <        copystruct(&fr, r);
306 <        if (fore != OVOID && coef > FTINY)
305 >        fr = *r;
306 >        if (coef > FTINY) {
307 >                scalecolor(fr.rcoef, coef);
308                  foremat = rayshade(&fr, fore);
309 +        }
310                                          /* background */
311 <        copystruct(&br, r);
312 <        if (back != OVOID && coef < 1.0-FTINY)
311 >        br = *r;
312 >        if (coef < 1.0-FTINY) {
313 >                scalecolor(br.rcoef, 1.0-coef);
314                  backmat = rayshade(&br, back);
315 <                                        /* check */
316 <        if (foremat < 0)
317 <                if (backmat < 0)
318 <                        foremat = backmat = 0;
319 <                else
320 <                        foremat = backmat;
321 <        else if (backmat < 0)
322 <                backmat = foremat;
239 <        if ((foremat==0) != (backmat==0))
240 <                objerror(r->ro, USER, "mixing material with non-material");
315 >        }
316 >                                        /* check for transparency */
317 >        if (backmat ^ foremat) {
318 >                if (backmat && coef > FTINY)
319 >                        raytrans(&fr);
320 >                else if (foremat && coef < 1.0-FTINY)
321 >                        raytrans(&br);
322 >        }
323                                          /* mix perturbations */
324          for (i = 0; i < 3; i++)
325                  r->pert[i] = coef*fr.pert[i] + (1.0-coef)*br.pert[i];
# Line 246 | Line 328 | double  coef;
328          scalecolor(br.pcol, 1.0-coef);
329          copycolor(r->pcol, fr.pcol);
330          addcolor(r->pcol, br.pcol);
331 +                                        /* return value tells if material */
332 +        if (!foremat & !backmat)
333 +                return(0);
334                                          /* mix returned ray values */
335 <        if (foremat) {
336 <                scalecolor(fr.rcol, coef);
337 <                scalecolor(br.rcol, 1.0-coef);
338 <                copycolor(r->rcol, fr.rcol);
339 <                addcolor(r->rcol, br.rcol);
340 <                r->rt = bright(fr.rcol) > bright(br.rcol) ? fr.rt : br.rt;
335 >        scalecolor(fr.rcol, coef);
336 >        scalecolor(br.rcol, 1.0-coef);
337 >        copycolor(r->rcol, fr.rcol);
338 >        addcolor(r->rcol, br.rcol);
339 >        r->rt = bright(fr.rcol) > bright(br.rcol) ? fr.rt : br.rt;
340 >        return(1);
341 > }
342 >
343 >
344 > extern double
345 > raydist(                /* compute (cumulative) ray distance */
346 >        register const RAY  *r,
347 >        register int  flags
348 > )
349 > {
350 >        double  sum = 0.0;
351 >
352 >        while (r != NULL && r->crtype&flags) {
353 >                sum += r->rot;
354 >                r = r->parent;
355          }
356 <                                        /* return value tells if material */
258 <        return(foremat);
356 >        return(sum);
357   }
358  
359  
360 < double
361 < raynormal(norm, r)              /* compute perturbed normal for ray */
362 < FVECT  norm;
363 < register RAY  *r;
360 > extern void
361 > raycontrib(             /* compute (cumulative) ray contribution */
362 >        double  rc[3],
363 >        const RAY  *r,
364 >        int  flags
365 > )
366   {
367 +        double  eext[3];
368 +        int     i;
369 +
370 +        eext[0] = eext[1] = eext[2] = 0.;
371 +        rc[0] = rc[1] = rc[2] = 1.;
372 +
373 +        while (r != NULL && r->crtype&flags) {
374 +                for (i = 3; i--; ) {
375 +                        rc[i] *= colval(r->rcoef,i);
376 +                        eext[i] += r->rot * colval(r->cext,i);
377 +                }
378 +                r = r->parent;
379 +        }
380 +        for (i = 3; i--; )
381 +                rc[i] *= (eext[i] <= FTINY) ? 1. :
382 +                                (eext[i] > 92.) ? 0. : exp(-eext[i]);
383 + }
384 +
385 +
386 + extern double
387 + raynormal(              /* compute perturbed normal for ray */
388 +        FVECT  norm,
389 +        register RAY  *r
390 + )
391 + {
392          double  newdot;
393          register int  i;
394  
# Line 294 | Line 419 | register RAY  *r;
419   }
420  
421  
422 < newrayxf(r)                     /* get new tranformation matrix for ray */
423 < RAY  *r;
422 > extern void
423 > newrayxf(                       /* get new tranformation matrix for ray */
424 >        RAY  *r
425 > )
426   {
427          static struct xfn {
428                  struct xfn  *next;
429                  FULLXF  xf;
430          }  xfseed = { &xfseed }, *xflast = &xfseed;
431          register struct xfn  *xp;
432 <        register RAY  *rp;
432 >        register const RAY  *rp;
433  
434          /*
435           * Search for transform in circular list that
# Line 313 | Line 440 | RAY  *r;
440                  if (rp->rox == &xp->xf) {               /* xp in use */
441                          xp = xp->next;                  /* move to next */
442                          if (xp == xflast) {             /* need new one */
443 <                                xp = (struct xfn *)bmalloc(sizeof(struct xfn));
443 >                                xp = (struct xfn *)malloc(sizeof(struct xfn));
444                                  if (xp == NULL)
445                                          error(SYSTEM,
446                                                  "out of memory in newrayxf");
# Line 330 | Line 457 | RAY  *r;
457   }
458  
459  
460 < flipsurface(r)                  /* reverse surface orientation */
461 < register RAY  *r;
460 > extern void
461 > flipsurface(                    /* reverse surface orientation */
462 >        register RAY  *r
463 > )
464   {
465          r->rod = -r->rod;
466          r->ron[0] = -r->ron[0];
# Line 343 | Line 472 | register RAY  *r;
472   }
473  
474  
475 < localhit(r, scene)              /* check for hit in the octree */
476 < register RAY  *r;
477 < register CUBE  *scene;
475 > extern void
476 > rayhit(                 /* standard ray hit test */
477 >        OBJECT  *oset,
478 >        RAY  *r
479 > )
480   {
481 +        OBJREC  *o;
482 +        int     i;
483 +
484 +        for (i = oset[0]; i > 0; i--) {
485 +                o = objptr(oset[i]);
486 +                if ((*ofun[o->otype].funp)(o, r))
487 +                        r->robj = oset[i];
488 +        }
489 + }
490 +
491 +
492 + extern int
493 + localhit(               /* check for hit in the octree */
494 +        register RAY  *r,
495 +        register CUBE  *scene
496 + )
497 + {
498          OBJECT  cxset[MAXCSET+1];       /* set of checked objects */
499          FVECT  curpos;                  /* current cube position */
500          int  sflags;                    /* sign flags */
# Line 400 | Line 548 | register CUBE  *scene;
548          }
549          cxset[0] = 0;
550          raymove(curpos, cxset, sflags, r, scene);
551 <        return(r->ro != NULL & r->ro != &Aftplane);
551 >        return((r->ro != NULL) & (r->ro != &Aftplane));
552   }
553  
554  
555   static int
556 < raymove(pos, cxs, dirf, r, cu)          /* check for hit as we move */
557 < FVECT  pos;                     /* current position, modified herein */
558 < OBJECT  *cxs;                   /* checked objects, modified by checkhit */
559 < int  dirf;                      /* direction indicators to speed tests */
560 < register RAY  *r;
561 < register CUBE  *cu;
556 > raymove(                /* check for hit as we move */
557 >        FVECT  pos,                     /* current position, modified herein */
558 >        OBJECT  *cxs,                   /* checked objects, modified by checkhit */
559 >        int  dirf,                      /* direction indicators to speed tests */
560 >        register RAY  *r,
561 >        register CUBE  *cu
562 > )
563   {
564          int  ax;
565          double  dt, t;
# Line 490 | Line 639 | register CUBE  *cu;
639   }
640  
641  
642 < static
643 < checkhit(r, cu, cxs)            /* check for hit in full cube */
644 < register RAY  *r;
645 < CUBE  *cu;
646 < OBJECT  *cxs;
642 > static int
643 > checkhit(               /* check for hit in full cube */
644 >        register RAY  *r,
645 >        CUBE  *cu,
646 >        OBJECT  *cxs
647 > )
648   {
649          OBJECT  oset[MAXSET+1];
500        register OBJREC  *o;
501        register int  i;
650  
651          objset(oset, cu->cutree);
652 <        checkset(oset, cxs);                    /* eliminate double-checking */
653 <        for (i = oset[0]; i > 0; i--) {
654 <                o = objptr(oset[i]);
655 <                (*ofun[o->otype].funp)(o, r);
656 <        }
509 <        if (r->ro == NULL)
652 >        checkset(oset, cxs);                    /* avoid double-checking */
653 >
654 >        (*r->hitf)(oset, r);                    /* test for hit in set */
655 >
656 >        if (r->robj == OVOID)
657                  return(0);                      /* no scores yet */
658  
659          return(incube(cu, r->rop));             /* hit OK if in current cube */
660   }
661  
662  
663 < static
664 < checkset(os, cs)                /* modify checked set and set to check */
665 < register OBJECT  *os;                   /* os' = os - cs */
666 < register OBJECT  *cs;                   /* cs' = cs + os */
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 */
667 > )
668   {
669          OBJECT  cset[MAXCSET+MAXSET+1];
670          register int  i, j;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines