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.17 by greg, Wed Dec 21 09:10:23 1994 UTC vs.
Revision 2.47 by greg, Thu Apr 14 18:04:12 2005 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  "ray.h"
10 > #include "copyright.h"
11  
12 < #include  "octree.h"
13 <
12 > #include  "ray.h"
13 > #include  "source.h"
14   #include  "otypes.h"
18
15   #include  "otspecial.h"
16  
17   #define  MAXCSET        ((MAXSET+1)*2-1)        /* maximum check set size */
18  
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
19   unsigned long  raynum = 0;              /* next unique ray number */
20   unsigned long  nrays = 0;               /* number of calls to localhit */
21  
22 < static FLOAT  Lambfa[5] = {PI, PI, PI, 0.0, 0.0};
22 > static RREAL  Lambfa[5] = {PI, PI, PI, 0.0, 0.0};
23   OBJREC  Lamb = {
24          OVOID, MAT_PLASTIC, "Lambertian",
25          {0, 5, NULL, Lambfa}, NULL,
# Line 36 | Line 27 | OBJREC  Lamb = {
27  
28   OBJREC  Aftplane;                       /* aft clipping plane object */
29  
39 static int  raymove(), checkset(), checkhit();
40
41 #define  MAXLOOP        128             /* modifier loop detection */
42
30   #define  RAYHIT         (-1)            /* return value for intercepted ray */
31  
32 + static int raymove(FVECT  pos, OBJECT  *cxs, int  dirf, RAY  *r, CUBE  *cu);
33 + static int checkhit(RAY  *r, CUBE  *cu, OBJECT  *cxs);
34 + static void checkset(OBJECT  *os, OBJECT  *cs);
35  
36 < rayorigin(r, ro, rt, rw)                /* start new ray from old one */
37 < register RAY  *r, *ro;
38 < int  rt;
39 < double  rw;
36 >
37 > extern int
38 > rayorigin(              /* start new ray from old one */
39 >        register RAY  *r,
40 >        register RAY  *ro,
41 >        int  rt,
42 >        double  rw
43 > )
44   {
45 +        double  re;
46 +
47          if ((r->parent = ro) == NULL) {         /* primary ray */
48                  r->rlvl = 0;
49                  r->rweight = rw;
# Line 55 | Line 51 | double  rw;
51                  r->rsrc = -1;
52                  r->clipset = NULL;
53                  r->revf = raytrace;
54 +                copycolor(r->cext, cextinction);
55 +                copycolor(r->albedo, salbedo);
56 +                r->gecc = seccg;
57 +                r->slights = NULL;
58 +        } else if (ro->rot >= FHUGE) {          /* illegal continuation */
59 +                memset(r, 0, sizeof(RAY));
60 +                return(-1);
61          } else {                                /* spawned ray */
62                  r->rlvl = ro->rlvl;
63                  if (rt & RAYREFL) {
64                          r->rlvl++;
65                          r->rsrc = -1;
66                          r->clipset = ro->clipset;
67 +                        r->rmax = 0.0;
68                  } else {
69                          r->rsrc = ro->rsrc;
70                          r->clipset = ro->newcset;
71 +                        r->rmax = ro->rmax <= FTINY ? 0.0 : ro->rmax - ro->rot;
72                  }
73                  r->revf = ro->revf;
74 <                r->rweight = ro->rweight * rw;
74 >                copycolor(r->cext, ro->cext);
75 >                copycolor(r->albedo, ro->albedo);
76 >                r->gecc = ro->gecc;
77 >                r->slights = ro->slights;
78                  r->crtype = ro->crtype | (r->rtype = rt);
79                  VCOPY(r->rorg, ro->rop);
80 <                r->rmax = 0.0;
80 >                r->rweight = ro->rweight * rw;
81 >                                                /* estimate absorption */
82 >                re = colval(ro->cext,RED) < colval(ro->cext,GRN) ?
83 >                                colval(ro->cext,RED) : colval(ro->cext,GRN);
84 >                if (colval(ro->cext,BLU) < re) re = colval(ro->cext,BLU);
85 >                if (re > 0.)
86 >                        r->rweight *= exp(-re*ro->rot);
87          }
88          rayclear(r);
89          return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1);
90   }
91  
92  
93 < rayclear(r)                     /* clear a ray for (re)evaluation */
94 < register RAY  *r;
93 > extern void
94 > rayclear(                       /* clear a ray for (re)evaluation */
95 >        register RAY  *r
96 > )
97   {
98          r->rno = raynum++;
99          r->newcset = r->clipset;
100 +        r->hitf = rayhit;
101 +        r->robj = OVOID;
102          r->ro = NULL;
103 <        r->rot = FHUGE;
103 >        r->rox = NULL;
104 >        r->rt = r->rot = FHUGE;
105          r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
106 +        r->uv[0] = r->uv[1] = 0.0;
107          setcolor(r->pcol, 1.0, 1.0, 1.0);
108          setcolor(r->rcol, 0.0, 0.0, 0.0);
89        r->rt = 0.0;
109   }
110  
111  
112 < raytrace(r)                     /* trace a ray and compute its value */
113 < RAY  *r;
112 > extern void
113 > raytrace(                       /* trace a ray and compute its value */
114 >        RAY  *r
115 > )
116   {
96        extern int  (*trace)();
97        int  gotmat;
98
117          if (localhit(r, &thescene))
118 <                gotmat = raycont(r);
118 >                raycont(r);             /* hit local surface, evaluate */
119          else if (r->ro == &Aftplane) {
120 <                r->ro = NULL;
120 >                r->ro = NULL;           /* hit aft clipping plane */
121                  r->rot = FHUGE;
122          } else if (sourcehit(r))
123 <                gotmat = rayshade(r, r->ro->omod);
123 >                rayshade(r, r->ro->omod);       /* distant source */
124  
125 <        if (r->ro != NULL && !gotmat)
108 <                objerror(r->ro, USER, "material not found");
125 >        rayparticipate(r);              /* for participating medium */
126  
127          if (trace != NULL)
128                  (*trace)(r);            /* trace execution */
129   }
130  
131  
132 < raycont(r)                      /* check for clipped object and continue */
133 < register RAY  *r;
132 > extern void
133 > raycont(                        /* check for clipped object and continue */
134 >        register RAY  *r
135 > )
136   {
137          if ((r->clipset != NULL && inset(r->clipset, r->ro->omod)) ||
138 <                        r->ro->omod == OVOID) {
138 >                        !rayshade(r, r->ro->omod))
139                  raytrans(r);
121                return(1);
122        }
123        return(rayshade(r, r->ro->omod));
140   }
141  
142  
143 < raytrans(r)                     /* transmit ray as is */
144 < register RAY  *r;
143 > extern void
144 > raytrans(                       /* transmit ray as is */
145 >        register RAY  *r
146 > )
147   {
148          RAY  tr;
149  
150          if (rayorigin(&tr, r, TRANS, 1.0) == 0) {
151                  VCOPY(tr.rdir, r->rdir);
134                if (r->rmax > FTINY)
135                        tr.rmax = r->rmax - r->rot;
152                  rayvalue(&tr);
153                  copycolor(r->rcol, tr.rcol);
154                  r->rt = r->rot + tr.rt;
# Line 140 | Line 156 | register RAY  *r;
156   }
157  
158  
159 < rayshade(r, mod)                /* shade ray r with material mod */
160 < register RAY  *r;
161 < int  mod;
159 > extern int
160 > rayshade(               /* shade ray r with material mod */
161 >        register RAY  *r,
162 >        int  mod
163 > )
164   {
147        static int  depth = 0;
148        int  gotmat;
165          register OBJREC  *m;
166 <                                        /* check for infinite loop */
151 <        if (depth++ >= MAXLOOP)
152 <                objerror(r->ro, USER, "possible modifier loop");
166 >
167          r->rt = r->rot;                 /* set effective ray length */
168 <        for (gotmat = 0; !gotmat && mod != OVOID; mod = m->omod) {
168 >        for ( ; mod != OVOID; mod = m->omod) {
169                  m = objptr(mod);
170                  /****** unnecessary test since modifier() is always called
171                  if (!ismodifier(m->otype)) {
# 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 >                                m->otype != MAT_CLIP &&
179 >                                (ofun[m->otype].flags & (T_M|T_X))) {
180                          if (irr_ignore(m->otype)) {
165                                depth--;
181                                  raytrans(r);
182                                  return(1);
183                          }
184                          if (!islight(m->otype))
185                                  m = &Lamb;
186                  }
187 <                                        /* materials call raytexture */
188 <                gotmat = (*ofun[m->otype].funp)(m, r);
187 >                if ((*ofun[m->otype].funp)(m, r))
188 >                        return(1);      /* materials call raytexture() */
189          }
190 <        depth--;
176 <        return(gotmat);
190 >        return(0);                      /* no material! */
191   }
192  
193  
194 < raytexture(r, mod)                      /* get material modifiers */
195 < RAY  *r;
196 < int  mod;
194 > extern void
195 > rayparticipate(                 /* compute ray medium participation */
196 >        register RAY  *r
197 > )
198   {
199 <        static int  depth = 0;
199 >        COLOR   ce, ca;
200 >        double  re, ge, be;
201 >
202 >        if (intens(r->cext) <= 1./FHUGE)
203 >                return;                         /* no medium */
204 >        re = r->rot*colval(r->cext,RED);
205 >        ge = r->rot*colval(r->cext,GRN);
206 >        be = r->rot*colval(r->cext,BLU);
207 >        if (r->crtype & SHADOW) {               /* no scattering for sources */
208 >                re *= 1. - colval(r->albedo,RED);
209 >                ge *= 1. - colval(r->albedo,GRN);
210 >                be *= 1. - colval(r->albedo,BLU);
211 >        }
212 >        setcolor(ce,    re<=0. ? 1. : re>92. ? 0. : exp(-re),
213 >                        ge<=0. ? 1. : ge>92. ? 0. : exp(-ge),
214 >                        be<=0. ? 1. : be>92. ? 0. : exp(-be));
215 >        multcolor(r->rcol, ce);                 /* path absorption */
216 >        if (r->crtype & SHADOW || intens(r->albedo) <= FTINY)
217 >                return;                         /* no scattering */
218 >        setcolor(ca,
219 >                colval(r->albedo,RED)*colval(ambval,RED)*(1.-colval(ce,RED)),
220 >                colval(r->albedo,GRN)*colval(ambval,GRN)*(1.-colval(ce,GRN)),
221 >                colval(r->albedo,BLU)*colval(ambval,BLU)*(1.-colval(ce,BLU)));
222 >        addcolor(r->rcol, ca);                  /* ambient in scattering */
223 >        srcscatter(r);                          /* source in scattering */
224 > }
225 >
226 >
227 > extern void
228 > raytexture(                     /* get material modifiers */
229 >        RAY  *r,
230 >        OBJECT  mod
231 > )
232 > {
233          register OBJREC  *m;
186                                        /* check for infinite loop */
187        if (depth++ >= MAXLOOP)
188                objerror(r->ro, USER, "modifier loop");
234                                          /* execute textures and patterns */
235          for ( ; mod != OVOID; mod = m->omod) {
236                  m = objptr(mod);
# Line 195 | Line 240 | int  mod;
240                          error(USER, errmsg);
241                  }
242                  ******/
243 <                if ((*ofun[m->otype].funp)(m, r))
244 <                        objerror(r->ro, USER, "conflicting materials");
243 >                if ((*ofun[m->otype].funp)(m, r)) {
244 >                        sprintf(errmsg, "conflicting material \"%s\"",
245 >                                        m->oname);
246 >                        objerror(r->ro, USER, errmsg);
247 >                }
248          }
201        depth--;                        /* end here */
249   }
250  
251  
252 < raymixture(r, fore, back, coef)         /* mix modifiers */
253 < register RAY  *r;
254 < OBJECT  fore, back;
255 < double  coef;
252 > extern int
253 > raymixture(             /* mix modifiers */
254 >        register RAY  *r,
255 >        OBJECT  fore,
256 >        OBJECT  back,
257 >        double  coef
258 > )
259   {
260          RAY  fr, br;
261          int  foremat, backmat;
262          register int  i;
263 <                                        /* clip coefficient */
263 >                                        /* bound coefficient */
264          if (coef > 1.0)
265                  coef = 1.0;
266          else if (coef < 0.0)
267                  coef = 0.0;
268                                          /* compute foreground and background */
269 <        foremat = backmat = -1;
269 >        foremat = backmat = 0;
270                                          /* foreground */
271 <        copystruct(&fr, r);
272 <        if (fore != OVOID && coef > FTINY)
271 >        fr = *r;
272 >        if (coef > FTINY)
273                  foremat = rayshade(&fr, fore);
274                                          /* background */
275 <        copystruct(&br, r);
276 <        if (back != OVOID && coef < 1.0-FTINY)
275 >        br = *r;
276 >        if (coef < 1.0-FTINY)
277                  backmat = rayshade(&br, back);
278 <                                        /* check */
279 <        if (foremat < 0)
280 <                if (backmat < 0)
281 <                        foremat = backmat = 0;
282 <                else
283 <                        foremat = backmat;
284 <        else if (backmat < 0)
235 <                backmat = foremat;
236 <        if ((foremat==0) != (backmat==0))
237 <                objerror(r->ro, USER, "mixing material with non-material");
278 >                                        /* check for transparency */
279 >        if (backmat ^ foremat) {
280 >                if (backmat && coef > FTINY)
281 >                        raytrans(&fr);
282 >                else if (foremat && coef < 1.0-FTINY)
283 >                        raytrans(&br);
284 >        }
285                                          /* mix perturbations */
286          for (i = 0; i < 3; i++)
287                  r->pert[i] = coef*fr.pert[i] + (1.0-coef)*br.pert[i];
# Line 243 | Line 290 | double  coef;
290          scalecolor(br.pcol, 1.0-coef);
291          copycolor(r->pcol, fr.pcol);
292          addcolor(r->pcol, br.pcol);
293 +                                        /* return value tells if material */
294 +        if (!foremat & !backmat)
295 +                return(0);
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;
297 >        scalecolor(fr.rcol, coef);
298 >        scalecolor(br.rcol, 1.0-coef);
299 >        copycolor(r->rcol, fr.rcol);
300 >        addcolor(r->rcol, br.rcol);
301 >        r->rt = bright(fr.rcol) > bright(br.rcol) ? fr.rt : br.rt;
302 >        return(1);
303 > }
304 >
305 >
306 > extern double
307 > raydist(                /* compute (cumulative) ray distance */
308 >        register RAY  *r,
309 >        register int  flags
310 > )
311 > {
312 >        double  sum = 0.0;
313 >
314 >        while (r != NULL && r->crtype&flags) {
315 >                sum += r->rot;
316 >                r = r->parent;
317          }
318 <                                        /* return value tells if material */
255 <        return(foremat);
318 >        return(sum);
319   }
320  
321  
322 < double
323 < raynormal(norm, r)              /* compute perturbed normal for ray */
324 < FVECT  norm;
325 < register RAY  *r;
322 > extern double
323 > raynormal(              /* compute perturbed normal for ray */
324 >        FVECT  norm,
325 >        register RAY  *r
326 > )
327   {
328          double  newdot;
329          register int  i;
# Line 291 | Line 355 | register RAY  *r;
355   }
356  
357  
358 < newrayxf(r)                     /* get new tranformation matrix for ray */
359 < RAY  *r;
358 > extern void
359 > newrayxf(                       /* get new tranformation matrix for ray */
360 >        RAY  *r
361 > )
362   {
363          static struct xfn {
364                  struct xfn  *next;
# Line 310 | Line 376 | RAY  *r;
376                  if (rp->rox == &xp->xf) {               /* xp in use */
377                          xp = xp->next;                  /* move to next */
378                          if (xp == xflast) {             /* need new one */
379 <                                xp = (struct xfn *)bmalloc(sizeof(struct xfn));
379 >                                xp = (struct xfn *)malloc(sizeof(struct xfn));
380                                  if (xp == NULL)
381                                          error(SYSTEM,
382                                                  "out of memory in newrayxf");
# Line 327 | Line 393 | RAY  *r;
393   }
394  
395  
396 < flipsurface(r)                  /* reverse surface orientation */
397 < register RAY  *r;
396 > extern void
397 > flipsurface(                    /* reverse surface orientation */
398 >        register RAY  *r
399 > )
400   {
401          r->rod = -r->rod;
402          r->ron[0] = -r->ron[0];
# Line 340 | Line 408 | register RAY  *r;
408   }
409  
410  
411 < localhit(r, scene)              /* check for hit in the octree */
412 < register RAY  *r;
413 < register CUBE  *scene;
411 > extern void
412 > rayhit(                 /* standard ray hit test */
413 >        OBJECT  *oset,
414 >        RAY  *r
415 > )
416   {
417 +        OBJREC  *o;
418 +        int     i;
419 +
420 +        for (i = oset[0]; i > 0; i--) {
421 +                o = objptr(oset[i]);
422 +                if ((*ofun[o->otype].funp)(o, r))
423 +                        r->robj = oset[i];
424 +        }
425 + }
426 +
427 +
428 + extern int
429 + localhit(               /* check for hit in the octree */
430 +        register RAY  *r,
431 +        register CUBE  *scene
432 + )
433 + {
434          OBJECT  cxset[MAXCSET+1];       /* set of checked objects */
435          FVECT  curpos;                  /* current cube position */
436          int  sflags;                    /* sign flags */
# Line 396 | Line 483 | register CUBE  *scene;
483                          return(0);
484          }
485          cxset[0] = 0;
486 <        return(raymove(curpos, cxset, sflags, r, scene) == RAYHIT &&
487 <                        r->ro != &Aftplane);
486 >        raymove(curpos, cxset, sflags, r, scene);
487 >        return((r->ro != NULL) & (r->ro != &Aftplane));
488   }
489  
490  
491   static int
492 < raymove(pos, cxs, dirf, r, cu)          /* check for hit as we move */
493 < FVECT  pos;                     /* current position, modified herein */
494 < OBJECT  *cxs;                   /* checked objects, modified by checkhit */
495 < int  dirf;                      /* direction indicators to speed tests */
496 < register RAY  *r;
497 < register CUBE  *cu;
492 > raymove(                /* check for hit as we move */
493 >        FVECT  pos,                     /* current position, modified herein */
494 >        OBJECT  *cxs,                   /* checked objects, modified by checkhit */
495 >        int  dirf,                      /* direction indicators to speed tests */
496 >        register RAY  *r,
497 >        register CUBE  *cu
498 > )
499   {
500          int  ax;
501          double  dt, t;
# Line 452 | Line 540 | register CUBE  *cu;
540                  }
541                  /*NOTREACHED*/
542          }
543 <        if (isfull(cu->cutree) && checkhit(r, cu, cxs))
543 >        if (isfull(cu->cutree)) {
544 >                if (checkhit(r, cu, cxs))
545 >                        return(RAYHIT);
546 >        } else if (r->ro == &Aftplane && incube(cu, r->rop))
547                  return(RAYHIT);
548                                          /* advance to next cube */
549          if (dirf&0x11) {
# Line 484 | Line 575 | register CUBE  *cu;
575   }
576  
577  
578 < static
579 < checkhit(r, cu, cxs)            /* check for hit in full cube */
580 < register RAY  *r;
581 < CUBE  *cu;
582 < OBJECT  *cxs;
578 > static int
579 > checkhit(               /* check for hit in full cube */
580 >        register RAY  *r,
581 >        CUBE  *cu,
582 >        OBJECT  *cxs
583 > )
584   {
585          OBJECT  oset[MAXSET+1];
494        register OBJREC  *o;
495        register int  i;
586  
587          objset(oset, cu->cutree);
588 <        checkset(oset, cxs);                    /* eliminate double-checking */
589 <        for (i = oset[0]; i > 0; i--) {
590 <                o = objptr(oset[i]);
591 <                (*ofun[o->otype].funp)(o, r);
592 <        }
503 <        if (r->ro == NULL)
588 >        checkset(oset, cxs);                    /* avoid double-checking */
589 >
590 >        (*r->hitf)(oset, r);                    /* test for hit in set */
591 >
592 >        if (r->robj == OVOID)
593                  return(0);                      /* no scores yet */
594  
595          return(incube(cu, r->rop));             /* hit OK if in current cube */
596   }
597  
598  
599 < static
600 < checkset(os, cs)                /* modify checked set and set to check */
601 < register OBJECT  *os;                   /* os' = os - cs */
602 < register OBJECT  *cs;                   /* cs' = cs + os */
599 > static void
600 > checkset(               /* modify checked set and set to check */
601 >        register OBJECT  *os,                   /* os' = os - cs */
602 >        register OBJECT  *cs                    /* cs' = cs + os */
603 > )
604   {
605          OBJECT  cset[MAXCSET+MAXSET+1];
606          register int  i, j;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines