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.34 by greg, Sat Feb 22 02:07:29 2003 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 + /* ====================================================================
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  
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
75   unsigned long  raynum = 0;              /* next unique ray number */
76   unsigned long  nrays = 0;               /* number of calls to localhit */
77  
# Line 36 | Line 83 | OBJREC  Lamb = {
83  
84   OBJREC  Aftplane;                       /* aft clipping plane object */
85  
86 < static int  raymove(), checkset(), checkhit();
86 > static int  raymove(), checkhit();
87 > static void  checkset();
88  
89 < #define  MAXLOOP        128             /* modifier loop detection */
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 55 | 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->rmax = 0.0;
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);
89        r->rt = 0.0;
160   }
161  
162  
163 + void
164   raytrace(r)                     /* trace a ray and compute its value */
165   RAY  *r;
166   {
96        extern int  (*trace)();
97        int  gotmat;
98
167          if (localhit(r, &thescene))
168 <                gotmat = raycont(r);
168 >                raycont(r);             /* hit local surface, evaluate */
169          else if (r->ro == &Aftplane) {
170 <                r->ro = NULL;
170 >                r->ro = NULL;           /* hit aft clipping plane */
171                  r->rot = FHUGE;
172          } else if (sourcehit(r))
173 <                gotmat = rayshade(r, r->ro->omod);
173 >                rayshade(r, r->ro->omod);       /* distant source */
174  
175 <        if (r->ro != NULL && !gotmat)
108 <                objerror(r->ro, USER, "material not found");
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)) ||
187 <                        r->ro->omod == OVOID) {
187 >                        !rayshade(r, r->ro->omod))
188                  raytrans(r);
121                return(1);
122        }
123        return(rayshade(r, r->ro->omod));
189   }
190  
191  
192 + void
193   raytrans(r)                     /* transmit ray as is */
194   register RAY  *r;
195   {
# Line 131 | Line 197 | register RAY  *r;
197  
198          if (rayorigin(&tr, r, TRANS, 1.0) == 0) {
199                  VCOPY(tr.rdir, r->rdir);
134                if (r->rmax > FTINY)
135                        tr.rmax = r->rmax - r->rot;
200                  rayvalue(&tr);
201                  copycolor(r->rcol, tr.rcol);
202                  r->rt = r->rot + tr.rt;
# Line 140 | 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   {
147        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 (gotmat = 0; !gotmat && mod != OVOID; mod = m->omod) {
222                  m = objptr(mod);
# Line 162 | 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(1);
237                          }
# Line 172 | Line 241 | int  mod;
241                                          /* materials call raytexture */
242                  gotmat = (*ofun[m->otype].funp)(m, r);
243          }
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   {
184        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);
# Line 201 | Line 306 | int  mod;
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;
# Line 213 | Line 321 | double  coef;
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                                          /* compute foreground and background */
330 <        foremat = backmat = -1;
330 >        foremat = backmat = 0;
331                                          /* foreground */
332          copystruct(&fr, r);
333 <        if (fore != OVOID && coef > FTINY)
333 >        if (coef > FTINY)
334                  foremat = rayshade(&fr, fore);
335                                          /* background */
336          copystruct(&br, r);
337 <        if (back != OVOID && coef < 1.0-FTINY)
337 >        if (coef < 1.0-FTINY)
338                  backmat = rayshade(&br, back);
339 <                                        /* check */
340 <        if (foremat < 0)
341 <                if (backmat < 0)
342 <                        foremat = backmat = 0;
343 <                else
344 <                        foremat = backmat;
237 <        else if (backmat < 0)
238 <                backmat = foremat;
239 <        if ((foremat==0) != (backmat==0))
240 <                objerror(r->ro, USER, "mixing material with non-material");
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] = coef*fr.pert[i] + (1.0-coef)*br.pert[i];
# Line 246 | Line 350 | double  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 <        if (foremat) {
358 <                scalecolor(fr.rcol, coef);
359 <                scalecolor(br.rcol, 1.0-coef);
360 <                copycolor(r->rcol, fr.rcol);
361 <                addcolor(r->rcol, br.rcol);
362 <                r->rt = bright(fr.rcol) > bright(br.rcol) ? fr.rt : br.rt;
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 value tells if material */
258 <        return(foremat);
377 >        return(sum);
378   }
379  
380  
# Line 294 | Line 413 | register RAY  *r;
413   }
414  
415  
416 + void
417   newrayxf(r)                     /* get new tranformation matrix for ray */
418   RAY  *r;
419   {
# Line 313 | 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 330 | Line 450 | RAY  *r;
450   }
451  
452  
453 + void
454   flipsurface(r)                  /* reverse surface orientation */
455   register RAY  *r;
456   {
# Line 343 | 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 490 | 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 504 | 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 <                (*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 513 | 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