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.3 by greg, Tue Jan 26 09:28:00 1993 UTC vs.
Revision 2.23 by greg, Fri Dec 8 18:49:09 1995 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1991 Regents of the University of California */
1 > /* Copyright (c) 1995 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 < long  raynum = 0L;                      /* next unique ray number */
30 < long  nrays = 0L;                       /* number of calls to localhit */
29 > extern COLOR  cextinction;              /* global extinction coefficient */
30 > extern double  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 +
37   static FLOAT  Lambfa[5] = {PI, PI, PI, 0.0, 0.0};
38   OBJREC  Lamb = {
39          OVOID, MAT_PLASTIC, "Lambertian",
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 */
48  
49   #define  RAYHIT         (-1)            /* return value for intercepted ray */
# Line 51 | Line 61 | double  rw;
61                  r->rsrc = -1;
62                  r->clipset = NULL;
63                  r->revf = raytrace;
64 +                copycolor(r->cext, cextinction);
65 +                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 +                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 89 | Line 109 | raytrace(r)                    /* trace a ray and compute its value */
109   RAY  *r;
110   {
111          extern int  (*trace)();
112 +        int  gotmat;
113  
114          if (localhit(r, &thescene))
115 <                raycont(r);
116 <        else if (sourcehit(r))
117 <                rayshade(r, r->ro->omod);
115 >                gotmat = 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 >                gotmat = rayshade(r, r->ro->omod);      /* distant source */
121  
122 +        if (r->ro != NULL && !gotmat)
123 +                objerror(r->ro, USER, "material not found");
124 +
125 +        rayparticipate(r);              /* for participating medium */
126 +
127          if (trace != NULL)
128                  (*trace)(r);            /* trace execution */
129   }
# Line 103 | Line 132 | RAY  *r;
132   raycont(r)                      /* check for clipped object and continue */
133   register RAY  *r;
134   {
135 <        if (r->clipset != NULL && inset(r->clipset, r->ro->omod))
135 >        if ((r->clipset != NULL && inset(r->clipset, r->ro->omod)) ||
136 >                        r->ro->omod == OVOID) {
137                  raytrans(r);
138 <        else
139 <                rayshade(r, r->ro->omod);
138 >                return(1);
139 >        }
140 >        return(rayshade(r, r->ro->omod));
141   }
142  
143  
# Line 129 | Line 160 | register RAY  *r;
160   int  mod;
161   {
162          static int  depth = 0;
163 +        int  gotmat;
164          register OBJREC  *m;
165                                          /* check for infinite loop */
166          if (depth++ >= MAXLOOP)
167                  objerror(r->ro, USER, "possible modifier loop");
168          r->rt = r->rot;                 /* set effective ray length */
169 <        for ( ; mod != OVOID; mod = m->omod) {
169 >        for (gotmat = 0; !gotmat && mod != OVOID; mod = m->omod) {
170                  m = objptr(mod);
171                  /****** unnecessary test since modifier() is always called
172                  if (!ismodifier(m->otype)) {
# Line 147 | Line 179 | int  mod;
179                          if (irr_ignore(m->otype)) {
180                                  depth--;
181                                  raytrans(r);
182 <                                return;
182 >                                return(1);
183                          }
184                          if (!islight(m->otype))
185                                  m = &Lamb;
186                  }
187 <                (*ofun[m->otype].funp)(m, r);   /* execute function */
188 <                if (ismaterial(m->otype)) {     /* materials call raytexture */
157 <                        depth--;
158 <                        return;         /* we're done */
159 <                }
187 >                                        /* materials call raytexture */
188 >                gotmat = (*ofun[m->otype].funp)(m, r);
189          }
190 <        objerror(r->ro, USER, "material not found");
190 >        depth--;
191 >        return(gotmat);
192   }
193  
194  
195 + rayparticipate(r)                       /* compute ray medium participation */
196 + register RAY  *r;
197 + {
198 +        COLOR   ce, ca;
199 +        double  dist;
200 +        double  re, ge, be;
201 +
202 +        if (intens(r->cext) <= 1./FHUGE)
203 +                return;                         /* no medium */
204 +        if ((dist = r->rot) >= FHUGE)
205 +                dist = 2.*thescene.cusize;      /* what to use for infinity? */
206 +        if (r->crtype & SHADOW)
207 +                dist *= 1. - salbedo;           /* no scattering for sources */
208 +        if (dist <= FTINY)
209 +                return;                         /* no effective ray travel */
210 +        re = dist*colval(r->cext,RED);
211 +        ge = dist*colval(r->cext,GRN);
212 +        be = dist*colval(r->cext,BLU);
213 +        setcolor(ce,    re>92. ? 0. : exp(-re),
214 +                        ge>92. ? 0. : exp(-ge),
215 +                        be>92. ? 0. : exp(-be));
216 +        multcolor(r->rcol, ce);                 /* path absorption */
217 +        if (r->albedo <= FTINY || r->crtype & SHADOW)
218 +                return;                         /* no scattering */
219 +        setcolor(ca,    salbedo*colval(ambval,RED)*(1.-colval(ce,RED)),
220 +                        salbedo*colval(ambval,GRN)*(1.-colval(ce,GRN)),
221 +                        salbedo*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   raytexture(r, mod)                      /* get material modifiers */
228   RAY  *r;
229   int  mod;
# Line 174 | Line 236 | int  mod;
236                                          /* execute textures and patterns */
237          for ( ; mod != OVOID; mod = m->omod) {
238                  m = objptr(mod);
239 <                if (!istexture(m->otype)) {
239 >                /****** unnecessary test since modifier() is always called
240 >                if (!ismodifier(m->otype)) {
241                          sprintf(errmsg, "illegal modifier \"%s\"", m->oname);
242                          error(USER, errmsg);
243                  }
244 <                (*ofun[m->otype].funp)(m, r);
244 >                ******/
245 >                if ((*ofun[m->otype].funp)(m, r)) {
246 >                        sprintf(errmsg, "conflicting material \"%s\"",
247 >                                        m->oname);
248 >                        objerror(r->ro, USER, errmsg);
249 >                }
250          }
251          depth--;                        /* end here */
252   }
# Line 189 | Line 257 | register RAY  *r;
257   OBJECT  fore, back;
258   double  coef;
259   {
260 <        FVECT  curpert, forepert, backpert;
261 <        COLOR  curpcol, forepcol, backpcol;
260 >        RAY  fr, br;
261 >        int  foremat, backmat;
262          register int  i;
263                                          /* clip coefficient */
264          if (coef > 1.0)
265                  coef = 1.0;
266          else if (coef < 0.0)
267                  coef = 0.0;
268 <                                        /* save current mods */
269 <        VCOPY(curpert, r->pert);
270 <        copycolor(curpcol, r->pcol);
271 <                                        /* compute new mods */
204 <                                                /* foreground */
205 <        r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
206 <        setcolor(r->pcol, 1.0, 1.0, 1.0);
268 >                                        /* compute foreground and background */
269 >        foremat = backmat = -1;
270 >                                        /* foreground */
271 >        copystruct(&fr, r);
272          if (fore != OVOID && coef > FTINY)
273 <                raytexture(r, fore);
274 <        VCOPY(forepert, r->pert);
275 <        copycolor(forepcol, r->pcol);
211 <                                                /* background */
212 <        r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
213 <        setcolor(r->pcol, 1.0, 1.0, 1.0);
273 >                foremat = rayshade(&fr, fore);
274 >                                        /* background */
275 >        copystruct(&br, r);
276          if (back != OVOID && coef < 1.0-FTINY)
277 <                raytexture(r, back);
278 <        VCOPY(backpert, r->pert);
279 <        copycolor(backpcol, r->pcol);
280 <                                        /* sum perturbations */
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)
285 >                backmat = foremat;
286 >        if ((foremat==0) != (backmat==0))
287 >                objerror(r->ro, USER, "mixing material with non-material");
288 >                                        /* mix perturbations */
289          for (i = 0; i < 3; i++)
290 <                r->pert[i] = curpert[i] + coef*forepert[i] +
291 <                                (1.0-coef)*backpert[i];
292 <                                        /* multiply colors */
293 <        setcolor(r->pcol, coef*colval(forepcol,RED) +
294 <                                (1.0-coef)*colval(backpcol,RED),
295 <                        coef*colval(forepcol,GRN) +
296 <                                (1.0-coef)*colval(backpcol,GRN),
297 <                        coef*colval(forepcol,BLU) +
298 <                                (1.0-coef)*colval(backpcol,BLU));
299 <        multcolor(r->pcol, curpcol);
290 >                r->pert[i] = coef*fr.pert[i] + (1.0-coef)*br.pert[i];
291 >                                        /* mix pattern colors */
292 >        scalecolor(fr.pcol, coef);
293 >        scalecolor(br.pcol, 1.0-coef);
294 >        copycolor(r->pcol, fr.pcol);
295 >        addcolor(r->pcol, br.pcol);
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;
303 >        }
304 >                                        /* return value tells if material */
305 >        return(foremat);
306   }
307  
308  
309   double
310 + raydist(r, flags)               /* compute (cumulative) ray distance */
311 + register RAY  *r;
312 + register int  flags;
313 + {
314 +        double  sum = 0.0;
315 +
316 +        while (r != NULL && r->crtype&flags) {
317 +                sum += r->rot;
318 +                r = r->parent;
319 +        }
320 +        return(sum);
321 + }
322 +
323 +
324 + double
325   raynormal(norm, r)              /* compute perturbed normal for ray */
326   FVECT  norm;
327   register RAY  *r;
# Line 328 | Line 419 | register CUBE  *scene;
419          sflags = 0;
420          for (i = 0; i < 3; i++) {
421                  curpos[i] = r->rorg[i];
422 <                if (r->rdir[i] > FTINY)
422 >                if (r->rdir[i] > 1e-7)
423                          sflags |= 1 << i;
424 <                else if (r->rdir[i] < -FTINY)
424 >                else if (r->rdir[i] < -1e-7)
425                          sflags |= 0x10 << i;
426          }
427          if (sflags == 0)
428                  error(CONSISTENCY, "zero ray direction in localhit");
429 +                                        /* start off assuming nothing hit */
430 +        if (r->rmax > FTINY) {          /* except aft plane if one */
431 +                r->ro = &Aftplane;
432 +                r->rot = r->rmax;
433 +                for (i = 0; i < 3; i++)
434 +                        r->rop[i] = r->rorg[i] + r->rot*r->rdir[i];
435 +        }
436 +                                        /* find global cube entrance point */
437          t = 0.0;
438          if (!incube(scene, curpos)) {
439                                          /* find distance to entry */
# Line 352 | Line 451 | register CUBE  *scene;
451                                  t = dt; /* farthest face is the one */
452                  }
453                  t += FTINY;             /* fudge to get inside cube */
454 +                if (t >= r->rot)        /* clipped already */
455 +                        return(0);
456                                          /* advance position */
457                  for (i = 0; i < 3; i++)
458                          curpos[i] += r->rdir[i]*t;
# Line 360 | Line 461 | register CUBE  *scene;
461                          return(0);
462          }
463          cxset[0] = 0;
464 <        return(raymove(curpos, cxset, sflags, r, scene) == RAYHIT);
464 >        raymove(curpos, cxset, sflags, r, scene);
465 >        return(r->ro != NULL & r->ro != &Aftplane);
466   }
467  
468  
# Line 415 | Line 517 | register CUBE  *cu;
517                  }
518                  /*NOTREACHED*/
519          }
520 <        if (isfull(cu->cutree) && checkhit(r, cu, cxs))
520 >        if (isfull(cu->cutree)) {
521 >                if (checkhit(r, cu, cxs))
522 >                        return(RAYHIT);
523 >        } else if (r->ro == &Aftplane && incube(cu, r->rop))
524                  return(RAYHIT);
525                                          /* advance to next cube */
526          if (dirf&0x11) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines