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