1 |
< |
/* Copyright (c) 1990 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1995 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
18 |
|
|
19 |
|
#include "otspecial.h" |
20 |
|
|
21 |
+ |
#define MAXCSET ((MAXSET+1)*2-1) /* maximum check set size */ |
22 |
+ |
|
23 |
|
extern CUBE thescene; /* our scene */ |
24 |
|
extern int maxdepth; /* maximum recursion depth */ |
25 |
|
extern double minweight; /* minimum ray weight */ |
26 |
|
extern int do_irrad; /* compute irradiance? */ |
27 |
+ |
extern COLOR ambval; /* ambient value */ |
28 |
|
|
29 |
< |
long nrays = 0L; /* number of rays traced */ |
29 |
> |
extern COLOR cextinction; /* global extinction coefficient */ |
30 |
> |
extern double salbedo; /* global scattering albedo */ |
31 |
> |
extern double seccg; /* global scattering eccentricity */ |
32 |
> |
extern double ssampdist; /* scatter sampling distance */ |
33 |
|
|
34 |
< |
static double Lambfa[5] = {PI, PI, PI, 0.0, 0.0}; |
34 |
> |
unsigned long raynum = 0; /* next unique ray number */ |
35 |
> |
unsigned long nrays = 0; /* number of calls to localhit */ |
36 |
> |
|
37 |
> |
static FLOAT Lambfa[5] = {PI, PI, PI, 0.0, 0.0}; |
38 |
|
OBJREC Lamb = { |
39 |
|
OVOID, MAT_PLASTIC, "Lambertian", |
40 |
< |
{0, 5, NULL, Lambfa}, NULL, -1, |
40 |
> |
{0, 5, NULL, Lambfa}, NULL, |
41 |
|
}; /* a Lambertian surface */ |
42 |
|
|
43 |
+ |
OBJREC Aftplane; /* aft clipping plane object */ |
44 |
+ |
|
45 |
+ |
static int raymove(), checkset(), checkhit(); |
46 |
+ |
|
47 |
|
#define MAXLOOP 128 /* modifier loop detection */ |
48 |
|
|
49 |
|
#define RAYHIT (-1) /* return value for intercepted ray */ |
60 |
|
r->crtype = r->rtype = rt; |
61 |
|
r->rsrc = -1; |
62 |
|
r->clipset = NULL; |
63 |
+ |
r->revf = raytrace; |
64 |
+ |
copycolor(r->cext, cextinction); |
65 |
+ |
r->albedo = salbedo; |
66 |
+ |
r->gecc = seccg; |
67 |
+ |
r->slights = NULL; |
68 |
|
} else { /* spawned ray */ |
69 |
|
r->rlvl = ro->rlvl; |
70 |
|
if (rt & RAYREFL) { |
71 |
|
r->rlvl++; |
72 |
|
r->rsrc = -1; |
73 |
|
r->clipset = ro->clipset; |
74 |
+ |
r->rmax = 0.0; |
75 |
|
} else { |
76 |
|
r->rsrc = ro->rsrc; |
77 |
|
r->clipset = ro->newcset; |
78 |
+ |
r->rmax = ro->rmax <= FTINY ? 0.0 : ro->rmax - ro->rot; |
79 |
|
} |
80 |
+ |
r->revf = ro->revf; |
81 |
+ |
copycolor(r->cext, ro->cext); |
82 |
+ |
r->albedo = ro->albedo; |
83 |
+ |
r->gecc = ro->gecc; |
84 |
+ |
r->slights = ro->slights; |
85 |
|
r->rweight = ro->rweight * rw; |
86 |
|
r->crtype = ro->crtype | (r->rtype = rt); |
87 |
|
VCOPY(r->rorg, ro->rop); |
88 |
|
} |
89 |
< |
r->rno = nrays; |
89 |
> |
rayclear(r); |
90 |
> |
return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1); |
91 |
> |
} |
92 |
> |
|
93 |
> |
|
94 |
> |
rayclear(r) /* clear a ray for (re)evaluation */ |
95 |
> |
register RAY *r; |
96 |
> |
{ |
97 |
> |
r->rno = raynum++; |
98 |
|
r->newcset = r->clipset; |
99 |
|
r->ro = NULL; |
100 |
|
r->rot = FHUGE; |
102 |
|
setcolor(r->pcol, 1.0, 1.0, 1.0); |
103 |
|
setcolor(r->rcol, 0.0, 0.0, 0.0); |
104 |
|
r->rt = 0.0; |
72 |
– |
return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1); |
105 |
|
} |
106 |
|
|
107 |
|
|
108 |
< |
rayvalue(r) /* compute a ray's value */ |
108 |
> |
raytrace(r) /* trace a ray and compute its value */ |
109 |
|
RAY *r; |
110 |
|
{ |
111 |
|
extern int (*trace)(); |
112 |
|
|
113 |
|
if (localhit(r, &thescene)) |
114 |
< |
raycont(r); |
115 |
< |
else if (sourcehit(r)) |
116 |
< |
rayshade(r, r->ro->omod); |
114 |
> |
raycont(r); /* hit local surface, evaluate */ |
115 |
> |
else if (r->ro == &Aftplane) { |
116 |
> |
r->ro = NULL; /* hit aft clipping plane */ |
117 |
> |
r->rot = FHUGE; |
118 |
> |
} else if (sourcehit(r)) |
119 |
> |
rayshade(r, r->ro->omod); /* distant source */ |
120 |
|
|
121 |
+ |
rayparticipate(r); /* for participating medium */ |
122 |
+ |
|
123 |
|
if (trace != NULL) |
124 |
|
(*trace)(r); /* trace execution */ |
125 |
|
} |
128 |
|
raycont(r) /* check for clipped object and continue */ |
129 |
|
register RAY *r; |
130 |
|
{ |
131 |
< |
if (r->clipset != NULL && inset(r->clipset, r->ro->omod)) |
131 |
> |
if ((r->clipset != NULL && inset(r->clipset, r->ro->omod)) || |
132 |
> |
!rayshade(r, r->ro->omod)) |
133 |
|
raytrans(r); |
96 |
– |
else |
97 |
– |
rayshade(r, r->ro->omod); |
134 |
|
} |
135 |
|
|
136 |
|
|
153 |
|
int mod; |
154 |
|
{ |
155 |
|
static int depth = 0; |
156 |
+ |
int gotmat; |
157 |
|
register OBJREC *m; |
158 |
|
/* check for infinite loop */ |
159 |
|
if (depth++ >= MAXLOOP) |
160 |
|
objerror(r->ro, USER, "possible modifier loop"); |
161 |
< |
for ( ; mod != OVOID; mod = m->omod) { |
161 |
> |
r->rt = r->rot; /* set effective ray length */ |
162 |
> |
for (gotmat = 0; !gotmat && mod != OVOID; mod = m->omod) { |
163 |
|
m = objptr(mod); |
164 |
|
/****** unnecessary test since modifier() is always called |
165 |
|
if (!ismodifier(m->otype)) { |
172 |
|
if (irr_ignore(m->otype)) { |
173 |
|
depth--; |
174 |
|
raytrans(r); |
175 |
< |
return; |
175 |
> |
return(1); |
176 |
|
} |
177 |
< |
if (m->otype != MAT_ILLUM) |
177 |
> |
if (!islight(m->otype)) |
178 |
|
m = &Lamb; |
179 |
|
} |
180 |
< |
(*ofun[m->otype].funp)(m, r); /* execute function */ |
181 |
< |
m->lastrno = r->rno; |
144 |
< |
if (ismaterial(m->otype)) { /* materials call raytexture */ |
145 |
< |
depth--; |
146 |
< |
return; /* we're done */ |
147 |
< |
} |
180 |
> |
/* materials call raytexture */ |
181 |
> |
gotmat = (*ofun[m->otype].funp)(m, r); |
182 |
|
} |
183 |
< |
objerror(r->ro, USER, "material not found"); |
183 |
> |
depth--; |
184 |
> |
return(gotmat); |
185 |
|
} |
186 |
|
|
187 |
|
|
188 |
+ |
rayparticipate(r) /* compute ray medium participation */ |
189 |
+ |
register RAY *r; |
190 |
+ |
{ |
191 |
+ |
COLOR ce, ca; |
192 |
+ |
double dist; |
193 |
+ |
double re, ge, be; |
194 |
+ |
|
195 |
+ |
if (intens(r->cext) <= 1./FHUGE) |
196 |
+ |
return; /* no medium */ |
197 |
+ |
if ((dist = r->rot) >= FHUGE) |
198 |
+ |
dist = 2.*thescene.cusize; /* what to use for infinity? */ |
199 |
+ |
if (r->crtype & SHADOW) |
200 |
+ |
dist *= 1. - salbedo; /* no scattering for sources */ |
201 |
+ |
if (dist <= FTINY) |
202 |
+ |
return; /* no effective ray travel */ |
203 |
+ |
re = dist*colval(r->cext,RED); |
204 |
+ |
ge = dist*colval(r->cext,GRN); |
205 |
+ |
be = dist*colval(r->cext,BLU); |
206 |
+ |
setcolor(ce, re>92. ? 0. : exp(-re), |
207 |
+ |
ge>92. ? 0. : exp(-ge), |
208 |
+ |
be>92. ? 0. : exp(-be)); |
209 |
+ |
multcolor(r->rcol, ce); /* path absorption */ |
210 |
+ |
if (r->albedo <= FTINY || r->crtype & SHADOW) |
211 |
+ |
return; /* no scattering */ |
212 |
+ |
setcolor(ca, salbedo*colval(ambval,RED)*(1.-colval(ce,RED)), |
213 |
+ |
salbedo*colval(ambval,GRN)*(1.-colval(ce,GRN)), |
214 |
+ |
salbedo*colval(ambval,BLU)*(1.-colval(ce,BLU))); |
215 |
+ |
addcolor(r->rcol, ca); /* ambient in scattering */ |
216 |
+ |
srcscatter(r); /* source in scattering */ |
217 |
+ |
} |
218 |
+ |
|
219 |
+ |
|
220 |
|
raytexture(r, mod) /* get material modifiers */ |
221 |
|
RAY *r; |
222 |
|
int mod; |
229 |
|
/* execute textures and patterns */ |
230 |
|
for ( ; mod != OVOID; mod = m->omod) { |
231 |
|
m = objptr(mod); |
232 |
< |
if (!istexture(m->otype)) { |
232 |
> |
/****** unnecessary test since modifier() is always called |
233 |
> |
if (!ismodifier(m->otype)) { |
234 |
|
sprintf(errmsg, "illegal modifier \"%s\"", m->oname); |
235 |
|
error(USER, errmsg); |
236 |
|
} |
237 |
< |
(*ofun[m->otype].funp)(m, r); |
238 |
< |
m->lastrno = r->rno; |
237 |
> |
******/ |
238 |
> |
if ((*ofun[m->otype].funp)(m, r)) { |
239 |
> |
sprintf(errmsg, "conflicting material \"%s\"", |
240 |
> |
m->oname); |
241 |
> |
objerror(r->ro, USER, errmsg); |
242 |
> |
} |
243 |
|
} |
244 |
|
depth--; /* end here */ |
245 |
|
} |
250 |
|
OBJECT fore, back; |
251 |
|
double coef; |
252 |
|
{ |
253 |
< |
FVECT curpert, forepert, backpert; |
254 |
< |
COLOR curpcol, forepcol, backpcol; |
253 |
> |
RAY fr, br; |
254 |
> |
int foremat, backmat; |
255 |
|
register int i; |
256 |
< |
/* clip coefficient */ |
256 |
> |
/* bound coefficient */ |
257 |
|
if (coef > 1.0) |
258 |
|
coef = 1.0; |
259 |
|
else if (coef < 0.0) |
260 |
|
coef = 0.0; |
261 |
< |
/* save current mods */ |
262 |
< |
VCOPY(curpert, r->pert); |
263 |
< |
copycolor(curpcol, r->pcol); |
264 |
< |
/* compute new mods */ |
265 |
< |
/* foreground */ |
266 |
< |
r->pert[0] = r->pert[1] = r->pert[2] = 0.0; |
267 |
< |
setcolor(r->pcol, 1.0, 1.0, 1.0); |
268 |
< |
if (fore != OVOID && coef > FTINY) |
269 |
< |
raytexture(r, fore); |
270 |
< |
VCOPY(forepert, r->pert); |
271 |
< |
copycolor(forepcol, r->pcol); |
272 |
< |
/* background */ |
273 |
< |
r->pert[0] = r->pert[1] = r->pert[2] = 0.0; |
274 |
< |
setcolor(r->pcol, 1.0, 1.0, 1.0); |
275 |
< |
if (back != OVOID && coef < 1.0-FTINY) |
276 |
< |
raytexture(r, back); |
277 |
< |
VCOPY(backpert, r->pert); |
206 |
< |
copycolor(backpcol, r->pcol); |
207 |
< |
/* sum perturbations */ |
261 |
> |
/* compute foreground and background */ |
262 |
> |
foremat = backmat = 0; |
263 |
> |
/* foreground */ |
264 |
> |
copystruct(&fr, r); |
265 |
> |
if (coef > FTINY) |
266 |
> |
foremat = rayshade(&fr, fore); |
267 |
> |
/* background */ |
268 |
> |
copystruct(&br, r); |
269 |
> |
if (coef < 1.0-FTINY) |
270 |
> |
backmat = rayshade(&br, back); |
271 |
> |
/* check for transparency */ |
272 |
> |
if (backmat ^ foremat) |
273 |
> |
if (backmat) |
274 |
> |
raytrans(&fr); |
275 |
> |
else |
276 |
> |
raytrans(&br); |
277 |
> |
/* mix perturbations */ |
278 |
|
for (i = 0; i < 3; i++) |
279 |
< |
r->pert[i] = curpert[i] + coef*forepert[i] + |
280 |
< |
(1.0-coef)*backpert[i]; |
281 |
< |
/* multiply colors */ |
282 |
< |
setcolor(r->pcol, coef*colval(forepcol,RED) + |
283 |
< |
(1.0-coef)*colval(backpcol,RED), |
284 |
< |
coef*colval(forepcol,GRN) + |
285 |
< |
(1.0-coef)*colval(backpcol,GRN), |
286 |
< |
coef*colval(forepcol,BLU) + |
287 |
< |
(1.0-coef)*colval(backpcol,BLU)); |
288 |
< |
multcolor(r->pcol, curpcol); |
279 |
> |
r->pert[i] = coef*fr.pert[i] + (1.0-coef)*br.pert[i]; |
280 |
> |
/* mix pattern colors */ |
281 |
> |
scalecolor(fr.pcol, coef); |
282 |
> |
scalecolor(br.pcol, 1.0-coef); |
283 |
> |
copycolor(r->pcol, fr.pcol); |
284 |
> |
addcolor(r->pcol, br.pcol); |
285 |
> |
/* return value tells if material */ |
286 |
> |
if (!foremat & !backmat) |
287 |
> |
return(0); |
288 |
> |
/* mix returned ray values */ |
289 |
> |
scalecolor(fr.rcol, coef); |
290 |
> |
scalecolor(br.rcol, 1.0-coef); |
291 |
> |
copycolor(r->rcol, fr.rcol); |
292 |
> |
addcolor(r->rcol, br.rcol); |
293 |
> |
r->rt = bright(fr.rcol) > bright(br.rcol) ? fr.rt : br.rt; |
294 |
> |
return(1); |
295 |
|
} |
296 |
|
|
297 |
|
|
298 |
|
double |
299 |
+ |
raydist(r, flags) /* compute (cumulative) ray distance */ |
300 |
+ |
register RAY *r; |
301 |
+ |
register int flags; |
302 |
+ |
{ |
303 |
+ |
double sum = 0.0; |
304 |
+ |
|
305 |
+ |
while (r != NULL && r->crtype&flags) { |
306 |
+ |
sum += r->rot; |
307 |
+ |
r = r->parent; |
308 |
+ |
} |
309 |
+ |
return(sum); |
310 |
+ |
} |
311 |
+ |
|
312 |
+ |
|
313 |
+ |
double |
314 |
|
raynormal(norm, r) /* compute perturbed normal for ray */ |
315 |
|
FVECT norm; |
316 |
|
register RAY *r; |
398 |
|
register RAY *r; |
399 |
|
register CUBE *scene; |
400 |
|
{ |
401 |
+ |
OBJECT cxset[MAXCSET+1]; /* set of checked objects */ |
402 |
|
FVECT curpos; /* current cube position */ |
403 |
|
int sflags; /* sign flags */ |
404 |
|
double t, dt; |
405 |
|
register int i; |
406 |
|
|
407 |
|
nrays++; /* increment trace counter */ |
316 |
– |
|
408 |
|
sflags = 0; |
409 |
|
for (i = 0; i < 3; i++) { |
410 |
|
curpos[i] = r->rorg[i]; |
411 |
< |
if (r->rdir[i] > FTINY) |
411 |
> |
if (r->rdir[i] > 1e-7) |
412 |
|
sflags |= 1 << i; |
413 |
< |
else if (r->rdir[i] < -FTINY) |
413 |
> |
else if (r->rdir[i] < -1e-7) |
414 |
|
sflags |= 0x10 << i; |
415 |
|
} |
416 |
+ |
if (sflags == 0) |
417 |
+ |
error(CONSISTENCY, "zero ray direction in localhit"); |
418 |
+ |
/* start off assuming nothing hit */ |
419 |
+ |
if (r->rmax > FTINY) { /* except aft plane if one */ |
420 |
+ |
r->ro = &Aftplane; |
421 |
+ |
r->rot = r->rmax; |
422 |
+ |
for (i = 0; i < 3; i++) |
423 |
+ |
r->rop[i] = r->rorg[i] + r->rot*r->rdir[i]; |
424 |
+ |
} |
425 |
+ |
/* find global cube entrance point */ |
426 |
|
t = 0.0; |
427 |
|
if (!incube(scene, curpos)) { |
428 |
|
/* find distance to entry */ |
440 |
|
t = dt; /* farthest face is the one */ |
441 |
|
} |
442 |
|
t += FTINY; /* fudge to get inside cube */ |
443 |
+ |
if (t >= r->rot) /* clipped already */ |
444 |
+ |
return(0); |
445 |
|
/* advance position */ |
446 |
|
for (i = 0; i < 3; i++) |
447 |
|
curpos[i] += r->rdir[i]*t; |
449 |
|
if (!incube(scene, curpos)) /* non-intersecting ray */ |
450 |
|
return(0); |
451 |
|
} |
452 |
< |
return(raymove(curpos, sflags, r, scene) == RAYHIT); |
452 |
> |
cxset[0] = 0; |
453 |
> |
raymove(curpos, cxset, sflags, r, scene); |
454 |
> |
return(r->ro != NULL & r->ro != &Aftplane); |
455 |
|
} |
456 |
|
|
457 |
|
|
458 |
|
static int |
459 |
< |
raymove(pos, dirf, r, cu) /* check for hit as we move */ |
460 |
< |
FVECT pos; /* modified */ |
459 |
> |
raymove(pos, cxs, dirf, r, cu) /* check for hit as we move */ |
460 |
> |
FVECT pos; /* current position, modified herein */ |
461 |
> |
OBJECT *cxs; /* checked objects, modified by checkhit */ |
462 |
|
int dirf; /* direction indicators to speed tests */ |
463 |
|
register RAY *r; |
464 |
|
register CUBE *cu; |
487 |
|
} |
488 |
|
for ( ; ; ) { |
489 |
|
cukid.cutree = octkid(cu->cutree, br); |
490 |
< |
if ((ax = raymove(pos,dirf,r,&cukid)) == RAYHIT) |
490 |
> |
if ((ax = raymove(pos,cxs,dirf,r,&cukid)) == RAYHIT) |
491 |
|
return(RAYHIT); |
492 |
|
sgn = 1 << ax; |
493 |
|
if (sgn & dirf) /* positive axis? */ |
506 |
|
} |
507 |
|
/*NOTREACHED*/ |
508 |
|
} |
509 |
< |
if (isfull(cu->cutree) && checkhit(r, cu)) |
509 |
> |
if (isfull(cu->cutree)) { |
510 |
> |
if (checkhit(r, cu, cxs)) |
511 |
> |
return(RAYHIT); |
512 |
> |
} else if (r->ro == &Aftplane && incube(cu, r->rop)) |
513 |
|
return(RAYHIT); |
514 |
|
/* advance to next cube */ |
515 |
|
if (dirf&0x11) { |
542 |
|
|
543 |
|
|
544 |
|
static |
545 |
< |
checkhit(r, cu) /* check for hit in full cube */ |
545 |
> |
checkhit(r, cu, cxs) /* check for hit in full cube */ |
546 |
|
register RAY *r; |
547 |
|
CUBE *cu; |
548 |
+ |
OBJECT *cxs; |
549 |
|
{ |
550 |
|
OBJECT oset[MAXSET+1]; |
551 |
|
register OBJREC *o; |
552 |
|
register int i; |
553 |
|
|
554 |
|
objset(oset, cu->cutree); |
555 |
+ |
checkset(oset, cxs); /* eliminate double-checking */ |
556 |
|
for (i = oset[0]; i > 0; i--) { |
557 |
|
o = objptr(oset[i]); |
447 |
– |
if (o->lastrno == r->rno) /* checked already? */ |
448 |
– |
continue; |
558 |
|
(*ofun[o->otype].funp)(o, r); |
450 |
– |
o->lastrno = r->rno; |
559 |
|
} |
560 |
|
if (r->ro == NULL) |
561 |
|
return(0); /* no scores yet */ |
562 |
|
|
563 |
|
return(incube(cu, r->rop)); /* hit OK if in current cube */ |
564 |
+ |
} |
565 |
+ |
|
566 |
+ |
|
567 |
+ |
static |
568 |
+ |
checkset(os, cs) /* modify checked set and set to check */ |
569 |
+ |
register OBJECT *os; /* os' = os - cs */ |
570 |
+ |
register OBJECT *cs; /* cs' = cs + os */ |
571 |
+ |
{ |
572 |
+ |
OBJECT cset[MAXCSET+MAXSET+1]; |
573 |
+ |
register int i, j; |
574 |
+ |
int k; |
575 |
+ |
/* copy os in place, cset <- cs */ |
576 |
+ |
cset[0] = 0; |
577 |
+ |
k = 0; |
578 |
+ |
for (i = j = 1; i <= os[0]; i++) { |
579 |
+ |
while (j <= cs[0] && cs[j] < os[i]) |
580 |
+ |
cset[++cset[0]] = cs[j++]; |
581 |
+ |
if (j > cs[0] || os[i] != cs[j]) { /* object to check */ |
582 |
+ |
os[++k] = os[i]; |
583 |
+ |
cset[++cset[0]] = os[i]; |
584 |
+ |
} |
585 |
+ |
} |
586 |
+ |
if (!(os[0] = k)) /* new "to check" set size */ |
587 |
+ |
return; /* special case */ |
588 |
+ |
while (j <= cs[0]) /* get the rest of cs */ |
589 |
+ |
cset[++cset[0]] = cs[j++]; |
590 |
+ |
if (cset[0] > MAXCSET) /* truncate "checked" set if nec. */ |
591 |
+ |
cset[0] = MAXCSET; |
592 |
+ |
/* setcopy(cs, cset); */ /* copy cset back to cs */ |
593 |
+ |
os = cset; |
594 |
+ |
for (i = os[0]; i-- >= 0; ) |
595 |
+ |
*cs++ = *os++; |
596 |
|
} |