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.5 by greg, Mon Mar 8 12:37:30 1993 UTC vs.
Revision 2.48 by greg, Fri Apr 15 04:44:51 2005 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1991 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  
19 < extern CUBE  thescene;                  /* our scene */
20 < extern int  maxdepth;                   /* maximum recursion depth */
25 < extern double  minweight;               /* minimum ray weight */
26 < extern int  do_irrad;                   /* compute irradiance? */
19 > unsigned long  raynum = 0;              /* next unique ray number */
20 > unsigned long  nrays = 0;               /* number of calls to localhit */
21  
22 < long  raynum = 0L;                      /* next unique ray number */
29 < long  nrays = 0L;                       /* number of calls to localhit */
30 <
31 < 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,
26   };                                      /* a Lambertian surface */
27  
28 < static int  raymove(), checkset(), checkhit();
28 > OBJREC  Aftplane;                       /* aft clipping plane object */
29  
39 #define  MAXLOOP        128             /* modifier loop detection */
40
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;
50                  r->crtype = r->rtype = rt;
51                  r->rsrc = -1;
52                  r->clipset = NULL;
53 <                r->revf = raytrace;
53 >                copycolor(r->cext, cextinction);
54 >                copycolor(r->albedo, salbedo);
55 >                r->gecc = seccg;
56 >                r->slights = NULL;
57 >        } else if (ro->rot >= FHUGE) {          /* illegal continuation */
58 >                memset(r, 0, sizeof(RAY));
59 >                return(-1);
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;
72 >                copycolor(r->cext, ro->cext);
73 >                copycolor(r->albedo, ro->albedo);
74 >                r->gecc = ro->gecc;
75 >                r->slights = ro->slights;
76                  r->crtype = ro->crtype | (r->rtype = rt);
77                  VCOPY(r->rorg, ro->rop);
78 +                r->rweight = ro->rweight * rw;
79 +                                                /* estimate absorption */
80 +                re = colval(ro->cext,RED) < colval(ro->cext,GRN) ?
81 +                                colval(ro->cext,RED) : colval(ro->cext,GRN);
82 +                if (colval(ro->cext,BLU) < re) re = colval(ro->cext,BLU);
83 +                if (re > 0.)
84 +                        r->rweight *= exp(-re*ro->rot);
85          }
86          rayclear(r);
87          return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1);
88   }
89  
90  
91 < rayclear(r)                     /* clear a ray for (re)evaluation */
92 < register RAY  *r;
91 > extern void
92 > rayclear(                       /* clear a ray for (re)evaluation */
93 >        register RAY  *r
94 > )
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);
86        r->rt = 0.0;
107   }
108  
109  
110 < raytrace(r)                     /* trace a ray and compute its value */
111 < RAY  *r;
110 > extern void
111 > rayvalue(                       /* trace a ray and compute its value */
112 >        RAY  *r
113 > )
114   {
93        extern int  (*trace)();
94
115          if (localhit(r, &thescene))
116 <                raycont(r);
117 <        else if (sourcehit(r))
118 <                rayshade(r, r->ro->omod);
116 >                raycont(r);             /* hit local surface, evaluate */
117 >        else if (r->ro == &Aftplane) {
118 >                r->ro = NULL;           /* hit aft clipping plane */
119 >                r->rot = FHUGE;
120 >        } else if (sourcehit(r))
121 >                rayshade(r, r->ro->omod);       /* distant source */
122  
123 +        rayparticipate(r);              /* for participating medium */
124 +
125          if (trace != NULL)
126                  (*trace)(r);            /* trace execution */
127   }
128  
129  
130 < raycont(r)                      /* check for clipped object and continue */
131 < register RAY  *r;
130 > extern void
131 > raycont(                        /* check for clipped object and continue */
132 >        register RAY  *r
133 > )
134   {
135 <        if (r->clipset != NULL && inset(r->clipset, r->ro->omod))
135 >        if ((r->clipset != NULL && inset(r->clipset, r->ro->omod)) ||
136 >                        !rayshade(r, r->ro->omod))
137                  raytrans(r);
110        else
111                rayshade(r, r->ro->omod);
138   }
139  
140  
141 < raytrans(r)                     /* transmit ray as is */
142 < register RAY  *r;
141 > extern void
142 > raytrans(                       /* transmit ray as is */
143 >        register RAY  *r
144 > )
145   {
146          RAY  tr;
147  
# Line 126 | Line 154 | register RAY  *r;
154   }
155  
156  
157 < rayshade(r, mod)                /* shade ray r with material mod */
158 < register RAY  *r;
159 < int  mod;
157 > extern int
158 > rayshade(               /* shade ray r with material mod */
159 >        register RAY  *r,
160 >        int  mod
161 > )
162   {
133        static int  depth = 0;
163          register OBJREC  *m;
164 <                                        /* check for infinite loop */
136 <        if (depth++ >= MAXLOOP)
137 <                objerror(r->ro, USER, "possible modifier loop");
164 >
165          r->rt = r->rot;                 /* set effective ray length */
166          for ( ; mod != OVOID; mod = m->omod) {
167                  m = objptr(mod);
# Line 145 | Line 172 | int  mod;
172                  }
173                  ******/
174                                          /* hack for irradiance calculation */
175 <                if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS))) {
175 >                if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS)) &&
176 >                                m->otype != MAT_CLIP &&
177 >                                (ofun[m->otype].flags & (T_M|T_X))) {
178                          if (irr_ignore(m->otype)) {
150                                depth--;
179                                  raytrans(r);
180 <                                return;
180 >                                return(1);
181                          }
182                          if (!islight(m->otype))
183                                  m = &Lamb;
184                  }
185 <                (*ofun[m->otype].funp)(m, r);   /* execute function */
186 <                if (ismaterial(m->otype)) {     /* materials call raytexture */
159 <                        depth--;
160 <                        return;         /* we're done */
161 <                }
185 >                if ((*ofun[m->otype].funp)(m, r))
186 >                        return(1);      /* materials call raytexture() */
187          }
188 <        objerror(r->ro, USER, "material not found");
188 >        return(0);                      /* no material! */
189   }
190  
191  
192 < raytexture(r, mod)                      /* get material modifiers */
193 < RAY  *r;
194 < int  mod;
192 > extern void
193 > rayparticipate(                 /* compute ray medium participation */
194 >        register RAY  *r
195 > )
196   {
197 <        static int  depth = 0;
197 >        COLOR   ce, ca;
198 >        double  re, ge, be;
199 >
200 >        if (intens(r->cext) <= 1./FHUGE)
201 >                return;                         /* no medium */
202 >        re = r->rot*colval(r->cext,RED);
203 >        ge = r->rot*colval(r->cext,GRN);
204 >        be = r->rot*colval(r->cext,BLU);
205 >        if (r->crtype & SHADOW) {               /* no scattering for sources */
206 >                re *= 1. - colval(r->albedo,RED);
207 >                ge *= 1. - colval(r->albedo,GRN);
208 >                be *= 1. - colval(r->albedo,BLU);
209 >        }
210 >        setcolor(ce,    re<=0. ? 1. : re>92. ? 0. : exp(-re),
211 >                        ge<=0. ? 1. : ge>92. ? 0. : exp(-ge),
212 >                        be<=0. ? 1. : be>92. ? 0. : exp(-be));
213 >        multcolor(r->rcol, ce);                 /* path absorption */
214 >        if (r->crtype & SHADOW || intens(r->albedo) <= FTINY)
215 >                return;                         /* no scattering */
216 >        setcolor(ca,
217 >                colval(r->albedo,RED)*colval(ambval,RED)*(1.-colval(ce,RED)),
218 >                colval(r->albedo,GRN)*colval(ambval,GRN)*(1.-colval(ce,GRN)),
219 >                colval(r->albedo,BLU)*colval(ambval,BLU)*(1.-colval(ce,BLU)));
220 >        addcolor(r->rcol, ca);                  /* ambient in scattering */
221 >        srcscatter(r);                          /* source in scattering */
222 > }
223 >
224 >
225 > extern void
226 > raytexture(                     /* get material modifiers */
227 >        RAY  *r,
228 >        OBJECT  mod
229 > )
230 > {
231          register OBJREC  *m;
173                                        /* check for infinite loop */
174        if (depth++ >= MAXLOOP)
175                objerror(r->ro, USER, "modifier loop");
232                                          /* execute textures and patterns */
233          for ( ; mod != OVOID; mod = m->omod) {
234                  m = objptr(mod);
235 <                if (!istexture(m->otype)) {
235 >                /****** unnecessary test since modifier() is always called
236 >                if (!ismodifier(m->otype)) {
237                          sprintf(errmsg, "illegal modifier \"%s\"", m->oname);
238                          error(USER, errmsg);
239                  }
240 <                (*ofun[m->otype].funp)(m, r);
240 >                ******/
241 >                if ((*ofun[m->otype].funp)(m, r)) {
242 >                        sprintf(errmsg, "conflicting material \"%s\"",
243 >                                        m->oname);
244 >                        objerror(r->ro, USER, errmsg);
245 >                }
246          }
185        depth--;                        /* end here */
247   }
248  
249  
250 < raymixture(r, fore, back, coef)         /* mix modifiers */
251 < register RAY  *r;
252 < OBJECT  fore, back;
253 < double  coef;
250 > extern int
251 > raymixture(             /* mix modifiers */
252 >        register RAY  *r,
253 >        OBJECT  fore,
254 >        OBJECT  back,
255 >        double  coef
256 > )
257   {
258 <        FVECT  curpert, forepert, backpert;
259 <        COLOR  curpcol, forepcol, backpcol;
258 >        RAY  fr, br;
259 >        int  foremat, backmat;
260          register int  i;
261 <                                        /* clip coefficient */
261 >                                        /* bound coefficient */
262          if (coef > 1.0)
263                  coef = 1.0;
264          else if (coef < 0.0)
265                  coef = 0.0;
266 <                                        /* save current mods */
267 <        VCOPY(curpert, r->pert);
268 <        copycolor(curpcol, r->pcol);
269 <                                        /* compute new mods */
270 <                                                /* foreground */
271 <        r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
272 <        setcolor(r->pcol, 1.0, 1.0, 1.0);
273 <        if (fore != OVOID && coef > FTINY)
274 <                raytexture(r, fore);
275 <        VCOPY(forepert, r->pert);
276 <        copycolor(forepcol, r->pcol);
277 <                                                /* background */
278 <        r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
279 <        setcolor(r->pcol, 1.0, 1.0, 1.0);
280 <        if (back != OVOID && coef < 1.0-FTINY)
281 <                raytexture(r, back);
282 <        VCOPY(backpert, r->pert);
283 <        copycolor(backpcol, r->pcol);
220 <                                        /* sum perturbations */
266 >                                        /* compute foreground and background */
267 >        foremat = backmat = 0;
268 >                                        /* foreground */
269 >        fr = *r;
270 >        if (coef > FTINY)
271 >                foremat = rayshade(&fr, fore);
272 >                                        /* background */
273 >        br = *r;
274 >        if (coef < 1.0-FTINY)
275 >                backmat = rayshade(&br, back);
276 >                                        /* check for transparency */
277 >        if (backmat ^ foremat) {
278 >                if (backmat && coef > FTINY)
279 >                        raytrans(&fr);
280 >                else if (foremat && coef < 1.0-FTINY)
281 >                        raytrans(&br);
282 >        }
283 >                                        /* mix perturbations */
284          for (i = 0; i < 3; i++)
285 <                r->pert[i] = curpert[i] + coef*forepert[i] +
286 <                                (1.0-coef)*backpert[i];
287 <                                        /* multiply colors */
288 <        setcolor(r->pcol, coef*colval(forepcol,RED) +
289 <                                (1.0-coef)*colval(backpcol,RED),
290 <                        coef*colval(forepcol,GRN) +
291 <                                (1.0-coef)*colval(backpcol,GRN),
292 <                        coef*colval(forepcol,BLU) +
293 <                                (1.0-coef)*colval(backpcol,BLU));
294 <        multcolor(r->pcol, curpcol);
285 >                r->pert[i] = coef*fr.pert[i] + (1.0-coef)*br.pert[i];
286 >                                        /* mix pattern colors */
287 >        scalecolor(fr.pcol, coef);
288 >        scalecolor(br.pcol, 1.0-coef);
289 >        copycolor(r->pcol, fr.pcol);
290 >        addcolor(r->pcol, br.pcol);
291 >                                        /* return value tells if material */
292 >        if (!foremat & !backmat)
293 >                return(0);
294 >                                        /* mix returned ray values */
295 >        scalecolor(fr.rcol, coef);
296 >        scalecolor(br.rcol, 1.0-coef);
297 >        copycolor(r->rcol, fr.rcol);
298 >        addcolor(r->rcol, br.rcol);
299 >        r->rt = bright(fr.rcol) > bright(br.rcol) ? fr.rt : br.rt;
300 >        return(1);
301   }
302  
303  
304 < double
305 < raynormal(norm, r)              /* compute perturbed normal for ray */
306 < FVECT  norm;
307 < register RAY  *r;
304 > extern double
305 > raydist(                /* compute (cumulative) ray distance */
306 >        register RAY  *r,
307 >        register int  flags
308 > )
309   {
310 +        double  sum = 0.0;
311 +
312 +        while (r != NULL && r->crtype&flags) {
313 +                sum += r->rot;
314 +                r = r->parent;
315 +        }
316 +        return(sum);
317 + }
318 +
319 +
320 + extern double
321 + raynormal(              /* compute perturbed normal for ray */
322 +        FVECT  norm,
323 +        register RAY  *r
324 + )
325 + {
326          double  newdot;
327          register int  i;
328  
# Line 267 | Line 353 | register RAY  *r;
353   }
354  
355  
356 < newrayxf(r)                     /* get new tranformation matrix for ray */
357 < RAY  *r;
356 > extern void
357 > newrayxf(                       /* get new tranformation matrix for ray */
358 >        RAY  *r
359 > )
360   {
361          static struct xfn {
362                  struct xfn  *next;
# Line 286 | Line 374 | RAY  *r;
374                  if (rp->rox == &xp->xf) {               /* xp in use */
375                          xp = xp->next;                  /* move to next */
376                          if (xp == xflast) {             /* need new one */
377 <                                xp = (struct xfn *)bmalloc(sizeof(struct xfn));
377 >                                xp = (struct xfn *)malloc(sizeof(struct xfn));
378                                  if (xp == NULL)
379                                          error(SYSTEM,
380                                                  "out of memory in newrayxf");
# Line 303 | Line 391 | RAY  *r;
391   }
392  
393  
394 < flipsurface(r)                  /* reverse surface orientation */
395 < register RAY  *r;
394 > extern void
395 > flipsurface(                    /* reverse surface orientation */
396 >        register RAY  *r
397 > )
398   {
399          r->rod = -r->rod;
400          r->ron[0] = -r->ron[0];
# Line 316 | Line 406 | register RAY  *r;
406   }
407  
408  
409 < localhit(r, scene)              /* check for hit in the octree */
410 < register RAY  *r;
411 < register CUBE  *scene;
409 > extern void
410 > rayhit(                 /* standard ray hit test */
411 >        OBJECT  *oset,
412 >        RAY  *r
413 > )
414   {
415 +        OBJREC  *o;
416 +        int     i;
417 +
418 +        for (i = oset[0]; i > 0; i--) {
419 +                o = objptr(oset[i]);
420 +                if ((*ofun[o->otype].funp)(o, r))
421 +                        r->robj = oset[i];
422 +        }
423 + }
424 +
425 +
426 + extern int
427 + localhit(               /* check for hit in the octree */
428 +        register RAY  *r,
429 +        register CUBE  *scene
430 + )
431 + {
432          OBJECT  cxset[MAXCSET+1];       /* set of checked objects */
433          FVECT  curpos;                  /* current cube position */
434          int  sflags;                    /* sign flags */
# Line 330 | Line 439 | register CUBE  *scene;
439          sflags = 0;
440          for (i = 0; i < 3; i++) {
441                  curpos[i] = r->rorg[i];
442 <                if (r->rdir[i] > FTINY)
442 >                if (r->rdir[i] > 1e-7)
443                          sflags |= 1 << i;
444 <                else if (r->rdir[i] < -FTINY)
444 >                else if (r->rdir[i] < -1e-7)
445                          sflags |= 0x10 << i;
446          }
447          if (sflags == 0)
448                  error(CONSISTENCY, "zero ray direction in localhit");
449 +                                        /* start off assuming nothing hit */
450 +        if (r->rmax > FTINY) {          /* except aft plane if one */
451 +                r->ro = &Aftplane;
452 +                r->rot = r->rmax;
453 +                for (i = 0; i < 3; i++)
454 +                        r->rop[i] = r->rorg[i] + r->rot*r->rdir[i];
455 +        }
456 +                                        /* find global cube entrance point */
457          t = 0.0;
458          if (!incube(scene, curpos)) {
459                                          /* find distance to entry */
# Line 354 | Line 471 | register CUBE  *scene;
471                                  t = dt; /* farthest face is the one */
472                  }
473                  t += FTINY;             /* fudge to get inside cube */
474 +                if (t >= r->rot)        /* clipped already */
475 +                        return(0);
476                                          /* advance position */
477                  for (i = 0; i < 3; i++)
478                          curpos[i] += r->rdir[i]*t;
# Line 362 | Line 481 | register CUBE  *scene;
481                          return(0);
482          }
483          cxset[0] = 0;
484 <        return(raymove(curpos, cxset, sflags, r, scene) == RAYHIT);
484 >        raymove(curpos, cxset, sflags, r, scene);
485 >        return((r->ro != NULL) & (r->ro != &Aftplane));
486   }
487  
488  
489   static int
490 < raymove(pos, cxs, dirf, r, cu)          /* check for hit as we move */
491 < FVECT  pos;                     /* current position, modified herein */
492 < OBJECT  *cxs;                   /* checked objects, modified by checkhit */
493 < int  dirf;                      /* direction indicators to speed tests */
494 < register RAY  *r;
495 < register CUBE  *cu;
490 > raymove(                /* check for hit as we move */
491 >        FVECT  pos,                     /* current position, modified herein */
492 >        OBJECT  *cxs,                   /* checked objects, modified by checkhit */
493 >        int  dirf,                      /* direction indicators to speed tests */
494 >        register RAY  *r,
495 >        register CUBE  *cu
496 > )
497   {
498          int  ax;
499          double  dt, t;
# Line 417 | Line 538 | register CUBE  *cu;
538                  }
539                  /*NOTREACHED*/
540          }
541 <        if (isfull(cu->cutree) && checkhit(r, cu, cxs))
541 >        if (isfull(cu->cutree)) {
542 >                if (checkhit(r, cu, cxs))
543 >                        return(RAYHIT);
544 >        } else if (r->ro == &Aftplane && incube(cu, r->rop))
545                  return(RAYHIT);
546                                          /* advance to next cube */
547          if (dirf&0x11) {
# Line 449 | Line 573 | register CUBE  *cu;
573   }
574  
575  
576 < static
577 < checkhit(r, cu, cxs)            /* check for hit in full cube */
578 < register RAY  *r;
579 < CUBE  *cu;
580 < OBJECT  *cxs;
576 > static int
577 > checkhit(               /* check for hit in full cube */
578 >        register RAY  *r,
579 >        CUBE  *cu,
580 >        OBJECT  *cxs
581 > )
582   {
583          OBJECT  oset[MAXSET+1];
459        register OBJREC  *o;
460        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 <                if (o->omod == OVOID && issurface(o->otype))
590 <                        continue;               /* ignore void surfaces */
468 <                (*ofun[o->otype].funp)(o, r);
469 <        }
470 <        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
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 */
597 > static void
598 > checkset(               /* modify checked set and set to check */
599 >        register OBJECT  *os,                   /* os' = os - cs */
600 >        register OBJECT  *cs                    /* cs' = cs + os */
601 > )
602   {
603          OBJECT  cset[MAXCSET+MAXSET+1];
604          register int  i, j;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines