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.4 by greg, Tue Feb 23 13:57:11 1993 UTC vs.
Revision 2.22 by greg, Wed Dec 6 12:07:50 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 25 | Line 25 | extern int  maxdepth;                  /* maximum recursion depth */
25   extern double  minweight;               /* minimum ray weight */
26   extern int  do_irrad;                   /* compute irradiance? */
27  
28 < long  raynum = 0L;                      /* next unique ray number */
29 < long  nrays = 0L;                       /* number of calls to localhit */
28 > unsigned long  raynum = 0;              /* next unique ray number */
29 > unsigned long  nrays = 0;               /* number of calls to localhit */
30  
31   static FLOAT  Lambfa[5] = {PI, PI, PI, 0.0, 0.0};
32   OBJREC  Lamb = {
# Line 34 | Line 34 | OBJREC  Lamb = {
34          {0, 5, NULL, Lambfa}, NULL,
35   };                                      /* a Lambertian surface */
36  
37 + OBJREC  Aftplane;                       /* aft clipping plane object */
38 +
39 + static int  raymove(), checkset(), checkhit();
40 +
41   #define  MAXLOOP        128             /* modifier loop detection */
42  
43   #define  RAYHIT         (-1)            /* return value for intercepted ray */
# Line 57 | Line 61 | double  rw;
61                          r->rlvl++;
62                          r->rsrc = -1;
63                          r->clipset = ro->clipset;
64 +                        r->rmax = 0.0;
65                  } else {
66                          r->rsrc = ro->rsrc;
67                          r->clipset = ro->newcset;
68 +                        r->rmax = ro->rmax <= FTINY ? 0.0 : ro->rmax - ro->rot;
69                  }
70                  r->revf = ro->revf;
71                  r->rweight = ro->rweight * rw;
# Line 89 | Line 95 | raytrace(r)                    /* trace a ray and compute its value */
95   RAY  *r;
96   {
97          extern int  (*trace)();
98 +        int  gotmat;
99  
100          if (localhit(r, &thescene))
101 <                raycont(r);
102 <        else if (sourcehit(r))
103 <                rayshade(r, r->ro->omod);
101 >                gotmat = raycont(r);
102 >        else if (r->ro == &Aftplane) {
103 >                r->ro = NULL;
104 >                r->rot = FHUGE;
105 >        } else if (sourcehit(r))
106 >                gotmat = rayshade(r, r->ro->omod);
107  
108 +        if (r->ro != NULL && !gotmat)
109 +                objerror(r->ro, USER, "material not found");
110 +
111          if (trace != NULL)
112                  (*trace)(r);            /* trace execution */
113   }
# Line 103 | Line 116 | RAY  *r;
116   raycont(r)                      /* check for clipped object and continue */
117   register RAY  *r;
118   {
119 <        if (r->clipset != NULL && inset(r->clipset, r->ro->omod))
119 >        if ((r->clipset != NULL && inset(r->clipset, r->ro->omod)) ||
120 >                        r->ro->omod == OVOID) {
121                  raytrans(r);
122 <        else
123 <                rayshade(r, r->ro->omod);
122 >                return(1);
123 >        }
124 >        return(rayshade(r, r->ro->omod));
125   }
126  
127  
# Line 129 | Line 144 | register RAY  *r;
144   int  mod;
145   {
146          static int  depth = 0;
147 +        int  gotmat;
148          register OBJREC  *m;
149                                          /* check for infinite loop */
150          if (depth++ >= MAXLOOP)
151                  objerror(r->ro, USER, "possible modifier loop");
152          r->rt = r->rot;                 /* set effective ray length */
153 <        for ( ; mod != OVOID; mod = m->omod) {
153 >        for (gotmat = 0; !gotmat && mod != OVOID; mod = m->omod) {
154                  m = objptr(mod);
155                  /****** unnecessary test since modifier() is always called
156                  if (!ismodifier(m->otype)) {
# Line 147 | Line 163 | int  mod;
163                          if (irr_ignore(m->otype)) {
164                                  depth--;
165                                  raytrans(r);
166 <                                return;
166 >                                return(1);
167                          }
168                          if (!islight(m->otype))
169                                  m = &Lamb;
170                  }
171 <                (*ofun[m->otype].funp)(m, r);   /* execute function */
172 <                if (ismaterial(m->otype)) {     /* materials call raytexture */
157 <                        depth--;
158 <                        return;         /* we're done */
159 <                }
171 >                                        /* materials call raytexture */
172 >                gotmat = (*ofun[m->otype].funp)(m, r);
173          }
174 <        objerror(r->ro, USER, "material not found");
174 >        depth--;
175 >        return(gotmat);
176   }
177  
178  
# Line 174 | Line 188 | int  mod;
188                                          /* execute textures and patterns */
189          for ( ; mod != OVOID; mod = m->omod) {
190                  m = objptr(mod);
191 <                if (!istexture(m->otype)) {
191 >                /****** unnecessary test since modifier() is always called
192 >                if (!ismodifier(m->otype)) {
193                          sprintf(errmsg, "illegal modifier \"%s\"", m->oname);
194                          error(USER, errmsg);
195                  }
196 <                (*ofun[m->otype].funp)(m, r);
196 >                ******/
197 >                if ((*ofun[m->otype].funp)(m, r)) {
198 >                        sprintf(errmsg, "conflicting material \"%s\"",
199 >                                        m->oname);
200 >                        objerror(r->ro, USER, errmsg);
201 >                }
202          }
203          depth--;                        /* end here */
204   }
# Line 189 | Line 209 | register RAY  *r;
209   OBJECT  fore, back;
210   double  coef;
211   {
212 <        FVECT  curpert, forepert, backpert;
213 <        COLOR  curpcol, forepcol, backpcol;
212 >        RAY  fr, br;
213 >        int  foremat, backmat;
214          register int  i;
215                                          /* clip coefficient */
216          if (coef > 1.0)
217                  coef = 1.0;
218          else if (coef < 0.0)
219                  coef = 0.0;
220 <                                        /* save current mods */
221 <        VCOPY(curpert, r->pert);
222 <        copycolor(curpcol, r->pcol);
223 <                                        /* 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);
220 >                                        /* compute foreground and background */
221 >        foremat = backmat = -1;
222 >                                        /* foreground */
223 >        copystruct(&fr, r);
224          if (fore != OVOID && coef > FTINY)
225 <                raytexture(r, fore);
226 <        VCOPY(forepert, r->pert);
227 <        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);
225 >                foremat = rayshade(&fr, fore);
226 >                                        /* background */
227 >        copystruct(&br, r);
228          if (back != OVOID && coef < 1.0-FTINY)
229 <                raytexture(r, back);
230 <        VCOPY(backpert, r->pert);
231 <        copycolor(backpcol, r->pcol);
232 <                                        /* sum perturbations */
229 >                backmat = rayshade(&br, back);
230 >                                        /* check */
231 >        if (foremat < 0)
232 >                if (backmat < 0)
233 >                        foremat = backmat = 0;
234 >                else
235 >                        foremat = backmat;
236 >        else if (backmat < 0)
237 >                backmat = foremat;
238 >        if ((foremat==0) != (backmat==0))
239 >                objerror(r->ro, USER, "mixing material with non-material");
240 >                                        /* mix perturbations */
241          for (i = 0; i < 3; i++)
242 <                r->pert[i] = curpert[i] + coef*forepert[i] +
243 <                                (1.0-coef)*backpert[i];
244 <                                        /* multiply colors */
245 <        setcolor(r->pcol, coef*colval(forepcol,RED) +
246 <                                (1.0-coef)*colval(backpcol,RED),
247 <                        coef*colval(forepcol,GRN) +
248 <                                (1.0-coef)*colval(backpcol,GRN),
249 <                        coef*colval(forepcol,BLU) +
250 <                                (1.0-coef)*colval(backpcol,BLU));
251 <        multcolor(r->pcol, curpcol);
242 >                r->pert[i] = coef*fr.pert[i] + (1.0-coef)*br.pert[i];
243 >                                        /* mix pattern colors */
244 >        scalecolor(fr.pcol, coef);
245 >        scalecolor(br.pcol, 1.0-coef);
246 >        copycolor(r->pcol, fr.pcol);
247 >        addcolor(r->pcol, br.pcol);
248 >                                        /* mix returned ray values */
249 >        if (foremat) {
250 >                scalecolor(fr.rcol, coef);
251 >                scalecolor(br.rcol, 1.0-coef);
252 >                copycolor(r->rcol, fr.rcol);
253 >                addcolor(r->rcol, br.rcol);
254 >                r->rt = bright(fr.rcol) > bright(br.rcol) ? fr.rt : br.rt;
255 >        }
256 >                                        /* return value tells if material */
257 >        return(foremat);
258   }
259  
260  
261   double
262 + raydist(r, flags)               /* compute (cumulative) ray distance */
263 + register RAY  *r;
264 + register int  flags;
265 + {
266 +        double  sum = 0.0;
267 +
268 +        while (r != NULL && r->crtype&flags) {
269 +                sum += r->rot;
270 +                r = r->parent;
271 +        }
272 +        return(sum);
273 + }
274 +
275 +
276 + double
277   raynormal(norm, r)              /* compute perturbed normal for ray */
278   FVECT  norm;
279   register RAY  *r;
# Line 328 | Line 371 | register CUBE  *scene;
371          sflags = 0;
372          for (i = 0; i < 3; i++) {
373                  curpos[i] = r->rorg[i];
374 <                if (r->rdir[i] > FTINY)
374 >                if (r->rdir[i] > 1e-7)
375                          sflags |= 1 << i;
376 <                else if (r->rdir[i] < -FTINY)
376 >                else if (r->rdir[i] < -1e-7)
377                          sflags |= 0x10 << i;
378          }
379          if (sflags == 0)
380                  error(CONSISTENCY, "zero ray direction in localhit");
381 +                                        /* start off assuming nothing hit */
382 +        if (r->rmax > FTINY) {          /* except aft plane if one */
383 +                r->ro = &Aftplane;
384 +                r->rot = r->rmax;
385 +                for (i = 0; i < 3; i++)
386 +                        r->rop[i] = r->rorg[i] + r->rot*r->rdir[i];
387 +        }
388 +                                        /* find global cube entrance point */
389          t = 0.0;
390          if (!incube(scene, curpos)) {
391                                          /* find distance to entry */
# Line 352 | Line 403 | register CUBE  *scene;
403                                  t = dt; /* farthest face is the one */
404                  }
405                  t += FTINY;             /* fudge to get inside cube */
406 +                if (t >= r->rot)        /* clipped already */
407 +                        return(0);
408                                          /* advance position */
409                  for (i = 0; i < 3; i++)
410                          curpos[i] += r->rdir[i]*t;
# Line 360 | Line 413 | register CUBE  *scene;
413                          return(0);
414          }
415          cxset[0] = 0;
416 <        return(raymove(curpos, cxset, sflags, r, scene) == RAYHIT);
416 >        raymove(curpos, cxset, sflags, r, scene);
417 >        return(r->ro != NULL & r->ro != &Aftplane);
418   }
419  
420  
# Line 415 | Line 469 | register CUBE  *cu;
469                  }
470                  /*NOTREACHED*/
471          }
472 <        if (isfull(cu->cutree) && checkhit(r, cu, cxs))
472 >        if (isfull(cu->cutree)) {
473 >                if (checkhit(r, cu, cxs))
474 >                        return(RAYHIT);
475 >        } else if (r->ro == &Aftplane && incube(cu, r->rop))
476                  return(RAYHIT);
477                                          /* advance to next cube */
478          if (dirf&0x11) {
# Line 461 | Line 518 | OBJECT  *cxs;
518          checkset(oset, cxs);                    /* eliminate double-checking */
519          for (i = oset[0]; i > 0; i--) {
520                  o = objptr(oset[i]);
464                if (o->omod == OVOID && issurface(o->otype))
465                        continue;               /* ignore void surfaces */
521                  (*ofun[o->otype].funp)(o, r);
522          }
523          if (r->ro == NULL)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines