1 |
/* Copyright (c) 1986 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* raytrace.c - routines for tracing and shading rays. |
9 |
* |
10 |
* 8/7/85 |
11 |
*/ |
12 |
|
13 |
#include "ray.h" |
14 |
|
15 |
#include "octree.h" |
16 |
|
17 |
#include "otypes.h" |
18 |
|
19 |
extern CUBE thescene; /* our scene */ |
20 |
extern int maxdepth; /* maximum recursion depth */ |
21 |
extern double minweight; /* minimum ray weight */ |
22 |
|
23 |
long nrays = 0L; /* number of rays traced */ |
24 |
|
25 |
#define MAXLOOP 32 /* modifier loop detection */ |
26 |
|
27 |
#define RAYHIT (-1) /* return value for intercepted ray */ |
28 |
|
29 |
|
30 |
rayorigin(r, ro, rt, rw) /* start new ray from old one */ |
31 |
register RAY *r, *ro; |
32 |
int rt; |
33 |
double rw; |
34 |
{ |
35 |
if ((r->parent = ro) == NULL) { /* primary ray */ |
36 |
r->rlvl = 0; |
37 |
r->rweight = rw; |
38 |
r->crtype = r->rtype = rt; |
39 |
r->rsrc = -1; |
40 |
r->clipset = NULL; |
41 |
} else { /* spawned ray */ |
42 |
r->rlvl = ro->rlvl; |
43 |
if (rt & RAYREFL) { |
44 |
r->rlvl++; |
45 |
r->rsrc = -1; |
46 |
r->clipset = ro->clipset; |
47 |
} else { |
48 |
r->rsrc = ro->rsrc; |
49 |
r->clipset = ro->newcset; |
50 |
} |
51 |
r->rweight = ro->rweight * rw; |
52 |
r->crtype = ro->crtype | (r->rtype = rt); |
53 |
VCOPY(r->rorg, ro->rop); |
54 |
} |
55 |
r->rno = nrays; |
56 |
r->newcset = r->clipset; |
57 |
r->ro = NULL; |
58 |
r->rot = FHUGE; |
59 |
r->rofs = 1.0; setident4(r->rofx); |
60 |
r->robs = 1.0; setident4(r->robx); |
61 |
r->pert[0] = r->pert[1] = r->pert[2] = 0.0; |
62 |
setcolor(r->pcol, 1.0, 1.0, 1.0); |
63 |
setcolor(r->rcol, 0.0, 0.0, 0.0); |
64 |
return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1); |
65 |
} |
66 |
|
67 |
|
68 |
rayvalue(r) /* compute a ray's value */ |
69 |
register RAY *r; |
70 |
{ |
71 |
extern int (*trace)(); |
72 |
|
73 |
if (localhit(r, &thescene)) |
74 |
if (r->clipset != NULL && inset(r->clipset, r->ro->omod)) |
75 |
raytrans(r); /* object is clipped */ |
76 |
else |
77 |
rayshade(r, r->ro->omod); |
78 |
else if (sourcehit(r)) |
79 |
rayshade(r, r->ro->omod); |
80 |
|
81 |
if (trace != NULL) |
82 |
(*trace)(r); /* trace execution */ |
83 |
} |
84 |
|
85 |
|
86 |
raytrans(r) /* transmit ray as is */ |
87 |
RAY *r; |
88 |
{ |
89 |
RAY tr; |
90 |
|
91 |
if (rayorigin(&tr, r, TRANS, 1.0) == 0) { |
92 |
VCOPY(tr.rdir, r->rdir); |
93 |
rayvalue(&tr); |
94 |
copycolor(r->rcol, tr.rcol); |
95 |
} |
96 |
} |
97 |
|
98 |
|
99 |
rayshade(r, mod) /* shade ray r with material mod */ |
100 |
register RAY *r; |
101 |
int mod; |
102 |
{ |
103 |
static int depth = 0; |
104 |
register OBJREC *m; |
105 |
/* check for infinite loop */ |
106 |
if (depth++ >= MAXLOOP) |
107 |
objerror(r->ro, USER, "material loop"); |
108 |
for ( ; mod != OVOID; mod = m->omod) { |
109 |
m = objptr(mod); |
110 |
if (!ismodifier(m->otype)) { |
111 |
sprintf(errmsg, "illegal modifier \"%s\"", m->oname); |
112 |
error(USER, errmsg); |
113 |
} |
114 |
(*ofun[m->otype].funp)(m, r); /* execute function */ |
115 |
m->lastrno = r->rno; |
116 |
if (ismaterial(m->otype)) { /* materials call raytexture */ |
117 |
depth--; |
118 |
return; /* we're done */ |
119 |
} |
120 |
} |
121 |
objerror(r->ro, USER, "material not found"); |
122 |
} |
123 |
|
124 |
|
125 |
raytexture(r, mod) /* get material modifiers */ |
126 |
RAY *r; |
127 |
int mod; |
128 |
{ |
129 |
static int depth = 0; |
130 |
register OBJREC *m; |
131 |
/* check for infinite loop */ |
132 |
if (depth++ >= MAXLOOP) |
133 |
objerror(r->ro, USER, "modifier loop"); |
134 |
/* execute textures and patterns */ |
135 |
for ( ; mod != OVOID; mod = m->omod) { |
136 |
m = objptr(mod); |
137 |
if (!istexture(m->otype)) { |
138 |
sprintf(errmsg, "illegal modifier \"%s\"", m->oname); |
139 |
error(USER, errmsg); |
140 |
} |
141 |
(*ofun[m->otype].funp)(m, r); |
142 |
m->lastrno = r->rno; |
143 |
} |
144 |
depth--; /* end here */ |
145 |
} |
146 |
|
147 |
|
148 |
raymixture(r, fore, back, coef) /* mix modifiers */ |
149 |
register RAY *r; |
150 |
OBJECT fore, back; |
151 |
double coef; |
152 |
{ |
153 |
FVECT curpert, forepert, backpert; |
154 |
COLOR curpcol, forepcol, backpcol; |
155 |
register int i; |
156 |
/* clip coefficient */ |
157 |
if (coef > 1.0) |
158 |
coef = 1.0; |
159 |
else if (coef < 0.0) |
160 |
coef = 0.0; |
161 |
/* save current mods */ |
162 |
VCOPY(curpert, r->pert); |
163 |
copycolor(curpcol, r->pcol); |
164 |
/* compute new mods */ |
165 |
/* foreground */ |
166 |
r->pert[0] = r->pert[1] = r->pert[2] = 0.0; |
167 |
setcolor(r->pcol, 1.0, 1.0, 1.0); |
168 |
if (fore != OVOID && coef > FTINY) |
169 |
raytexture(r, fore); |
170 |
VCOPY(forepert, r->pert); |
171 |
copycolor(forepcol, r->pcol); |
172 |
/* background */ |
173 |
r->pert[0] = r->pert[1] = r->pert[2] = 0.0; |
174 |
setcolor(r->pcol, 1.0, 1.0, 1.0); |
175 |
if (back != OVOID && coef < 1.0-FTINY) |
176 |
raytexture(r, back); |
177 |
VCOPY(backpert, r->pert); |
178 |
copycolor(backpcol, r->pcol); |
179 |
/* sum perturbations */ |
180 |
for (i = 0; i < 3; i++) |
181 |
r->pert[i] = curpert[i] + coef*forepert[i] + |
182 |
(1.0-coef)*backpert[i]; |
183 |
/* multiply colors */ |
184 |
setcolor(r->pcol, coef*colval(forepcol,RED) + |
185 |
(1.0-coef)*colval(backpcol,RED), |
186 |
coef*colval(forepcol,GRN) + |
187 |
(1.0-coef)*colval(backpcol,GRN), |
188 |
coef*colval(forepcol,BLU) + |
189 |
(1.0-coef)*colval(backpcol,BLU)); |
190 |
multcolor(r->pcol, curpcol); |
191 |
} |
192 |
|
193 |
|
194 |
double |
195 |
raynormal(norm, r) /* compute perturbed normal for ray */ |
196 |
FVECT norm; |
197 |
register RAY *r; |
198 |
{ |
199 |
double newdot; |
200 |
register int i; |
201 |
|
202 |
/* The perturbation is added to the surface normal to obtain |
203 |
* the new normal. If the new normal would affect the surface |
204 |
* orientation wrt. the ray, a correction is made. The method is |
205 |
* still fraught with problems since reflected rays and similar |
206 |
* directions calculated from the surface normal may spawn rays behind |
207 |
* the surface. The only solution is to curb textures at high |
208 |
* incidence (Rdot << 1). |
209 |
*/ |
210 |
|
211 |
for (i = 0; i < 3; i++) |
212 |
norm[i] = r->ron[i] + r->pert[i]; |
213 |
|
214 |
if (normalize(norm) == 0.0) { |
215 |
objerror(r->ro, WARNING, "illegal normal perturbation"); |
216 |
VCOPY(norm, r->ron); |
217 |
return(r->rod); |
218 |
} |
219 |
newdot = -DOT(norm, r->rdir); |
220 |
if ((newdot > 0.0) != (r->rod > 0.0)) { /* fix orientation */ |
221 |
for (i = 0; i < 3; i++) |
222 |
norm[i] += 2.0*newdot*r->rdir[i]; |
223 |
newdot = -newdot; |
224 |
} |
225 |
return(newdot); |
226 |
} |
227 |
|
228 |
|
229 |
flipsurface(r) /* reverse surface orientation */ |
230 |
register RAY *r; |
231 |
{ |
232 |
r->rod = -r->rod; |
233 |
r->ron[0] = -r->ron[0]; |
234 |
r->ron[1] = -r->ron[1]; |
235 |
r->ron[2] = -r->ron[2]; |
236 |
r->pert[0] = -r->pert[0]; |
237 |
r->pert[1] = -r->pert[1]; |
238 |
r->pert[2] = -r->pert[2]; |
239 |
} |
240 |
|
241 |
|
242 |
localhit(r, scene) /* check for hit in the octree */ |
243 |
register RAY *r; |
244 |
register CUBE *scene; |
245 |
{ |
246 |
FVECT curpos; /* current cube position */ |
247 |
int mpos, mneg; /* sign flags */ |
248 |
double t, dt; |
249 |
register int i; |
250 |
|
251 |
nrays++; /* increment trace counter */ |
252 |
|
253 |
mpos = mneg = 0; |
254 |
for (i = 0; i < 3; i++) { |
255 |
curpos[i] = r->rorg[i]; |
256 |
if (r->rdir[i] > FTINY) |
257 |
mpos |= 1 << i; |
258 |
else if (r->rdir[i] < -FTINY) |
259 |
mneg |= 1 << i; |
260 |
} |
261 |
t = 0.0; |
262 |
if (!incube(scene, curpos)) { |
263 |
/* find distance to entry */ |
264 |
for (i = 0; i < 3; i++) { |
265 |
/* plane in our direction */ |
266 |
if (mpos & 1<<i) |
267 |
dt = scene->cuorg[i]; |
268 |
else if (mneg & 1<<i) |
269 |
dt = scene->cuorg[i] + scene->cusize; |
270 |
else |
271 |
continue; |
272 |
/* distance to the plane */ |
273 |
dt = (dt - r->rorg[i])/r->rdir[i]; |
274 |
if (dt > t) |
275 |
t = dt; /* farthest face is the one */ |
276 |
} |
277 |
t += FTINY; /* fudge to get inside cube */ |
278 |
/* advance position */ |
279 |
for (i = 0; i < 3; i++) |
280 |
curpos[i] += r->rdir[i]*t; |
281 |
|
282 |
if (!incube(scene, curpos)) /* non-intersecting ray */ |
283 |
return(0); |
284 |
} |
285 |
return(raymove(curpos, mpos, mneg, r, scene) == RAYHIT); |
286 |
} |
287 |
|
288 |
|
289 |
static int |
290 |
raymove(pos, plus, minus, r, cu) /* check for hit as we move */ |
291 |
FVECT pos; /* modified */ |
292 |
int plus, minus; /* direction indicators to speed tests */ |
293 |
register RAY *r; |
294 |
register CUBE *cu; |
295 |
{ |
296 |
int ax; |
297 |
double dt, t; |
298 |
register int sgn; |
299 |
|
300 |
if (istree(cu->cutree)) { /* recurse on subcubes */ |
301 |
CUBE cukid; |
302 |
register int br; |
303 |
|
304 |
cukid.cusize = cu->cusize * 0.5; /* find subcube */ |
305 |
VCOPY(cukid.cuorg, cu->cuorg); |
306 |
br = 0; |
307 |
if (pos[0] >= cukid.cuorg[0]+cukid.cusize) { |
308 |
cukid.cuorg[0] += cukid.cusize; |
309 |
br |= 1; |
310 |
} |
311 |
if (pos[1] >= cukid.cuorg[1]+cukid.cusize) { |
312 |
cukid.cuorg[1] += cukid.cusize; |
313 |
br |= 2; |
314 |
} |
315 |
if (pos[2] >= cukid.cuorg[2]+cukid.cusize) { |
316 |
cukid.cuorg[2] += cukid.cusize; |
317 |
br |= 4; |
318 |
} |
319 |
for ( ; ; ) { |
320 |
cukid.cutree = octkid(cu->cutree, br); |
321 |
if ((ax = raymove(pos,plus,minus,r,&cukid)) == RAYHIT) |
322 |
return(RAYHIT); |
323 |
sgn = 1 << ax; |
324 |
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 |
331 |
if (sgn & br) |
332 |
return(ax); /* overflow */ |
333 |
else { |
334 |
cukid.cuorg[ax] += cukid.cusize; |
335 |
br |= sgn; |
336 |
} |
337 |
} |
338 |
/*NOTREACHED*/ |
339 |
} |
340 |
if (isfull(cu->cutree) && checkhit(r, cu)) |
341 |
return(RAYHIT); |
342 |
/* advance to next cube */ |
343 |
sgn = plus | minus; |
344 |
if (sgn&1) { |
345 |
dt = plus&1 ? cu->cuorg[0] + cu->cusize : cu->cuorg[0]; |
346 |
t = (dt - pos[0])/r->rdir[0]; |
347 |
ax = 0; |
348 |
} else |
349 |
t = FHUGE; |
350 |
if (sgn&2) { |
351 |
dt = plus&2 ? cu->cuorg[1] + cu->cusize : cu->cuorg[1]; |
352 |
dt = (dt - pos[1])/r->rdir[1]; |
353 |
if (dt < t) { |
354 |
t = dt; |
355 |
ax = 1; |
356 |
} |
357 |
} |
358 |
if (sgn&4) { |
359 |
dt = plus&4 ? cu->cuorg[2] + cu->cusize : cu->cuorg[2]; |
360 |
dt = (dt - pos[2])/r->rdir[2]; |
361 |
if (dt < t) { |
362 |
t = dt; |
363 |
ax = 2; |
364 |
} |
365 |
} |
366 |
pos[0] += r->rdir[0]*t; |
367 |
pos[1] += r->rdir[1]*t; |
368 |
pos[2] += r->rdir[2]*t; |
369 |
return(ax); |
370 |
} |
371 |
|
372 |
|
373 |
static |
374 |
checkhit(r, cu) /* check for hit in full cube */ |
375 |
register RAY *r; |
376 |
CUBE *cu; |
377 |
{ |
378 |
OBJECT oset[MAXSET+1]; |
379 |
register OBJREC *o; |
380 |
register int i; |
381 |
|
382 |
objset(oset, cu->cutree); |
383 |
for (i = oset[0]; i > 0; i--) { |
384 |
o = objptr(oset[i]); |
385 |
if (o->lastrno == r->rno) /* checked already? */ |
386 |
continue; |
387 |
(*ofun[o->otype].funp)(o, r); |
388 |
o->lastrno = r->rno; |
389 |
} |
390 |
if (r->ro == NULL) |
391 |
return(0); /* no scores yet */ |
392 |
|
393 |
return(incube(cu, r->rop)); /* hit OK if in current cube */ |
394 |
} |