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.4 by greg, Tue Feb 23 13:57:11 1993 UTC vs.
Revision 2.34 by greg, Sat Feb 22 02:07:29 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1991 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 + /* ====================================================================
11 + * The Radiance Software License, Version 1.0
12 + *
13 + * Copyright (c) 1990 - 2002 The Regents of the University of California,
14 + * through Lawrence Berkeley National Laboratory.   All rights reserved.
15 + *
16 + * Redistribution and use in source and binary forms, with or without
17 + * modification, are permitted provided that the following conditions
18 + * are met:
19 + *
20 + * 1. Redistributions of source code must retain the above copyright
21 + *         notice, this list of conditions and the following disclaimer.
22 + *
23 + * 2. Redistributions in binary form must reproduce the above copyright
24 + *       notice, this list of conditions and the following disclaimer in
25 + *       the documentation and/or other materials provided with the
26 + *       distribution.
27 + *
28 + * 3. The end-user documentation included with the redistribution,
29 + *           if any, must include the following acknowledgment:
30 + *             "This product includes Radiance software
31 + *                 (http://radsite.lbl.gov/)
32 + *                 developed by the Lawrence Berkeley National Laboratory
33 + *               (http://www.lbl.gov/)."
34 + *       Alternately, this acknowledgment may appear in the software itself,
35 + *       if and wherever such third-party acknowledgments normally appear.
36 + *
37 + * 4. The names "Radiance," "Lawrence Berkeley National Laboratory"
38 + *       and "The Regents of the University of California" must
39 + *       not be used to endorse or promote products derived from this
40 + *       software without prior written permission. For written
41 + *       permission, please contact [email protected].
42 + *
43 + * 5. Products derived from this software may not be called "Radiance",
44 + *       nor may "Radiance" appear in their name, without prior written
45 + *       permission of Lawrence Berkeley National Laboratory.
46 + *
47 + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
48 + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49 + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
50 + * DISCLAIMED.   IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR
51 + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
52 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
53 + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
54 + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
55 + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
56 + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
57 + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 + * SUCH DAMAGE.
59 + * ====================================================================
60 + *
61 + * This software consists of voluntary contributions made by many
62 + * individuals on behalf of Lawrence Berkeley National Laboratory.   For more
63 + * information on Lawrence Berkeley National Laboratory, please see
64 + * <http://www.lbl.gov/>.
65 + */
66 +
67   #include  "ray.h"
68  
15 #include  "octree.h"
16
69   #include  "otypes.h"
70  
71   #include  "otspecial.h"
72  
73   #define  MAXCSET        ((MAXSET+1)*2-1)        /* maximum check set size */
74  
75 < extern CUBE  thescene;                  /* our scene */
76 < extern int  maxdepth;                   /* maximum recursion depth */
25 < extern double  minweight;               /* minimum ray weight */
26 < extern int  do_irrad;                   /* compute irradiance? */
75 > unsigned long  raynum = 0;              /* next unique ray number */
76 > unsigned long  nrays = 0;               /* number of calls to localhit */
77  
28 long  raynum = 0L;                      /* next unique ray number */
29 long  nrays = 0L;                       /* number of calls to localhit */
30
78   static FLOAT  Lambfa[5] = {PI, PI, PI, 0.0, 0.0};
79   OBJREC  Lamb = {
80          OVOID, MAT_PLASTIC, "Lambertian",
81          {0, 5, NULL, Lambfa}, NULL,
82   };                                      /* a Lambertian surface */
83  
84 < #define  MAXLOOP        128             /* modifier loop detection */
84 > OBJREC  Aftplane;                       /* aft clipping plane object */
85  
86 + static int  raymove(), checkhit();
87 + static void  checkset();
88 +
89 + #ifndef  MAXLOOP
90 + #define  MAXLOOP        0               /* modifier loop detection */
91 + #endif
92 +
93   #define  RAYHIT         (-1)            /* return value for intercepted ray */
94  
95  
96 + int
97   rayorigin(r, ro, rt, rw)                /* start new ray from old one */
98   register RAY  *r, *ro;
99   int  rt;
100   double  rw;
101   {
102 +        double  re;
103 +
104          if ((r->parent = ro) == NULL) {         /* primary ray */
105                  r->rlvl = 0;
106                  r->rweight = rw;
# Line 51 | Line 108 | double  rw;
108                  r->rsrc = -1;
109                  r->clipset = NULL;
110                  r->revf = raytrace;
111 +                copycolor(r->cext, cextinction);
112 +                copycolor(r->albedo, salbedo);
113 +                r->gecc = seccg;
114 +                r->slights = NULL;
115          } else {                                /* spawned ray */
116                  r->rlvl = ro->rlvl;
117                  if (rt & RAYREFL) {
118                          r->rlvl++;
119                          r->rsrc = -1;
120                          r->clipset = ro->clipset;
121 +                        r->rmax = 0.0;
122                  } else {
123                          r->rsrc = ro->rsrc;
124                          r->clipset = ro->newcset;
125 +                        r->rmax = ro->rmax <= FTINY ? 0.0 : ro->rmax - ro->rot;
126                  }
127                  r->revf = ro->revf;
128 <                r->rweight = ro->rweight * rw;
128 >                copycolor(r->cext, ro->cext);
129 >                copycolor(r->albedo, ro->albedo);
130 >                r->gecc = ro->gecc;
131 >                r->slights = ro->slights;
132                  r->crtype = ro->crtype | (r->rtype = rt);
133                  VCOPY(r->rorg, ro->rop);
134 +                r->rweight = ro->rweight * rw;
135 +                                                /* estimate absorption */
136 +                re = colval(ro->cext,RED) < colval(ro->cext,GRN) ?
137 +                                colval(ro->cext,RED) : colval(ro->cext,GRN);
138 +                if (colval(ro->cext,BLU) < re) re = colval(ro->cext,BLU);
139 +                if (re > 0.)
140 +                        r->rweight *= exp(-re*ro->rot);
141          }
142          rayclear(r);
143          return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1);
144   }
145  
146  
147 + void
148   rayclear(r)                     /* clear a ray for (re)evaluation */
149   register RAY  *r;
150   {
151          r->rno = raynum++;
152          r->newcset = r->clipset;
153 +        r->robj = OVOID;
154          r->ro = NULL;
155 <        r->rot = FHUGE;
155 >        r->rox = NULL;
156 >        r->rt = r->rot = FHUGE;
157          r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
158          setcolor(r->pcol, 1.0, 1.0, 1.0);
159          setcolor(r->rcol, 0.0, 0.0, 0.0);
84        r->rt = 0.0;
160   }
161  
162  
163 + void
164   raytrace(r)                     /* trace a ray and compute its value */
165   RAY  *r;
166   {
91        extern int  (*trace)();
92
167          if (localhit(r, &thescene))
168 <                raycont(r);
169 <        else if (sourcehit(r))
170 <                rayshade(r, r->ro->omod);
168 >                raycont(r);             /* hit local surface, evaluate */
169 >        else if (r->ro == &Aftplane) {
170 >                r->ro = NULL;           /* hit aft clipping plane */
171 >                r->rot = FHUGE;
172 >        } else if (sourcehit(r))
173 >                rayshade(r, r->ro->omod);       /* distant source */
174  
175 +        rayparticipate(r);              /* for participating medium */
176 +
177          if (trace != NULL)
178                  (*trace)(r);            /* trace execution */
179   }
180  
181  
182 + void
183   raycont(r)                      /* check for clipped object and continue */
184   register RAY  *r;
185   {
186 <        if (r->clipset != NULL && inset(r->clipset, r->ro->omod))
186 >        if ((r->clipset != NULL && inset(r->clipset, r->ro->omod)) ||
187 >                        !rayshade(r, r->ro->omod))
188                  raytrans(r);
108        else
109                rayshade(r, r->ro->omod);
189   }
190  
191  
192 + void
193   raytrans(r)                     /* transmit ray as is */
194   register RAY  *r;
195   {
# Line 124 | Line 204 | register RAY  *r;
204   }
205  
206  
207 + int
208   rayshade(r, mod)                /* shade ray r with material mod */
209   register RAY  *r;
210   int  mod;
211   {
212 <        static int  depth = 0;
212 >        int  gotmat;
213          register OBJREC  *m;
214 + #if  MAXLOOP
215 +        static int  depth = 0;
216                                          /* check for infinite loop */
217          if (depth++ >= MAXLOOP)
218                  objerror(r->ro, USER, "possible modifier loop");
219 + #endif
220          r->rt = r->rot;                 /* set effective ray length */
221 <        for ( ; mod != OVOID; mod = m->omod) {
221 >        for (gotmat = 0; !gotmat && mod != OVOID; mod = m->omod) {
222                  m = objptr(mod);
223                  /****** unnecessary test since modifier() is always called
224                  if (!ismodifier(m->otype)) {
# Line 145 | Line 229 | int  mod;
229                                          /* hack for irradiance calculation */
230                  if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS))) {
231                          if (irr_ignore(m->otype)) {
232 + #if  MAXLOOP
233                                  depth--;
234 + #endif
235                                  raytrans(r);
236 <                                return;
236 >                                return(1);
237                          }
238                          if (!islight(m->otype))
239                                  m = &Lamb;
240                  }
241 <                (*ofun[m->otype].funp)(m, r);   /* execute function */
242 <                if (ismaterial(m->otype)) {     /* materials call raytexture */
157 <                        depth--;
158 <                        return;         /* we're done */
159 <                }
241 >                                        /* materials call raytexture */
242 >                gotmat = (*ofun[m->otype].funp)(m, r);
243          }
244 <        objerror(r->ro, USER, "material not found");
244 > #if  MAXLOOP
245 >        depth--;
246 > #endif
247 >        return(gotmat);
248   }
249  
250  
251 + void
252 + rayparticipate(r)                       /* compute ray medium participation */
253 + register RAY  *r;
254 + {
255 +        COLOR   ce, ca;
256 +        double  re, ge, be;
257 +
258 +        if (intens(r->cext) <= 1./FHUGE)
259 +                return;                         /* no medium */
260 +        re = r->rot*colval(r->cext,RED);
261 +        ge = r->rot*colval(r->cext,GRN);
262 +        be = r->rot*colval(r->cext,BLU);
263 +        if (r->crtype & SHADOW) {               /* no scattering for sources */
264 +                re *= 1. - colval(r->albedo,RED);
265 +                ge *= 1. - colval(r->albedo,GRN);
266 +                be *= 1. - colval(r->albedo,BLU);
267 +        }
268 +        setcolor(ce,    re<=0. ? 1. : re>92. ? 0. : exp(-re),
269 +                        ge<=0. ? 1. : ge>92. ? 0. : exp(-ge),
270 +                        be<=0. ? 1. : be>92. ? 0. : exp(-be));
271 +        multcolor(r->rcol, ce);                 /* path absorption */
272 +        if (r->crtype & SHADOW || intens(r->albedo) <= FTINY)
273 +                return;                         /* no scattering */
274 +        setcolor(ca,
275 +                colval(r->albedo,RED)*colval(ambval,RED)*(1.-colval(ce,RED)),
276 +                colval(r->albedo,GRN)*colval(ambval,GRN)*(1.-colval(ce,GRN)),
277 +                colval(r->albedo,BLU)*colval(ambval,BLU)*(1.-colval(ce,BLU)));
278 +        addcolor(r->rcol, ca);                  /* ambient in scattering */
279 +        srcscatter(r);                          /* source in scattering */
280 + }
281 +
282 +
283   raytexture(r, mod)                      /* get material modifiers */
284   RAY  *r;
285   int  mod;
286   {
169        static int  depth = 0;
287          register OBJREC  *m;
288 + #if  MAXLOOP
289 +        static int  depth = 0;
290                                          /* check for infinite loop */
291          if (depth++ >= MAXLOOP)
292                  objerror(r->ro, USER, "modifier loop");
293 + #endif
294                                          /* execute textures and patterns */
295          for ( ; mod != OVOID; mod = m->omod) {
296                  m = objptr(mod);
297 <                if (!istexture(m->otype)) {
297 >                /****** unnecessary test since modifier() is always called
298 >                if (!ismodifier(m->otype)) {
299                          sprintf(errmsg, "illegal modifier \"%s\"", m->oname);
300                          error(USER, errmsg);
301                  }
302 <                (*ofun[m->otype].funp)(m, r);
302 >                ******/
303 >                if ((*ofun[m->otype].funp)(m, r)) {
304 >                        sprintf(errmsg, "conflicting material \"%s\"",
305 >                                        m->oname);
306 >                        objerror(r->ro, USER, errmsg);
307 >                }
308          }
309 + #if  MAXLOOP
310          depth--;                        /* end here */
311 + #endif
312   }
313  
314  
315 + int
316   raymixture(r, fore, back, coef)         /* mix modifiers */
317   register RAY  *r;
318   OBJECT  fore, back;
319   double  coef;
320   {
321 <        FVECT  curpert, forepert, backpert;
322 <        COLOR  curpcol, forepcol, backpcol;
321 >        RAY  fr, br;
322 >        int  foremat, backmat;
323          register int  i;
324 <                                        /* clip coefficient */
324 >                                        /* bound coefficient */
325          if (coef > 1.0)
326                  coef = 1.0;
327          else if (coef < 0.0)
328                  coef = 0.0;
329 <                                        /* save current mods */
330 <        VCOPY(curpert, r->pert);
331 <        copycolor(curpcol, r->pcol);
332 <                                        /* compute new mods */
333 <                                                /* foreground */
334 <        r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
335 <        setcolor(r->pcol, 1.0, 1.0, 1.0);
336 <        if (fore != OVOID && coef > FTINY)
337 <                raytexture(r, fore);
338 <        VCOPY(forepert, r->pert);
339 <        copycolor(forepcol, r->pcol);
340 <                                                /* background */
341 <        r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
342 <        setcolor(r->pcol, 1.0, 1.0, 1.0);
343 <        if (back != OVOID && coef < 1.0-FTINY)
344 <                raytexture(r, back);
345 <        VCOPY(backpert, r->pert);
217 <        copycolor(backpcol, r->pcol);
218 <                                        /* sum perturbations */
329 >                                        /* compute foreground and background */
330 >        foremat = backmat = 0;
331 >                                        /* foreground */
332 >        copystruct(&fr, r);
333 >        if (coef > FTINY)
334 >                foremat = rayshade(&fr, fore);
335 >                                        /* background */
336 >        copystruct(&br, r);
337 >        if (coef < 1.0-FTINY)
338 >                backmat = rayshade(&br, back);
339 >                                        /* check for transparency */
340 >        if (backmat ^ foremat)
341 >                if (backmat && coef > FTINY)
342 >                        raytrans(&fr);
343 >                else if (foremat && coef < 1.0-FTINY)
344 >                        raytrans(&br);
345 >                                        /* mix perturbations */
346          for (i = 0; i < 3; i++)
347 <                r->pert[i] = curpert[i] + coef*forepert[i] +
348 <                                (1.0-coef)*backpert[i];
349 <                                        /* multiply colors */
350 <        setcolor(r->pcol, coef*colval(forepcol,RED) +
351 <                                (1.0-coef)*colval(backpcol,RED),
352 <                        coef*colval(forepcol,GRN) +
353 <                                (1.0-coef)*colval(backpcol,GRN),
354 <                        coef*colval(forepcol,BLU) +
355 <                                (1.0-coef)*colval(backpcol,BLU));
356 <        multcolor(r->pcol, curpcol);
347 >                r->pert[i] = coef*fr.pert[i] + (1.0-coef)*br.pert[i];
348 >                                        /* mix pattern colors */
349 >        scalecolor(fr.pcol, coef);
350 >        scalecolor(br.pcol, 1.0-coef);
351 >        copycolor(r->pcol, fr.pcol);
352 >        addcolor(r->pcol, br.pcol);
353 >                                        /* return value tells if material */
354 >        if (!foremat & !backmat)
355 >                return(0);
356 >                                        /* mix returned ray values */
357 >        scalecolor(fr.rcol, coef);
358 >        scalecolor(br.rcol, 1.0-coef);
359 >        copycolor(r->rcol, fr.rcol);
360 >        addcolor(r->rcol, br.rcol);
361 >        r->rt = bright(fr.rcol) > bright(br.rcol) ? fr.rt : br.rt;
362 >        return(1);
363   }
364  
365  
366   double
367 + raydist(r, flags)               /* compute (cumulative) ray distance */
368 + register RAY  *r;
369 + register int  flags;
370 + {
371 +        double  sum = 0.0;
372 +
373 +        while (r != NULL && r->crtype&flags) {
374 +                sum += r->rot;
375 +                r = r->parent;
376 +        }
377 +        return(sum);
378 + }
379 +
380 +
381 + double
382   raynormal(norm, r)              /* compute perturbed normal for ray */
383   FVECT  norm;
384   register RAY  *r;
# Line 265 | Line 413 | register RAY  *r;
413   }
414  
415  
416 + void
417   newrayxf(r)                     /* get new tranformation matrix for ray */
418   RAY  *r;
419   {
# Line 284 | Line 433 | RAY  *r;
433                  if (rp->rox == &xp->xf) {               /* xp in use */
434                          xp = xp->next;                  /* move to next */
435                          if (xp == xflast) {             /* need new one */
436 <                                xp = (struct xfn *)bmalloc(sizeof(struct xfn));
436 >                                xp = (struct xfn *)malloc(sizeof(struct xfn));
437                                  if (xp == NULL)
438                                          error(SYSTEM,
439                                                  "out of memory in newrayxf");
# Line 301 | Line 450 | RAY  *r;
450   }
451  
452  
453 + void
454   flipsurface(r)                  /* reverse surface orientation */
455   register RAY  *r;
456   {
# Line 314 | Line 464 | register RAY  *r;
464   }
465  
466  
467 + int
468   localhit(r, scene)              /* check for hit in the octree */
469   register RAY  *r;
470   register CUBE  *scene;
# Line 328 | Line 479 | register CUBE  *scene;
479          sflags = 0;
480          for (i = 0; i < 3; i++) {
481                  curpos[i] = r->rorg[i];
482 <                if (r->rdir[i] > FTINY)
482 >                if (r->rdir[i] > 1e-7)
483                          sflags |= 1 << i;
484 <                else if (r->rdir[i] < -FTINY)
484 >                else if (r->rdir[i] < -1e-7)
485                          sflags |= 0x10 << i;
486          }
487          if (sflags == 0)
488                  error(CONSISTENCY, "zero ray direction in localhit");
489 +                                        /* start off assuming nothing hit */
490 +        if (r->rmax > FTINY) {          /* except aft plane if one */
491 +                r->ro = &Aftplane;
492 +                r->rot = r->rmax;
493 +                for (i = 0; i < 3; i++)
494 +                        r->rop[i] = r->rorg[i] + r->rot*r->rdir[i];
495 +        }
496 +                                        /* find global cube entrance point */
497          t = 0.0;
498          if (!incube(scene, curpos)) {
499                                          /* find distance to entry */
# Line 352 | Line 511 | register CUBE  *scene;
511                                  t = dt; /* farthest face is the one */
512                  }
513                  t += FTINY;             /* fudge to get inside cube */
514 +                if (t >= r->rot)        /* clipped already */
515 +                        return(0);
516                                          /* advance position */
517                  for (i = 0; i < 3; i++)
518                          curpos[i] += r->rdir[i]*t;
# Line 360 | Line 521 | register CUBE  *scene;
521                          return(0);
522          }
523          cxset[0] = 0;
524 <        return(raymove(curpos, cxset, sflags, r, scene) == RAYHIT);
524 >        raymove(curpos, cxset, sflags, r, scene);
525 >        return(r->ro != NULL & r->ro != &Aftplane);
526   }
527  
528  
# Line 415 | Line 577 | register CUBE  *cu;
577                  }
578                  /*NOTREACHED*/
579          }
580 <        if (isfull(cu->cutree) && checkhit(r, cu, cxs))
580 >        if (isfull(cu->cutree)) {
581 >                if (checkhit(r, cu, cxs))
582 >                        return(RAYHIT);
583 >        } else if (r->ro == &Aftplane && incube(cu, r->rop))
584                  return(RAYHIT);
585                                          /* advance to next cube */
586          if (dirf&0x11) {
# Line 447 | Line 612 | register CUBE  *cu;
612   }
613  
614  
615 < static
615 > static int
616   checkhit(r, cu, cxs)            /* check for hit in full cube */
617   register RAY  *r;
618   CUBE  *cu;
# Line 461 | Line 626 | OBJECT  *cxs;
626          checkset(oset, cxs);                    /* eliminate double-checking */
627          for (i = oset[0]; i > 0; i--) {
628                  o = objptr(oset[i]);
629 <                if (o->omod == OVOID && issurface(o->otype))
630 <                        continue;               /* ignore void surfaces */
466 <                (*ofun[o->otype].funp)(o, r);
629 >                if ((*ofun[o->otype].funp)(o, r))
630 >                        r->robj = oset[i];
631          }
632          if (r->ro == NULL)
633                  return(0);                      /* no scores yet */
# Line 472 | Line 636 | OBJECT  *cxs;
636   }
637  
638  
639 < static
639 > static void
640   checkset(os, cs)                /* modify checked set and set to check */
641   register OBJECT  *os;                   /* os' = os - cs */
642   register OBJECT  *cs;                   /* cs' = cs + os */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines