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.25 by greg, Sat Mar 30 11:18:36 1996 UTC vs.
Revision 2.36 by greg, Tue Mar 4 19:02:22 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1996 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          setcolor(r->pcol, 1.0, 1.0, 1.0);
105          setcolor(r->rcol, 0.0, 0.0, 0.0);
104        r->rt = 0.0;
106   }
107  
108  
109 + void
110   raytrace(r)                     /* trace a ray and compute its value */
111   RAY  *r;
112   {
111        extern int  (*trace)();
112
113          if (localhit(r, &thescene))
114                  raycont(r);             /* hit local surface, evaluate */
115          else if (r->ro == &Aftplane) {
# Line 125 | Line 125 | RAY  *r;
125   }
126  
127  
128 + void
129   raycont(r)                      /* check for clipped object and continue */
130   register RAY  *r;
131   {
# Line 134 | Line 135 | register RAY  *r;
135   }
136  
137  
138 + void
139   raytrans(r)                     /* transmit ray as is */
140   register RAY  *r;
141   {
# Line 148 | Line 150 | register RAY  *r;
150   }
151  
152  
153 + int
154   rayshade(r, mod)                /* shade ray r with material mod */
155   register RAY  *r;
156   int  mod;
157   {
155        static int  depth = 0;
158          int  gotmat;
159          register OBJREC  *m;
160 + #if  MAXLOOP
161 +        static int  depth = 0;
162                                          /* check for infinite loop */
163          if (depth++ >= MAXLOOP)
164                  objerror(r->ro, USER, "possible modifier loop");
165 + #endif
166          r->rt = r->rot;                 /* set effective ray length */
167          for (gotmat = 0; !gotmat && mod != OVOID; mod = m->omod) {
168                  m = objptr(mod);
# Line 170 | Line 175 | int  mod;
175                                          /* hack for irradiance calculation */
176                  if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS))) {
177                          if (irr_ignore(m->otype)) {
178 + #if  MAXLOOP
179                                  depth--;
180 + #endif
181                                  raytrans(r);
182                                  return(1);
183                          }
# Line 180 | Line 187 | int  mod;
187                                          /* materials call raytexture */
188                  gotmat = (*ofun[m->otype].funp)(m, r);
189          }
190 + #if  MAXLOOP
191          depth--;
192 + #endif
193          return(gotmat);
194   }
195  
196  
197 + void
198   rayparticipate(r)                       /* compute ray medium participation */
199   register RAY  *r;
200   {
201          COLOR   ce, ca;
192        double  dist;
202          double  re, ge, be;
203  
204          if (intens(r->cext) <= 1./FHUGE)
205                  return;                         /* no medium */
206 <        if ((dist = r->rot) >= FHUGE)
207 <                dist = 2.*thescene.cusize;      /* what to use for infinity? */
208 <        if (r->crtype & SHADOW)
209 <                dist *= 1. - r->albedo;         /* no scattering for sources */
210 <        if (dist <= FTINY)
211 <                return;                         /* no effective ray travel */
212 <        re = dist*colval(r->cext,RED);
213 <        ge = dist*colval(r->cext,GRN);
214 <        be = dist*colval(r->cext,BLU);
215 <        setcolor(ce,    re>92. ? 0. : exp(-re),
216 <                        ge>92. ? 0. : exp(-ge),
208 <                        be>92. ? 0. : exp(-be));
206 >        re = r->rot*colval(r->cext,RED);
207 >        ge = r->rot*colval(r->cext,GRN);
208 >        be = r->rot*colval(r->cext,BLU);
209 >        if (r->crtype & SHADOW) {               /* no scattering for sources */
210 >                re *= 1. - colval(r->albedo,RED);
211 >                ge *= 1. - colval(r->albedo,GRN);
212 >                be *= 1. - colval(r->albedo,BLU);
213 >        }
214 >        setcolor(ce,    re<=0. ? 1. : re>92. ? 0. : exp(-re),
215 >                        ge<=0. ? 1. : ge>92. ? 0. : exp(-ge),
216 >                        be<=0. ? 1. : be>92. ? 0. : exp(-be));
217          multcolor(r->rcol, ce);                 /* path absorption */
218 <        if (r->albedo <= FTINY || r->crtype & SHADOW)
218 >        if (r->crtype & SHADOW || intens(r->albedo) <= FTINY)
219                  return;                         /* no scattering */
220 <        setcolor(ca,    r->albedo*colval(ambval,RED)*(1.-colval(ce,RED)),
221 <                        r->albedo*colval(ambval,GRN)*(1.-colval(ce,GRN)),
222 <                        r->albedo*colval(ambval,BLU)*(1.-colval(ce,BLU)));
220 >        setcolor(ca,
221 >                colval(r->albedo,RED)*colval(ambval,RED)*(1.-colval(ce,RED)),
222 >                colval(r->albedo,GRN)*colval(ambval,GRN)*(1.-colval(ce,GRN)),
223 >                colval(r->albedo,BLU)*colval(ambval,BLU)*(1.-colval(ce,BLU)));
224          addcolor(r->rcol, ca);                  /* ambient in scattering */
225          srcscatter(r);                          /* source in scattering */
226   }
# Line 221 | Line 230 | raytexture(r, mod)                     /* get material modifiers */
230   RAY  *r;
231   int  mod;
232   {
224        static int  depth = 0;
233          register OBJREC  *m;
234 + #if  MAXLOOP
235 +        static int  depth = 0;
236                                          /* check for infinite loop */
237          if (depth++ >= MAXLOOP)
238                  objerror(r->ro, USER, "modifier loop");
239 + #endif
240                                          /* execute textures and patterns */
241          for ( ; mod != OVOID; mod = m->omod) {
242                  m = objptr(mod);
# Line 241 | Line 252 | int  mod;
252                          objerror(r->ro, USER, errmsg);
253                  }
254          }
255 + #if  MAXLOOP
256          depth--;                        /* end here */
257 + #endif
258   }
259  
260  
261 + int
262   raymixture(r, fore, back, coef)         /* mix modifiers */
263   register RAY  *r;
264   OBJECT  fore, back;
# Line 270 | Line 284 | double  coef;
284                  backmat = rayshade(&br, back);
285                                          /* check for transparency */
286          if (backmat ^ foremat)
287 <                if (backmat)
287 >                if (backmat && coef > FTINY)
288                          raytrans(&fr);
289 <                else
289 >                else if (foremat && coef < 1.0-FTINY)
290                          raytrans(&br);
291                                          /* mix perturbations */
292          for (i = 0; i < 3; i++)
# Line 345 | Line 359 | register RAY  *r;
359   }
360  
361  
362 + void
363   newrayxf(r)                     /* get new tranformation matrix for ray */
364   RAY  *r;
365   {
# Line 364 | Line 379 | RAY  *r;
379                  if (rp->rox == &xp->xf) {               /* xp in use */
380                          xp = xp->next;                  /* move to next */
381                          if (xp == xflast) {             /* need new one */
382 <                                xp = (struct xfn *)bmalloc(sizeof(struct xfn));
382 >                                xp = (struct xfn *)malloc(sizeof(struct xfn));
383                                  if (xp == NULL)
384                                          error(SYSTEM,
385                                                  "out of memory in newrayxf");
# Line 381 | Line 396 | RAY  *r;
396   }
397  
398  
399 + void
400   flipsurface(r)                  /* reverse surface orientation */
401   register RAY  *r;
402   {
# Line 394 | Line 410 | register RAY  *r;
410   }
411  
412  
413 + void
414 + rayhit(oset, r)                 /* standard ray hit test */
415 + OBJECT  *oset;
416 + RAY  *r;
417 + {
418 +        OBJREC  *o;
419 +        int     i;
420 +
421 +        for (i = oset[0]; i > 0; i--) {
422 +                o = objptr(oset[i]);
423 +                if ((*ofun[o->otype].funp)(o, r))
424 +                        r->robj = oset[i];
425 +        }
426 + }
427 +
428 +
429 + int
430   localhit(r, scene)              /* check for hit in the octree */
431   register RAY  *r;
432   register CUBE  *scene;
# Line 541 | Line 574 | register CUBE  *cu;
574   }
575  
576  
577 < static
577 > static int
578   checkhit(r, cu, cxs)            /* check for hit in full cube */
579   register RAY  *r;
580   CUBE  *cu;
581   OBJECT  *cxs;
582   {
583          OBJECT  oset[MAXSET+1];
551        register OBJREC  *o;
552        register int  i;
584  
585          objset(oset, cu->cutree);
586 <        checkset(oset, cxs);                    /* eliminate double-checking */
587 <        for (i = oset[0]; i > 0; i--) {
588 <                o = objptr(oset[i]);
589 <                (*ofun[o->otype].funp)(o, r);
590 <        }
560 <        if (r->ro == NULL)
586 >        checkset(oset, cxs);                    /* avoid double-checking */
587 >
588 >        (*r->hitf)(oset, r);                    /* test for hit in set */
589 >
590 >        if (r->robj == OVOID)
591                  return(0);                      /* no scores yet */
592  
593          return(incube(cu, r->rop));             /* hit OK if in current cube */
594   }
595  
596  
597 < static
597 > static void
598   checkset(os, cs)                /* modify checked set and set to check */
599   register OBJECT  *os;                   /* os' = os - cs */
600   register OBJECT  *cs;                   /* cs' = cs + os */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines