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.23 by greg, Fri Dec 8 18:49:09 1995 UTC vs.
Revision 2.37 by greg, Tue Mar 11 17:08:55 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 + #include "copyright.h"
11 +
12   #include  "ray.h"
13  
15 #include  "octree.h"
16
14   #include  "otypes.h"
15  
16   #include  "otspecial.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 extern COLOR  ambval;                   /* ambient value */
28
29 extern COLOR  cextinction;              /* global extinction coefficient */
30 extern double  salbedo;                 /* global scattering albedo */
31 extern double  seccg;                   /* global scattering eccentricity */
32 extern double  ssampdist;               /* scatter sampling distance */
33
20   unsigned long  raynum = 0;              /* next unique ray number */
21   unsigned long  nrays = 0;               /* number of calls to localhit */
22  
# Line 42 | Line 28 | OBJREC  Lamb = {
28  
29   OBJREC  Aftplane;                       /* aft clipping plane object */
30  
31 < static int  raymove(), checkset(), checkhit();
31 > static int  raymove(), checkhit();
32 > static void  checkset();
33  
34 < #define  MAXLOOP        128             /* modifier loop detection */
34 > #ifndef  MAXLOOP
35 > #define  MAXLOOP        0               /* modifier loop detection */
36 > #endif
37  
38   #define  RAYHIT         (-1)            /* return value for intercepted ray */
39  
40  
41 + int
42   rayorigin(r, ro, rt, rw)                /* start new ray from old one */
43   register RAY  *r, *ro;
44   int  rt;
45   double  rw;
46   {
47 +        double  re;
48 +
49          if ((r->parent = ro) == NULL) {         /* primary ray */
50                  r->rlvl = 0;
51                  r->rweight = rw;
# Line 62 | Line 54 | double  rw;
54                  r->clipset = NULL;
55                  r->revf = raytrace;
56                  copycolor(r->cext, cextinction);
57 <                r->albedo = salbedo;
57 >                copycolor(r->albedo, salbedo);
58                  r->gecc = seccg;
59                  r->slights = NULL;
60          } else {                                /* spawned ray */
# Line 79 | Line 71 | double  rw;
71                  }
72                  r->revf = ro->revf;
73                  copycolor(r->cext, ro->cext);
74 <                r->albedo = ro->albedo;
74 >                copycolor(r->albedo, ro->albedo);
75                  r->gecc = ro->gecc;
76                  r->slights = ro->slights;
85                r->rweight = ro->rweight * rw;
77                  r->crtype = ro->crtype | (r->rtype = rt);
78                  VCOPY(r->rorg, ro->rop);
79 +                r->rweight = ro->rweight * rw;
80 +                                                /* estimate absorption */
81 +                re = colval(ro->cext,RED) < colval(ro->cext,GRN) ?
82 +                                colval(ro->cext,RED) : colval(ro->cext,GRN);
83 +                if (colval(ro->cext,BLU) < re) re = colval(ro->cext,BLU);
84 +                if (re > 0.)
85 +                        r->rweight *= exp(-re*ro->rot);
86          }
87          rayclear(r);
88          return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1);
89   }
90  
91  
92 + void
93   rayclear(r)                     /* clear a ray for (re)evaluation */
94   register RAY  *r;
95   {
96          r->rno = raynum++;
97          r->newcset = r->clipset;
98 +        r->hitf = rayhit;
99 +        r->robj = OVOID;
100          r->ro = NULL;
101 <        r->rot = FHUGE;
101 >        r->rox = NULL;
102 >        r->rt = r->rot = FHUGE;
103          r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
104 +        r->uv[0] = r->uv[1] = 0.0;
105          setcolor(r->pcol, 1.0, 1.0, 1.0);
106          setcolor(r->rcol, 0.0, 0.0, 0.0);
104        r->rt = 0.0;
107   }
108  
109  
110 + void
111   raytrace(r)                     /* trace a ray and compute its value */
112   RAY  *r;
113   {
111        extern int  (*trace)();
112        int  gotmat;
113
114          if (localhit(r, &thescene))
115 <                gotmat = raycont(r);    /* hit local surface, evaluate */
115 >                raycont(r);             /* hit local surface, evaluate */
116          else if (r->ro == &Aftplane) {
117                  r->ro = NULL;           /* hit aft clipping plane */
118                  r->rot = FHUGE;
119          } else if (sourcehit(r))
120 <                gotmat = rayshade(r, r->ro->omod);      /* distant source */
120 >                rayshade(r, r->ro->omod);       /* distant source */
121  
122        if (r->ro != NULL && !gotmat)
123                objerror(r->ro, USER, "material not found");
124
122          rayparticipate(r);              /* for participating medium */
123  
124          if (trace != NULL)
# Line 129 | Line 126 | RAY  *r;
126   }
127  
128  
129 + void
130   raycont(r)                      /* check for clipped object and continue */
131   register RAY  *r;
132   {
133          if ((r->clipset != NULL && inset(r->clipset, r->ro->omod)) ||
134 <                        r->ro->omod == OVOID) {
134 >                        !rayshade(r, r->ro->omod))
135                  raytrans(r);
138                return(1);
139        }
140        return(rayshade(r, r->ro->omod));
136   }
137  
138  
139 + void
140   raytrans(r)                     /* transmit ray as is */
141   register RAY  *r;
142   {
# Line 155 | Line 151 | register RAY  *r;
151   }
152  
153  
154 + int
155   rayshade(r, mod)                /* shade ray r with material mod */
156   register RAY  *r;
157   int  mod;
158   {
162        static int  depth = 0;
159          int  gotmat;
160          register OBJREC  *m;
161 + #if  MAXLOOP
162 +        static int  depth = 0;
163                                          /* check for infinite loop */
164          if (depth++ >= MAXLOOP)
165                  objerror(r->ro, USER, "possible modifier loop");
166 + #endif
167          r->rt = r->rot;                 /* set effective ray length */
168          for (gotmat = 0; !gotmat && mod != OVOID; mod = m->omod) {
169                  m = objptr(mod);
# Line 177 | Line 176 | int  mod;
176                                          /* hack for irradiance calculation */
177                  if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS))) {
178                          if (irr_ignore(m->otype)) {
179 + #if  MAXLOOP
180                                  depth--;
181 + #endif
182                                  raytrans(r);
183                                  return(1);
184                          }
# Line 187 | Line 188 | int  mod;
188                                          /* materials call raytexture */
189                  gotmat = (*ofun[m->otype].funp)(m, r);
190          }
191 + #if  MAXLOOP
192          depth--;
193 + #endif
194          return(gotmat);
195   }
196  
197  
198 + void
199   rayparticipate(r)                       /* compute ray medium participation */
200   register RAY  *r;
201   {
202          COLOR   ce, ca;
199        double  dist;
203          double  re, ge, be;
204  
205          if (intens(r->cext) <= 1./FHUGE)
206                  return;                         /* no medium */
207 <        if ((dist = r->rot) >= FHUGE)
208 <                dist = 2.*thescene.cusize;      /* what to use for infinity? */
209 <        if (r->crtype & SHADOW)
210 <                dist *= 1. - salbedo;           /* no scattering for sources */
211 <        if (dist <= FTINY)
212 <                return;                         /* no effective ray travel */
213 <        re = dist*colval(r->cext,RED);
214 <        ge = dist*colval(r->cext,GRN);
215 <        be = dist*colval(r->cext,BLU);
216 <        setcolor(ce,    re>92. ? 0. : exp(-re),
217 <                        ge>92. ? 0. : exp(-ge),
215 <                        be>92. ? 0. : exp(-be));
207 >        re = r->rot*colval(r->cext,RED);
208 >        ge = r->rot*colval(r->cext,GRN);
209 >        be = r->rot*colval(r->cext,BLU);
210 >        if (r->crtype & SHADOW) {               /* no scattering for sources */
211 >                re *= 1. - colval(r->albedo,RED);
212 >                ge *= 1. - colval(r->albedo,GRN);
213 >                be *= 1. - colval(r->albedo,BLU);
214 >        }
215 >        setcolor(ce,    re<=0. ? 1. : re>92. ? 0. : exp(-re),
216 >                        ge<=0. ? 1. : ge>92. ? 0. : exp(-ge),
217 >                        be<=0. ? 1. : be>92. ? 0. : exp(-be));
218          multcolor(r->rcol, ce);                 /* path absorption */
219 <        if (r->albedo <= FTINY || r->crtype & SHADOW)
219 >        if (r->crtype & SHADOW || intens(r->albedo) <= FTINY)
220                  return;                         /* no scattering */
221 <        setcolor(ca,    salbedo*colval(ambval,RED)*(1.-colval(ce,RED)),
222 <                        salbedo*colval(ambval,GRN)*(1.-colval(ce,GRN)),
223 <                        salbedo*colval(ambval,BLU)*(1.-colval(ce,BLU)));
221 >        setcolor(ca,
222 >                colval(r->albedo,RED)*colval(ambval,RED)*(1.-colval(ce,RED)),
223 >                colval(r->albedo,GRN)*colval(ambval,GRN)*(1.-colval(ce,GRN)),
224 >                colval(r->albedo,BLU)*colval(ambval,BLU)*(1.-colval(ce,BLU)));
225          addcolor(r->rcol, ca);                  /* ambient in scattering */
226          srcscatter(r);                          /* source in scattering */
227   }
# Line 228 | Line 231 | raytexture(r, mod)                     /* get material modifiers */
231   RAY  *r;
232   int  mod;
233   {
231        static int  depth = 0;
234          register OBJREC  *m;
235 + #if  MAXLOOP
236 +        static int  depth = 0;
237                                          /* check for infinite loop */
238          if (depth++ >= MAXLOOP)
239                  objerror(r->ro, USER, "modifier loop");
240 + #endif
241                                          /* execute textures and patterns */
242          for ( ; mod != OVOID; mod = m->omod) {
243                  m = objptr(mod);
# Line 248 | Line 253 | int  mod;
253                          objerror(r->ro, USER, errmsg);
254                  }
255          }
256 + #if  MAXLOOP
257          depth--;                        /* end here */
258 + #endif
259   }
260  
261  
262 + int
263   raymixture(r, fore, back, coef)         /* mix modifiers */
264   register RAY  *r;
265   OBJECT  fore, back;
# Line 260 | Line 268 | double  coef;
268          RAY  fr, br;
269          int  foremat, backmat;
270          register int  i;
271 <                                        /* clip coefficient */
271 >                                        /* bound coefficient */
272          if (coef > 1.0)
273                  coef = 1.0;
274          else if (coef < 0.0)
275                  coef = 0.0;
276                                          /* compute foreground and background */
277 <        foremat = backmat = -1;
277 >        foremat = backmat = 0;
278                                          /* foreground */
279          copystruct(&fr, r);
280 <        if (fore != OVOID && coef > FTINY)
280 >        if (coef > FTINY)
281                  foremat = rayshade(&fr, fore);
282                                          /* background */
283          copystruct(&br, r);
284 <        if (back != OVOID && coef < 1.0-FTINY)
284 >        if (coef < 1.0-FTINY)
285                  backmat = rayshade(&br, back);
286 <                                        /* check */
287 <        if (foremat < 0)
288 <                if (backmat < 0)
289 <                        foremat = backmat = 0;
290 <                else
291 <                        foremat = backmat;
284 <        else if (backmat < 0)
285 <                backmat = foremat;
286 <        if ((foremat==0) != (backmat==0))
287 <                objerror(r->ro, USER, "mixing material with non-material");
286 >                                        /* check for transparency */
287 >        if (backmat ^ foremat)
288 >                if (backmat && coef > FTINY)
289 >                        raytrans(&fr);
290 >                else if (foremat && coef < 1.0-FTINY)
291 >                        raytrans(&br);
292                                          /* mix perturbations */
293          for (i = 0; i < 3; i++)
294                  r->pert[i] = coef*fr.pert[i] + (1.0-coef)*br.pert[i];
# Line 293 | Line 297 | double  coef;
297          scalecolor(br.pcol, 1.0-coef);
298          copycolor(r->pcol, fr.pcol);
299          addcolor(r->pcol, br.pcol);
296                                        /* mix returned ray values */
297        if (foremat) {
298                scalecolor(fr.rcol, coef);
299                scalecolor(br.rcol, 1.0-coef);
300                copycolor(r->rcol, fr.rcol);
301                addcolor(r->rcol, br.rcol);
302                r->rt = bright(fr.rcol) > bright(br.rcol) ? fr.rt : br.rt;
303        }
300                                          /* return value tells if material */
301 <        return(foremat);
301 >        if (!foremat & !backmat)
302 >                return(0);
303 >                                        /* mix returned ray values */
304 >        scalecolor(fr.rcol, coef);
305 >        scalecolor(br.rcol, 1.0-coef);
306 >        copycolor(r->rcol, fr.rcol);
307 >        addcolor(r->rcol, br.rcol);
308 >        r->rt = bright(fr.rcol) > bright(br.rcol) ? fr.rt : br.rt;
309 >        return(1);
310   }
311  
312  
# Line 356 | Line 360 | register RAY  *r;
360   }
361  
362  
363 + void
364   newrayxf(r)                     /* get new tranformation matrix for ray */
365   RAY  *r;
366   {
# Line 375 | Line 380 | RAY  *r;
380                  if (rp->rox == &xp->xf) {               /* xp in use */
381                          xp = xp->next;                  /* move to next */
382                          if (xp == xflast) {             /* need new one */
383 <                                xp = (struct xfn *)bmalloc(sizeof(struct xfn));
383 >                                xp = (struct xfn *)malloc(sizeof(struct xfn));
384                                  if (xp == NULL)
385                                          error(SYSTEM,
386                                                  "out of memory in newrayxf");
# Line 392 | Line 397 | RAY  *r;
397   }
398  
399  
400 + void
401   flipsurface(r)                  /* reverse surface orientation */
402   register RAY  *r;
403   {
# Line 405 | Line 411 | register RAY  *r;
411   }
412  
413  
414 + void
415 + rayhit(oset, r)                 /* standard ray hit test */
416 + OBJECT  *oset;
417 + RAY  *r;
418 + {
419 +        OBJREC  *o;
420 +        int     i;
421 +
422 +        for (i = oset[0]; i > 0; i--) {
423 +                o = objptr(oset[i]);
424 +                if ((*ofun[o->otype].funp)(o, r))
425 +                        r->robj = oset[i];
426 +        }
427 + }
428 +
429 +
430 + int
431   localhit(r, scene)              /* check for hit in the octree */
432   register RAY  *r;
433   register CUBE  *scene;
# Line 552 | Line 575 | register CUBE  *cu;
575   }
576  
577  
578 < static
578 > static int
579   checkhit(r, cu, cxs)            /* check for hit in full cube */
580   register RAY  *r;
581   CUBE  *cu;
582   OBJECT  *cxs;
583   {
584          OBJECT  oset[MAXSET+1];
562        register OBJREC  *o;
563        register int  i;
585  
586          objset(oset, cu->cutree);
587 <        checkset(oset, cxs);                    /* eliminate double-checking */
588 <        for (i = oset[0]; i > 0; i--) {
589 <                o = objptr(oset[i]);
590 <                (*ofun[o->otype].funp)(o, r);
591 <        }
571 <        if (r->ro == NULL)
587 >        checkset(oset, cxs);                    /* avoid double-checking */
588 >
589 >        (*r->hitf)(oset, r);                    /* test for hit in set */
590 >
591 >        if (r->robj == OVOID)
592                  return(0);                      /* no scores yet */
593  
594          return(incube(cu, r->rop));             /* hit OK if in current cube */
595   }
596  
597  
598 < static
598 > static void
599   checkset(os, cs)                /* modify checked set and set to check */
600   register OBJECT  *os;                   /* os' = os - cs */
601   register OBJECT  *cs;                   /* cs' = cs + os */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines