1 |
< |
/* Copyright (c) 1991 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1994 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 |
|
|
28 |
< |
long raynum = 0L; /* next unique ray number */ |
29 |
< |
long nrays = 0L; /* number of calls to localhit */ |
28 |
> |
unsigned long raynum = 0; /* next unique ray number */ |
29 |
> |
unsigned long nrays = 0; /* number of calls to localhit */ |
30 |
|
|
31 |
|
static FLOAT Lambfa[5] = {PI, PI, PI, 0.0, 0.0}; |
32 |
|
OBJREC Lamb = { |
34 |
|
{0, 5, NULL, Lambfa}, NULL, |
35 |
|
}; /* a Lambertian surface */ |
36 |
|
|
37 |
+ |
static int raymove(), checkset(), checkhit(); |
38 |
+ |
|
39 |
|
#define MAXLOOP 128 /* modifier loop detection */ |
40 |
|
|
41 |
|
#define RAYHIT (-1) /* return value for intercepted ray */ |
77 |
|
register RAY *r; |
78 |
|
{ |
79 |
|
r->rno = raynum++; |
76 |
– |
r->cxs[0] = 0; |
80 |
|
r->newcset = r->clipset; |
81 |
|
r->ro = NULL; |
82 |
|
r->rot = FHUGE; |
91 |
|
RAY *r; |
92 |
|
{ |
93 |
|
extern int (*trace)(); |
94 |
+ |
int gotmat; |
95 |
|
|
96 |
|
if (localhit(r, &thescene)) |
97 |
< |
raycont(r); |
97 |
> |
gotmat = raycont(r); |
98 |
|
else if (sourcehit(r)) |
99 |
< |
rayshade(r, r->ro->omod); |
99 |
> |
gotmat = rayshade(r, r->ro->omod); |
100 |
|
|
101 |
+ |
if (r->ro != NULL && !gotmat) |
102 |
+ |
objerror(r->ro, USER, "material not found"); |
103 |
+ |
|
104 |
|
if (trace != NULL) |
105 |
|
(*trace)(r); /* trace execution */ |
106 |
|
} |
109 |
|
raycont(r) /* check for clipped object and continue */ |
110 |
|
register RAY *r; |
111 |
|
{ |
112 |
< |
if (r->clipset != NULL && inset(r->clipset, r->ro->omod)) |
112 |
> |
if ((r->clipset != NULL && inset(r->clipset, r->ro->omod)) || |
113 |
> |
r->ro->omod == OVOID) { |
114 |
|
raytrans(r); |
115 |
< |
else |
116 |
< |
rayshade(r, r->ro->omod); |
115 |
> |
return(1); |
116 |
> |
} |
117 |
> |
return(rayshade(r, r->ro->omod)); |
118 |
|
} |
119 |
|
|
120 |
|
|
137 |
|
int mod; |
138 |
|
{ |
139 |
|
static int depth = 0; |
140 |
+ |
int gotmat; |
141 |
|
register OBJREC *m; |
142 |
|
/* check for infinite loop */ |
143 |
|
if (depth++ >= MAXLOOP) |
144 |
|
objerror(r->ro, USER, "possible modifier loop"); |
145 |
|
r->rt = r->rot; /* set effective ray length */ |
146 |
< |
for ( ; mod != OVOID; mod = m->omod) { |
146 |
> |
for (gotmat = 0; !gotmat && mod != OVOID; mod = m->omod) { |
147 |
|
m = objptr(mod); |
148 |
|
/****** unnecessary test since modifier() is always called |
149 |
|
if (!ismodifier(m->otype)) { |
161 |
|
if (!islight(m->otype)) |
162 |
|
m = &Lamb; |
163 |
|
} |
164 |
< |
(*ofun[m->otype].funp)(m, r); /* execute function */ |
165 |
< |
if (ismaterial(m->otype)) { /* materials call raytexture */ |
156 |
< |
depth--; |
157 |
< |
return; /* we're done */ |
158 |
< |
} |
164 |
> |
/* materials call raytexture */ |
165 |
> |
gotmat = (*ofun[m->otype].funp)(m, r); |
166 |
|
} |
167 |
< |
objerror(r->ro, USER, "material not found"); |
167 |
> |
depth--; |
168 |
> |
return(gotmat); |
169 |
|
} |
170 |
|
|
171 |
|
|
181 |
|
/* execute textures and patterns */ |
182 |
|
for ( ; mod != OVOID; mod = m->omod) { |
183 |
|
m = objptr(mod); |
184 |
< |
if (!istexture(m->otype)) { |
184 |
> |
/****** unnecessary test since modifier() is always called |
185 |
> |
if (!ismodifier(m->otype)) { |
186 |
|
sprintf(errmsg, "illegal modifier \"%s\"", m->oname); |
187 |
|
error(USER, errmsg); |
188 |
|
} |
189 |
< |
(*ofun[m->otype].funp)(m, r); |
189 |
> |
******/ |
190 |
> |
if ((*ofun[m->otype].funp)(m, r)) |
191 |
> |
objerror(r->ro, USER, "conflicting materials"); |
192 |
|
} |
193 |
|
depth--; /* end here */ |
194 |
|
} |
199 |
|
OBJECT fore, back; |
200 |
|
double coef; |
201 |
|
{ |
202 |
< |
FVECT curpert, forepert, backpert; |
203 |
< |
COLOR curpcol, forepcol, backpcol; |
202 |
> |
RAY fr, br; |
203 |
> |
int foremat, backmat; |
204 |
|
register int i; |
205 |
|
/* clip coefficient */ |
206 |
|
if (coef > 1.0) |
207 |
|
coef = 1.0; |
208 |
|
else if (coef < 0.0) |
209 |
|
coef = 0.0; |
210 |
< |
/* save current mods */ |
211 |
< |
VCOPY(curpert, r->pert); |
212 |
< |
copycolor(curpcol, r->pcol); |
213 |
< |
/* compute new mods */ |
203 |
< |
/* foreground */ |
204 |
< |
r->pert[0] = r->pert[1] = r->pert[2] = 0.0; |
205 |
< |
setcolor(r->pcol, 1.0, 1.0, 1.0); |
210 |
> |
/* compute foreground and background */ |
211 |
> |
foremat = backmat = -1; |
212 |
> |
/* foreground */ |
213 |
> |
copystruct(&fr, r); |
214 |
|
if (fore != OVOID && coef > FTINY) |
215 |
< |
raytexture(r, fore); |
216 |
< |
VCOPY(forepert, r->pert); |
217 |
< |
copycolor(forepcol, r->pcol); |
210 |
< |
/* background */ |
211 |
< |
r->pert[0] = r->pert[1] = r->pert[2] = 0.0; |
212 |
< |
setcolor(r->pcol, 1.0, 1.0, 1.0); |
215 |
> |
foremat = rayshade(&fr, fore); |
216 |
> |
/* background */ |
217 |
> |
copystruct(&br, r); |
218 |
|
if (back != OVOID && coef < 1.0-FTINY) |
219 |
< |
raytexture(r, back); |
220 |
< |
VCOPY(backpert, r->pert); |
221 |
< |
copycolor(backpcol, r->pcol); |
222 |
< |
/* sum perturbations */ |
219 |
> |
backmat = rayshade(&br, back); |
220 |
> |
/* check */ |
221 |
> |
if (foremat < 0) |
222 |
> |
if (backmat < 0) |
223 |
> |
foremat = backmat = 0; |
224 |
> |
else |
225 |
> |
foremat = backmat; |
226 |
> |
else if (backmat < 0) |
227 |
> |
backmat = foremat; |
228 |
> |
if ((foremat==0) != (backmat==0)) |
229 |
> |
objerror(r->ro, USER, "mixing material with non-material"); |
230 |
> |
/* mix perturbations */ |
231 |
|
for (i = 0; i < 3; i++) |
232 |
< |
r->pert[i] = curpert[i] + coef*forepert[i] + |
233 |
< |
(1.0-coef)*backpert[i]; |
234 |
< |
/* multiply colors */ |
235 |
< |
setcolor(r->pcol, coef*colval(forepcol,RED) + |
236 |
< |
(1.0-coef)*colval(backpcol,RED), |
237 |
< |
coef*colval(forepcol,GRN) + |
238 |
< |
(1.0-coef)*colval(backpcol,GRN), |
239 |
< |
coef*colval(forepcol,BLU) + |
240 |
< |
(1.0-coef)*colval(backpcol,BLU)); |
241 |
< |
multcolor(r->pcol, curpcol); |
232 |
> |
r->pert[i] = coef*fr.pert[i] + (1.0-coef)*br.pert[i]; |
233 |
> |
/* mix pattern colors */ |
234 |
> |
scalecolor(fr.pcol, coef); |
235 |
> |
scalecolor(br.pcol, 1.0-coef); |
236 |
> |
copycolor(r->pcol, fr.pcol); |
237 |
> |
addcolor(r->pcol, br.pcol); |
238 |
> |
/* mix returned ray values */ |
239 |
> |
if (foremat) { |
240 |
> |
scalecolor(fr.rcol, coef); |
241 |
> |
scalecolor(br.rcol, 1.0-coef); |
242 |
> |
copycolor(r->rcol, fr.rcol); |
243 |
> |
addcolor(r->rcol, br.rcol); |
244 |
> |
r->rt = bright(fr.rcol) > bright(br.rcol) ? fr.rt : br.rt; |
245 |
> |
} |
246 |
> |
/* return value tells if material */ |
247 |
> |
return(foremat); |
248 |
|
} |
249 |
|
|
250 |
|
|
336 |
|
register RAY *r; |
337 |
|
register CUBE *scene; |
338 |
|
{ |
339 |
+ |
OBJECT cxset[MAXCSET+1]; /* set of checked objects */ |
340 |
|
FVECT curpos; /* current cube position */ |
341 |
|
int sflags; /* sign flags */ |
342 |
|
double t, dt; |
346 |
|
sflags = 0; |
347 |
|
for (i = 0; i < 3; i++) { |
348 |
|
curpos[i] = r->rorg[i]; |
349 |
< |
if (r->rdir[i] > FTINY) |
349 |
> |
if (r->rdir[i] > 1e-7) |
350 |
|
sflags |= 1 << i; |
351 |
< |
else if (r->rdir[i] < -FTINY) |
351 |
> |
else if (r->rdir[i] < -1e-7) |
352 |
|
sflags |= 0x10 << i; |
353 |
|
} |
354 |
|
if (sflags == 0) |
377 |
|
if (!incube(scene, curpos)) /* non-intersecting ray */ |
378 |
|
return(0); |
379 |
|
} |
380 |
< |
return(raymove(curpos, sflags, r, scene) == RAYHIT); |
380 |
> |
cxset[0] = 0; |
381 |
> |
return(raymove(curpos, cxset, sflags, r, scene) == RAYHIT); |
382 |
|
} |
383 |
|
|
384 |
|
|
385 |
|
static int |
386 |
< |
raymove(pos, dirf, r, cu) /* check for hit as we move */ |
387 |
< |
FVECT pos; /* modified */ |
386 |
> |
raymove(pos, cxs, dirf, r, cu) /* check for hit as we move */ |
387 |
> |
FVECT pos; /* current position, modified herein */ |
388 |
> |
OBJECT *cxs; /* checked objects, modified by checkhit */ |
389 |
|
int dirf; /* direction indicators to speed tests */ |
390 |
|
register RAY *r; |
391 |
|
register CUBE *cu; |
414 |
|
} |
415 |
|
for ( ; ; ) { |
416 |
|
cukid.cutree = octkid(cu->cutree, br); |
417 |
< |
if ((ax = raymove(pos,dirf,r,&cukid)) == RAYHIT) |
417 |
> |
if ((ax = raymove(pos,cxs,dirf,r,&cukid)) == RAYHIT) |
418 |
|
return(RAYHIT); |
419 |
|
sgn = 1 << ax; |
420 |
|
if (sgn & dirf) /* positive axis? */ |
433 |
|
} |
434 |
|
/*NOTREACHED*/ |
435 |
|
} |
436 |
< |
if (isfull(cu->cutree) && checkhit(r, cu)) |
436 |
> |
if (isfull(cu->cutree) && checkhit(r, cu, cxs)) |
437 |
|
return(RAYHIT); |
438 |
|
/* advance to next cube */ |
439 |
|
if (dirf&0x11) { |
466 |
|
|
467 |
|
|
468 |
|
static |
469 |
< |
checkhit(r, cu) /* check for hit in full cube */ |
469 |
> |
checkhit(r, cu, cxs) /* check for hit in full cube */ |
470 |
|
register RAY *r; |
471 |
|
CUBE *cu; |
472 |
+ |
OBJECT *cxs; |
473 |
|
{ |
474 |
|
OBJECT oset[MAXSET+1]; |
475 |
|
register OBJREC *o; |
476 |
|
register int i; |
477 |
|
|
478 |
|
objset(oset, cu->cutree); |
479 |
< |
checkset(oset, r->cxs); /* eliminate double-checking */ |
479 |
> |
checkset(oset, cxs); /* eliminate double-checking */ |
480 |
|
for (i = oset[0]; i > 0; i--) { |
481 |
|
o = objptr(oset[i]); |
482 |
|
(*ofun[o->otype].funp)(o, r); |
490 |
|
|
491 |
|
static |
492 |
|
checkset(os, cs) /* modify checked set and set to check */ |
493 |
< |
register OBJECT os[MAXSET+1]; /* os' = os - cs */ |
494 |
< |
register OBJECT cs[MAXCSET+1]; /* cs' = cs + os */ |
493 |
> |
register OBJECT *os; /* os' = os - cs */ |
494 |
> |
register OBJECT *cs; /* cs' = cs + os */ |
495 |
|
{ |
496 |
|
OBJECT cset[MAXCSET+MAXSET+1]; |
497 |
< |
register int i, j, k; |
497 |
> |
register int i, j; |
498 |
> |
int k; |
499 |
|
/* copy os in place, cset <- cs */ |
500 |
|
cset[0] = 0; |
501 |
|
k = 0; |
507 |
|
cset[++cset[0]] = os[i]; |
508 |
|
} |
509 |
|
} |
510 |
+ |
if (!(os[0] = k)) /* new "to check" set size */ |
511 |
+ |
return; /* special case */ |
512 |
|
while (j <= cs[0]) /* get the rest of cs */ |
513 |
|
cset[++cset[0]] = cs[j++]; |
514 |
< |
if (cset[0] > MAXCSET) /* truncate if necessary */ |
514 |
> |
if (cset[0] > MAXCSET) /* truncate "checked" set if nec. */ |
515 |
|
cset[0] = MAXCSET; |
516 |
< |
setcopy(cs, cset); /* copy new "checked" set back */ |
517 |
< |
os[0] = k; /* new "to check" set size */ |
516 |
> |
/* setcopy(cs, cset); */ /* copy cset back to cs */ |
517 |
> |
os = cset; |
518 |
> |
for (i = os[0]; i-- >= 0; ) |
519 |
> |
*cs++ = *os++; |
520 |
|
} |