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 1.13 by greg, Sat Jan 12 14:05:01 1991 UTC vs.
Revision 2.38 by greg, Tue Mar 11 19:29:05 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1990 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 < extern CUBE  thescene;                  /* our scene */
20 < extern int  maxdepth;                   /* maximum recursion depth */
21 < extern double  minweight;               /* minimum ray weight */
16 > #include  "otspecial.h"
17  
18 < long  nrays = 0L;                       /* number of rays traced */
18 > #define  MAXCSET        ((MAXSET+1)*2-1)        /* maximum check set size */
19  
20 < #define  MAXLOOP        128             /* modifier loop detection */
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};
24 + OBJREC  Lamb = {
25 +        OVOID, MAT_PLASTIC, "Lambertian",
26 +        {0, 5, NULL, Lambfa}, NULL,
27 + };                                      /* a Lambertian surface */
28 +
29 + OBJREC  Aftplane;                       /* aft clipping plane object */
30 +
31 + static int  raymove(), checkhit();
32 + static void  checkset();
33 +
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;
52                  r->crtype = r->rtype = rt;
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->rweight = ro->rweight * rw;
72 >                r->revf = ro->revf;
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->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 <        r->rno = nrays;
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);
62        r->rt = 0.0;
63        return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1);
107   }
108  
109  
110 < rayvalue(r)                     /* compute a ray's value */
110 > void
111 > raytrace(r)                     /* trace a ray and compute its value */
112   RAY  *r;
113   {
114 <        extern int  (*trace)();
114 >        if (localhit(r, &thescene))
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 >                rayshade(r, r->ro->omod);       /* distant source */
121  
122 <        if (localhit(r, &thescene) || sourcehit(r))
73 <                raycont(r);
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))
133 >        if ((r->clipset != NULL && inset(r->clipset, r->ro->omod)) ||
134 >                        !rayshade(r, r->ro->omod))
135                  raytrans(r);
85        else
86                rayshade(r, r->ro->omod);
136   }
137  
138  
139 + void
140   raytrans(r)                     /* transmit ray as is */
141   register RAY  *r;
142   {
# Line 101 | 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   {
159 <        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 <        for ( ; mod != OVOID; mod = m->omod) {
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);
170                  /****** unnecessary test since modifier() is always called
171                  if (!ismodifier(m->otype)) {
# Line 118 | Line 173 | int  mod;
173                          error(USER, errmsg);
174                  }
175                  ******/
176 <                (*ofun[m->otype].funp)(m, r);   /* execute function */
177 <                m->lastrno = r->rno;
178 <                if (ismaterial(m->otype)) {     /* materials call raytexture */
179 <                        depth--;
180 <                        return;         /* we're done */
176 >                                        /* hack for irradiance calculation */
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 >                        }
186 >                        if (!islight(m->otype))
187 >                                m = &Lamb;
188                  }
189 +                                        /* materials call raytexture */
190 +                gotmat = (*ofun[m->otype].funp)(m, r);
191          }
192 <        objerror(r->ro, USER, "material not found");
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   raytexture(r, mod)                      /* get material modifiers */
232   RAY  *r;
233   int  mod;
234   {
136        static int  depth = 0;
235          register OBJREC  *m;
236 + #if  MAXLOOP
237 +        static int  depth = 0;
238                                          /* check for infinite loop */
239          if (depth++ >= MAXLOOP)
240                  objerror(r->ro, USER, "modifier loop");
241 + #endif
242                                          /* execute textures and patterns */
243          for ( ; mod != OVOID; mod = m->omod) {
244                  m = objptr(mod);
245 <                if (!istexture(m->otype)) {
245 >                /****** unnecessary test since modifier() is always called
246 >                if (!ismodifier(m->otype)) {
247                          sprintf(errmsg, "illegal modifier \"%s\"", m->oname);
248                          error(USER, errmsg);
249                  }
250 <                (*ofun[m->otype].funp)(m, r);
251 <                m->lastrno = r->rno;
250 >                ******/
251 >                if ((*ofun[m->otype].funp)(m, r)) {
252 >                        sprintf(errmsg, "conflicting material \"%s\"",
253 >                                        m->oname);
254 >                        objerror(r->ro, USER, errmsg);
255 >                }
256          }
257 + #if  MAXLOOP
258          depth--;                        /* end here */
259 + #endif
260   }
261  
262  
263 + int
264   raymixture(r, fore, back, coef)         /* mix modifiers */
265   register RAY  *r;
266   OBJECT  fore, back;
267   double  coef;
268   {
269 <        FVECT  curpert, forepert, backpert;
270 <        COLOR  curpcol, forepcol, backpcol;
269 >        RAY  fr, br;
270 >        int  foremat, backmat;
271          register int  i;
272 <                                        /* clip coefficient */
272 >                                        /* bound coefficient */
273          if (coef > 1.0)
274                  coef = 1.0;
275          else if (coef < 0.0)
276                  coef = 0.0;
277 <                                        /* save current mods */
278 <        VCOPY(curpert, r->pert);
279 <        copycolor(curpcol, r->pcol);
280 <                                        /* compute new mods */
281 <                                                /* foreground */
282 <        r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
283 <        setcolor(r->pcol, 1.0, 1.0, 1.0);
284 <        if (fore != OVOID && coef > FTINY)
285 <                raytexture(r, fore);
286 <        VCOPY(forepert, r->pert);
287 <        copycolor(forepcol, r->pcol);
288 <                                                /* background */
289 <        r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
290 <        setcolor(r->pcol, 1.0, 1.0, 1.0);
291 <        if (back != OVOID && coef < 1.0-FTINY)
292 <                raytexture(r, back);
293 <        VCOPY(backpert, r->pert);
185 <        copycolor(backpcol, r->pcol);
186 <                                        /* sum perturbations */
277 >                                        /* compute foreground and background */
278 >        foremat = backmat = 0;
279 >                                        /* foreground */
280 >        copystruct(&fr, r);
281 >        if (coef > FTINY)
282 >                foremat = rayshade(&fr, fore);
283 >                                        /* background */
284 >        copystruct(&br, r);
285 >        if (coef < 1.0-FTINY)
286 >                backmat = rayshade(&br, back);
287 >                                        /* check for transparency */
288 >        if (backmat ^ foremat)
289 >                if (backmat && coef > FTINY)
290 >                        raytrans(&fr);
291 >                else if (foremat && coef < 1.0-FTINY)
292 >                        raytrans(&br);
293 >                                        /* mix perturbations */
294          for (i = 0; i < 3; i++)
295 <                r->pert[i] = curpert[i] + coef*forepert[i] +
296 <                                (1.0-coef)*backpert[i];
297 <                                        /* multiply colors */
298 <        setcolor(r->pcol, coef*colval(forepcol,RED) +
299 <                                (1.0-coef)*colval(backpcol,RED),
300 <                        coef*colval(forepcol,GRN) +
301 <                                (1.0-coef)*colval(backpcol,GRN),
302 <                        coef*colval(forepcol,BLU) +
303 <                                (1.0-coef)*colval(backpcol,BLU));
304 <        multcolor(r->pcol, curpcol);
295 >                r->pert[i] = coef*fr.pert[i] + (1.0-coef)*br.pert[i];
296 >                                        /* mix pattern colors */
297 >        scalecolor(fr.pcol, coef);
298 >        scalecolor(br.pcol, 1.0-coef);
299 >        copycolor(r->pcol, fr.pcol);
300 >        addcolor(r->pcol, br.pcol);
301 >                                        /* return value tells if material */
302 >        if (!foremat & !backmat)
303 >                return(0);
304 >                                        /* mix returned ray values */
305 >        scalecolor(fr.rcol, coef);
306 >        scalecolor(br.rcol, 1.0-coef);
307 >        copycolor(r->rcol, fr.rcol);
308 >        addcolor(r->rcol, br.rcol);
309 >        r->rt = bright(fr.rcol) > bright(br.rcol) ? fr.rt : br.rt;
310 >        return(1);
311   }
312  
313  
314   double
315 + raydist(r, flags)               /* compute (cumulative) ray distance */
316 + register RAY  *r;
317 + register int  flags;
318 + {
319 +        double  sum = 0.0;
320 +
321 +        while (r != NULL && r->crtype&flags) {
322 +                sum += r->rot;
323 +                r = r->parent;
324 +        }
325 +        return(sum);
326 + }
327 +
328 +
329 + double
330   raynormal(norm, r)              /* compute perturbed normal for ray */
331   FVECT  norm;
332   register RAY  *r;
# Line 233 | Line 361 | register RAY  *r;
361   }
362  
363  
364 + void
365   newrayxf(r)                     /* get new tranformation matrix for ray */
366   RAY  *r;
367   {
# Line 269 | Line 398 | RAY  *r;
398   }
399  
400  
401 + void
402   flipsurface(r)                  /* reverse surface orientation */
403   register RAY  *r;
404   {
# Line 282 | Line 412 | register RAY  *r;
412   }
413  
414  
415 + void
416 + rayhit(oset, r)                 /* standard ray hit test */
417 + OBJECT  *oset;
418 + RAY  *r;
419 + {
420 +        OBJREC  *o;
421 +        int     i;
422 +
423 +        for (i = oset[0]; i > 0; i--) {
424 +                o = objptr(oset[i]);
425 +                if ((*ofun[o->otype].funp)(o, r))
426 +                        r->robj = oset[i];
427 +        }
428 + }
429 +
430 +
431 + int
432   localhit(r, scene)              /* check for hit in the octree */
433   register RAY  *r;
434   register CUBE  *scene;
435   {
436 +        OBJECT  cxset[MAXCSET+1];       /* set of checked objects */
437          FVECT  curpos;                  /* current cube position */
438          int  sflags;                    /* sign flags */
439          double  t, dt;
440          register int  i;
441  
442          nrays++;                        /* increment trace counter */
295
443          sflags = 0;
444          for (i = 0; i < 3; i++) {
445                  curpos[i] = r->rorg[i];
446 <                if (r->rdir[i] > FTINY)
446 >                if (r->rdir[i] > 1e-7)
447                          sflags |= 1 << i;
448 <                else if (r->rdir[i] < -FTINY)
448 >                else if (r->rdir[i] < -1e-7)
449                          sflags |= 0x10 << i;
450          }
451 +        if (sflags == 0)
452 +                error(CONSISTENCY, "zero ray direction in localhit");
453 +                                        /* start off assuming nothing hit */
454 +        if (r->rmax > FTINY) {          /* except aft plane if one */
455 +                r->ro = &Aftplane;
456 +                r->rot = r->rmax;
457 +                for (i = 0; i < 3; i++)
458 +                        r->rop[i] = r->rorg[i] + r->rot*r->rdir[i];
459 +        }
460 +                                        /* find global cube entrance point */
461          t = 0.0;
462          if (!incube(scene, curpos)) {
463                                          /* find distance to entry */
# Line 318 | Line 475 | register CUBE  *scene;
475                                  t = dt; /* farthest face is the one */
476                  }
477                  t += FTINY;             /* fudge to get inside cube */
478 +                if (t >= r->rot)        /* clipped already */
479 +                        return(0);
480                                          /* advance position */
481                  for (i = 0; i < 3; i++)
482                          curpos[i] += r->rdir[i]*t;
# Line 325 | Line 484 | register CUBE  *scene;
484                  if (!incube(scene, curpos))     /* non-intersecting ray */
485                          return(0);
486          }
487 <        return(raymove(curpos, sflags, r, scene) == RAYHIT);
487 >        cxset[0] = 0;
488 >        raymove(curpos, cxset, sflags, r, scene);
489 >        return(r->ro != NULL & r->ro != &Aftplane);
490   }
491  
492  
493   static int
494 < raymove(pos, dirf, r, cu)               /* check for hit as we move */
495 < FVECT  pos;                     /* modified */
494 > raymove(pos, cxs, dirf, r, cu)          /* check for hit as we move */
495 > FVECT  pos;                     /* current position, modified herein */
496 > OBJECT  *cxs;                   /* checked objects, modified by checkhit */
497   int  dirf;                      /* direction indicators to speed tests */
498   register RAY  *r;
499   register CUBE  *cu;
# Line 360 | Line 522 | register CUBE  *cu;
522                  }
523                  for ( ; ; ) {
524                          cukid.cutree = octkid(cu->cutree, br);
525 <                        if ((ax = raymove(pos,dirf,r,&cukid)) == RAYHIT)
525 >                        if ((ax = raymove(pos,cxs,dirf,r,&cukid)) == RAYHIT)
526                                  return(RAYHIT);
527                          sgn = 1 << ax;
528                          if (sgn & dirf)                 /* positive axis? */
# Line 379 | Line 541 | register CUBE  *cu;
541                  }
542                  /*NOTREACHED*/
543          }
544 <        if (isfull(cu->cutree) && checkhit(r, cu))
544 >        if (isfull(cu->cutree)) {
545 >                if (checkhit(r, cu, cxs))
546 >                        return(RAYHIT);
547 >        } else if (r->ro == &Aftplane && incube(cu, r->rop))
548                  return(RAYHIT);
549                                          /* advance to next cube */
550          if (dirf&0x11) {
# Line 411 | Line 576 | register CUBE  *cu;
576   }
577  
578  
579 < static
580 < checkhit(r, cu)                 /* check for hit in full cube */
579 > static int
580 > checkhit(r, cu, cxs)            /* check for hit in full cube */
581   register RAY  *r;
582   CUBE  *cu;
583 + OBJECT  *cxs;
584   {
585          OBJECT  oset[MAXSET+1];
420        register OBJREC  *o;
421        register int  i;
586  
587          objset(oset, cu->cutree);
588 <        for (i = oset[0]; i > 0; i--) {
589 <                o = objptr(oset[i]);
590 <                if (o->lastrno == r->rno)               /* checked already? */
591 <                        continue;
592 <                (*ofun[o->otype].funp)(o, r);
429 <                o->lastrno = r->rno;
430 <        }
431 <        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 void
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 */
603 + {
604 +        OBJECT  cset[MAXCSET+MAXSET+1];
605 +        register int  i, j;
606 +        int  k;
607 +                                        /* copy os in place, cset <- cs */
608 +        cset[0] = 0;
609 +        k = 0;
610 +        for (i = j = 1; i <= os[0]; i++) {
611 +                while (j <= cs[0] && cs[j] < os[i])
612 +                        cset[++cset[0]] = cs[j++];
613 +                if (j > cs[0] || os[i] != cs[j]) {      /* object to check */
614 +                        os[++k] = os[i];
615 +                        cset[++cset[0]] = os[i];
616 +                }
617 +        }
618 +        if (!(os[0] = k))               /* new "to check" set size */
619 +                return;                 /* special case */
620 +        while (j <= cs[0])              /* get the rest of cs */
621 +                cset[++cset[0]] = cs[j++];
622 +        if (cset[0] > MAXCSET)          /* truncate "checked" set if nec. */
623 +                cset[0] = MAXCSET;
624 +        /* setcopy(cs, cset); */        /* copy cset back to cs */
625 +        os = cset;
626 +        for (i = os[0]; i-- >= 0; )
627 +                *cs++ = *os++;
628   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines