1 |
< |
/* Copyright (c) 1991 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"; |
34 |
|
{0, 5, NULL, Lambfa}, NULL, |
35 |
|
}; /* a Lambertian surface */ |
36 |
|
|
37 |
+ |
OBJREC Aftplane; /* aft clipping plane object */ |
38 |
+ |
|
39 |
|
static int raymove(), checkset(), checkhit(); |
40 |
|
|
41 |
|
#define MAXLOOP 128 /* modifier loop detection */ |
69 |
|
r->rweight = ro->rweight * rw; |
70 |
|
r->crtype = ro->crtype | (r->rtype = rt); |
71 |
|
VCOPY(r->rorg, ro->rop); |
72 |
+ |
r->rmax = 0.0; |
73 |
|
} |
74 |
|
rayclear(r); |
75 |
|
return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1); |
94 |
|
RAY *r; |
95 |
|
{ |
96 |
|
extern int (*trace)(); |
97 |
+ |
int gotmat; |
98 |
|
|
99 |
|
if (localhit(r, &thescene)) |
100 |
< |
raycont(r); |
101 |
< |
else if (sourcehit(r)) |
102 |
< |
rayshade(r, r->ro->omod); |
100 |
> |
gotmat = raycont(r); |
101 |
> |
else if (r->ro == &Aftplane) { |
102 |
> |
r->ro = NULL; |
103 |
> |
r->rot = FHUGE; |
104 |
> |
} else if (sourcehit(r)) |
105 |
> |
gotmat = rayshade(r, r->ro->omod); |
106 |
|
|
107 |
+ |
if (r->ro != NULL && !gotmat) |
108 |
+ |
objerror(r->ro, USER, "material not found"); |
109 |
+ |
|
110 |
|
if (trace != NULL) |
111 |
|
(*trace)(r); /* trace execution */ |
112 |
|
} |
115 |
|
raycont(r) /* check for clipped object and continue */ |
116 |
|
register RAY *r; |
117 |
|
{ |
118 |
< |
if (r->clipset != NULL && inset(r->clipset, r->ro->omod)) |
118 |
> |
if ((r->clipset != NULL && inset(r->clipset, r->ro->omod)) || |
119 |
> |
r->ro->omod == OVOID) { |
120 |
|
raytrans(r); |
121 |
< |
else |
122 |
< |
rayshade(r, r->ro->omod); |
121 |
> |
return(1); |
122 |
> |
} |
123 |
> |
return(rayshade(r, r->ro->omod)); |
124 |
|
} |
125 |
|
|
126 |
|
|
131 |
|
|
132 |
|
if (rayorigin(&tr, r, TRANS, 1.0) == 0) { |
133 |
|
VCOPY(tr.rdir, r->rdir); |
134 |
+ |
if (r->rmax > FTINY) |
135 |
+ |
tr.rmax = r->rmax - r->rot; |
136 |
|
rayvalue(&tr); |
137 |
|
copycolor(r->rcol, tr.rcol); |
138 |
|
r->rt = r->rot + tr.rt; |
145 |
|
int mod; |
146 |
|
{ |
147 |
|
static int depth = 0; |
148 |
+ |
int gotmat; |
149 |
|
register OBJREC *m; |
150 |
|
/* check for infinite loop */ |
151 |
|
if (depth++ >= MAXLOOP) |
152 |
|
objerror(r->ro, USER, "possible modifier loop"); |
153 |
|
r->rt = r->rot; /* set effective ray length */ |
154 |
< |
for ( ; mod != OVOID; mod = m->omod) { |
154 |
> |
for (gotmat = 0; !gotmat && mod != OVOID; mod = m->omod) { |
155 |
|
m = objptr(mod); |
156 |
|
/****** unnecessary test since modifier() is always called |
157 |
|
if (!ismodifier(m->otype)) { |
164 |
|
if (irr_ignore(m->otype)) { |
165 |
|
depth--; |
166 |
|
raytrans(r); |
167 |
< |
return; |
167 |
> |
return(1); |
168 |
|
} |
169 |
|
if (!islight(m->otype)) |
170 |
|
m = &Lamb; |
171 |
|
} |
172 |
< |
(*ofun[m->otype].funp)(m, r); /* execute function */ |
173 |
< |
if (ismaterial(m->otype)) { /* materials call raytexture */ |
159 |
< |
depth--; |
160 |
< |
return; /* we're done */ |
161 |
< |
} |
172 |
> |
/* materials call raytexture */ |
173 |
> |
gotmat = (*ofun[m->otype].funp)(m, r); |
174 |
|
} |
175 |
< |
objerror(r->ro, USER, "material not found"); |
175 |
> |
depth--; |
176 |
> |
return(gotmat); |
177 |
|
} |
178 |
|
|
179 |
|
|
189 |
|
/* execute textures and patterns */ |
190 |
|
for ( ; mod != OVOID; mod = m->omod) { |
191 |
|
m = objptr(mod); |
192 |
< |
if (!istexture(m->otype)) { |
192 |
> |
/****** unnecessary test since modifier() is always called |
193 |
> |
if (!ismodifier(m->otype)) { |
194 |
|
sprintf(errmsg, "illegal modifier \"%s\"", m->oname); |
195 |
|
error(USER, errmsg); |
196 |
|
} |
197 |
< |
(*ofun[m->otype].funp)(m, r); |
197 |
> |
******/ |
198 |
> |
if ((*ofun[m->otype].funp)(m, r)) { |
199 |
> |
sprintf(errmsg, "conflicting material \"%s\"", |
200 |
> |
m->oname); |
201 |
> |
objerror(r->ro, USER, errmsg); |
202 |
> |
} |
203 |
|
} |
204 |
|
depth--; /* end here */ |
205 |
|
} |
210 |
|
OBJECT fore, back; |
211 |
|
double coef; |
212 |
|
{ |
213 |
< |
FVECT curpert, forepert, backpert; |
214 |
< |
COLOR curpcol, forepcol, backpcol; |
213 |
> |
RAY fr, br; |
214 |
> |
int foremat, backmat; |
215 |
|
register int i; |
216 |
|
/* clip coefficient */ |
217 |
|
if (coef > 1.0) |
218 |
|
coef = 1.0; |
219 |
|
else if (coef < 0.0) |
220 |
|
coef = 0.0; |
221 |
< |
/* save current mods */ |
222 |
< |
VCOPY(curpert, r->pert); |
223 |
< |
copycolor(curpcol, r->pcol); |
224 |
< |
/* compute new mods */ |
206 |
< |
/* foreground */ |
207 |
< |
r->pert[0] = r->pert[1] = r->pert[2] = 0.0; |
208 |
< |
setcolor(r->pcol, 1.0, 1.0, 1.0); |
221 |
> |
/* compute foreground and background */ |
222 |
> |
foremat = backmat = -1; |
223 |
> |
/* foreground */ |
224 |
> |
copystruct(&fr, r); |
225 |
|
if (fore != OVOID && coef > FTINY) |
226 |
< |
raytexture(r, fore); |
227 |
< |
VCOPY(forepert, r->pert); |
228 |
< |
copycolor(forepcol, r->pcol); |
213 |
< |
/* background */ |
214 |
< |
r->pert[0] = r->pert[1] = r->pert[2] = 0.0; |
215 |
< |
setcolor(r->pcol, 1.0, 1.0, 1.0); |
226 |
> |
foremat = rayshade(&fr, fore); |
227 |
> |
/* background */ |
228 |
> |
copystruct(&br, r); |
229 |
|
if (back != OVOID && coef < 1.0-FTINY) |
230 |
< |
raytexture(r, back); |
231 |
< |
VCOPY(backpert, r->pert); |
232 |
< |
copycolor(backpcol, r->pcol); |
233 |
< |
/* sum perturbations */ |
230 |
> |
backmat = rayshade(&br, back); |
231 |
> |
/* check */ |
232 |
> |
if (foremat < 0) |
233 |
> |
if (backmat < 0) |
234 |
> |
foremat = backmat = 0; |
235 |
> |
else |
236 |
> |
foremat = backmat; |
237 |
> |
else if (backmat < 0) |
238 |
> |
backmat = foremat; |
239 |
> |
if ((foremat==0) != (backmat==0)) |
240 |
> |
objerror(r->ro, USER, "mixing material with non-material"); |
241 |
> |
/* mix perturbations */ |
242 |
|
for (i = 0; i < 3; i++) |
243 |
< |
r->pert[i] = curpert[i] + coef*forepert[i] + |
244 |
< |
(1.0-coef)*backpert[i]; |
245 |
< |
/* multiply colors */ |
246 |
< |
setcolor(r->pcol, coef*colval(forepcol,RED) + |
247 |
< |
(1.0-coef)*colval(backpcol,RED), |
248 |
< |
coef*colval(forepcol,GRN) + |
249 |
< |
(1.0-coef)*colval(backpcol,GRN), |
250 |
< |
coef*colval(forepcol,BLU) + |
251 |
< |
(1.0-coef)*colval(backpcol,BLU)); |
252 |
< |
multcolor(r->pcol, curpcol); |
243 |
> |
r->pert[i] = coef*fr.pert[i] + (1.0-coef)*br.pert[i]; |
244 |
> |
/* mix pattern colors */ |
245 |
> |
scalecolor(fr.pcol, coef); |
246 |
> |
scalecolor(br.pcol, 1.0-coef); |
247 |
> |
copycolor(r->pcol, fr.pcol); |
248 |
> |
addcolor(r->pcol, br.pcol); |
249 |
> |
/* mix returned ray values */ |
250 |
> |
if (foremat) { |
251 |
> |
scalecolor(fr.rcol, coef); |
252 |
> |
scalecolor(br.rcol, 1.0-coef); |
253 |
> |
copycolor(r->rcol, fr.rcol); |
254 |
> |
addcolor(r->rcol, br.rcol); |
255 |
> |
r->rt = bright(fr.rcol) > bright(br.rcol) ? fr.rt : br.rt; |
256 |
> |
} |
257 |
> |
/* return value tells if material */ |
258 |
> |
return(foremat); |
259 |
|
} |
260 |
|
|
261 |
|
|
262 |
|
double |
263 |
+ |
raydist(r, flags) /* compute (cumulative) ray distance */ |
264 |
+ |
register RAY *r; |
265 |
+ |
register int flags; |
266 |
+ |
{ |
267 |
+ |
double sum = 0.0; |
268 |
+ |
|
269 |
+ |
while (r != NULL && r->crtype&flags) { |
270 |
+ |
sum += r->rot; |
271 |
+ |
r = r->parent; |
272 |
+ |
} |
273 |
+ |
return(sum); |
274 |
+ |
} |
275 |
+ |
|
276 |
+ |
|
277 |
+ |
double |
278 |
|
raynormal(norm, r) /* compute perturbed normal for ray */ |
279 |
|
FVECT norm; |
280 |
|
register RAY *r; |
372 |
|
sflags = 0; |
373 |
|
for (i = 0; i < 3; i++) { |
374 |
|
curpos[i] = r->rorg[i]; |
375 |
< |
if (r->rdir[i] > FTINY) |
375 |
> |
if (r->rdir[i] > 1e-7) |
376 |
|
sflags |= 1 << i; |
377 |
< |
else if (r->rdir[i] < -FTINY) |
377 |
> |
else if (r->rdir[i] < -1e-7) |
378 |
|
sflags |= 0x10 << i; |
379 |
|
} |
380 |
|
if (sflags == 0) |
381 |
|
error(CONSISTENCY, "zero ray direction in localhit"); |
382 |
+ |
/* start off assuming nothing hit */ |
383 |
+ |
if (r->rmax > FTINY) { /* except aft plane if one */ |
384 |
+ |
r->ro = &Aftplane; |
385 |
+ |
r->rot = r->rmax; |
386 |
+ |
for (i = 0; i < 3; i++) |
387 |
+ |
r->rop[i] = r->rorg[i] + r->rot*r->rdir[i]; |
388 |
+ |
} |
389 |
+ |
/* find global cube entrance point */ |
390 |
|
t = 0.0; |
391 |
|
if (!incube(scene, curpos)) { |
392 |
|
/* find distance to entry */ |
404 |
|
t = dt; /* farthest face is the one */ |
405 |
|
} |
406 |
|
t += FTINY; /* fudge to get inside cube */ |
407 |
+ |
if (t >= r->rot) /* clipped already */ |
408 |
+ |
return(0); |
409 |
|
/* advance position */ |
410 |
|
for (i = 0; i < 3; i++) |
411 |
|
curpos[i] += r->rdir[i]*t; |
414 |
|
return(0); |
415 |
|
} |
416 |
|
cxset[0] = 0; |
417 |
< |
return(raymove(curpos, cxset, sflags, r, scene) == RAYHIT); |
417 |
> |
raymove(curpos, cxset, sflags, r, scene); |
418 |
> |
return(r->ro != NULL & r->ro != &Aftplane); |
419 |
|
} |
420 |
|
|
421 |
|
|
470 |
|
} |
471 |
|
/*NOTREACHED*/ |
472 |
|
} |
473 |
< |
if (isfull(cu->cutree) && checkhit(r, cu, cxs)) |
473 |
> |
if (isfull(cu->cutree)) { |
474 |
> |
if (checkhit(r, cu, cxs)) |
475 |
> |
return(RAYHIT); |
476 |
> |
} else if (r->ro == &Aftplane && incube(cu, r->rop)) |
477 |
|
return(RAYHIT); |
478 |
|
/* advance to next cube */ |
479 |
|
if (dirf&0x11) { |
519 |
|
checkset(oset, cxs); /* eliminate double-checking */ |
520 |
|
for (i = oset[0]; i > 0; i--) { |
521 |
|
o = objptr(oset[i]); |
466 |
– |
if (o->omod == OVOID && issurface(o->otype)) |
467 |
– |
continue; /* ignore void surfaces */ |
522 |
|
(*ofun[o->otype].funp)(o, r); |
523 |
|
} |
524 |
|
if (r->ro == NULL) |