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.95 by greg, Thu Feb 6 02:17:33 2025 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1994 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 + #include  "random.h"
17 + #include  "pmap.h"
18  
19   #define  MAXCSET        ((MAXSET+1)*2-1)        /* maximum check set size */
20  
21 < extern CUBE  thescene;                  /* our scene */
22 < extern int  maxdepth;                   /* maximum recursion depth */
25 < extern double  minweight;               /* minimum ray weight */
26 < extern int  do_irrad;                   /* compute irradiance? */
21 > RNUMBER  raynum = 0;            /* next unique ray number */
22 > RNUMBER  nrays = 0;             /* number of calls to localhit */
23  
24 < 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};
24 > static RREAL  Lambfa[5] = {PI, PI, PI, 0.0, 0.0};
25   OBJREC  Lamb = {
26          OVOID, MAT_PLASTIC, "Lambertian",
27 <        {0, 5, NULL, Lambfa}, NULL,
27 >        {NULL, Lambfa, 0, 5}, NULL
28   };                                      /* a Lambertian surface */
29  
30 < static int  raymove(), checkset(), checkhit();
30 > OBJREC  Aftplane;                       /* aft clipping plane object */
31  
39 #define  MAXLOOP        128             /* modifier loop detection */
40
32   #define  RAYHIT         (-1)            /* return value for intercepted ray */
33  
34 + static int raymove(FVECT  pos, OBJECT  *cxs, int  dirf, RAY  *r, CUBE  *cu);
35 + static int checkhit(RAY  *r, CUBE  *cu, OBJECT  *cxs);
36 + static void checkset(OBJECT  *os, OBJECT  *cs);
37  
38 < rayorigin(r, ro, rt, rw)                /* start new ray from old one */
39 < register RAY  *r, *ro;
40 < int  rt;
41 < double  rw;
38 >
39 > int
40 > rayorigin(              /* start new ray from old one */
41 >        RAY  *r,
42 >        int  rt,
43 >        const RAY  *ro,
44 >        const SCOLOR rc
45 > )
46   {
47 +        double  rw, re;
48 +                                                /* assign coefficient/weight */
49 +        if (rc == NULL) {
50 +                rw = 1.0;
51 +                setscolor(r->rcoef, 1., 1., 1.);
52 +        } else {
53 +                rw = sintens((COLORV *)rc);
54 +                if (rw > 1.0)
55 +                        rw = 1.0;               /* avoid calculation growth */
56 +                if (rc != r->rcoef)
57 +                        copyscolor(r->rcoef, rc);
58 +        }
59          if ((r->parent = ro) == NULL) {         /* primary ray */
60                  r->rlvl = 0;
61                  r->rweight = rw;
# Line 53 | Line 63 | double  rw;
63                  r->rsrc = -1;
64                  r->clipset = NULL;
65                  r->revf = raytrace;
66 +                copycolor(r->cext, cextinction);
67 +                copycolor(r->albedo, salbedo);
68 +                r->gecc = seccg;
69 +                r->slights = NULL;
70          } else {                                /* spawned ray */
71 +                if (ro->rot >= FHUGE*.99) {
72 +                        memset(r, 0, sizeof(RAY));
73 +                        return(-1);             /* illegal continuation */
74 +                }
75                  r->rlvl = ro->rlvl;
76 +                r->rsrc = ro->rsrc;
77                  if (rt & RAYREFL) {
78                          r->rlvl++;
79 <                        r->rsrc = -1;
79 >                        if (r->rsrc >= 0)       /* malfunctioning material? */
80 >                                r->rsrc = -1;
81                          r->clipset = ro->clipset;
82 +                        r->rmax = 0.0;
83                  } else {
63                        r->rsrc = ro->rsrc;
84                          r->clipset = ro->newcset;
85 +                        r->rmax = (ro->rmax > FTINY)*(ro->rmax - ro->rot);
86                  }
87                  r->revf = ro->revf;
88 <                r->rweight = ro->rweight * rw;
88 >                copycolor(r->cext, ro->cext);
89 >                copycolor(r->albedo, ro->albedo);
90 >                r->gecc = ro->gecc;
91 >                r->slights = ro->slights;
92                  r->crtype = ro->crtype | (r->rtype = rt);
93                  VCOPY(r->rorg, ro->rop);
94 +                r->rweight = ro->rweight * rw;
95 +                                                /* estimate extinction */
96 +                re = colval(ro->cext,RED) < colval(ro->cext,GRN) ?
97 +                                colval(ro->cext,RED) : colval(ro->cext,GRN);
98 +                if (colval(ro->cext,BLU) < re) re = colval(ro->cext,BLU);
99 +                re *= ro->rot;
100 +                if (re > 0.1) {
101 +                        if (re > 92.) {
102 +                                r->rweight = 0.0;
103 +                        } else {
104 +                                r->rweight *= exp(-re);
105 +                        }
106 +                }
107          }
108          rayclear(r);
109 <        return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1);
109 >        if (r->rweight <= 0.0)                  /* check for expiration */
110 >                return(-1);
111 >        if (r->crtype & SHADOW)                 /* shadow commitment */
112 >                return(0);
113 >                                                /* ambient in photon map? */
114 >        if (ro != NULL && ro->crtype & AMBIENT) {
115 >                if (causticPhotonMapping)
116 >                        return(-1);
117 >                if (photonMapping && rt != TRANS)
118 >                        return(-1);
119 >        }
120 >        if ((maxdepth <= 0) & (rc != NULL)) {   /* Russian roulette */
121 >                if (minweight <= 0.0)
122 >                        error(USER, "zero ray weight in Russian roulette");
123 >                if ((maxdepth < 0) & (r->rlvl > -maxdepth))
124 >                        return(-1);             /* upper reflection limit */
125 >                if (r->rweight >= minweight)
126 >                        return(0);
127 >                if (frandom() > r->rweight/minweight)
128 >                        return(-1);
129 >                rw = minweight/r->rweight;      /* promote survivor */
130 >                scalescolor(r->rcoef, rw);
131 >                r->rweight = minweight;
132 >                return(0);
133 >        }
134 >        return((r->rweight >= minweight) & (r->rlvl <= abs(maxdepth)) ? 0 : -1);
135   }
136  
137  
138 < rayclear(r)                     /* clear a ray for (re)evaluation */
139 < register RAY  *r;
138 > void
139 > rayclear(                       /* clear a ray for (re)evaluation */
140 >        RAY  *r
141 > )
142   {
143          r->rno = raynum++;
144          r->newcset = r->clipset;
145 +        r->hitf = rayhit;
146 +        r->robj = OVOID;
147          r->ro = NULL;
148 <        r->rot = FHUGE;
148 >        r->rox = NULL;
149 >        r->rxt = r->rmt = r->rot = FHUGE;
150 >        VCOPY(r->rop, r->rorg);
151 >        r->ron[0] = -r->rdir[0]; r->ron[1] = -r->rdir[1]; r->ron[2] = -r->rdir[2];
152 >        r->rod = 1.0;
153          r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
154 <        setcolor(r->pcol, 1.0, 1.0, 1.0);
155 <        setcolor(r->rcol, 0.0, 0.0, 0.0);
156 <        r->rt = 0.0;
154 >        r->rflips = 0;
155 >        r->uv[0] = r->uv[1] = 0.0;
156 >        setscolor(r->pcol, 1.0, 1.0, 1.0);
157 >        scolorblack(r->mcol);
158 >        scolorblack(r->rcol);
159   }
160  
161  
162 < raytrace(r)                     /* trace a ray and compute its value */
163 < RAY  *r;
162 > void
163 > raytrace(                       /* trace a ray and compute its value */
164 >        RAY  *r
165 > )
166   {
93        extern int  (*trace)();
94        int  gotmat;
95
167          if (localhit(r, &thescene))
168 <                gotmat = raycont(r);
169 <        else if (sourcehit(r))
170 <                gotmat = rayshade(r, r->ro->omod);
168 >                raycont(r);             /* hit local surface, evaluate */
169 >        else if (r->ro == &Aftplane) {
170 >                r->ro = NULL;           /* hit aft clipping plane */
171 >                r->rot = FHUGE;
172 >        } else if (sourcehit(r))
173 >                rayshade(r, r->ro->omod);       /* distant source */
174  
101        if (!gotmat)
102                objerror(r->ro, USER, "material not found");
103
175          if (trace != NULL)
176                  (*trace)(r);            /* trace execution */
177 +
178 +        rayparticipate(r);              /* for participating medium */
179   }
180  
181  
182 < raycont(r)                      /* check for clipped object and continue */
183 < register RAY  *r;
182 > void
183 > raycont(                        /* check for clipped object and continue */
184 >        RAY  *r
185 > )
186   {
187          if ((r->clipset != NULL && inset(r->clipset, r->ro->omod)) ||
188 <                        r->ro->omod == OVOID) {
188 >                        !rayshade(r, r->ro->omod))
189                  raytrans(r);
115                return(1);
116        }
117        return(rayshade(r, r->ro->omod));
190   }
191  
192  
193 < raytrans(r)                     /* transmit ray as is */
194 < register RAY  *r;
193 > void
194 > raytrans(                       /* transmit ray as is */
195 >        RAY  *r
196 > )
197   {
198          RAY  tr;
199  
200 <        if (rayorigin(&tr, r, TRANS, 1.0) == 0) {
201 <                VCOPY(tr.rdir, r->rdir);
202 <                rayvalue(&tr);
203 <                copycolor(r->rcol, tr.rcol);
204 <                r->rt = r->rot + tr.rt;
200 >        rayorigin(&tr, TRANS, r, NULL);         /* always continue */
201 >        VCOPY(tr.rdir, r->rdir);
202 >        rayvalue(&tr);
203 >        copyscolor(r->mcol, tr.mcol);
204 >        copyscolor(r->rcol, tr.rcol);
205 >        r->rmt = r->rot + tr.rmt;
206 >        r->rxt = r->rot + tr.rxt;
207 > }
208 >
209 >
210 > int
211 > raytirrad(                      /* irradiance hack */
212 >        OBJREC  *m,
213 >        RAY     *r
214 > )
215 > {
216 >        if (m->otype != MAT_CLIP && ismaterial(m->otype)) {
217 >                if (istransp(m) || isBSDFproxy(m)) {
218 >                        raytrans(r);
219 >                        return(1);
220 >                }
221 >                if (!islight(m->otype)) {
222 >                        setscolor(r->pcol, 1.0, 1.0, 1.0);
223 >                        return((*ofun[Lamb.otype].funp)(&Lamb, r));
224 >                }
225          }
226 +        return(0);              /* not a qualifying surface */
227   }
228  
229  
230 < rayshade(r, mod)                /* shade ray r with material mod */
231 < register RAY  *r;
232 < int  mod;
230 > int
231 > rayshade(               /* shade ray r with material mod */
232 >        RAY  *r,
233 >        int  mod
234 > )
235   {
236 <        static int  depth = 0;
237 <        int  gotmat;
238 <        register OBJREC  *m;
239 <                                        /* check for infinite loop */
240 <        if (depth++ >= MAXLOOP)
144 <                objerror(r->ro, USER, "possible modifier loop");
145 <        r->rt = r->rot;                 /* set effective ray length */
146 <        for (gotmat = 0; !gotmat && mod != OVOID; mod = m->omod) {
236 >        int     tst_irrad = do_irrad && !(r->crtype & ~(PRIMARY|TRANS));
237 >        OBJREC  *m;
238 >
239 >        r->rxt = r->rot;                /* preset effective ray length */
240 >        for ( ; mod != OVOID; mod = m->omod) {
241                  m = objptr(mod);
242                  /****** unnecessary test since modifier() is always called
243                  if (!ismodifier(m->otype)) {
# Line 152 | Line 246 | int  mod;
246                  }
247                  ******/
248                                          /* hack for irradiance calculation */
249 <                if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS))) {
250 <                        if (irr_ignore(m->otype)) {
251 <                                depth--;
252 <                                raytrans(r);
253 <                                return;
160 <                        }
161 <                        if (!islight(m->otype))
162 <                                m = &Lamb;
163 <                }
164 <                                        /* materials call raytexture */
165 <                gotmat = (*ofun[m->otype].funp)(m, r);
249 >                if (tst_irrad && raytirrad(m, r))
250 >                        return(1);
251 >
252 >                if ((*ofun[m->otype].funp)(m, r))
253 >                        return(1);      /* materials call raytexture() */
254          }
255 <        depth--;
168 <        return(gotmat);
255 >        return(0);                      /* no material! */
256   }
257  
258  
259 < raytexture(r, mod)                      /* get material modifiers */
260 < RAY  *r;
261 < int  mod;
259 > void
260 > rayparticipate(                 /* compute ray medium participation */
261 >        RAY  *r
262 > )
263   {
264 <        static int  depth = 0;
265 <        register OBJREC  *m;
266 <                                        /* check for infinite loop */
267 <        if (depth++ >= MAXLOOP)
268 <                objerror(r->ro, USER, "modifier loop");
264 >        SCOLOR  ce, ca;
265 >        double  re, ge, be;
266 >
267 >        if (intens(r->cext) <= 1./FHUGE)
268 >                return;                         /* no medium */
269 >        re = r->rot*colval(r->cext,RED);
270 >        ge = r->rot*colval(r->cext,GRN);
271 >        be = r->rot*colval(r->cext,BLU);
272 >        if (r->crtype & SHADOW) {               /* no scattering for sources */
273 >                re *= 1. - colval(r->albedo,RED);
274 >                ge *= 1. - colval(r->albedo,GRN);
275 >                be *= 1. - colval(r->albedo,BLU);
276 >        }
277 >        setscolor(ce,   re<=FTINY ? 1. : re>92. ? 0. : exp(-re),
278 >                        ge<=FTINY ? 1. : ge>92. ? 0. : exp(-ge),
279 >                        be<=FTINY ? 1. : be>92. ? 0. : exp(-be));
280 >        smultscolor(r->rcol, ce);               /* path extinction */
281 >        if (r->crtype & SHADOW || intens(r->albedo) <= FTINY)
282 >                return;                         /* no scattering */
283 >        
284 >        /* PMAP: indirect inscattering accounted for by volume photons? */
285 >        if (!volumePhotonMapping) {
286 >                setscolor(ca,
287 >                        colval(r->albedo,RED)*colval(ambval,RED)*(1.-scolval(ce,RED)),
288 >                        colval(r->albedo,GRN)*colval(ambval,GRN)*(1.-scolval(ce,GRN)),
289 >                        colval(r->albedo,BLU)*colval(ambval,BLU)*(1.-scolval(ce,BLU)));
290 >                saddscolor(r->rcol, ca);                /* ambient in scattering */
291 >        }
292 >        
293 >        srcscatter(r);                          /* source in scattering */
294 > }
295 >
296 >
297 > void
298 > raytexture(                     /* get material modifiers */
299 >        RAY  *r,
300 >        OBJECT  mod
301 > )
302 > {
303 >        OBJREC  *m;
304                                          /* execute textures and patterns */
305          for ( ; mod != OVOID; mod = m->omod) {
306                  m = objptr(mod);
# Line 187 | Line 310 | int  mod;
310                          error(USER, errmsg);
311                  }
312                  ******/
313 <                if ((*ofun[m->otype].funp)(m, r))
314 <                        objerror(USER, r->ro, "conflicting materials");
313 >                if ((*ofun[m->otype].funp)(m, r)) {
314 >                        sprintf(errmsg, "conflicting material \"%s\"",
315 >                                        m->oname);
316 >                        objerror(r->ro, USER, errmsg);
317 >                }
318          }
193        depth--;                        /* end here */
319   }
320  
321  
322 < raymixture(r, fore, back, coef)         /* mix modifiers */
323 < register RAY  *r;
324 < OBJECT  fore, back;
325 < double  coef;
322 > int
323 > raymixture(             /* mix modifiers */
324 >        RAY  *r,
325 >        OBJECT  fore,
326 >        OBJECT  back,
327 >        double  coef
328 > )
329   {
330          RAY  fr, br;
331 <        COLOR  ctmp;
331 >        double  mfore, mback;
332          int  foremat, backmat;
333 <        register int  i;
334 <                                        /* clip coefficient */
333 >        int  i;
334 >                                        /* bound coefficient */
335          if (coef > 1.0)
336                  coef = 1.0;
337          else if (coef < 0.0)
338                  coef = 0.0;
339 +                                        /* compute foreground and background */
340 +        foremat = backmat = 0;
341                                          /* foreground */
342 <        copystruct(&fr, r);
343 <        fr.pert[0] = fr.pert[1] = fr.pert[2] = 0.0;
344 <        setcolor(fr.pcol, 1.0, 1.0, 1.0);
345 <        setcolor(fr.rcol, 0.0, 0.0, 0.0);
216 <        if (fore != OVOID && coef > FTINY)
342 >        fr = *r;
343 >        if (coef > FTINY) {
344 >                fr.rweight *= coef;
345 >                scalescolor(fr.rcoef, coef);
346                  foremat = rayshade(&fr, fore);
347 <        else
219 <                foremat = 0;
347 >        }
348                                          /* background */
349 <        copystruct(&br, r);
350 <        br.pert[0] = br.pert[1] = br.pert[2] = 0.0;
351 <        setcolor(br.pcol, 1.0, 1.0, 1.0);
352 <        setcolor(br.rcol, 0.0, 0.0, 0.0);
225 <        if (back != OVOID && coef < 1.0-FTINY)
349 >        br = *r;
350 >        if (coef < 1.0-FTINY) {
351 >                br.rweight *= 1.0-coef;
352 >                scalescolor(br.rcoef, 1.0-coef);
353                  backmat = rayshade(&br, back);
354 <        else
355 <                backmat = foremat;
356 <                                        /* check */
357 <        if (backmat != foremat)
358 <                objerror(USER, r->ro, "mixing material with non-material");
359 <                                        /* sum perturbations */
354 >        }
355 >                                        /* check for transparency */
356 >        if (backmat ^ foremat) {
357 >                if (backmat && coef > FTINY)
358 >                        raytrans(&fr);
359 >                else if (foremat && coef < 1.0-FTINY)
360 >                        raytrans(&br);
361 >        }
362 >                                        /* mix perturbations */
363          for (i = 0; i < 3; i++)
364 <                r->pert[i] += coef*fr.pert[i] + (1.0-coef)*br.pert[i];
365 <                                        /* multiply pattern colors */
366 <        scalecolor(fr.pcol, coef);
367 <        scalecolor(br.pcol, 1.0-coef);
368 <        copycolor(ctmp, fr.pcol);
369 <        addcolor(ctmp, br.pcol);
240 <        multcolor(r->pcol, ctmp);
241 <                                        /* sum returned ray values */
242 <        scalecolor(fr.rcol, coef);
243 <        scalecolor(br.rcol, 1.0-coef);
244 <        addcolor(r->rcol, fr.rcol);
245 <        addcolor(r->rcol, br.rcol);
364 >                r->pert[i] = coef*fr.pert[i] + (1.0-coef)*br.pert[i];
365 >                                        /* mix pattern colors */
366 >        scalescolor(fr.pcol, coef);
367 >        scalescolor(br.pcol, 1.0-coef);
368 >        copyscolor(r->pcol, fr.pcol);
369 >        saddscolor(r->pcol, br.pcol);
370                                          /* return value tells if material */
371 <        return(foremat);
371 >        if (!foremat & !backmat)
372 >                return(0);
373 >                                        /* mix returned ray values */
374 >        scalescolor(fr.rcol, coef);
375 >        scalescolor(br.rcol, 1.0-coef);
376 >        copyscolor(r->rcol, fr.rcol);
377 >        saddscolor(r->rcol, br.rcol);
378 >        scalescolor(fr.mcol, coef);
379 >        scalescolor(br.mcol, 1.0-coef);
380 >        copyscolor(r->mcol, fr.mcol);
381 >        saddscolor(r->mcol, br.mcol);
382 >        mfore = pbright(fr.mcol); mback = pbright(br.mcol);
383 >        r->rmt = mfore > mback ? fr.rmt : br.rmt;
384 >        r->rxt = pbright(fr.rcol)-mfore > pbright(br.rcol)-mback ?
385 >                        fr.rxt : br.rxt;
386 >        return(1);
387   }
388  
389  
390   double
391 < raynormal(norm, r)              /* compute perturbed normal for ray */
392 < FVECT  norm;
393 < register RAY  *r;
391 > raydist(                /* compute (cumulative) ray distance */
392 >        const RAY  *r,
393 >        int  flags
394 > )
395   {
396 +        double  sum = 0.0;
397 +
398 +        while (r != NULL && r->crtype&flags) {
399 +                sum += r->rot;
400 +                r = r->parent;
401 +        }
402 +        return(sum);
403 + }
404 +
405 +
406 + void
407 + raycontrib(             /* compute (cumulative) ray contribution */
408 +        SCOLOR  rc,
409 +        const RAY  *r,
410 +        int  flags
411 + )
412 + {
413 +        static int      warnedPM = 0;
414 +
415 +        setscolor(rc, 1., 1., 1.);
416 +
417 +        while (r != NULL && r->crtype&flags) {
418 +                smultscolor(rc, r->rcoef);
419 +                                        /* check for participating medium */
420 +                if (bright(r->cext) > FTINY) {
421 +                        double  re = r->rot*colval(r->cext,RED),
422 +                                ge = r->rot*colval(r->cext,GRN),
423 +                                be = r->rot*colval(r->cext,BLU);
424 +                        SCOLOR  ce;
425 +                        setscolor(ce,   re<=FTINY ? 1. : re>92. ? 0. : exp(-re),
426 +                                        ge<=FTINY ? 1. : ge>92. ? 0. : exp(-ge),
427 +                                        be<=FTINY ? 1. : be>92. ? 0. : exp(-be));
428 +                        smultscolor(rc, ce);
429 +                }
430 +                if (!warnedPM && bright(r->albedo) > FTINY) {
431 +                        error(WARNING,
432 +        "ray contribution calculation does not support participating media");
433 +                        warnedPM++;
434 +                }
435 +                r = r->parent;
436 +        }
437 + }
438 +
439 +
440 + double
441 + raynormal(              /* compute perturbed normal for ray */
442 +        FVECT  norm,
443 +        RAY  *r
444 + )
445 + {
446          double  newdot;
447 <        register int  i;
447 >        int  i;
448  
449          /*      The perturbation is added to the surface normal to obtain
450           *  the new normal.  If the new normal would affect the surface
# Line 274 | Line 464 | register RAY  *r;
464                  return(r->rod);
465          }
466          newdot = -DOT(norm, r->rdir);
467 <        if ((newdot > 0.0) != (r->rod > 0.0)) {         /* fix orientation */
467 >        if ((newdot > 0.0) ^ (r->rod > 0.0)) {          /* fix orientation */
468                  for (i = 0; i < 3; i++)
469                          norm[i] += 2.0*newdot*r->rdir[i];
470                  newdot = -newdot;
# Line 283 | Line 473 | register RAY  *r;
473   }
474  
475  
476 < newrayxf(r)                     /* get new tranformation matrix for ray */
477 < RAY  *r;
476 > void
477 > newrayxf(                       /* get new tranformation matrix for ray */
478 >        RAY  *r
479 > )
480   {
481          static struct xfn {
482                  struct xfn  *next;
483                  FULLXF  xf;
484          }  xfseed = { &xfseed }, *xflast = &xfseed;
485 <        register struct xfn  *xp;
486 <        register RAY  *rp;
485 >        struct xfn  *xp;
486 >        const RAY  *rp;
487  
488          /*
489           * Search for transform in circular list that
# Line 319 | Line 511 | RAY  *r;
511   }
512  
513  
514 < flipsurface(r)                  /* reverse surface orientation */
515 < register RAY  *r;
514 > void
515 > flipsurface(                    /* reverse surface orientation */
516 >        RAY  *r
517 > )
518   {
519          r->rod = -r->rod;
520          r->ron[0] = -r->ron[0];
# Line 329 | Line 523 | register RAY  *r;
523          r->pert[0] = -r->pert[0];
524          r->pert[1] = -r->pert[1];
525          r->pert[2] = -r->pert[2];
526 +        r->rflips++;
527   }
528  
529  
530 < localhit(r, scene)              /* check for hit in the octree */
531 < register RAY  *r;
532 < register CUBE  *scene;
530 > int
531 > rayreject(              /* check if candidate hit is worse than current */
532 >        OBJREC *o,
533 >        RAY *r,
534 >        double t,
535 >        double rod
536 > )
537   {
538 +        OBJREC  *mnew, *mray;
539 +
540 +        if ((t <= FTINY) | (t > r->rot + FTINY))
541 +                return(1);
542 +        if (t < r->rot - FTINY)         /* is new hit significantly closer? */
543 +                return(0);
544 +                                        /* coincident point, so decide... */
545 +        if (o == r->ro)
546 +                return(1);              /* shouldn't happen */
547 +        if (r->ro == NULL)
548 +                return(0);              /* ditto */
549 +        mnew = findmaterial(o);
550 +        mray = findmaterial(r->ro);     /* check material transparencies */
551 +        if (mnew == NULL) {
552 +                if (mray != NULL)
553 +                        return(1);      /* old has material, new does not */
554 +        } else if (mray == NULL) {
555 +                return(0);              /* new has material, old does not */
556 +        } else if (istransp(mnew)) {
557 +                if (!istransp(mray))
558 +                        return(1);      /* new is transparent, old is not */
559 +        } else if (istransp(mray)) {
560 +                return(0);              /* old is transparent, new is not */
561 +        }
562 +        if (rod <= 0) {                 /* check which side we hit */
563 +                if (r->rod > 0)
564 +                        return(1);      /* old hit front, new did not */
565 +        } else if (r->rod <= 0) {
566 +                return(0);              /* new hit front, old did not */
567 +        }
568 +                        /* earlier modifier definition wins tie */
569 +        return (r->ro->omod >= o->omod);
570 + }
571 +
572 + void
573 + rayhit(                 /* standard ray hit test */
574 +        OBJECT  *oset,
575 +        RAY  *r
576 + )
577 + {
578 +        OBJREC  *o;
579 +        int     i;
580 +
581 +        for (i = oset[0]; i > 0; i--) {
582 +                o = objptr(oset[i]);
583 +                if ((*ofun[o->otype].funp)(o, r))
584 +                        r->robj = oset[i];
585 +        }
586 + }
587 +
588 +
589 + int
590 + localhit(               /* check for hit in the octree */
591 +        RAY  *r,
592 +        CUBE  *scene
593 + )
594 + {
595          OBJECT  cxset[MAXCSET+1];       /* set of checked objects */
596          FVECT  curpos;                  /* current cube position */
597          int  sflags;                    /* sign flags */
598          double  t, dt;
599 <        register int  i;
599 >        int  i;
600  
601          nrays++;                        /* increment trace counter */
602          sflags = 0;
# Line 351 | Line 607 | register CUBE  *scene;
607                  else if (r->rdir[i] < -1e-7)
608                          sflags |= 0x10 << i;
609          }
610 <        if (sflags == 0)
611 <                error(CONSISTENCY, "zero ray direction in localhit");
610 >        if (!sflags) {
611 >                error(WARNING, "zero ray direction in localhit");
612 >                return(0);
613 >        }
614 >                                        /* start off assuming nothing hit */
615 >        if (r->rmax > FTINY) {          /* except aft plane if one */
616 >                r->ro = &Aftplane;
617 >                r->rot = r->rmax;
618 >                VSUM(r->rop, r->rorg, r->rdir, r->rot);
619 >        }
620 >                                        /* find global cube entrance point */
621          t = 0.0;
622          if (!incube(scene, curpos)) {
623                                          /* find distance to entry */
# Line 370 | Line 635 | register CUBE  *scene;
635                                  t = dt; /* farthest face is the one */
636                  }
637                  t += FTINY;             /* fudge to get inside cube */
638 +                if (t >= r->rot)        /* clipped already */
639 +                        return(0);
640                                          /* advance position */
641 <                for (i = 0; i < 3; i++)
375 <                        curpos[i] += r->rdir[i]*t;
641 >                VSUM(curpos, curpos, r->rdir, t);
642  
643                  if (!incube(scene, curpos))     /* non-intersecting ray */
644                          return(0);
645          }
646          cxset[0] = 0;
647 <        return(raymove(curpos, cxset, sflags, r, scene) == RAYHIT);
647 >        raymove(curpos, cxset, sflags, r, scene);
648 >        return((r->ro != NULL) & (r->ro != &Aftplane));
649   }
650  
651  
652   static int
653 < raymove(pos, cxs, dirf, r, cu)          /* check for hit as we move */
654 < FVECT  pos;                     /* current position, modified herein */
655 < OBJECT  *cxs;                   /* checked objects, modified by checkhit */
656 < int  dirf;                      /* direction indicators to speed tests */
657 < register RAY  *r;
658 < register CUBE  *cu;
653 > raymove(                /* check for hit as we move */
654 >        FVECT  pos,                     /* current position, modified herein */
655 >        OBJECT  *cxs,                   /* checked objects, modified by checkhit */
656 >        int  dirf,                      /* direction indicators to speed tests */
657 >        RAY  *r,
658 >        CUBE  *cu
659 > )
660   {
661          int  ax;
662          double  dt, t;
663  
664          if (istree(cu->cutree)) {               /* recurse on subcubes */
665                  CUBE  cukid;
666 <                register int  br, sgn;
666 >                int  br, sgn;
667  
668                  cukid.cusize = cu->cusize * 0.5;        /* find subcube */
669                  VCOPY(cukid.cuorg, cu->cuorg);
# Line 433 | Line 701 | register CUBE  *cu;
701                  }
702                  /*NOTREACHED*/
703          }
704 <        if (isfull(cu->cutree) && checkhit(r, cu, cxs))
704 >        if (isfull(cu->cutree)) {
705 >                if (checkhit(r, cu, cxs))
706 >                        return(RAYHIT);
707 >        } else if (r->ro == &Aftplane && incube(cu, r->rop))
708                  return(RAYHIT);
709                                          /* advance to next cube */
710          if (dirf&0x11) {
# Line 458 | Line 729 | register CUBE  *cu;
729                          ax = 2;
730                  }
731          }
732 <        pos[0] += r->rdir[0]*t;
462 <        pos[1] += r->rdir[1]*t;
463 <        pos[2] += r->rdir[2]*t;
732 >        VSUM(pos, pos, r->rdir, t);
733          return(ax);
734   }
735  
736  
737 < static
738 < checkhit(r, cu, cxs)            /* check for hit in full cube */
739 < register RAY  *r;
740 < CUBE  *cu;
741 < OBJECT  *cxs;
737 > static int
738 > checkhit(               /* check for hit in full cube */
739 >        RAY  *r,
740 >        CUBE  *cu,
741 >        OBJECT  *cxs
742 > )
743   {
744          OBJECT  oset[MAXSET+1];
475        register OBJREC  *o;
476        register int  i;
745  
746          objset(oset, cu->cutree);
747 <        checkset(oset, cxs);                    /* eliminate double-checking */
748 <        for (i = oset[0]; i > 0; i--) {
749 <                o = objptr(oset[i]);
750 <                (*ofun[o->otype].funp)(o, r);
751 <        }
484 <        if (r->ro == NULL)
747 >        checkset(oset, cxs);                    /* avoid double-checking */
748 >
749 >        (*r->hitf)(oset, r);                    /* test for hit in set */
750 >
751 >        if (r->robj == OVOID)
752                  return(0);                      /* no scores yet */
753  
754          return(incube(cu, r->rop));             /* hit OK if in current cube */
755   }
756  
757  
758 < static
759 < checkset(os, cs)                /* modify checked set and set to check */
760 < register OBJECT  *os;                   /* os' = os - cs */
761 < register OBJECT  *cs;                   /* cs' = cs + os */
758 > static void
759 > checkset(               /* modify checked set and set to check */
760 >        OBJECT  *os,                    /* os' = os - cs */
761 >        OBJECT  *cs                     /* cs' = cs + os */
762 > )
763   {
764          OBJECT  cset[MAXCSET+MAXSET+1];
765 <        register int  i, j;
765 >        int  i, j;
766          int  k;
767                                          /* copy os in place, cset <- cs */
768          cset[0] = 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines