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.18 by greg, Fri Jan 6 14:00:47 1995 UTC vs.
Revision 2.41 by schorsch, Mon Jul 21 22:30:19 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1994 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
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 36 | 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 55 | Line 53 | double  rw;
53                  r->rsrc = -1;
54                  r->clipset = NULL;
55                  r->revf = raytrace;
56 +                copycolor(r->cext, cextinction);
57 +                copycolor(r->albedo, salbedo);
58 +                r->gecc = seccg;
59 +                r->slights = NULL;
60          } else {                                /* spawned ray */
61                  r->rlvl = ro->rlvl;
62                  if (rt & RAYREFL) {
63                          r->rlvl++;
64                          r->rsrc = -1;
65                          r->clipset = ro->clipset;
66 +                        r->rmax = 0.0;
67                  } else {
68                          r->rsrc = ro->rsrc;
69                          r->clipset = ro->newcset;
70 +                        r->rmax = ro->rmax <= FTINY ? 0.0 : ro->rmax - ro->rot;
71                  }
72                  r->revf = ro->revf;
73 <                r->rweight = ro->rweight * rw;
73 >                copycolor(r->cext, ro->cext);
74 >                copycolor(r->albedo, ro->albedo);
75 >                r->gecc = ro->gecc;
76 >                r->slights = ro->slights;
77                  r->crtype = ro->crtype | (r->rtype = rt);
78                  VCOPY(r->rorg, ro->rop);
79 <                r->rmax = 0.0;
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);
89        r->rt = 0.0;
107   }
108  
109  
110 + void
111   raytrace(r)                     /* trace a ray and compute its value */
112   RAY  *r;
113   {
96        extern int  (*trace)();
97        int  gotmat;
98
114          if (localhit(r, &thescene))
115 <                gotmat = raycont(r);
115 >                raycont(r);             /* hit local surface, evaluate */
116          else if (r->ro == &Aftplane) {
117 <                r->ro = NULL;
117 >                r->ro = NULL;           /* hit aft clipping plane */
118                  r->rot = FHUGE;
119          } else if (sourcehit(r))
120 <                gotmat = rayshade(r, r->ro->omod);
120 >                rayshade(r, r->ro->omod);       /* distant source */
121  
122 <        if (r->ro != NULL && !gotmat)
108 <                objerror(r->ro, USER, "material not found");
122 >        rayparticipate(r);              /* for participating medium */
123  
124          if (trace != NULL)
125                  (*trace)(r);            /* trace execution */
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);
121                return(1);
122        }
123        return(rayshade(r, r->ro->omod));
136   }
137  
138  
139 + void
140   raytrans(r)                     /* transmit ray as is */
141   register RAY  *r;
142   {
# Line 131 | Line 144 | register RAY  *r;
144  
145          if (rayorigin(&tr, r, TRANS, 1.0) == 0) {
146                  VCOPY(tr.rdir, r->rdir);
134                if (r->rmax > FTINY)
135                        tr.rmax = r->rmax - r->rot;
147                  rayvalue(&tr);
148                  copycolor(r->rcol, tr.rcol);
149                  r->rt = r->rot + tr.rt;
# Line 140 | 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   {
147        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 160 | Line 174 | int  mod;
174                  }
175                  ******/
176                                          /* hack for irradiance calculation */
177 <                if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS))) {
177 >                if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS)) &&
178 >                                (ofun[m->otype].flags & (T_M|T_X))) {
179                          if (irr_ignore(m->otype)) {
180 + #if  MAXLOOP
181                                  depth--;
182 + #endif
183                                  raytrans(r);
184                                  return(1);
185                          }
# Line 172 | Line 189 | int  mod;
189                                          /* materials call raytexture */
190                  gotmat = (*ofun[m->otype].funp)(m, r);
191          }
192 + #if  MAXLOOP
193          depth--;
194 + #endif
195          return(gotmat);
196   }
197  
198  
199 + void
200 + rayparticipate(r)                       /* compute ray medium participation */
201 + register RAY  *r;
202 + {
203 +        COLOR   ce, ca;
204 +        double  re, ge, be;
205 +
206 +        if (intens(r->cext) <= 1./FHUGE)
207 +                return;                         /* no medium */
208 +        re = r->rot*colval(r->cext,RED);
209 +        ge = r->rot*colval(r->cext,GRN);
210 +        be = r->rot*colval(r->cext,BLU);
211 +        if (r->crtype & SHADOW) {               /* no scattering for sources */
212 +                re *= 1. - colval(r->albedo,RED);
213 +                ge *= 1. - colval(r->albedo,GRN);
214 +                be *= 1. - colval(r->albedo,BLU);
215 +        }
216 +        setcolor(ce,    re<=0. ? 1. : re>92. ? 0. : exp(-re),
217 +                        ge<=0. ? 1. : ge>92. ? 0. : exp(-ge),
218 +                        be<=0. ? 1. : be>92. ? 0. : exp(-be));
219 +        multcolor(r->rcol, ce);                 /* path absorption */
220 +        if (r->crtype & SHADOW || intens(r->albedo) <= FTINY)
221 +                return;                         /* no scattering */
222 +        setcolor(ca,
223 +                colval(r->albedo,RED)*colval(ambval,RED)*(1.-colval(ce,RED)),
224 +                colval(r->albedo,GRN)*colval(ambval,GRN)*(1.-colval(ce,GRN)),
225 +                colval(r->albedo,BLU)*colval(ambval,BLU)*(1.-colval(ce,BLU)));
226 +        addcolor(r->rcol, ca);                  /* ambient in scattering */
227 +        srcscatter(r);                          /* source in scattering */
228 + }
229 +
230 +
231 + void
232   raytexture(r, mod)                      /* get material modifiers */
233   RAY  *r;
234 < int  mod;
234 > OBJECT  mod;
235   {
184        static int  depth = 0;
236          register OBJREC  *m;
237 + #if  MAXLOOP
238 +        static int  depth = 0;
239                                          /* check for infinite loop */
240          if (depth++ >= MAXLOOP)
241                  objerror(r->ro, USER, "modifier loop");
242 + #endif
243                                          /* execute textures and patterns */
244          for ( ; mod != OVOID; mod = m->omod) {
245                  m = objptr(mod);
# Line 195 | Line 249 | int  mod;
249                          error(USER, errmsg);
250                  }
251                  ******/
252 <                if ((*ofun[m->otype].funp)(m, r))
253 <                        objerror(r->ro, USER, "conflicting materials");
252 >                if ((*ofun[m->otype].funp)(m, r)) {
253 >                        sprintf(errmsg, "conflicting material \"%s\"",
254 >                                        m->oname);
255 >                        objerror(r->ro, USER, errmsg);
256 >                }
257          }
258 + #if  MAXLOOP
259          depth--;                        /* end here */
260 + #endif
261   }
262  
263  
264 + int
265   raymixture(r, fore, back, coef)         /* mix modifiers */
266   register RAY  *r;
267   OBJECT  fore, back;
# Line 210 | Line 270 | double  coef;
270          RAY  fr, br;
271          int  foremat, backmat;
272          register int  i;
273 <                                        /* clip coefficient */
273 >                                        /* bound coefficient */
274          if (coef > 1.0)
275                  coef = 1.0;
276          else if (coef < 0.0)
277                  coef = 0.0;
278                                          /* compute foreground and background */
279 <        foremat = backmat = -1;
279 >        foremat = backmat = 0;
280                                          /* foreground */
281 <        copystruct(&fr, r);
282 <        if (fore != OVOID && coef > FTINY)
281 >        fr = *r;
282 >        if (coef > FTINY)
283                  foremat = rayshade(&fr, fore);
284                                          /* background */
285 <        copystruct(&br, r);
286 <        if (back != OVOID && coef < 1.0-FTINY)
285 >        br = *r;
286 >        if (coef < 1.0-FTINY)
287                  backmat = rayshade(&br, back);
288 <                                        /* check */
289 <        if (foremat < 0)
290 <                if (backmat < 0)
291 <                        foremat = backmat = 0;
292 <                else
293 <                        foremat = backmat;
294 <        else if (backmat < 0)
235 <                backmat = foremat;
236 <        if ((foremat==0) != (backmat==0))
237 <                objerror(r->ro, USER, "mixing material with non-material");
288 >                                        /* check for transparency */
289 >        if (backmat ^ foremat) {
290 >                if (backmat && coef > FTINY)
291 >                        raytrans(&fr);
292 >                else if (foremat && coef < 1.0-FTINY)
293 >                        raytrans(&br);
294 >        }
295                                          /* mix perturbations */
296          for (i = 0; i < 3; i++)
297                  r->pert[i] = coef*fr.pert[i] + (1.0-coef)*br.pert[i];
# Line 243 | Line 300 | double  coef;
300          scalecolor(br.pcol, 1.0-coef);
301          copycolor(r->pcol, fr.pcol);
302          addcolor(r->pcol, br.pcol);
303 +                                        /* return value tells if material */
304 +        if (!foremat & !backmat)
305 +                return(0);
306                                          /* mix returned ray values */
307 <        if (foremat) {
308 <                scalecolor(fr.rcol, coef);
309 <                scalecolor(br.rcol, 1.0-coef);
310 <                copycolor(r->rcol, fr.rcol);
311 <                addcolor(r->rcol, br.rcol);
312 <                r->rt = bright(fr.rcol) > bright(br.rcol) ? fr.rt : br.rt;
307 >        scalecolor(fr.rcol, coef);
308 >        scalecolor(br.rcol, 1.0-coef);
309 >        copycolor(r->rcol, fr.rcol);
310 >        addcolor(r->rcol, br.rcol);
311 >        r->rt = bright(fr.rcol) > bright(br.rcol) ? fr.rt : br.rt;
312 >        return(1);
313 > }
314 >
315 >
316 > double
317 > raydist(r, flags)               /* compute (cumulative) ray distance */
318 > register RAY  *r;
319 > register int  flags;
320 > {
321 >        double  sum = 0.0;
322 >
323 >        while (r != NULL && r->crtype&flags) {
324 >                sum += r->rot;
325 >                r = r->parent;
326          }
327 <                                        /* return value tells if material */
255 <        return(foremat);
327 >        return(sum);
328   }
329  
330  
# Line 291 | Line 363 | register RAY  *r;
363   }
364  
365  
366 + void
367   newrayxf(r)                     /* get new tranformation matrix for ray */
368   RAY  *r;
369   {
# Line 310 | Line 383 | RAY  *r;
383                  if (rp->rox == &xp->xf) {               /* xp in use */
384                          xp = xp->next;                  /* move to next */
385                          if (xp == xflast) {             /* need new one */
386 <                                xp = (struct xfn *)bmalloc(sizeof(struct xfn));
386 >                                xp = (struct xfn *)malloc(sizeof(struct xfn));
387                                  if (xp == NULL)
388                                          error(SYSTEM,
389                                                  "out of memory in newrayxf");
# Line 327 | Line 400 | RAY  *r;
400   }
401  
402  
403 + void
404   flipsurface(r)                  /* reverse surface orientation */
405   register RAY  *r;
406   {
# Line 340 | Line 414 | register RAY  *r;
414   }
415  
416  
417 + void
418 + rayhit(oset, r)                 /* standard ray hit test */
419 + OBJECT  *oset;
420 + RAY  *r;
421 + {
422 +        OBJREC  *o;
423 +        int     i;
424 +
425 +        for (i = oset[0]; i > 0; i--) {
426 +                o = objptr(oset[i]);
427 +                if ((*ofun[o->otype].funp)(o, r))
428 +                        r->robj = oset[i];
429 +        }
430 + }
431 +
432 +
433 + int
434   localhit(r, scene)              /* check for hit in the octree */
435   register RAY  *r;
436   register CUBE  *scene;
# Line 396 | Line 487 | register CUBE  *scene;
487                          return(0);
488          }
489          cxset[0] = 0;
490 <        return(raymove(curpos, cxset, sflags, r, scene) == RAYHIT &&
491 <                        r->ro != &Aftplane);
490 >        raymove(curpos, cxset, sflags, r, scene);
491 >        return(r->ro != NULL & r->ro != &Aftplane);
492   }
493  
494  
# Line 487 | Line 578 | register CUBE  *cu;
578   }
579  
580  
581 < static
581 > static int
582   checkhit(r, cu, cxs)            /* check for hit in full cube */
583   register RAY  *r;
584   CUBE  *cu;
585   OBJECT  *cxs;
586   {
587          OBJECT  oset[MAXSET+1];
497        register OBJREC  *o;
498        register int  i;
588  
589          objset(oset, cu->cutree);
590 <        checkset(oset, cxs);                    /* eliminate double-checking */
591 <        for (i = oset[0]; i > 0; i--) {
592 <                o = objptr(oset[i]);
593 <                (*ofun[o->otype].funp)(o, r);
594 <        }
506 <        if (r->ro == NULL)
590 >        checkset(oset, cxs);                    /* avoid double-checking */
591 >
592 >        (*r->hitf)(oset, r);                    /* test for hit in set */
593 >
594 >        if (r->robj == OVOID)
595                  return(0);                      /* no scores yet */
596  
597          return(incube(cu, r->rop));             /* hit OK if in current cube */
598   }
599  
600  
601 < static
601 > static void
602   checkset(os, cs)                /* modify checked set and set to check */
603   register OBJECT  *os;                   /* os' = os - cs */
604   register OBJECT  *cs;                   /* cs' = cs + os */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines