ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/raytrace.c
Revision: 2.96
Committed: Fri Feb 7 16:32:56 2025 UTC (2 months, 3 weeks ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.95: +16 -12 lines
Log Message:
perf: Faster version of extinction handling in raycontrib()

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.96 static const char RCSid[] = "$Id: raytrace.c,v 2.95 2025/02/06 02:17:33 greg Exp $";
3 greg 1.1 #endif
4     /*
5     * raytrace.c - routines for tracing and shading rays.
6     *
7 greg 2.34 * External symbols declared in ray.h
8     */
9    
10 greg 2.35 #include "copyright.h"
11 greg 1.1
12     #include "ray.h"
13 schorsch 2.45 #include "source.h"
14 greg 1.1 #include "otypes.h"
15 greg 1.15 #include "otspecial.h"
16 greg 2.51 #include "random.h"
17 greg 2.66 #include "pmap.h"
18 greg 1.15
19 greg 2.3 #define MAXCSET ((MAXSET+1)*2-1) /* maximum check set size */
20    
21 greg 2.60 RNUMBER raynum = 0; /* next unique ray number */
22     RNUMBER nrays = 0; /* number of calls to localhit */
23 greg 1.1
24 schorsch 2.40 static RREAL Lambfa[5] = {PI, PI, PI, 0.0, 0.0};
25 greg 1.15 OBJREC Lamb = {
26     OVOID, MAT_PLASTIC, "Lambertian",
27 greg 2.57 {NULL, Lambfa, 0, 5}, NULL
28 greg 1.15 }; /* a Lambertian surface */
29    
30 greg 2.17 OBJREC Aftplane; /* aft clipping plane object */
31 greg 2.16
32 schorsch 2.45 #define RAYHIT (-1) /* return value for intercepted ray */
33 greg 2.5
34 schorsch 2.45 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 greg 1.1
38    
39 greg 2.65 int
40 schorsch 2.45 rayorigin( /* start new ray from old one */
41 greg 2.49 RAY *r,
42 schorsch 2.45 int rt,
43 greg 2.49 const RAY *ro,
44 greg 2.88 const SCOLOR rc
45 schorsch 2.45 )
46 greg 1.1 {
47 greg 2.49 double rw, re;
48     /* assign coefficient/weight */
49     if (rc == NULL) {
50     rw = 1.0;
51 greg 2.88 setscolor(r->rcoef, 1., 1., 1.);
52 greg 2.49 } else {
53 greg 2.89 rw = sintens((COLORV *)rc);
54 greg 2.70 if (rw > 1.0)
55     rw = 1.0; /* avoid calculation growth */
56 greg 2.49 if (rc != r->rcoef)
57 greg 2.88 copyscolor(r->rcoef, rc);
58 greg 2.49 }
59 greg 1.1 if ((r->parent = ro) == NULL) { /* primary ray */
60     r->rlvl = 0;
61     r->rweight = rw;
62     r->crtype = r->rtype = rt;
63     r->rsrc = -1;
64     r->clipset = NULL;
65 greg 2.50 r->revf = raytrace;
66 greg 2.23 copycolor(r->cext, cextinction);
67 greg 2.26 copycolor(r->albedo, salbedo);
68 greg 2.23 r->gecc = seccg;
69     r->slights = NULL;
70 greg 1.1 } else { /* spawned ray */
71 greg 2.80 if (ro->rot >= FHUGE*.99) {
72 greg 2.49 memset(r, 0, sizeof(RAY));
73     return(-1); /* illegal continuation */
74     }
75 greg 1.1 r->rlvl = ro->rlvl;
76 greg 2.90 r->rsrc = ro->rsrc;
77 greg 1.1 if (rt & RAYREFL) {
78     r->rlvl++;
79 greg 2.90 if (r->rsrc >= 0) /* malfunctioning material? */
80     r->rsrc = -1;
81 greg 1.1 r->clipset = ro->clipset;
82 greg 2.22 r->rmax = 0.0;
83 greg 1.1 } else {
84     r->clipset = ro->newcset;
85 greg 2.90 r->rmax = (ro->rmax > FTINY)*(ro->rmax - ro->rot);
86 greg 1.1 }
87 greg 2.50 r->revf = ro->revf;
88 greg 2.23 copycolor(r->cext, ro->cext);
89 greg 2.26 copycolor(r->albedo, ro->albedo);
90 greg 2.23 r->gecc = ro->gecc;
91     r->slights = ro->slights;
92 greg 1.1 r->crtype = ro->crtype | (r->rtype = rt);
93     VCOPY(r->rorg, ro->rop);
94 gwlarson 2.31 r->rweight = ro->rweight * rw;
95 greg 2.49 /* estimate extinction */
96 gwlarson 2.31 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 greg 2.49 re *= ro->rot;
100 schorsch 2.58 if (re > 0.1) {
101     if (re > 92.) {
102 greg 2.53 r->rweight = 0.0;
103 schorsch 2.58 } else {
104 greg 2.53 r->rweight *= exp(-re);
105 schorsch 2.58 }
106     }
107 greg 1.1 }
108 greg 1.22 rayclear(r);
109 greg 2.53 if (r->rweight <= 0.0) /* check for expiration */
110     return(-1);
111 greg 2.52 if (r->crtype & SHADOW) /* shadow commitment */
112     return(0);
113 greg 2.67 /* ambient in photon map? */
114 greg 2.69 if (ro != NULL && ro->crtype & AMBIENT) {
115     if (causticPhotonMapping)
116     return(-1);
117     if (photonMapping && rt != TRANS)
118     return(-1);
119     }
120 greg 2.77 if ((maxdepth <= 0) & (rc != NULL)) { /* Russian roulette */
121 greg 2.51 if (minweight <= 0.0)
122     error(USER, "zero ray weight in Russian roulette");
123 greg 2.77 if ((maxdepth < 0) & (r->rlvl > -maxdepth))
124 greg 2.51 return(-1); /* upper reflection limit */
125     if (r->rweight >= minweight)
126     return(0);
127 greg 2.55 if (frandom() > r->rweight/minweight)
128 greg 2.51 return(-1);
129     rw = minweight/r->rweight; /* promote survivor */
130 greg 2.88 scalescolor(r->rcoef, rw);
131 greg 2.51 r->rweight = minweight;
132     return(0);
133     }
134 greg 2.77 return((r->rweight >= minweight) & (r->rlvl <= abs(maxdepth)) ? 0 : -1);
135 greg 1.22 }
136    
137    
138 greg 2.65 void
139 schorsch 2.45 rayclear( /* clear a ray for (re)evaluation */
140 greg 2.64 RAY *r
141 schorsch 2.45 )
142 greg 1.22 {
143 greg 1.20 r->rno = raynum++;
144 greg 1.1 r->newcset = r->clipset;
145 greg 2.36 r->hitf = rayhit;
146 greg 2.28 r->robj = OVOID;
147 greg 2.17 r->ro = NULL;
148 greg 2.34 r->rox = NULL;
149 greg 2.73 r->rxt = r->rmt = r->rot = FHUGE;
150 greg 2.81 VCOPY(r->rop, r->rorg);
151 greg 2.82 r->ron[0] = -r->rdir[0]; r->ron[1] = -r->rdir[1]; r->ron[2] = -r->rdir[2];
152     r->rod = 1.0;
153 greg 1.1 r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
154 greg 2.79 r->rflips = 0;
155 greg 2.37 r->uv[0] = r->uv[1] = 0.0;
156 greg 2.88 setscolor(r->pcol, 1.0, 1.0, 1.0);
157     scolorblack(r->mcol);
158     scolorblack(r->rcol);
159 greg 1.1 }
160    
161    
162 greg 2.65 void
163 greg 2.50 raytrace( /* trace a ray and compute its value */
164 schorsch 2.45 RAY *r
165     )
166 greg 1.1 {
167 greg 1.15 if (localhit(r, &thescene))
168 greg 2.24 raycont(r); /* hit local surface, evaluate */
169 greg 2.16 else if (r->ro == &Aftplane) {
170 greg 2.23 r->ro = NULL; /* hit aft clipping plane */
171 greg 2.16 r->rot = FHUGE;
172     } else if (sourcehit(r))
173 greg 2.24 rayshade(r, r->ro->omod); /* distant source */
174 greg 1.1
175     if (trace != NULL)
176     (*trace)(r); /* trace execution */
177 greg 2.49
178     rayparticipate(r); /* for participating medium */
179 greg 1.1 }
180    
181    
182 greg 2.65 void
183 schorsch 2.45 raycont( /* check for clipped object and continue */
184 greg 2.64 RAY *r
185 schorsch 2.45 )
186 greg 1.8 {
187 greg 2.7 if ((r->clipset != NULL && inset(r->clipset, r->ro->omod)) ||
188 greg 2.24 !rayshade(r, r->ro->omod))
189 greg 1.8 raytrans(r);
190     }
191    
192    
193 greg 2.65 void
194 schorsch 2.45 raytrans( /* transmit ray as is */
195 greg 2.64 RAY *r
196 schorsch 2.45 )
197 greg 1.1 {
198     RAY tr;
199    
200 greg 2.67 rayorigin(&tr, TRANS, r, NULL); /* always continue */
201     VCOPY(tr.rdir, r->rdir);
202     rayvalue(&tr);
203 greg 2.88 copyscolor(r->mcol, tr.mcol);
204     copyscolor(r->rcol, tr.rcol);
205 greg 2.73 r->rmt = r->rot + tr.rmt;
206     r->rxt = r->rot + tr.rxt;
207 greg 1.1 }
208    
209    
210 greg 2.65 int
211 greg 2.74 raytirrad( /* irradiance hack */
212     OBJREC *m,
213     RAY *r
214     )
215     {
216 greg 2.91 if (m->otype != MAT_CLIP && ismaterial(m->otype)) {
217 greg 2.93 if (istransp(m) || isBSDFproxy(m)) {
218 greg 2.74 raytrans(r);
219     return(1);
220     }
221 greg 2.86 if (!islight(m->otype)) {
222 greg 2.88 setscolor(r->pcol, 1.0, 1.0, 1.0);
223 greg 2.74 return((*ofun[Lamb.otype].funp)(&Lamb, r));
224 greg 2.86 }
225 greg 2.74 }
226     return(0); /* not a qualifying surface */
227     }
228    
229    
230     int
231 schorsch 2.45 rayshade( /* shade ray r with material mod */
232 greg 2.64 RAY *r,
233 schorsch 2.45 int mod
234     )
235 greg 1.1 {
236 greg 2.74 int tst_irrad = do_irrad && !(r->crtype & ~(PRIMARY|TRANS));
237 greg 2.64 OBJREC *m;
238 greg 2.47
239 greg 2.78 r->rxt = r->rot; /* preset effective ray length */
240 greg 2.47 for ( ; mod != OVOID; mod = m->omod) {
241 greg 1.1 m = objptr(mod);
242 greg 1.4 /****** unnecessary test since modifier() is always called
243 greg 1.1 if (!ismodifier(m->otype)) {
244     sprintf(errmsg, "illegal modifier \"%s\"", m->oname);
245     error(USER, errmsg);
246     }
247 greg 1.4 ******/
248 greg 1.16 /* hack for irradiance calculation */
249 greg 2.74 if (tst_irrad && raytirrad(m, r))
250     return(1);
251    
252 greg 2.47 if ((*ofun[m->otype].funp)(m, r))
253     return(1); /* materials call raytexture() */
254 greg 1.1 }
255 greg 2.47 return(0); /* no material! */
256 greg 2.23 }
257    
258    
259 greg 2.65 void
260 schorsch 2.45 rayparticipate( /* compute ray medium participation */
261 greg 2.64 RAY *r
262 schorsch 2.45 )
263 greg 2.23 {
264 greg 2.88 SCOLOR ce, ca;
265 greg 2.23 double re, ge, be;
266    
267     if (intens(r->cext) <= 1./FHUGE)
268     return; /* no medium */
269 greg 2.27 re = r->rot*colval(r->cext,RED);
270     ge = r->rot*colval(r->cext,GRN);
271     be = r->rot*colval(r->cext,BLU);
272 greg 2.26 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 greg 2.88 setscolor(ce, re<=FTINY ? 1. : re>92. ? 0. : exp(-re),
278 greg 2.49 ge<=FTINY ? 1. : ge>92. ? 0. : exp(-ge),
279     be<=FTINY ? 1. : be>92. ? 0. : exp(-be));
280 greg 2.88 smultscolor(r->rcol, ce); /* path extinction */
281 greg 2.26 if (r->crtype & SHADOW || intens(r->albedo) <= FTINY)
282 greg 2.23 return; /* no scattering */
283 greg 2.66
284     /* PMAP: indirect inscattering accounted for by volume photons? */
285     if (!volumePhotonMapping) {
286 greg 2.88 setscolor(ca,
287 greg 2.95 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 greg 2.88 saddscolor(r->rcol, ca); /* ambient in scattering */
291 greg 2.66 }
292    
293 greg 2.23 srcscatter(r); /* source in scattering */
294 greg 1.1 }
295    
296    
297 greg 2.65 void
298 schorsch 2.45 raytexture( /* get material modifiers */
299     RAY *r,
300     OBJECT mod
301     )
302 greg 1.1 {
303 greg 2.64 OBJREC *m;
304 greg 1.1 /* execute textures and patterns */
305     for ( ; mod != OVOID; mod = m->omod) {
306     m = objptr(mod);
307 greg 2.9 /****** unnecessary test since modifier() is always called
308     if (!ismodifier(m->otype)) {
309 greg 1.1 sprintf(errmsg, "illegal modifier \"%s\"", m->oname);
310     error(USER, errmsg);
311     }
312 greg 2.9 ******/
313 greg 2.20 if ((*ofun[m->otype].funp)(m, r)) {
314     sprintf(errmsg, "conflicting material \"%s\"",
315     m->oname);
316     objerror(r->ro, USER, errmsg);
317     }
318 greg 1.1 }
319     }
320    
321    
322 greg 2.65 int
323 schorsch 2.45 raymixture( /* mix modifiers */
324 greg 2.64 RAY *r,
325 schorsch 2.45 OBJECT fore,
326     OBJECT back,
327     double coef
328     )
329 greg 1.1 {
330 greg 2.9 RAY fr, br;
331 greg 2.73 double mfore, mback;
332 greg 2.9 int foremat, backmat;
333 greg 2.64 int i;
334 greg 2.24 /* bound coefficient */
335 greg 1.1 if (coef > 1.0)
336     coef = 1.0;
337     else if (coef < 0.0)
338     coef = 0.0;
339 greg 2.13 /* compute foreground and background */
340 greg 2.24 foremat = backmat = 0;
341 greg 2.9 /* foreground */
342 schorsch 2.41 fr = *r;
343 greg 2.54 if (coef > FTINY) {
344 greg 2.59 fr.rweight *= coef;
345 greg 2.88 scalescolor(fr.rcoef, coef);
346 greg 2.9 foremat = rayshade(&fr, fore);
347 greg 2.54 }
348 greg 2.9 /* background */
349 schorsch 2.41 br = *r;
350 greg 2.54 if (coef < 1.0-FTINY) {
351 greg 2.59 br.rweight *= 1.0-coef;
352 greg 2.88 scalescolor(br.rcoef, 1.0-coef);
353 greg 2.9 backmat = rayshade(&br, back);
354 greg 2.54 }
355 greg 2.24 /* check for transparency */
356 schorsch 2.41 if (backmat ^ foremat) {
357 gwlarson 2.33 if (backmat && coef > FTINY)
358 greg 2.24 raytrans(&fr);
359 gwlarson 2.33 else if (foremat && coef < 1.0-FTINY)
360 greg 2.24 raytrans(&br);
361 schorsch 2.41 }
362 greg 2.12 /* mix perturbations */
363 greg 1.1 for (i = 0; i < 3; i++)
364 greg 2.12 r->pert[i] = coef*fr.pert[i] + (1.0-coef)*br.pert[i];
365     /* mix pattern colors */
366 greg 2.88 scalescolor(fr.pcol, coef);
367     scalescolor(br.pcol, 1.0-coef);
368     copyscolor(r->pcol, fr.pcol);
369     saddscolor(r->pcol, br.pcol);
370 greg 2.24 /* return value tells if material */
371     if (!foremat & !backmat)
372     return(0);
373 greg 2.12 /* mix returned ray values */
374 greg 2.88 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 greg 2.73 r->rmt = mfore > mback ? fr.rmt : br.rmt;
384 greg 2.88 r->rxt = pbright(fr.rcol)-mfore > pbright(br.rcol)-mback ?
385 greg 2.73 fr.rxt : br.rxt;
386 greg 2.24 return(1);
387 greg 1.1 }
388    
389    
390 greg 2.65 double
391 schorsch 2.45 raydist( /* compute (cumulative) ray distance */
392 greg 2.64 const RAY *r,
393     int flags
394 schorsch 2.45 )
395 greg 2.21 {
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 greg 2.65 void
407 greg 2.49 raycontrib( /* compute (cumulative) ray contribution */
408 greg 2.88 SCOLOR rc,
409 greg 2.49 const RAY *r,
410     int flags
411     )
412     {
413 greg 2.77 static int warnedPM = 0;
414 greg 2.96 double re, ge, be;
415     SCOLOR ce;
416 greg 2.77
417 greg 2.88 setscolor(rc, 1., 1., 1.);
418 greg 2.96 re = ge = be = 0.;
419 greg 2.49
420     while (r != NULL && r->crtype&flags) {
421 greg 2.96 /* include this ray coefficient */
422 greg 2.88 smultscolor(rc, r->rcoef);
423 greg 2.96 /* check participating medium */
424 greg 2.95 if (!warnedPM && bright(r->albedo) > FTINY) {
425 greg 2.77 error(WARNING,
426     "ray contribution calculation does not support participating media");
427     warnedPM++;
428     }
429 greg 2.96 /* sum PM extinction */
430     re += r->rot*colval(r->cext,RED);
431     ge += r->rot*colval(r->cext,GRN);
432     be += r->rot*colval(r->cext,BLU);
433     /* descend the tree */
434 greg 2.49 r = r->parent;
435     }
436 greg 2.96 /* cumulative extinction */
437     setscolor(ce, re<=FTINY ? 1. : re>92. ? 0. : exp(-re),
438     ge<=FTINY ? 1. : ge>92. ? 0. : exp(-ge),
439     be<=FTINY ? 1. : be>92. ? 0. : exp(-be));
440     smultscolor(rc, ce);
441 greg 2.49 }
442    
443    
444 greg 2.65 double
445 schorsch 2.45 raynormal( /* compute perturbed normal for ray */
446     FVECT norm,
447 greg 2.64 RAY *r
448 schorsch 2.45 )
449 greg 1.1 {
450     double newdot;
451 greg 2.64 int i;
452 greg 1.1
453     /* The perturbation is added to the surface normal to obtain
454     * the new normal. If the new normal would affect the surface
455     * orientation wrt. the ray, a correction is made. The method is
456     * still fraught with problems since reflected rays and similar
457     * directions calculated from the surface normal may spawn rays behind
458     * the surface. The only solution is to curb textures at high
459 greg 1.9 * incidence (namely, keep DOT(rdir,pert) < Rdot).
460 greg 1.1 */
461    
462     for (i = 0; i < 3; i++)
463     norm[i] = r->ron[i] + r->pert[i];
464    
465     if (normalize(norm) == 0.0) {
466     objerror(r->ro, WARNING, "illegal normal perturbation");
467     VCOPY(norm, r->ron);
468     return(r->rod);
469     }
470     newdot = -DOT(norm, r->rdir);
471 greg 2.92 if ((newdot > 0.0) ^ (r->rod > 0.0)) { /* fix orientation */
472 greg 1.1 for (i = 0; i < 3; i++)
473     norm[i] += 2.0*newdot*r->rdir[i];
474     newdot = -newdot;
475     }
476     return(newdot);
477 greg 1.12 }
478    
479    
480 greg 2.65 void
481 schorsch 2.45 newrayxf( /* get new tranformation matrix for ray */
482     RAY *r
483     )
484 greg 1.12 {
485     static struct xfn {
486     struct xfn *next;
487     FULLXF xf;
488     } xfseed = { &xfseed }, *xflast = &xfseed;
489 greg 2.64 struct xfn *xp;
490     const RAY *rp;
491 greg 1.12
492     /*
493     * Search for transform in circular list that
494     * has no associated ray in the tree.
495     */
496     xp = xflast;
497     for (rp = r->parent; rp != NULL; rp = rp->parent)
498     if (rp->rox == &xp->xf) { /* xp in use */
499     xp = xp->next; /* move to next */
500     if (xp == xflast) { /* need new one */
501 greg 2.64 xp = (struct xfn *)bmalloc(sizeof(struct xfn));
502 greg 1.12 if (xp == NULL)
503     error(SYSTEM,
504     "out of memory in newrayxf");
505     /* insert in list */
506     xp->next = xflast->next;
507     xflast->next = xp;
508     break; /* we're done */
509     }
510     rp = r; /* start check over */
511     }
512     /* got it */
513     r->rox = &xp->xf;
514     xflast = xp;
515 greg 1.1 }
516    
517    
518 greg 2.65 void
519 schorsch 2.45 flipsurface( /* reverse surface orientation */
520 greg 2.64 RAY *r
521 schorsch 2.45 )
522 greg 1.1 {
523     r->rod = -r->rod;
524     r->ron[0] = -r->ron[0];
525     r->ron[1] = -r->ron[1];
526     r->ron[2] = -r->ron[2];
527     r->pert[0] = -r->pert[0];
528     r->pert[1] = -r->pert[1];
529     r->pert[2] = -r->pert[2];
530 greg 2.79 r->rflips++;
531 greg 1.1 }
532    
533    
534 greg 2.83 int
535     rayreject( /* check if candidate hit is worse than current */
536     OBJREC *o,
537     RAY *r,
538 greg 2.87 double t,
539     double rod
540 greg 2.83 )
541     {
542     OBJREC *mnew, *mray;
543    
544     if ((t <= FTINY) | (t > r->rot + FTINY))
545 greg 2.84 return(1);
546 greg 2.83 if (t < r->rot - FTINY) /* is new hit significantly closer? */
547 greg 2.84 return(0);
548     /* coincident point, so decide... */
549 greg 2.83 if (o == r->ro)
550 greg 2.84 return(1); /* shouldn't happen */
551 greg 2.83 if (r->ro == NULL)
552 greg 2.84 return(0); /* ditto */
553 greg 2.85 mnew = findmaterial(o);
554     mray = findmaterial(r->ro); /* check material transparencies */
555     if (mnew == NULL) {
556     if (mray != NULL)
557 greg 2.87 return(1); /* old has material, new does not */
558 greg 2.85 } else if (mray == NULL) {
559 greg 2.87 return(0); /* new has material, old does not */
560 greg 2.93 } else if (istransp(mnew)) {
561     if (!istransp(mray))
562 greg 2.87 return(1); /* new is transparent, old is not */
563 greg 2.93 } else if (istransp(mray)) {
564 greg 2.87 return(0); /* old is transparent, new is not */
565 greg 2.85 }
566 greg 2.87 if (rod <= 0) { /* check which side we hit */
567     if (r->rod > 0)
568     return(1); /* old hit front, new did not */
569     } else if (r->rod <= 0) {
570     return(0); /* new hit front, old did not */
571     }
572     /* earlier modifier definition wins tie */
573 greg 2.84 return (r->ro->omod >= o->omod);
574 greg 2.83 }
575    
576 greg 2.65 void
577 schorsch 2.45 rayhit( /* standard ray hit test */
578     OBJECT *oset,
579     RAY *r
580     )
581 greg 2.36 {
582     OBJREC *o;
583     int i;
584    
585     for (i = oset[0]; i > 0; i--) {
586     o = objptr(oset[i]);
587     if ((*ofun[o->otype].funp)(o, r))
588     r->robj = oset[i];
589     }
590     }
591    
592    
593 greg 2.65 int
594 schorsch 2.45 localhit( /* check for hit in the octree */
595 greg 2.64 RAY *r,
596     CUBE *scene
597 schorsch 2.45 )
598 greg 1.1 {
599 greg 2.3 OBJECT cxset[MAXCSET+1]; /* set of checked objects */
600 greg 1.1 FVECT curpos; /* current cube position */
601 greg 1.11 int sflags; /* sign flags */
602 greg 1.1 double t, dt;
603 greg 2.64 int i;
604 greg 1.1
605 greg 1.21 nrays++; /* increment trace counter */
606 greg 1.11 sflags = 0;
607 greg 1.1 for (i = 0; i < 3; i++) {
608     curpos[i] = r->rorg[i];
609 greg 2.8 if (r->rdir[i] > 1e-7)
610 greg 1.11 sflags |= 1 << i;
611 greg 2.8 else if (r->rdir[i] < -1e-7)
612 greg 1.11 sflags |= 0x10 << i;
613 greg 1.1 }
614 greg 2.61 if (!sflags) {
615     error(WARNING, "zero ray direction in localhit");
616     return(0);
617     }
618 greg 2.17 /* start off assuming nothing hit */
619     if (r->rmax > FTINY) { /* except aft plane if one */
620     r->ro = &Aftplane;
621     r->rot = r->rmax;
622 greg 2.62 VSUM(r->rop, r->rorg, r->rdir, r->rot);
623 greg 2.17 }
624     /* find global cube entrance point */
625 greg 1.1 t = 0.0;
626     if (!incube(scene, curpos)) {
627     /* find distance to entry */
628     for (i = 0; i < 3; i++) {
629     /* plane in our direction */
630 greg 1.11 if (sflags & 1<<i)
631 greg 1.1 dt = scene->cuorg[i];
632 greg 1.11 else if (sflags & 0x10<<i)
633 greg 1.1 dt = scene->cuorg[i] + scene->cusize;
634     else
635     continue;
636     /* distance to the plane */
637     dt = (dt - r->rorg[i])/r->rdir[i];
638     if (dt > t)
639     t = dt; /* farthest face is the one */
640     }
641     t += FTINY; /* fudge to get inside cube */
642 greg 2.17 if (t >= r->rot) /* clipped already */
643     return(0);
644 greg 1.1 /* advance position */
645 greg 2.62 VSUM(curpos, curpos, r->rdir, t);
646 greg 1.1
647     if (!incube(scene, curpos)) /* non-intersecting ray */
648     return(0);
649     }
650 greg 2.3 cxset[0] = 0;
651 greg 2.19 raymove(curpos, cxset, sflags, r, scene);
652 schorsch 2.42 return((r->ro != NULL) & (r->ro != &Aftplane));
653 greg 1.1 }
654    
655    
656     static int
657 schorsch 2.45 raymove( /* check for hit as we move */
658     FVECT pos, /* current position, modified herein */
659     OBJECT *cxs, /* checked objects, modified by checkhit */
660     int dirf, /* direction indicators to speed tests */
661 greg 2.64 RAY *r,
662     CUBE *cu
663 schorsch 2.45 )
664 greg 1.1 {
665     int ax;
666     double dt, t;
667    
668     if (istree(cu->cutree)) { /* recurse on subcubes */
669     CUBE cukid;
670 greg 2.64 int br, sgn;
671 greg 1.1
672     cukid.cusize = cu->cusize * 0.5; /* find subcube */
673     VCOPY(cukid.cuorg, cu->cuorg);
674     br = 0;
675     if (pos[0] >= cukid.cuorg[0]+cukid.cusize) {
676     cukid.cuorg[0] += cukid.cusize;
677     br |= 1;
678     }
679     if (pos[1] >= cukid.cuorg[1]+cukid.cusize) {
680     cukid.cuorg[1] += cukid.cusize;
681     br |= 2;
682     }
683     if (pos[2] >= cukid.cuorg[2]+cukid.cusize) {
684     cukid.cuorg[2] += cukid.cusize;
685     br |= 4;
686     }
687     for ( ; ; ) {
688     cukid.cutree = octkid(cu->cutree, br);
689 greg 2.3 if ((ax = raymove(pos,cxs,dirf,r,&cukid)) == RAYHIT)
690 greg 1.1 return(RAYHIT);
691     sgn = 1 << ax;
692 greg 1.11 if (sgn & dirf) /* positive axis? */
693 greg 1.1 if (sgn & br)
694     return(ax); /* overflow */
695     else {
696     cukid.cuorg[ax] += cukid.cusize;
697     br |= sgn;
698     }
699 greg 1.11 else
700     if (sgn & br) {
701     cukid.cuorg[ax] -= cukid.cusize;
702     br &= ~sgn;
703     } else
704     return(ax); /* underflow */
705 greg 1.1 }
706     /*NOTREACHED*/
707     }
708 greg 2.18 if (isfull(cu->cutree)) {
709     if (checkhit(r, cu, cxs))
710     return(RAYHIT);
711     } else if (r->ro == &Aftplane && incube(cu, r->rop))
712 greg 1.1 return(RAYHIT);
713     /* advance to next cube */
714 greg 1.11 if (dirf&0x11) {
715     dt = dirf&1 ? cu->cuorg[0] + cu->cusize : cu->cuorg[0];
716 greg 1.1 t = (dt - pos[0])/r->rdir[0];
717     ax = 0;
718     } else
719     t = FHUGE;
720 greg 1.11 if (dirf&0x22) {
721     dt = dirf&2 ? cu->cuorg[1] + cu->cusize : cu->cuorg[1];
722 greg 1.1 dt = (dt - pos[1])/r->rdir[1];
723     if (dt < t) {
724     t = dt;
725     ax = 1;
726     }
727     }
728 greg 1.11 if (dirf&0x44) {
729     dt = dirf&4 ? cu->cuorg[2] + cu->cusize : cu->cuorg[2];
730 greg 1.1 dt = (dt - pos[2])/r->rdir[2];
731     if (dt < t) {
732     t = dt;
733     ax = 2;
734     }
735     }
736 greg 2.62 VSUM(pos, pos, r->rdir, t);
737 greg 1.1 return(ax);
738     }
739    
740    
741 greg 2.34 static int
742 schorsch 2.45 checkhit( /* check for hit in full cube */
743 greg 2.64 RAY *r,
744 schorsch 2.45 CUBE *cu,
745     OBJECT *cxs
746     )
747 greg 1.1 {
748     OBJECT oset[MAXSET+1];
749    
750     objset(oset, cu->cutree);
751 greg 2.36 checkset(oset, cxs); /* avoid double-checking */
752    
753     (*r->hitf)(oset, r); /* test for hit in set */
754    
755     if (r->robj == OVOID)
756 greg 1.1 return(0); /* no scores yet */
757    
758     return(incube(cu, r->rop)); /* hit OK if in current cube */
759 greg 2.2 }
760    
761    
762 greg 2.34 static void
763 schorsch 2.45 checkset( /* modify checked set and set to check */
764 greg 2.64 OBJECT *os, /* os' = os - cs */
765     OBJECT *cs /* cs' = cs + os */
766 schorsch 2.45 )
767 greg 2.2 {
768     OBJECT cset[MAXCSET+MAXSET+1];
769 greg 2.64 int i, j;
770 greg 2.3 int k;
771 greg 2.2 /* copy os in place, cset <- cs */
772     cset[0] = 0;
773     k = 0;
774     for (i = j = 1; i <= os[0]; i++) {
775     while (j <= cs[0] && cs[j] < os[i])
776     cset[++cset[0]] = cs[j++];
777     if (j > cs[0] || os[i] != cs[j]) { /* object to check */
778     os[++k] = os[i];
779     cset[++cset[0]] = os[i];
780     }
781     }
782 greg 2.3 if (!(os[0] = k)) /* new "to check" set size */
783     return; /* special case */
784 greg 2.2 while (j <= cs[0]) /* get the rest of cs */
785     cset[++cset[0]] = cs[j++];
786 greg 2.3 if (cset[0] > MAXCSET) /* truncate "checked" set if nec. */
787 greg 2.2 cset[0] = MAXCSET;
788 greg 2.3 /* setcopy(cs, cset); */ /* copy cset back to cs */
789     os = cset;
790     for (i = os[0]; i-- >= 0; )
791     *cs++ = *os++;
792 greg 1.1 }