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.9 by greg, Wed Jan 12 16:52:08 1994 UTC vs.
Revision 2.29 by gregl, Tue Dec 23 18:25:15 1997 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1994 Regents of the University of California */
1 > /* Copyright (c) 1996 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 24 | Line 24 | 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 COLOR  salbedo;                  /* global scattering albedo */
31 + extern double  seccg;                   /* global scattering eccentricity */
32 + extern double  ssampdist;               /* scatter sampling distance */
33 +
34   unsigned long  raynum = 0;              /* next unique ray number */
35   unsigned long  nrays = 0;               /* number of calls to localhit */
36  
# Line 34 | Line 40 | OBJREC  Lamb = {
40          {0, 5, NULL, Lambfa}, NULL,
41   };                                      /* a Lambertian surface */
42  
43 + OBJREC  Aftplane;                       /* aft clipping plane object */
44 +
45   static int  raymove(), checkset(), checkhit();
46  
47   #define  MAXLOOP        128             /* modifier loop detection */
# Line 53 | Line 61 | double  rw;
61                  r->rsrc = -1;
62                  r->clipset = NULL;
63                  r->revf = raytrace;
64 +                copycolor(r->cext, cextinction);
65 +                copycolor(r->albedo, salbedo);
66 +                r->gecc = seccg;
67 +                r->slights = NULL;
68          } else {                                /* spawned ray */
69                  r->rlvl = ro->rlvl;
70                  if (rt & RAYREFL) {
71                          r->rlvl++;
72                          r->rsrc = -1;
73                          r->clipset = ro->clipset;
74 +                        r->rmax = 0.0;
75                  } else {
76                          r->rsrc = ro->rsrc;
77                          r->clipset = ro->newcset;
78 +                        r->rmax = ro->rmax <= FTINY ? 0.0 : ro->rmax - ro->rot;
79                  }
80                  r->revf = ro->revf;
81 +                copycolor(r->cext, ro->cext);
82 +                copycolor(r->albedo, ro->albedo);
83 +                r->gecc = ro->gecc;
84 +                r->slights = ro->slights;
85                  r->rweight = ro->rweight * rw;
86                  r->crtype = ro->crtype | (r->rtype = rt);
87                  VCOPY(r->rorg, ro->rop);
# Line 78 | Line 96 | register RAY  *r;
96   {
97          r->rno = raynum++;
98          r->newcset = r->clipset;
99 +        r->robj = OVOID;
100          r->ro = NULL;
101 <        r->rot = FHUGE;
101 >        r->rt = r->rot = FHUGE;
102          r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
103          setcolor(r->pcol, 1.0, 1.0, 1.0);
104          setcolor(r->rcol, 0.0, 0.0, 0.0);
86        r->rt = 0.0;
105   }
106  
107  
# Line 91 | Line 109 | raytrace(r)                    /* trace a ray and compute its value */
109   RAY  *r;
110   {
111          extern int  (*trace)();
94        int  gotmat;
112  
113          if (localhit(r, &thescene))
114 <                gotmat = raycont(r);
115 <        else if (sourcehit(r))
116 <                gotmat = rayshade(r, r->ro->omod);
114 >                raycont(r);             /* hit local surface, evaluate */
115 >        else if (r->ro == &Aftplane) {
116 >                r->ro = NULL;           /* hit aft clipping plane */
117 >                r->rot = FHUGE;
118 >        } else if (sourcehit(r))
119 >                rayshade(r, r->ro->omod);       /* distant source */
120  
121 <        if (!gotmat)
102 <                objerror(r->ro, USER, "material not found");
121 >        rayparticipate(r);              /* for participating medium */
122  
123          if (trace != NULL)
124                  (*trace)(r);            /* trace execution */
# Line 110 | Line 129 | raycont(r)                     /* check for clipped object and continue
129   register RAY  *r;
130   {
131          if ((r->clipset != NULL && inset(r->clipset, r->ro->omod)) ||
132 <                        r->ro->omod == OVOID) {
132 >                        !rayshade(r, r->ro->omod))
133                  raytrans(r);
115                return(1);
116        }
117        return(rayshade(r, r->ro->omod));
134   }
135  
136  
# Line 156 | Line 172 | int  mod;
172                          if (irr_ignore(m->otype)) {
173                                  depth--;
174                                  raytrans(r);
175 <                                return;
175 >                                return(1);
176                          }
177                          if (!islight(m->otype))
178                                  m = &Lamb;
# Line 169 | Line 185 | int  mod;
185   }
186  
187  
188 + rayparticipate(r)                       /* compute ray medium participation */
189 + register RAY  *r;
190 + {
191 +        COLOR   ce, ca;
192 +        double  re, ge, be;
193 +
194 +        if (intens(r->cext) <= 1./FHUGE)
195 +                return;                         /* no medium */
196 +        re = r->rot*colval(r->cext,RED);
197 +        ge = r->rot*colval(r->cext,GRN);
198 +        be = r->rot*colval(r->cext,BLU);
199 +        if (r->crtype & SHADOW) {               /* no scattering for sources */
200 +                re *= 1. - colval(r->albedo,RED);
201 +                ge *= 1. - colval(r->albedo,GRN);
202 +                be *= 1. - colval(r->albedo,BLU);
203 +        }
204 +        setcolor(ce,    re<=0. ? 1. : re>92. ? 0. : exp(-re),
205 +                        ge<=0. ? 1. : ge>92. ? 0. : exp(-ge),
206 +                        be<=0. ? 1. : be>92. ? 0. : exp(-be));
207 +        multcolor(r->rcol, ce);                 /* path absorption */
208 +        if (r->crtype & SHADOW || intens(r->albedo) <= FTINY)
209 +                return;                         /* no scattering */
210 +        setcolor(ca,
211 +                colval(r->albedo,RED)*colval(ambval,RED)*(1.-colval(ce,RED)),
212 +                colval(r->albedo,GRN)*colval(ambval,GRN)*(1.-colval(ce,GRN)),
213 +                colval(r->albedo,BLU)*colval(ambval,BLU)*(1.-colval(ce,BLU)));
214 +        addcolor(r->rcol, ca);                  /* ambient in scattering */
215 +        srcscatter(r);                          /* source in scattering */
216 + }
217 +
218 +
219   raytexture(r, mod)                      /* get material modifiers */
220   RAY  *r;
221   int  mod;
# Line 187 | Line 234 | int  mod;
234                          error(USER, errmsg);
235                  }
236                  ******/
237 <                if ((*ofun[m->otype].funp)(m, r))
238 <                        objerror(USER, r->ro, "conflicting materials");
237 >                if ((*ofun[m->otype].funp)(m, r)) {
238 >                        sprintf(errmsg, "conflicting material \"%s\"",
239 >                                        m->oname);
240 >                        objerror(r->ro, USER, errmsg);
241 >                }
242          }
243          depth--;                        /* end here */
244   }
# Line 200 | Line 250 | OBJECT  fore, back;
250   double  coef;
251   {
252          RAY  fr, br;
203        COLOR  ctmp;
253          int  foremat, backmat;
254          register int  i;
255 <                                        /* clip coefficient */
255 >                                        /* bound coefficient */
256          if (coef > 1.0)
257                  coef = 1.0;
258          else if (coef < 0.0)
259                  coef = 0.0;
260 +                                        /* compute foreground and background */
261 +        foremat = backmat = 0;
262                                          /* foreground */
263          copystruct(&fr, r);
264 <        fr.pert[0] = fr.pert[1] = fr.pert[2] = 0.0;
214 <        setcolor(fr.pcol, 1.0, 1.0, 1.0);
215 <        setcolor(fr.rcol, 0.0, 0.0, 0.0);
216 <        if (fore != OVOID && coef > FTINY)
264 >        if (coef > FTINY)
265                  foremat = rayshade(&fr, fore);
218        else
219                foremat = 0;
266                                          /* background */
267          copystruct(&br, r);
268 <        br.pert[0] = br.pert[1] = br.pert[2] = 0.0;
223 <        setcolor(br.pcol, 1.0, 1.0, 1.0);
224 <        setcolor(br.rcol, 0.0, 0.0, 0.0);
225 <        if (back != OVOID && coef < 1.0-FTINY)
268 >        if (coef < 1.0-FTINY)
269                  backmat = rayshade(&br, back);
270 <        else
271 <                backmat = foremat;
272 <                                        /* check */
273 <        if (backmat != foremat)
274 <                objerror(USER, r->ro, "mixing material with non-material");
275 <                                        /* sum perturbations */
270 >                                        /* check for transparency */
271 >        if (backmat ^ foremat)
272 >                if (backmat)
273 >                        raytrans(&fr);
274 >                else
275 >                        raytrans(&br);
276 >                                        /* mix perturbations */
277          for (i = 0; i < 3; i++)
278 <                r->pert[i] += coef*fr.pert[i] + (1.0-coef)*br.pert[i];
279 <                                        /* multiply pattern colors */
278 >                r->pert[i] = coef*fr.pert[i] + (1.0-coef)*br.pert[i];
279 >                                        /* mix pattern colors */
280          scalecolor(fr.pcol, coef);
281          scalecolor(br.pcol, 1.0-coef);
282 <        copycolor(ctmp, fr.pcol);
283 <        addcolor(ctmp, br.pcol);
284 <        multcolor(r->pcol, ctmp);
285 <                                        /* sum returned ray values */
282 >        copycolor(r->pcol, fr.pcol);
283 >        addcolor(r->pcol, br.pcol);
284 >                                        /* return value tells if material */
285 >        if (!foremat & !backmat)
286 >                return(0);
287 >                                        /* mix returned ray values */
288          scalecolor(fr.rcol, coef);
289          scalecolor(br.rcol, 1.0-coef);
290 <        addcolor(r->rcol, fr.rcol);
290 >        copycolor(r->rcol, fr.rcol);
291          addcolor(r->rcol, br.rcol);
292 <                                        /* return value tells if material */
293 <        return(foremat);
292 >        r->rt = bright(fr.rcol) > bright(br.rcol) ? fr.rt : br.rt;
293 >        return(1);
294   }
295  
296  
297   double
298 + raydist(r, flags)               /* compute (cumulative) ray distance */
299 + register RAY  *r;
300 + register int  flags;
301 + {
302 +        double  sum = 0.0;
303 +
304 +        while (r != NULL && r->crtype&flags) {
305 +                sum += r->rot;
306 +                r = r->parent;
307 +        }
308 +        return(sum);
309 + }
310 +
311 +
312 + double
313   raynormal(norm, r)              /* compute perturbed normal for ray */
314   FVECT  norm;
315   register RAY  *r;
# Line 353 | Line 414 | register CUBE  *scene;
414          }
415          if (sflags == 0)
416                  error(CONSISTENCY, "zero ray direction in localhit");
417 +                                        /* start off assuming nothing hit */
418 +        if (r->rmax > FTINY) {          /* except aft plane if one */
419 +                r->ro = &Aftplane;
420 +                r->rot = r->rmax;
421 +                for (i = 0; i < 3; i++)
422 +                        r->rop[i] = r->rorg[i] + r->rot*r->rdir[i];
423 +        }
424 +                                        /* find global cube entrance point */
425          t = 0.0;
426          if (!incube(scene, curpos)) {
427                                          /* find distance to entry */
# Line 370 | Line 439 | register CUBE  *scene;
439                                  t = dt; /* farthest face is the one */
440                  }
441                  t += FTINY;             /* fudge to get inside cube */
442 +                if (t >= r->rot)        /* clipped already */
443 +                        return(0);
444                                          /* advance position */
445                  for (i = 0; i < 3; i++)
446                          curpos[i] += r->rdir[i]*t;
# Line 378 | Line 449 | register CUBE  *scene;
449                          return(0);
450          }
451          cxset[0] = 0;
452 <        return(raymove(curpos, cxset, sflags, r, scene) == RAYHIT);
452 >        raymove(curpos, cxset, sflags, r, scene);
453 >        return(r->ro != NULL & r->ro != &Aftplane);
454   }
455  
456  
# Line 433 | Line 505 | register CUBE  *cu;
505                  }
506                  /*NOTREACHED*/
507          }
508 <        if (isfull(cu->cutree) && checkhit(r, cu, cxs))
508 >        if (isfull(cu->cutree)) {
509 >                if (checkhit(r, cu, cxs))
510 >                        return(RAYHIT);
511 >        } else if (r->ro == &Aftplane && incube(cu, r->rop))
512                  return(RAYHIT);
513                                          /* advance to next cube */
514          if (dirf&0x11) {
# Line 479 | Line 554 | OBJECT  *cxs;
554          checkset(oset, cxs);                    /* eliminate double-checking */
555          for (i = oset[0]; i > 0; i--) {
556                  o = objptr(oset[i]);
557 <                (*ofun[o->otype].funp)(o, r);
557 >                if ((*ofun[o->otype].funp)(o, r))
558 >                        r->robj = oset[i];
559          }
560          if (r->ro == NULL)
561                  return(0);                      /* no scores yet */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines