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 1.2 by greg, Tue Apr 11 13:30:31 1989 UTC vs.
Revision 2.66 by greg, Tue Feb 24 19:39:27 2015 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1986 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"
15 + #include  "otspecial.h"
16 + #include  "random.h"
17 + #include  "pmap.h"
18  
19 < extern CUBE  thescene;                  /* our scene */
20 < extern int  maxdepth;                   /* maximum recursion depth */
21 < extern double  minweight;               /* minimum ray weight */
19 > #define  MAXCSET        ((MAXSET+1)*2-1)        /* maximum check set size */
20  
21 < long  nrays = 0L;                       /* number of rays traced */
21 > RNUMBER  raynum = 0;            /* next unique ray number */
22 > RNUMBER  nrays = 0;             /* number of calls to localhit */
23  
24 < #define  MAXLOOP        32              /* modifier loop detection */
24 > static RREAL  Lambfa[5] = {PI, PI, PI, 0.0, 0.0};
25 > OBJREC  Lamb = {
26 >        OVOID, MAT_PLASTIC, "Lambertian",
27 >        {NULL, Lambfa, 0, 5}, NULL
28 > };                                      /* a Lambertian surface */
29  
30 + OBJREC  Aftplane;                       /* aft clipping plane object */
31 +
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 COLOR rc
45 > )
46   {
47 +        double  rw, re;
48 +                                                /* assign coefficient/weight */
49 +        if (rc == NULL) {
50 +                rw = 1.0;
51 +                setcolor(r->rcoef, 1., 1., 1.);
52 +        } else {
53 +                rw = intens(rc);
54 +                if (rc != r->rcoef)
55 +                        copycolor(r->rcoef, rc);
56 +        }
57          if ((r->parent = ro) == NULL) {         /* primary ray */
58                  r->rlvl = 0;
59                  r->rweight = rw;
60                  r->crtype = r->rtype = rt;
61                  r->rsrc = -1;
62                  r->clipset = NULL;
63 +                r->revf = raytrace;
64 +                copycolor(r->cext, cextinction);
65 +                copycolor(r->albedo, salbedo);
66 +                r->gecc = seccg;
67 +                r->slights = NULL;
68          } else {                                /* spawned ray */
69 +                if (ro->rot >= FHUGE) {
70 +                        memset(r, 0, sizeof(RAY));
71 +                        return(-1);             /* illegal continuation */
72 +                }
73                  r->rlvl = ro->rlvl;
74                  if (rt & RAYREFL) {
75                          r->rlvl++;
76                          r->rsrc = -1;
77                          r->clipset = ro->clipset;
78 +                        r->rmax = 0.0;
79                  } else {
80                          r->rsrc = ro->rsrc;
81                          r->clipset = ro->newcset;
82 +                        r->rmax = ro->rmax <= FTINY ? 0.0 : ro->rmax - ro->rot;
83                  }
84 <                r->rweight = ro->rweight * rw;
84 >                r->revf = ro->revf;
85 >                copycolor(r->cext, ro->cext);
86 >                copycolor(r->albedo, ro->albedo);
87 >                r->gecc = ro->gecc;
88 >                r->slights = ro->slights;
89                  r->crtype = ro->crtype | (r->rtype = rt);
90                  VCOPY(r->rorg, ro->rop);
91 +                r->rweight = ro->rweight * rw;
92 +                                                /* estimate extinction */
93 +                re = colval(ro->cext,RED) < colval(ro->cext,GRN) ?
94 +                                colval(ro->cext,RED) : colval(ro->cext,GRN);
95 +                if (colval(ro->cext,BLU) < re) re = colval(ro->cext,BLU);
96 +                re *= ro->rot;
97 +                if (re > 0.1) {
98 +                        if (re > 92.) {
99 +                                r->rweight = 0.0;
100 +                        } else {
101 +                                r->rweight *= exp(-re);
102 +                        }
103 +                }
104          }
105 <        r->rno = nrays;
105 >        rayclear(r);
106 >        if (r->rweight <= 0.0)                  /* check for expiration */
107 >                return(-1);
108 >        if (r->crtype & SHADOW)                 /* shadow commitment */
109 >                return(0);
110 >        if (maxdepth <= 0 && rc != NULL) {      /* Russian roulette */
111 >                if (minweight <= 0.0)
112 >                        error(USER, "zero ray weight in Russian roulette");
113 >                if (maxdepth < 0 && r->rlvl > -maxdepth)
114 >                        return(-1);             /* upper reflection limit */
115 >                if (r->rweight >= minweight)
116 >                        return(0);
117 >                if (frandom() > r->rweight/minweight)
118 >                        return(-1);
119 >                rw = minweight/r->rweight;      /* promote survivor */
120 >                scalecolor(r->rcoef, rw);
121 >                r->rweight = minweight;
122 >                return(0);
123 >        }
124 >        return(r->rweight >= minweight && r->rlvl <= abs(maxdepth) ? 0 : -1);
125 > }
126 >
127 >
128 > void
129 > rayclear(                       /* clear a ray for (re)evaluation */
130 >        RAY  *r
131 > )
132 > {
133 >        r->rno = raynum++;
134          r->newcset = r->clipset;
135 +        r->hitf = rayhit;
136 +        r->robj = OVOID;
137          r->ro = NULL;
138 <        r->rot = FHUGE;
139 <        r->rofs = 1.0; setident4(r->rofx);
60 <        r->robs = 1.0; setident4(r->robx);
138 >        r->rox = NULL;
139 >        r->rt = r->rot = FHUGE;
140          r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
141 +        r->uv[0] = r->uv[1] = 0.0;
142          setcolor(r->pcol, 1.0, 1.0, 1.0);
143          setcolor(r->rcol, 0.0, 0.0, 0.0);
64        return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1);
144   }
145  
146  
147 < rayvalue(r)                     /* compute a ray's value */
148 < register RAY  *r;
147 > void
148 > raytrace(                       /* trace a ray and compute its value */
149 >        RAY  *r
150 > )
151   {
71        extern int  (*trace)();
72
152          if (localhit(r, &thescene))
153 <                if (r->clipset != NULL && inset(r->clipset, r->ro->omod))
154 <                        raytrans(r);            /* object is clipped */
155 <                else
156 <                        rayshade(r, r->ro->omod);
157 <        else if (sourcehit(r))
158 <                rayshade(r, r->ro->omod);
153 >                raycont(r);             /* hit local surface, evaluate */
154 >        else if (r->ro == &Aftplane) {
155 >                r->ro = NULL;           /* hit aft clipping plane */
156 >                r->rot = FHUGE;
157 >        } else if (sourcehit(r))
158 >                rayshade(r, r->ro->omod);       /* distant source */
159  
160          if (trace != NULL)
161                  (*trace)(r);            /* trace execution */
162 +
163 +        rayparticipate(r);              /* for participating medium */
164   }
165  
166  
167 < raytrans(r)                     /* transmit ray as is */
168 < RAY  *r;
167 > void
168 > raycont(                        /* check for clipped object and continue */
169 >        RAY  *r
170 > )
171   {
172 +        if ((r->clipset != NULL && inset(r->clipset, r->ro->omod)) ||
173 +                        !rayshade(r, r->ro->omod))
174 +                raytrans(r);
175 + }
176 +
177 +
178 + void
179 + raytrans(                       /* transmit ray as is */
180 +        RAY  *r
181 + )
182 + {
183          RAY  tr;
184  
185 <        if (rayorigin(&tr, r, TRANS, 1.0) == 0) {
185 >        if (rayorigin(&tr, TRANS, r, NULL) == 0) {
186                  VCOPY(tr.rdir, r->rdir);
187                  rayvalue(&tr);
188                  copycolor(r->rcol, tr.rcol);
189 +                r->rt = r->rot + tr.rt;
190          }
191   }
192  
193  
194 < rayshade(r, mod)                /* shade ray r with material mod */
195 < register RAY  *r;
196 < int  mod;
194 > int
195 > rayshade(               /* shade ray r with material mod */
196 >        RAY  *r,
197 >        int  mod
198 > )
199   {
200 <        static int  depth = 0;
201 <        register OBJREC  *m;
202 <                                        /* check for infinite loop */
106 <        if (depth++ >= MAXLOOP)
107 <                objerror(r->ro, USER, "material loop");
200 >        OBJREC  *m;
201 >
202 >        r->rt = r->rot;                 /* set effective ray length */
203          for ( ; mod != OVOID; mod = m->omod) {
204                  m = objptr(mod);
205 +                /****** unnecessary test since modifier() is always called
206                  if (!ismodifier(m->otype)) {
207                          sprintf(errmsg, "illegal modifier \"%s\"", m->oname);
208                          error(USER, errmsg);
209                  }
210 <                (*ofun[m->otype].funp)(m, r);   /* execute function */
211 <                m->lastrno = r->rno;
212 <                if (ismaterial(m->otype)) {     /* materials call raytexture */
213 <                        depth--;
214 <                        return;         /* we're done */
210 >                ******/
211 >                                        /* hack for irradiance calculation */
212 >                if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS)) &&
213 >                                m->otype != MAT_CLIP &&
214 >                                (ofun[m->otype].flags & (T_M|T_X))) {
215 >                        if (irr_ignore(m->otype)) {
216 >                                raytrans(r);
217 >                                return(1);
218 >                        }
219 >                        if (!islight(m->otype))
220 >                                m = &Lamb;
221                  }
222 +                if ((*ofun[m->otype].funp)(m, r))
223 +                        return(1);      /* materials call raytexture() */
224          }
225 <        objerror(r->ro, USER, "material not found");
225 >        return(0);                      /* no material! */
226   }
227  
228  
229 < raytexture(r, mod)                      /* get material modifiers */
230 < RAY  *r;
231 < int  mod;
229 > void
230 > rayparticipate(                 /* compute ray medium participation */
231 >        RAY  *r
232 > )
233   {
234 <        static int  depth = 0;
235 <        register OBJREC  *m;
236 <                                        /* check for infinite loop */
237 <        if (depth++ >= MAXLOOP)
238 <                objerror(r->ro, USER, "modifier loop");
234 >        COLOR   ce, ca;
235 >        double  re, ge, be;
236 >
237 >        if (intens(r->cext) <= 1./FHUGE)
238 >                return;                         /* no medium */
239 >        re = r->rot*colval(r->cext,RED);
240 >        ge = r->rot*colval(r->cext,GRN);
241 >        be = r->rot*colval(r->cext,BLU);
242 >        if (r->crtype & SHADOW) {               /* no scattering for sources */
243 >                re *= 1. - colval(r->albedo,RED);
244 >                ge *= 1. - colval(r->albedo,GRN);
245 >                be *= 1. - colval(r->albedo,BLU);
246 >        }
247 >        setcolor(ce,    re<=FTINY ? 1. : re>92. ? 0. : exp(-re),
248 >                        ge<=FTINY ? 1. : ge>92. ? 0. : exp(-ge),
249 >                        be<=FTINY ? 1. : be>92. ? 0. : exp(-be));
250 >        multcolor(r->rcol, ce);                 /* path extinction */
251 >        if (r->crtype & SHADOW || intens(r->albedo) <= FTINY)
252 >                return;                         /* no scattering */
253 >        
254 >        /* PMAP: indirect inscattering accounted for by volume photons? */
255 >        if (!volumePhotonMapping) {
256 >                setcolor(ca,
257 >                        colval(r->albedo,RED)*colval(ambval,RED)*(1.-colval(ce,RED)),
258 >                        colval(r->albedo,GRN)*colval(ambval,GRN)*(1.-colval(ce,GRN)),
259 >                        colval(r->albedo,BLU)*colval(ambval,BLU)*(1.-colval(ce,BLU)));
260 >                addcolor(r->rcol, ca);                  /* ambient in scattering */
261 >        }
262 >        
263 >        srcscatter(r);                          /* source in scattering */
264 > }
265 >
266 >
267 > void
268 > raytexture(                     /* get material modifiers */
269 >        RAY  *r,
270 >        OBJECT  mod
271 > )
272 > {
273 >        OBJREC  *m;
274                                          /* execute textures and patterns */
275          for ( ; mod != OVOID; mod = m->omod) {
276                  m = objptr(mod);
277 <                if (!istexture(m->otype)) {
277 >                /****** unnecessary test since modifier() is always called
278 >                if (!ismodifier(m->otype)) {
279                          sprintf(errmsg, "illegal modifier \"%s\"", m->oname);
280                          error(USER, errmsg);
281                  }
282 <                (*ofun[m->otype].funp)(m, r);
283 <                m->lastrno = r->rno;
282 >                ******/
283 >                if ((*ofun[m->otype].funp)(m, r)) {
284 >                        sprintf(errmsg, "conflicting material \"%s\"",
285 >                                        m->oname);
286 >                        objerror(r->ro, USER, errmsg);
287 >                }
288          }
144        depth--;                        /* end here */
289   }
290  
291  
292 < raymixture(r, fore, back, coef)         /* mix modifiers */
293 < register RAY  *r;
294 < OBJECT  fore, back;
295 < double  coef;
292 > int
293 > raymixture(             /* mix modifiers */
294 >        RAY  *r,
295 >        OBJECT  fore,
296 >        OBJECT  back,
297 >        double  coef
298 > )
299   {
300 <        FVECT  curpert, forepert, backpert;
301 <        COLOR  curpcol, forepcol, backpcol;
302 <        register int  i;
303 <                                        /* clip coefficient */
300 >        RAY  fr, br;
301 >        int  foremat, backmat;
302 >        int  i;
303 >                                        /* bound coefficient */
304          if (coef > 1.0)
305                  coef = 1.0;
306          else if (coef < 0.0)
307                  coef = 0.0;
308 <                                        /* save current mods */
309 <        VCOPY(curpert, r->pert);
310 <        copycolor(curpcol, r->pcol);
311 <                                        /* compute new mods */
312 <                                                /* foreground */
313 <        r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
314 <        setcolor(r->pcol, 1.0, 1.0, 1.0);
315 <        if (fore != OVOID && coef > FTINY)
316 <                raytexture(r, fore);
317 <        VCOPY(forepert, r->pert);
318 <        copycolor(forepcol, r->pcol);
319 <                                                /* background */
320 <        r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
321 <        setcolor(r->pcol, 1.0, 1.0, 1.0);
322 <        if (back != OVOID && coef < 1.0-FTINY)
323 <                raytexture(r, back);
324 <        VCOPY(backpert, r->pert);
325 <        copycolor(backpcol, r->pcol);
326 <                                        /* sum perturbations */
308 >                                        /* compute foreground and background */
309 >        foremat = backmat = 0;
310 >                                        /* foreground */
311 >        fr = *r;
312 >        if (coef > FTINY) {
313 >                fr.rweight *= coef;
314 >                scalecolor(fr.rcoef, coef);
315 >                foremat = rayshade(&fr, fore);
316 >        }
317 >                                        /* background */
318 >        br = *r;
319 >        if (coef < 1.0-FTINY) {
320 >                br.rweight *= 1.0-coef;
321 >                scalecolor(br.rcoef, 1.0-coef);
322 >                backmat = rayshade(&br, back);
323 >        }
324 >                                        /* check for transparency */
325 >        if (backmat ^ foremat) {
326 >                if (backmat && coef > FTINY)
327 >                        raytrans(&fr);
328 >                else if (foremat && coef < 1.0-FTINY)
329 >                        raytrans(&br);
330 >        }
331 >                                        /* mix perturbations */
332          for (i = 0; i < 3; i++)
333 <                r->pert[i] = curpert[i] + coef*forepert[i] +
334 <                                (1.0-coef)*backpert[i];
335 <                                        /* multiply colors */
336 <        setcolor(r->pcol, coef*colval(forepcol,RED) +
337 <                                (1.0-coef)*colval(backpcol,RED),
338 <                        coef*colval(forepcol,GRN) +
339 <                                (1.0-coef)*colval(backpcol,GRN),
340 <                        coef*colval(forepcol,BLU) +
341 <                                (1.0-coef)*colval(backpcol,BLU));
342 <        multcolor(r->pcol, curpcol);
333 >                r->pert[i] = coef*fr.pert[i] + (1.0-coef)*br.pert[i];
334 >                                        /* mix pattern colors */
335 >        scalecolor(fr.pcol, coef);
336 >        scalecolor(br.pcol, 1.0-coef);
337 >        copycolor(r->pcol, fr.pcol);
338 >        addcolor(r->pcol, br.pcol);
339 >                                        /* return value tells if material */
340 >        if (!foremat & !backmat)
341 >                return(0);
342 >                                        /* mix returned ray values */
343 >        scalecolor(fr.rcol, coef);
344 >        scalecolor(br.rcol, 1.0-coef);
345 >        copycolor(r->rcol, fr.rcol);
346 >        addcolor(r->rcol, br.rcol);
347 >        r->rt = bright(fr.rcol) > bright(br.rcol) ? fr.rt : br.rt;
348 >        return(1);
349   }
350  
351  
352   double
353 < raynormal(norm, r)              /* compute perturbed normal for ray */
354 < FVECT  norm;
355 < register RAY  *r;
353 > raydist(                /* compute (cumulative) ray distance */
354 >        const RAY  *r,
355 >        int  flags
356 > )
357   {
358 +        double  sum = 0.0;
359 +
360 +        while (r != NULL && r->crtype&flags) {
361 +                sum += r->rot;
362 +                r = r->parent;
363 +        }
364 +        return(sum);
365 + }
366 +
367 +
368 + void
369 + raycontrib(             /* compute (cumulative) ray contribution */
370 +        RREAL  rc[3],
371 +        const RAY  *r,
372 +        int  flags
373 + )
374 + {
375 +        double  eext[3];
376 +        int     i;
377 +
378 +        eext[0] = eext[1] = eext[2] = 0.;
379 +        rc[0] = rc[1] = rc[2] = 1.;
380 +
381 +        while (r != NULL && r->crtype&flags) {
382 +                for (i = 3; i--; ) {
383 +                        rc[i] *= colval(r->rcoef,i);
384 +                        eext[i] += r->rot * colval(r->cext,i);
385 +                }
386 +                r = r->parent;
387 +        }
388 +        for (i = 3; i--; )
389 +                rc[i] *= (eext[i] <= FTINY) ? 1. :
390 +                                (eext[i] > 92.) ? 0. : exp(-eext[i]);
391 + }
392 +
393 +
394 + double
395 + raynormal(              /* compute perturbed normal for ray */
396 +        FVECT  norm,
397 +        RAY  *r
398 + )
399 + {
400          double  newdot;
401 <        register int  i;
401 >        int  i;
402  
403          /*      The perturbation is added to the surface normal to obtain
404           *  the new normal.  If the new normal would affect the surface
# Line 205 | Line 406 | register RAY  *r;
406           *  still fraught with problems since reflected rays and similar
407           *  directions calculated from the surface normal may spawn rays behind
408           *  the surface.  The only solution is to curb textures at high
409 <         *  incidence (Rdot << 1).
409 >         *  incidence (namely, keep DOT(rdir,pert) < Rdot).
410           */
411  
412          for (i = 0; i < 3; i++)
# Line 226 | Line 427 | register RAY  *r;
427   }
428  
429  
430 < flipsurface(r)                  /* reverse surface orientation */
431 < register RAY  *r;
430 > void
431 > newrayxf(                       /* get new tranformation matrix for ray */
432 >        RAY  *r
433 > )
434   {
435 +        static struct xfn {
436 +                struct xfn  *next;
437 +                FULLXF  xf;
438 +        }  xfseed = { &xfseed }, *xflast = &xfseed;
439 +        struct xfn  *xp;
440 +        const RAY  *rp;
441 +
442 +        /*
443 +         * Search for transform in circular list that
444 +         * has no associated ray in the tree.
445 +         */
446 +        xp = xflast;
447 +        for (rp = r->parent; rp != NULL; rp = rp->parent)
448 +                if (rp->rox == &xp->xf) {               /* xp in use */
449 +                        xp = xp->next;                  /* move to next */
450 +                        if (xp == xflast) {             /* need new one */
451 +                                xp = (struct xfn *)bmalloc(sizeof(struct xfn));
452 +                                if (xp == NULL)
453 +                                        error(SYSTEM,
454 +                                                "out of memory in newrayxf");
455 +                                                        /* insert in list */
456 +                                xp->next = xflast->next;
457 +                                xflast->next = xp;
458 +                                break;                  /* we're done */
459 +                        }
460 +                        rp = r;                 /* start check over */
461 +                }
462 +                                        /* got it */
463 +        r->rox = &xp->xf;
464 +        xflast = xp;
465 + }
466 +
467 +
468 + void
469 + flipsurface(                    /* reverse surface orientation */
470 +        RAY  *r
471 + )
472 + {
473          r->rod = -r->rod;
474          r->ron[0] = -r->ron[0];
475          r->ron[1] = -r->ron[1];
# Line 239 | Line 480 | register RAY  *r;
480   }
481  
482  
483 < localhit(r, scene)              /* check for hit in the octree */
484 < register RAY  *r;
485 < register CUBE  *scene;
483 > void
484 > rayhit(                 /* standard ray hit test */
485 >        OBJECT  *oset,
486 >        RAY  *r
487 > )
488   {
489 +        OBJREC  *o;
490 +        int     i;
491 +
492 +        for (i = oset[0]; i > 0; i--) {
493 +                o = objptr(oset[i]);
494 +                if ((*ofun[o->otype].funp)(o, r))
495 +                        r->robj = oset[i];
496 +        }
497 + }
498 +
499 +
500 + int
501 + localhit(               /* check for hit in the octree */
502 +        RAY  *r,
503 +        CUBE  *scene
504 + )
505 + {
506 +        OBJECT  cxset[MAXCSET+1];       /* set of checked objects */
507          FVECT  curpos;                  /* current cube position */
508 <        int  mpos, mneg;                /* sign flags */
508 >        int  sflags;                    /* sign flags */
509          double  t, dt;
510 <        register int  i;
510 >        int  i;
511  
512          nrays++;                        /* increment trace counter */
513 <
253 <        mpos = mneg = 0;
513 >        sflags = 0;
514          for (i = 0; i < 3; i++) {
515                  curpos[i] = r->rorg[i];
516 <                if (r->rdir[i] > FTINY)
517 <                        mpos |= 1 << i;
518 <                else if (r->rdir[i] < -FTINY)
519 <                        mneg |= 1 << i;
516 >                if (r->rdir[i] > 1e-7)
517 >                        sflags |= 1 << i;
518 >                else if (r->rdir[i] < -1e-7)
519 >                        sflags |= 0x10 << i;
520          }
521 +        if (!sflags) {
522 +                error(WARNING, "zero ray direction in localhit");
523 +                return(0);
524 +        }
525 +                                        /* start off assuming nothing hit */
526 +        if (r->rmax > FTINY) {          /* except aft plane if one */
527 +                r->ro = &Aftplane;
528 +                r->rot = r->rmax;
529 +                VSUM(r->rop, r->rorg, r->rdir, r->rot);
530 +        }
531 +                                        /* find global cube entrance point */
532          t = 0.0;
533          if (!incube(scene, curpos)) {
534                                          /* find distance to entry */
535                  for (i = 0; i < 3; i++) {
536                                          /* plane in our direction */
537 <                        if (mpos & 1<<i)
537 >                        if (sflags & 1<<i)
538                                  dt = scene->cuorg[i];
539 <                        else if (mneg & 1<<i)
539 >                        else if (sflags & 0x10<<i)
540                                  dt = scene->cuorg[i] + scene->cusize;
541                          else
542                                  continue;
# Line 275 | Line 546 | register CUBE  *scene;
546                                  t = dt; /* farthest face is the one */
547                  }
548                  t += FTINY;             /* fudge to get inside cube */
549 +                if (t >= r->rot)        /* clipped already */
550 +                        return(0);
551                                          /* advance position */
552 <                for (i = 0; i < 3; i++)
280 <                        curpos[i] += r->rdir[i]*t;
552 >                VSUM(curpos, curpos, r->rdir, t);
553  
554                  if (!incube(scene, curpos))     /* non-intersecting ray */
555                          return(0);
556          }
557 <        return(raymove(curpos, mpos, mneg, r, scene) == RAYHIT);
557 >        cxset[0] = 0;
558 >        raymove(curpos, cxset, sflags, r, scene);
559 >        return((r->ro != NULL) & (r->ro != &Aftplane));
560   }
561  
562  
563   static int
564 < raymove(pos, plus, minus, r, cu)        /* check for hit as we move */
565 < FVECT  pos;                     /* modified */
566 < int  plus, minus;               /* direction indicators to speed tests */
567 < register RAY  *r;
568 < register CUBE  *cu;
564 > raymove(                /* check for hit as we move */
565 >        FVECT  pos,                     /* current position, modified herein */
566 >        OBJECT  *cxs,                   /* checked objects, modified by checkhit */
567 >        int  dirf,                      /* direction indicators to speed tests */
568 >        RAY  *r,
569 >        CUBE  *cu
570 > )
571   {
572          int  ax;
573          double  dt, t;
298        register int  sgn;
574  
575          if (istree(cu->cutree)) {               /* recurse on subcubes */
576                  CUBE  cukid;
577 <                register int  br;
577 >                int  br, sgn;
578  
579                  cukid.cusize = cu->cusize * 0.5;        /* find subcube */
580                  VCOPY(cukid.cuorg, cu->cuorg);
# Line 318 | Line 593 | register CUBE  *cu;
593                  }
594                  for ( ; ; ) {
595                          cukid.cutree = octkid(cu->cutree, br);
596 <                        if ((ax = raymove(pos,plus,minus,r,&cukid)) == RAYHIT)
596 >                        if ((ax = raymove(pos,cxs,dirf,r,&cukid)) == RAYHIT)
597                                  return(RAYHIT);
598                          sgn = 1 << ax;
599 <                        if (sgn & minus)                /* negative axis? */
325 <                                if (sgn & br) {
326 <                                        cukid.cuorg[ax] -= cukid.cusize;
327 <                                        br &= ~sgn;
328 <                                } else
329 <                                        return(ax);     /* underflow */
330 <                        else
599 >                        if (sgn & dirf)                 /* positive axis? */
600                                  if (sgn & br)
601                                          return(ax);     /* overflow */
602                                  else {
603                                          cukid.cuorg[ax] += cukid.cusize;
604                                          br |= sgn;
605                                  }
606 +                        else
607 +                                if (sgn & br) {
608 +                                        cukid.cuorg[ax] -= cukid.cusize;
609 +                                        br &= ~sgn;
610 +                                } else
611 +                                        return(ax);     /* underflow */
612                  }
613                  /*NOTREACHED*/
614          }
615 <        if (isfull(cu->cutree) && checkhit(r, cu))
615 >        if (isfull(cu->cutree)) {
616 >                if (checkhit(r, cu, cxs))
617 >                        return(RAYHIT);
618 >        } else if (r->ro == &Aftplane && incube(cu, r->rop))
619                  return(RAYHIT);
620                                          /* advance to next cube */
621 <        sgn = plus | minus;
622 <        if (sgn&1) {
345 <                dt = plus&1 ? cu->cuorg[0] + cu->cusize : cu->cuorg[0];
621 >        if (dirf&0x11) {
622 >                dt = dirf&1 ? cu->cuorg[0] + cu->cusize : cu->cuorg[0];
623                  t = (dt - pos[0])/r->rdir[0];
624                  ax = 0;
625          } else
626                  t = FHUGE;
627 <        if (sgn&2) {
628 <                dt = plus&2 ? cu->cuorg[1] + cu->cusize : cu->cuorg[1];
627 >        if (dirf&0x22) {
628 >                dt = dirf&2 ? cu->cuorg[1] + cu->cusize : cu->cuorg[1];
629                  dt = (dt - pos[1])/r->rdir[1];
630                  if (dt < t) {
631                          t = dt;
632                          ax = 1;
633                  }
634          }
635 <        if (sgn&4) {
636 <                dt = plus&4 ? cu->cuorg[2] + cu->cusize : cu->cuorg[2];
635 >        if (dirf&0x44) {
636 >                dt = dirf&4 ? cu->cuorg[2] + cu->cusize : cu->cuorg[2];
637                  dt = (dt - pos[2])/r->rdir[2];
638                  if (dt < t) {
639                          t = dt;
640                          ax = 2;
641                  }
642          }
643 <        pos[0] += r->rdir[0]*t;
367 <        pos[1] += r->rdir[1]*t;
368 <        pos[2] += r->rdir[2]*t;
643 >        VSUM(pos, pos, r->rdir, t);
644          return(ax);
645   }
646  
647  
648 < static
649 < checkhit(r, cu)                 /* check for hit in full cube */
650 < register RAY  *r;
651 < CUBE  *cu;
648 > static int
649 > checkhit(               /* check for hit in full cube */
650 >        RAY  *r,
651 >        CUBE  *cu,
652 >        OBJECT  *cxs
653 > )
654   {
655          OBJECT  oset[MAXSET+1];
379        register OBJREC  *o;
380        register int  i;
656  
657          objset(oset, cu->cutree);
658 <        for (i = oset[0]; i > 0; i--) {
659 <                o = objptr(oset[i]);
660 <                if (o->lastrno == r->rno)               /* checked already? */
661 <                        continue;
662 <                (*ofun[o->otype].funp)(o, r);
388 <                o->lastrno = r->rno;
389 <        }
390 <        if (r->ro == NULL)
658 >        checkset(oset, cxs);                    /* avoid double-checking */
659 >
660 >        (*r->hitf)(oset, r);                    /* test for hit in set */
661 >
662 >        if (r->robj == OVOID)
663                  return(0);                      /* no scores yet */
664  
665          return(incube(cu, r->rop));             /* hit OK if in current cube */
666 + }
667 +
668 +
669 + static void
670 + checkset(               /* modify checked set and set to check */
671 +        OBJECT  *os,                    /* os' = os - cs */
672 +        OBJECT  *cs                     /* cs' = cs + os */
673 + )
674 + {
675 +        OBJECT  cset[MAXCSET+MAXSET+1];
676 +        int  i, j;
677 +        int  k;
678 +                                        /* copy os in place, cset <- cs */
679 +        cset[0] = 0;
680 +        k = 0;
681 +        for (i = j = 1; i <= os[0]; i++) {
682 +                while (j <= cs[0] && cs[j] < os[i])
683 +                        cset[++cset[0]] = cs[j++];
684 +                if (j > cs[0] || os[i] != cs[j]) {      /* object to check */
685 +                        os[++k] = os[i];
686 +                        cset[++cset[0]] = os[i];
687 +                }
688 +        }
689 +        if (!(os[0] = k))               /* new "to check" set size */
690 +                return;                 /* special case */
691 +        while (j <= cs[0])              /* get the rest of cs */
692 +                cset[++cset[0]] = cs[j++];
693 +        if (cset[0] > MAXCSET)          /* truncate "checked" set if nec. */
694 +                cset[0] = MAXCSET;
695 +        /* setcopy(cs, cset); */        /* copy cset back to cs */
696 +        os = cset;
697 +        for (i = os[0]; i-- >= 0; )
698 +                *cs++ = *os++;
699   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines