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.39 by greg, Fri May 9 22:18:03 2003 UTC vs.
Revision 2.94 by greg, Sat Jan 18 03:49:00 2025 UTC

# Line 10 | Line 10 | static const char RCSid[] = "$Id$";
10   #include "copyright.h"
11  
12   #include  "ray.h"
13 <
13 > #include  "source.h"
14   #include  "otypes.h"
15
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 < unsigned long  raynum = 0;              /* next unique ray number */
22 < unsigned long  nrays = 0;               /* number of calls to localhit */
21 > RNUMBER  raynum = 0;            /* next unique ray number */
22 > RNUMBER  nrays = 0;             /* number of calls to localhit */
23  
24 < 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   OBJREC  Aftplane;                       /* aft clipping plane object */
31  
31 static int  raymove(), checkhit();
32 static void  checkset();
33
34 #ifndef  MAXLOOP
35 #define  MAXLOOP        0               /* modifier loop detection */
36 #endif
37
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 +
39   int
40 < rayorigin(r, ro, rt, rw)                /* start new ray from old one */
41 < register RAY  *r, *ro;
42 < int  rt;
43 < double  rw;
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  re;
48 <
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 58 | Line 68 | double  rw;
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 {
68                        r->rsrc = ro->rsrc;
84                          r->clipset = ro->newcset;
85 <                        r->rmax = ro->rmax <= FTINY ? 0.0 : ro->rmax - ro->rot;
85 >                        r->rmax = (ro->rmax > FTINY)*(ro->rmax - ro->rot);
86                  }
87                  r->revf = ro->revf;
88                  copycolor(r->cext, ro->cext);
# Line 77 | Line 92 | double  rw;
92                  r->crtype = ro->crtype | (r->rtype = rt);
93                  VCOPY(r->rorg, ro->rop);
94                  r->rweight = ro->rweight * rw;
95 <                                                /* estimate absorption */
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 <                if (re > 0.)
100 <                        r->rweight *= exp(-re*ro->rot);
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   void
139 < rayclear(r)                     /* clear a ray for (re)evaluation */
140 < register RAY  *r;
139 > rayclear(                       /* clear a ray for (re)evaluation */
140 >        RAY  *r
141 > )
142   {
143          r->rno = raynum++;
144          r->newcset = r->clipset;
# Line 99 | Line 146 | register RAY  *r;
146          r->robj = OVOID;
147          r->ro = NULL;
148          r->rox = NULL;
149 <        r->rt = r->rot = FHUGE;
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 +        r->rflips = 0;
155          r->uv[0] = r->uv[1] = 0.0;
156 <        setcolor(r->pcol, 1.0, 1.0, 1.0);
157 <        setcolor(r->rcol, 0.0, 0.0, 0.0);
156 >        setscolor(r->pcol, 1.0, 1.0, 1.0);
157 >        scolorblack(r->mcol);
158 >        scolorblack(r->rcol);
159   }
160  
161  
162   void
163 < raytrace(r)                     /* trace a ray and compute its value */
164 < RAY  *r;
163 > raytrace(                       /* trace a ray and compute its value */
164 >        RAY  *r
165 > )
166   {
167          if (localhit(r, &thescene))
168                  raycont(r);             /* hit local surface, evaluate */
# Line 119 | Line 172 | RAY  *r;
172          } else if (sourcehit(r))
173                  rayshade(r, r->ro->omod);       /* distant source */
174  
122        rayparticipate(r);              /* for participating medium */
123
175          if (trace != NULL)
176                  (*trace)(r);            /* trace execution */
177 +
178 +        rayparticipate(r);              /* for participating medium */
179   }
180  
181  
182   void
183 < raycont(r)                      /* check for clipped object and continue */
184 < register RAY  *r;
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                          !rayshade(r, r->ro->omod))
# Line 137 | Line 191 | register RAY  *r;
191  
192  
193   void
194 < raytrans(r)                     /* transmit ray as is */
195 < register RAY  *r;
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   int
231 < rayshade(r, mod)                /* shade ray r with material mod */
232 < register RAY  *r;
233 < int  mod;
231 > rayshade(               /* shade ray r with material mod */
232 >        RAY  *r,
233 >        int  mod
234 > )
235   {
236 <        int  gotmat;
237 <        register OBJREC  *m;
238 < #if  MAXLOOP
239 <        static int  depth = 0;
240 <                                        /* check for infinite loop */
164 <        if (depth++ >= MAXLOOP)
165 <                objerror(r->ro, USER, "possible modifier loop");
166 < #endif
167 <        r->rt = r->rot;                 /* set effective ray length */
168 <        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 174 | Line 246 | int  mod;
246                  }
247                  ******/
248                                          /* hack for irradiance calculation */
249 <                if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS)) &&
250 <                                (ofun[m->otype].flags & (T_M|T_X))) {
251 <                        if (irr_ignore(m->otype)) {
252 < #if  MAXLOOP
253 <                                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);
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 < #if  MAXLOOP
193 <        depth--;
194 < #endif
195 <        return(gotmat);
255 >        return(0);                      /* no material! */
256   }
257  
258  
259   void
260 < rayparticipate(r)                       /* compute ray medium participation */
261 < register RAY  *r;
260 > rayparticipate(                 /* compute ray medium participation */
261 >        RAY  *r
262 > )
263   {
264 <        COLOR   ce, ca;
264 >        SCOLOR  ce, ca;
265          double  re, ge, be;
266  
267          if (intens(r->cext) <= 1./FHUGE)
# Line 213 | Line 274 | register RAY  *r;
274                  ge *= 1. - colval(r->albedo,GRN);
275                  be *= 1. - colval(r->albedo,BLU);
276          }
277 <        setcolor(ce,    re<=0. ? 1. : re>92. ? 0. : exp(-re),
278 <                        ge<=0. ? 1. : ge>92. ? 0. : exp(-ge),
279 <                        be<=0. ? 1. : be>92. ? 0. : exp(-be));
280 <        multcolor(r->rcol, ce);                 /* path absorption */
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 <        setcolor(ca,
284 <                colval(r->albedo,RED)*colval(ambval,RED)*(1.-colval(ce,RED)),
285 <                colval(r->albedo,GRN)*colval(ambval,GRN)*(1.-colval(ce,GRN)),
286 <                colval(r->albedo,BLU)*colval(ambval,BLU)*(1.-colval(ce,BLU)));
287 <        addcolor(r->rcol, ca);                  /* ambient in 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.-colval(ce,RED)),
288 >                        colval(r->albedo,GRN)*colval(ambval,GRN)*(1.-colval(ce,GRN)),
289 >                        colval(r->albedo,BLU)*colval(ambval,BLU)*(1.-colval(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(r, mod)                      /* get material modifiers */
299 < RAY  *r;
300 < OBJECT  mod;
298 > raytexture(                     /* get material modifiers */
299 >        RAY  *r,
300 >        OBJECT  mod
301 > )
302   {
303 <        register OBJREC  *m;
237 < #if  MAXLOOP
238 <        static int  depth = 0;
239 <                                        /* check for infinite loop */
240 <        if (depth++ >= MAXLOOP)
241 <                objerror(r->ro, USER, "modifier loop");
242 < #endif
303 >        OBJREC  *m;
304                                          /* execute textures and patterns */
305          for ( ; mod != OVOID; mod = m->omod) {
306                  m = objptr(mod);
# Line 255 | Line 316 | OBJECT  mod;
316                          objerror(r->ro, USER, errmsg);
317                  }
318          }
258 #if  MAXLOOP
259        depth--;                        /* end here */
260 #endif
319   }
320  
321  
322   int
323 < raymixture(r, fore, back, coef)         /* mix modifiers */
324 < register RAY  *r;
325 < OBJECT  fore, back;
326 < double  coef;
323 > raymixture(             /* mix modifiers */
324 >        RAY  *r,
325 >        OBJECT  fore,
326 >        OBJECT  back,
327 >        double  coef
328 > )
329   {
330          RAY  fr, br;
331 +        double  mfore, mback;
332          int  foremat, backmat;
333 <        register int  i;
333 >        int  i;
334                                          /* bound coefficient */
335          if (coef > 1.0)
336                  coef = 1.0;
# Line 278 | Line 339 | double  coef;
339                                          /* compute foreground and background */
340          foremat = backmat = 0;
341                                          /* foreground */
342 <        copystruct(&fr, r);
343 <        if (coef > FTINY)
342 >        fr = *r;
343 >        if (coef > FTINY) {
344 >                fr.rweight *= coef;
345 >                scalescolor(fr.rcoef, coef);
346                  foremat = rayshade(&fr, fore);
347 +        }
348                                          /* background */
349 <        copystruct(&br, r);
350 <        if (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 +        }
355                                          /* check for transparency */
356 <        if (backmat ^ foremat)
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                                          /* mix pattern colors */
366 <        scalecolor(fr.pcol, coef);
367 <        scalecolor(br.pcol, 1.0-coef);
368 <        copycolor(r->pcol, fr.pcol);
369 <        addcolor(r->pcol, br.pcol);
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          if (!foremat & !backmat)
372                  return(0);
373                                          /* mix returned ray values */
374 <        scalecolor(fr.rcol, coef);
375 <        scalecolor(br.rcol, 1.0-coef);
376 <        copycolor(r->rcol, fr.rcol);
377 <        addcolor(r->rcol, br.rcol);
378 <        r->rt = bright(fr.rcol) > bright(br.rcol) ? fr.rt : br.rt;
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 < raydist(r, flags)               /* compute (cumulative) ray distance */
392 < register RAY  *r;
393 < register int  flags;
391 > raydist(                /* compute (cumulative) ray distance */
392 >        const RAY  *r,
393 >        int  flags
394 > )
395   {
396          double  sum = 0.0;
397  
# Line 327 | Line 403 | register int  flags;
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 (!warnedPM && (bright(r->cext) > FTINY) |
421 +                                (bright(r->albedo) > FTINY)) {
422 +                        error(WARNING,
423 +        "ray contribution calculation does not support participating media");
424 +                        warnedPM++;
425 +                }
426 +                r = r->parent;
427 +        }
428 + }
429 +
430 +
431   double
432 < raynormal(norm, r)              /* compute perturbed normal for ray */
433 < FVECT  norm;
434 < register RAY  *r;
432 > raynormal(              /* compute perturbed normal for ray */
433 >        FVECT  norm,
434 >        RAY  *r
435 > )
436   {
437          double  newdot;
438 <        register int  i;
438 >        int  i;
439  
440          /*      The perturbation is added to the surface normal to obtain
441           *  the new normal.  If the new normal would affect the surface
# Line 353 | Line 455 | register RAY  *r;
455                  return(r->rod);
456          }
457          newdot = -DOT(norm, r->rdir);
458 <        if ((newdot > 0.0) != (r->rod > 0.0)) {         /* fix orientation */
458 >        if ((newdot > 0.0) ^ (r->rod > 0.0)) {          /* fix orientation */
459                  for (i = 0; i < 3; i++)
460                          norm[i] += 2.0*newdot*r->rdir[i];
461                  newdot = -newdot;
# Line 363 | Line 465 | register RAY  *r;
465  
466  
467   void
468 < newrayxf(r)                     /* get new tranformation matrix for ray */
469 < RAY  *r;
468 > newrayxf(                       /* get new tranformation matrix for ray */
469 >        RAY  *r
470 > )
471   {
472          static struct xfn {
473                  struct xfn  *next;
474                  FULLXF  xf;
475          }  xfseed = { &xfseed }, *xflast = &xfseed;
476 <        register struct xfn  *xp;
477 <        register RAY  *rp;
476 >        struct xfn  *xp;
477 >        const RAY  *rp;
478  
479          /*
480           * Search for transform in circular list that
# Line 382 | Line 485 | RAY  *r;
485                  if (rp->rox == &xp->xf) {               /* xp in use */
486                          xp = xp->next;                  /* move to next */
487                          if (xp == xflast) {             /* need new one */
488 <                                xp = (struct xfn *)malloc(sizeof(struct xfn));
488 >                                xp = (struct xfn *)bmalloc(sizeof(struct xfn));
489                                  if (xp == NULL)
490                                          error(SYSTEM,
491                                                  "out of memory in newrayxf");
# Line 400 | Line 503 | RAY  *r;
503  
504  
505   void
506 < flipsurface(r)                  /* reverse surface orientation */
507 < register RAY  *r;
506 > flipsurface(                    /* reverse surface orientation */
507 >        RAY  *r
508 > )
509   {
510          r->rod = -r->rod;
511          r->ron[0] = -r->ron[0];
# Line 410 | Line 514 | register RAY  *r;
514          r->pert[0] = -r->pert[0];
515          r->pert[1] = -r->pert[1];
516          r->pert[2] = -r->pert[2];
517 +        r->rflips++;
518   }
519  
520  
521 + int
522 + rayreject(              /* check if candidate hit is worse than current */
523 +        OBJREC *o,
524 +        RAY *r,
525 +        double t,
526 +        double rod
527 + )
528 + {
529 +        OBJREC  *mnew, *mray;
530 +
531 +        if ((t <= FTINY) | (t > r->rot + FTINY))
532 +                return(1);
533 +        if (t < r->rot - FTINY)         /* is new hit significantly closer? */
534 +                return(0);
535 +                                        /* coincident point, so decide... */
536 +        if (o == r->ro)
537 +                return(1);              /* shouldn't happen */
538 +        if (r->ro == NULL)
539 +                return(0);              /* ditto */
540 +        mnew = findmaterial(o);
541 +        mray = findmaterial(r->ro);     /* check material transparencies */
542 +        if (mnew == NULL) {
543 +                if (mray != NULL)
544 +                        return(1);      /* old has material, new does not */
545 +        } else if (mray == NULL) {
546 +                return(0);              /* new has material, old does not */
547 +        } else if (istransp(mnew)) {
548 +                if (!istransp(mray))
549 +                        return(1);      /* new is transparent, old is not */
550 +        } else if (istransp(mray)) {
551 +                return(0);              /* old is transparent, new is not */
552 +        }
553 +        if (rod <= 0) {                 /* check which side we hit */
554 +                if (r->rod > 0)
555 +                        return(1);      /* old hit front, new did not */
556 +        } else if (r->rod <= 0) {
557 +                return(0);              /* new hit front, old did not */
558 +        }
559 +                        /* earlier modifier definition wins tie */
560 +        return (r->ro->omod >= o->omod);
561 + }
562 +
563   void
564 < rayhit(oset, r)                 /* standard ray hit test */
565 < OBJECT  *oset;
566 < RAY  *r;
564 > rayhit(                 /* standard ray hit test */
565 >        OBJECT  *oset,
566 >        RAY  *r
567 > )
568   {
569          OBJREC  *o;
570          int     i;
# Line 430 | Line 578 | RAY  *r;
578  
579  
580   int
581 < localhit(r, scene)              /* check for hit in the octree */
582 < register RAY  *r;
583 < register CUBE  *scene;
581 > localhit(               /* check for hit in the octree */
582 >        RAY  *r,
583 >        CUBE  *scene
584 > )
585   {
586          OBJECT  cxset[MAXCSET+1];       /* set of checked objects */
587          FVECT  curpos;                  /* current cube position */
588          int  sflags;                    /* sign flags */
589          double  t, dt;
590 <        register int  i;
590 >        int  i;
591  
592          nrays++;                        /* increment trace counter */
593          sflags = 0;
# Line 449 | Line 598 | register CUBE  *scene;
598                  else if (r->rdir[i] < -1e-7)
599                          sflags |= 0x10 << i;
600          }
601 <        if (sflags == 0)
602 <                error(CONSISTENCY, "zero ray direction in localhit");
601 >        if (!sflags) {
602 >                error(WARNING, "zero ray direction in localhit");
603 >                return(0);
604 >        }
605                                          /* start off assuming nothing hit */
606          if (r->rmax > FTINY) {          /* except aft plane if one */
607                  r->ro = &Aftplane;
608                  r->rot = r->rmax;
609 <                for (i = 0; i < 3; i++)
459 <                        r->rop[i] = r->rorg[i] + r->rot*r->rdir[i];
609 >                VSUM(r->rop, r->rorg, r->rdir, r->rot);
610          }
611                                          /* find global cube entrance point */
612          t = 0.0;
# Line 479 | Line 629 | register CUBE  *scene;
629                  if (t >= r->rot)        /* clipped already */
630                          return(0);
631                                          /* advance position */
632 <                for (i = 0; i < 3; i++)
483 <                        curpos[i] += r->rdir[i]*t;
632 >                VSUM(curpos, curpos, r->rdir, t);
633  
634                  if (!incube(scene, curpos))     /* non-intersecting ray */
635                          return(0);
636          }
637          cxset[0] = 0;
638          raymove(curpos, cxset, sflags, r, scene);
639 <        return(r->ro != NULL & r->ro != &Aftplane);
639 >        return((r->ro != NULL) & (r->ro != &Aftplane));
640   }
641  
642  
643   static int
644 < raymove(pos, cxs, dirf, r, cu)          /* check for hit as we move */
645 < FVECT  pos;                     /* current position, modified herein */
646 < OBJECT  *cxs;                   /* checked objects, modified by checkhit */
647 < int  dirf;                      /* direction indicators to speed tests */
648 < register RAY  *r;
649 < register CUBE  *cu;
644 > raymove(                /* check for hit as we move */
645 >        FVECT  pos,                     /* current position, modified herein */
646 >        OBJECT  *cxs,                   /* checked objects, modified by checkhit */
647 >        int  dirf,                      /* direction indicators to speed tests */
648 >        RAY  *r,
649 >        CUBE  *cu
650 > )
651   {
652          int  ax;
653          double  dt, t;
654  
655          if (istree(cu->cutree)) {               /* recurse on subcubes */
656                  CUBE  cukid;
657 <                register int  br, sgn;
657 >                int  br, sgn;
658  
659                  cukid.cusize = cu->cusize * 0.5;        /* find subcube */
660                  VCOPY(cukid.cuorg, cu->cuorg);
# Line 570 | Line 720 | register CUBE  *cu;
720                          ax = 2;
721                  }
722          }
723 <        pos[0] += r->rdir[0]*t;
574 <        pos[1] += r->rdir[1]*t;
575 <        pos[2] += r->rdir[2]*t;
723 >        VSUM(pos, pos, r->rdir, t);
724          return(ax);
725   }
726  
727  
728   static int
729 < checkhit(r, cu, cxs)            /* check for hit in full cube */
730 < register RAY  *r;
731 < CUBE  *cu;
732 < OBJECT  *cxs;
729 > checkhit(               /* check for hit in full cube */
730 >        RAY  *r,
731 >        CUBE  *cu,
732 >        OBJECT  *cxs
733 > )
734   {
735          OBJECT  oset[MAXSET+1];
736  
# Line 598 | Line 747 | OBJECT  *cxs;
747  
748  
749   static void
750 < checkset(os, cs)                /* modify checked set and set to check */
751 < register OBJECT  *os;                   /* os' = os - cs */
752 < register OBJECT  *cs;                   /* cs' = cs + os */
750 > checkset(               /* modify checked set and set to check */
751 >        OBJECT  *os,                    /* os' = os - cs */
752 >        OBJECT  *cs                     /* cs' = cs + os */
753 > )
754   {
755          OBJECT  cset[MAXCSET+MAXSET+1];
756 <        register int  i, j;
756 >        int  i, j;
757          int  k;
758                                          /* copy os in place, cset <- cs */
759          cset[0] = 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines