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.34 by greg, Sat Feb 22 02:07:29 2003 UTC vs.
Revision 2.51 by greg, Tue May 31 18:01:09 2005 UTC

# Line 1 | Line 1
1   #ifndef lint
2 < static const char       RCSid[] = "$Id$";
2 > static const char RCSid[] = "$Id$";
3   #endif
4   /*
5   *  raytrace.c - routines for tracing and shading rays.
# Line 7 | Line 7 | static const char      RCSid[] = "$Id$";
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 < */
10 > #include "copyright.h"
11  
12   #include  "ray.h"
13 <
13 > #include  "source.h"
14   #include  "otypes.h"
70
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 */
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,
# Line 83 | Line 28 | OBJREC  Lamb = {
28  
29   OBJREC  Aftplane;                       /* aft clipping plane object */
30  
86 static int  raymove(), checkhit();
87 static void  checkset();
88
89 #ifndef  MAXLOOP
90 #define  MAXLOOP        0               /* modifier loop detection */
91 #endif
92
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  
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;
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 113 | Line 65 | double  rw;
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++;
# Line 132 | Line 88 | double  rw;
88                  r->crtype = ro->crtype | (r->rtype = rt);
89                  VCOPY(r->rorg, ro->rop);
90                  r->rweight = ro->rweight * rw;
91 <                                                /* estimate absorption */
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 <                if (re > 0.)
96 <                        r->rweight *= exp(-re*ro->rot);
95 >                re *= ro->rot;
96 >                if (re > .1)
97 >                        r->rweight *= exp(-re);
98          }
99          rayclear(r);
100 +        if (maxdepth <= 0 && rc != NULL) {      /* Russian roulette */
101 +                if (minweight <= 0.0)
102 +                        error(USER, "zero ray weight in Russian roulette");
103 +                if (maxdepth < 0 && r->rlvl > -maxdepth)
104 +                        return(-1);             /* upper reflection limit */
105 +                if (r->rweight >= minweight)
106 +                        return(0);
107 +                if (frandom() < r->rweight/minweight)
108 +                        return(-1);
109 +                rw = minweight/r->rweight;      /* promote survivor */
110 +                scalecolor(r->rcoef, rw);
111 +                r->rweight = minweight;
112 +                return(0);
113 +        }
114          return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1);
115   }
116  
117  
118 < void
119 < rayclear(r)                     /* clear a ray for (re)evaluation */
120 < register RAY  *r;
118 > extern void
119 > rayclear(                       /* clear a ray for (re)evaluation */
120 >        register RAY  *r
121 > )
122   {
123          r->rno = raynum++;
124          r->newcset = r->clipset;
125 +        r->hitf = rayhit;
126          r->robj = OVOID;
127          r->ro = NULL;
128          r->rox = NULL;
129          r->rt = r->rot = FHUGE;
130          r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
131 +        r->uv[0] = r->uv[1] = 0.0;
132          setcolor(r->pcol, 1.0, 1.0, 1.0);
133          setcolor(r->rcol, 0.0, 0.0, 0.0);
134   }
135  
136  
137 < void
138 < raytrace(r)                     /* trace a ray and compute its value */
139 < RAY  *r;
137 > extern void
138 > raytrace(                       /* trace a ray and compute its value */
139 >        RAY  *r
140 > )
141   {
142          if (localhit(r, &thescene))
143                  raycont(r);             /* hit local surface, evaluate */
# Line 172 | Line 147 | RAY  *r;
147          } else if (sourcehit(r))
148                  rayshade(r, r->ro->omod);       /* distant source */
149  
175        rayparticipate(r);              /* for participating medium */
176
150          if (trace != NULL)
151                  (*trace)(r);            /* trace execution */
152 +
153 +        rayparticipate(r);              /* for participating medium */
154   }
155  
156  
157 < void
158 < raycont(r)                      /* check for clipped object and continue */
159 < register RAY  *r;
157 > extern void
158 > raycont(                        /* check for clipped object and continue */
159 >        register RAY  *r
160 > )
161   {
162          if ((r->clipset != NULL && inset(r->clipset, r->ro->omod)) ||
163                          !rayshade(r, r->ro->omod))
# Line 189 | Line 165 | register RAY  *r;
165   }
166  
167  
168 < void
169 < raytrans(r)                     /* transmit ray as is */
170 < register RAY  *r;
168 > extern void
169 > raytrans(                       /* transmit ray as is */
170 >        register RAY  *r
171 > )
172   {
173          RAY  tr;
174  
175 <        if (rayorigin(&tr, r, TRANS, 1.0) == 0) {
175 >        if (rayorigin(&tr, TRANS, r, NULL) == 0) {
176                  VCOPY(tr.rdir, r->rdir);
177                  rayvalue(&tr);
178                  copycolor(r->rcol, tr.rcol);
# Line 204 | Line 181 | register RAY  *r;
181   }
182  
183  
184 < int
185 < rayshade(r, mod)                /* shade ray r with material mod */
186 < register RAY  *r;
187 < int  mod;
184 > extern int
185 > rayshade(               /* shade ray r with material mod */
186 >        register RAY  *r,
187 >        int  mod
188 > )
189   {
212        int  gotmat;
190          register OBJREC  *m;
191 < #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
191 >
192          r->rt = r->rot;                 /* set effective ray length */
193 <        for (gotmat = 0; !gotmat && mod != OVOID; mod = m->omod) {
193 >        for ( ; mod != OVOID; mod = m->omod) {
194                  m = objptr(mod);
195                  /****** unnecessary test since modifier() is always called
196                  if (!ismodifier(m->otype)) {
# Line 227 | Line 199 | int  mod;
199                  }
200                  ******/
201                                          /* hack for irradiance calculation */
202 <                if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS))) {
202 >                if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS)) &&
203 >                                m->otype != MAT_CLIP &&
204 >                                (ofun[m->otype].flags & (T_M|T_X))) {
205                          if (irr_ignore(m->otype)) {
232 #if  MAXLOOP
233                                depth--;
234 #endif
206                                  raytrans(r);
207                                  return(1);
208                          }
209                          if (!islight(m->otype))
210                                  m = &Lamb;
211                  }
212 <                                        /* materials call raytexture */
213 <                gotmat = (*ofun[m->otype].funp)(m, r);
212 >                if ((*ofun[m->otype].funp)(m, r))
213 >                        return(1);      /* materials call raytexture() */
214          }
215 < #if  MAXLOOP
245 <        depth--;
246 < #endif
247 <        return(gotmat);
215 >        return(0);                      /* no material! */
216   }
217  
218  
219 < void
220 < rayparticipate(r)                       /* compute ray medium participation */
221 < register RAY  *r;
219 > extern void
220 > rayparticipate(                 /* compute ray medium participation */
221 >        register RAY  *r
222 > )
223   {
224          COLOR   ce, ca;
225          double  re, ge, be;
# Line 265 | Line 234 | register RAY  *r;
234                  ge *= 1. - colval(r->albedo,GRN);
235                  be *= 1. - colval(r->albedo,BLU);
236          }
237 <        setcolor(ce,    re<=0. ? 1. : re>92. ? 0. : exp(-re),
238 <                        ge<=0. ? 1. : ge>92. ? 0. : exp(-ge),
239 <                        be<=0. ? 1. : be>92. ? 0. : exp(-be));
240 <        multcolor(r->rcol, ce);                 /* path absorption */
237 >        setcolor(ce,    re<=FTINY ? 1. : re>92. ? 0. : exp(-re),
238 >                        ge<=FTINY ? 1. : ge>92. ? 0. : exp(-ge),
239 >                        be<=FTINY ? 1. : be>92. ? 0. : exp(-be));
240 >        multcolor(r->rcol, ce);                 /* path extinction */
241          if (r->crtype & SHADOW || intens(r->albedo) <= FTINY)
242                  return;                         /* no scattering */
243          setcolor(ca,
# Line 280 | Line 249 | register RAY  *r;
249   }
250  
251  
252 < raytexture(r, mod)                      /* get material modifiers */
253 < RAY  *r;
254 < int  mod;
252 > extern void
253 > raytexture(                     /* get material modifiers */
254 >        RAY  *r,
255 >        OBJECT  mod
256 > )
257   {
258          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
259                                          /* execute textures and patterns */
260          for ( ; mod != OVOID; mod = m->omod) {
261                  m = objptr(mod);
# Line 306 | Line 271 | int  mod;
271                          objerror(r->ro, USER, errmsg);
272                  }
273          }
309 #if  MAXLOOP
310        depth--;                        /* end here */
311 #endif
274   }
275  
276  
277 < int
278 < raymixture(r, fore, back, coef)         /* mix modifiers */
279 < register RAY  *r;
280 < OBJECT  fore, back;
281 < double  coef;
277 > extern int
278 > raymixture(             /* mix modifiers */
279 >        register RAY  *r,
280 >        OBJECT  fore,
281 >        OBJECT  back,
282 >        double  coef
283 > )
284   {
285          RAY  fr, br;
286          int  foremat, backmat;
# Line 329 | Line 293 | double  coef;
293                                          /* compute foreground and background */
294          foremat = backmat = 0;
295                                          /* foreground */
296 <        copystruct(&fr, r);
296 >        fr = *r;
297          if (coef > FTINY)
298                  foremat = rayshade(&fr, fore);
299                                          /* background */
300 <        copystruct(&br, r);
300 >        br = *r;
301          if (coef < 1.0-FTINY)
302                  backmat = rayshade(&br, back);
303                                          /* check for transparency */
304 <        if (backmat ^ foremat)
304 >        if (backmat ^ foremat) {
305                  if (backmat && coef > FTINY)
306                          raytrans(&fr);
307                  else if (foremat && coef < 1.0-FTINY)
308                          raytrans(&br);
309 +        }
310                                          /* mix perturbations */
311          for (i = 0; i < 3; i++)
312                  r->pert[i] = coef*fr.pert[i] + (1.0-coef)*br.pert[i];
# Line 363 | Line 328 | double  coef;
328   }
329  
330  
331 < double
332 < raydist(r, flags)               /* compute (cumulative) ray distance */
333 < register RAY  *r;
334 < register int  flags;
331 > extern double
332 > raydist(                /* compute (cumulative) ray distance */
333 >        register const RAY  *r,
334 >        register int  flags
335 > )
336   {
337          double  sum = 0.0;
338  
# Line 378 | Line 344 | register int  flags;
344   }
345  
346  
347 < double
348 < raynormal(norm, r)              /* compute perturbed normal for ray */
349 < FVECT  norm;
350 < register RAY  *r;
347 > extern void
348 > raycontrib(             /* compute (cumulative) ray contribution */
349 >        COLOR  rc,
350 >        const RAY  *r,
351 >        int  flags
352 > )
353   {
354 +        COLOR   eext, ext1;
355 +        
356 +        setcolor(eext, 0., 0., 0.);
357 +        setcolor(rc, 1., 1., 1.);
358 +
359 +        while (r != NULL && r->crtype&flags) {
360 +                multcolor(rc, r->rcoef);
361 +                copycolor(ext1, r->cext);
362 +                scalecolor(ext1, r->rot);
363 +                addcolor(eext, ext1);
364 +                r = r->parent;
365 +        }
366 +        if (intens(eext) > FTINY) {
367 +                setcolor(ext1,  exp(-colval(eext,RED)),
368 +                                exp(-colval(eext,GRN)),
369 +                                exp(-colval(eext,BLU)));
370 +                multcolor(rc, ext1);
371 +        }
372 + }
373 +
374 +
375 + extern double
376 + raynormal(              /* compute perturbed normal for ray */
377 +        FVECT  norm,
378 +        register RAY  *r
379 + )
380 + {
381          double  newdot;
382          register int  i;
383  
# Line 413 | Line 408 | register RAY  *r;
408   }
409  
410  
411 < void
412 < newrayxf(r)                     /* get new tranformation matrix for ray */
413 < RAY  *r;
411 > extern void
412 > newrayxf(                       /* get new tranformation matrix for ray */
413 >        RAY  *r
414 > )
415   {
416          static struct xfn {
417                  struct xfn  *next;
418                  FULLXF  xf;
419          }  xfseed = { &xfseed }, *xflast = &xfseed;
420          register struct xfn  *xp;
421 <        register RAY  *rp;
421 >        register const RAY  *rp;
422  
423          /*
424           * Search for transform in circular list that
# Line 450 | Line 446 | RAY  *r;
446   }
447  
448  
449 < void
450 < flipsurface(r)                  /* reverse surface orientation */
451 < register RAY  *r;
449 > extern void
450 > flipsurface(                    /* reverse surface orientation */
451 >        register RAY  *r
452 > )
453   {
454          r->rod = -r->rod;
455          r->ron[0] = -r->ron[0];
# Line 464 | Line 461 | register RAY  *r;
461   }
462  
463  
464 < int
465 < localhit(r, scene)              /* check for hit in the octree */
466 < register RAY  *r;
467 < register CUBE  *scene;
464 > extern void
465 > rayhit(                 /* standard ray hit test */
466 >        OBJECT  *oset,
467 >        RAY  *r
468 > )
469   {
470 +        OBJREC  *o;
471 +        int     i;
472 +
473 +        for (i = oset[0]; i > 0; i--) {
474 +                o = objptr(oset[i]);
475 +                if ((*ofun[o->otype].funp)(o, r))
476 +                        r->robj = oset[i];
477 +        }
478 + }
479 +
480 +
481 + extern int
482 + localhit(               /* check for hit in the octree */
483 +        register RAY  *r,
484 +        register CUBE  *scene
485 + )
486 + {
487          OBJECT  cxset[MAXCSET+1];       /* set of checked objects */
488          FVECT  curpos;                  /* current cube position */
489          int  sflags;                    /* sign flags */
# Line 522 | Line 537 | register CUBE  *scene;
537          }
538          cxset[0] = 0;
539          raymove(curpos, cxset, sflags, r, scene);
540 <        return(r->ro != NULL & r->ro != &Aftplane);
540 >        return((r->ro != NULL) & (r->ro != &Aftplane));
541   }
542  
543  
544   static int
545 < raymove(pos, cxs, dirf, r, cu)          /* check for hit as we move */
546 < FVECT  pos;                     /* current position, modified herein */
547 < OBJECT  *cxs;                   /* checked objects, modified by checkhit */
548 < int  dirf;                      /* direction indicators to speed tests */
549 < register RAY  *r;
550 < register CUBE  *cu;
545 > raymove(                /* check for hit as we move */
546 >        FVECT  pos,                     /* current position, modified herein */
547 >        OBJECT  *cxs,                   /* checked objects, modified by checkhit */
548 >        int  dirf,                      /* direction indicators to speed tests */
549 >        register RAY  *r,
550 >        register CUBE  *cu
551 > )
552   {
553          int  ax;
554          double  dt, t;
# Line 613 | Line 629 | register CUBE  *cu;
629  
630  
631   static int
632 < checkhit(r, cu, cxs)            /* check for hit in full cube */
633 < register RAY  *r;
634 < CUBE  *cu;
635 < OBJECT  *cxs;
632 > checkhit(               /* check for hit in full cube */
633 >        register RAY  *r,
634 >        CUBE  *cu,
635 >        OBJECT  *cxs
636 > )
637   {
638          OBJECT  oset[MAXSET+1];
622        register OBJREC  *o;
623        register int  i;
639  
640          objset(oset, cu->cutree);
641 <        checkset(oset, cxs);                    /* eliminate double-checking */
642 <        for (i = oset[0]; i > 0; i--) {
643 <                o = objptr(oset[i]);
644 <                if ((*ofun[o->otype].funp)(o, r))
645 <                        r->robj = oset[i];
631 <        }
632 <        if (r->ro == NULL)
641 >        checkset(oset, cxs);                    /* avoid double-checking */
642 >
643 >        (*r->hitf)(oset, r);                    /* test for hit in set */
644 >
645 >        if (r->robj == OVOID)
646                  return(0);                      /* no scores yet */
647  
648          return(incube(cu, r->rop));             /* hit OK if in current cube */
# Line 637 | Line 650 | OBJECT  *cxs;
650  
651  
652   static void
653 < checkset(os, cs)                /* modify checked set and set to check */
654 < register OBJECT  *os;                   /* os' = os - cs */
655 < register OBJECT  *cs;                   /* cs' = cs + os */
653 > checkset(               /* modify checked set and set to check */
654 >        register OBJECT  *os,                   /* os' = os - cs */
655 >        register OBJECT  *cs                    /* cs' = cs + os */
656 > )
657   {
658          OBJECT  cset[MAXCSET+MAXSET+1];
659          register int  i, j;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines