1 |
/* Copyright (c) 1991 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 |
#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 */ |
30 |
|
31 |
static FLOAT Lambfa[5] = {PI, PI, PI, 0.0, 0.0}; |
32 |
OBJREC Lamb = { |
33 |
OVOID, MAT_PLASTIC, "Lambertian", |
34 |
{0, 5, NULL, Lambfa}, NULL, |
35 |
}; /* a Lambertian surface */ |
36 |
|
37 |
#define MAXLOOP 128 /* modifier loop detection */ |
38 |
|
39 |
#define RAYHIT (-1) /* return value for intercepted ray */ |
40 |
|
41 |
|
42 |
rayorigin(r, ro, rt, rw) /* start new ray from old one */ |
43 |
register RAY *r, *ro; |
44 |
int rt; |
45 |
double rw; |
46 |
{ |
47 |
if ((r->parent = ro) == NULL) { /* primary ray */ |
48 |
r->rlvl = 0; |
49 |
r->rweight = rw; |
50 |
r->crtype = r->rtype = rt; |
51 |
r->rsrc = -1; |
52 |
r->clipset = NULL; |
53 |
r->revf = raytrace; |
54 |
} else { /* spawned ray */ |
55 |
r->rlvl = ro->rlvl; |
56 |
if (rt & RAYREFL) { |
57 |
r->rlvl++; |
58 |
r->rsrc = -1; |
59 |
r->clipset = ro->clipset; |
60 |
} else { |
61 |
r->rsrc = ro->rsrc; |
62 |
r->clipset = ro->newcset; |
63 |
} |
64 |
r->revf = ro->revf; |
65 |
r->rweight = ro->rweight * rw; |
66 |
r->crtype = ro->crtype | (r->rtype = rt); |
67 |
VCOPY(r->rorg, ro->rop); |
68 |
} |
69 |
rayclear(r); |
70 |
return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1); |
71 |
} |
72 |
|
73 |
|
74 |
rayclear(r) /* clear a ray for (re)evaluation */ |
75 |
register RAY *r; |
76 |
{ |
77 |
r->rno = raynum++; |
78 |
r->newcset = r->clipset; |
79 |
r->ro = NULL; |
80 |
r->rot = FHUGE; |
81 |
r->pert[0] = r->pert[1] = r->pert[2] = 0.0; |
82 |
setcolor(r->pcol, 1.0, 1.0, 1.0); |
83 |
setcolor(r->rcol, 0.0, 0.0, 0.0); |
84 |
r->rt = 0.0; |
85 |
} |
86 |
|
87 |
|
88 |
raytrace(r) /* trace a ray and compute its value */ |
89 |
RAY *r; |
90 |
{ |
91 |
extern int (*trace)(); |
92 |
|
93 |
if (localhit(r, &thescene)) |
94 |
raycont(r); |
95 |
else if (sourcehit(r)) |
96 |
rayshade(r, r->ro->omod); |
97 |
|
98 |
if (trace != NULL) |
99 |
(*trace)(r); /* trace execution */ |
100 |
} |
101 |
|
102 |
|
103 |
raycont(r) /* check for clipped object and continue */ |
104 |
register RAY *r; |
105 |
{ |
106 |
if (r->clipset != NULL && inset(r->clipset, r->ro->omod)) |
107 |
raytrans(r); |
108 |
else |
109 |
rayshade(r, r->ro->omod); |
110 |
} |
111 |
|
112 |
|
113 |
raytrans(r) /* transmit ray as is */ |
114 |
register RAY *r; |
115 |
{ |
116 |
RAY tr; |
117 |
|
118 |
if (rayorigin(&tr, r, TRANS, 1.0) == 0) { |
119 |
VCOPY(tr.rdir, r->rdir); |
120 |
rayvalue(&tr); |
121 |
copycolor(r->rcol, tr.rcol); |
122 |
r->rt = r->rot + tr.rt; |
123 |
} |
124 |
} |
125 |
|
126 |
|
127 |
rayshade(r, mod) /* shade ray r with material mod */ |
128 |
register RAY *r; |
129 |
int mod; |
130 |
{ |
131 |
static int depth = 0; |
132 |
register OBJREC *m; |
133 |
/* check for infinite loop */ |
134 |
if (depth++ >= MAXLOOP) |
135 |
objerror(r->ro, USER, "possible modifier loop"); |
136 |
r->rt = r->rot; /* set effective ray length */ |
137 |
for ( ; mod != OVOID; mod = m->omod) { |
138 |
m = objptr(mod); |
139 |
/****** unnecessary test since modifier() is always called |
140 |
if (!ismodifier(m->otype)) { |
141 |
sprintf(errmsg, "illegal modifier \"%s\"", m->oname); |
142 |
error(USER, errmsg); |
143 |
} |
144 |
******/ |
145 |
/* hack for irradiance calculation */ |
146 |
if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS))) { |
147 |
if (irr_ignore(m->otype)) { |
148 |
depth--; |
149 |
raytrans(r); |
150 |
return; |
151 |
} |
152 |
if (!islight(m->otype)) |
153 |
m = &Lamb; |
154 |
} |
155 |
(*ofun[m->otype].funp)(m, r); /* execute function */ |
156 |
if (ismaterial(m->otype)) { /* materials call raytexture */ |
157 |
depth--; |
158 |
return; /* we're done */ |
159 |
} |
160 |
} |
161 |
objerror(r->ro, USER, "material not found"); |
162 |
} |
163 |
|
164 |
|
165 |
raytexture(r, mod) /* get material modifiers */ |
166 |
RAY *r; |
167 |
int mod; |
168 |
{ |
169 |
static int depth = 0; |
170 |
register OBJREC *m; |
171 |
/* check for infinite loop */ |
172 |
if (depth++ >= MAXLOOP) |
173 |
objerror(r->ro, USER, "modifier loop"); |
174 |
/* execute textures and patterns */ |
175 |
for ( ; mod != OVOID; mod = m->omod) { |
176 |
m = objptr(mod); |
177 |
if (!istexture(m->otype)) { |
178 |
sprintf(errmsg, "illegal modifier \"%s\"", m->oname); |
179 |
error(USER, errmsg); |
180 |
} |
181 |
(*ofun[m->otype].funp)(m, r); |
182 |
} |
183 |
depth--; /* end here */ |
184 |
} |
185 |
|
186 |
|
187 |
raymixture(r, fore, back, coef) /* mix modifiers */ |
188 |
register RAY *r; |
189 |
OBJECT fore, back; |
190 |
double coef; |
191 |
{ |
192 |
FVECT curpert, forepert, backpert; |
193 |
COLOR curpcol, forepcol, backpcol; |
194 |
register int i; |
195 |
/* clip coefficient */ |
196 |
if (coef > 1.0) |
197 |
coef = 1.0; |
198 |
else if (coef < 0.0) |
199 |
coef = 0.0; |
200 |
/* save current mods */ |
201 |
VCOPY(curpert, r->pert); |
202 |
copycolor(curpcol, r->pcol); |
203 |
/* compute new mods */ |
204 |
/* foreground */ |
205 |
r->pert[0] = r->pert[1] = r->pert[2] = 0.0; |
206 |
setcolor(r->pcol, 1.0, 1.0, 1.0); |
207 |
if (fore != OVOID && coef > FTINY) |
208 |
raytexture(r, fore); |
209 |
VCOPY(forepert, r->pert); |
210 |
copycolor(forepcol, r->pcol); |
211 |
/* background */ |
212 |
r->pert[0] = r->pert[1] = r->pert[2] = 0.0; |
213 |
setcolor(r->pcol, 1.0, 1.0, 1.0); |
214 |
if (back != OVOID && coef < 1.0-FTINY) |
215 |
raytexture(r, back); |
216 |
VCOPY(backpert, r->pert); |
217 |
copycolor(backpcol, r->pcol); |
218 |
/* sum perturbations */ |
219 |
for (i = 0; i < 3; i++) |
220 |
r->pert[i] = curpert[i] + coef*forepert[i] + |
221 |
(1.0-coef)*backpert[i]; |
222 |
/* multiply colors */ |
223 |
setcolor(r->pcol, coef*colval(forepcol,RED) + |
224 |
(1.0-coef)*colval(backpcol,RED), |
225 |
coef*colval(forepcol,GRN) + |
226 |
(1.0-coef)*colval(backpcol,GRN), |
227 |
coef*colval(forepcol,BLU) + |
228 |
(1.0-coef)*colval(backpcol,BLU)); |
229 |
multcolor(r->pcol, curpcol); |
230 |
} |
231 |
|
232 |
|
233 |
double |
234 |
raynormal(norm, r) /* compute perturbed normal for ray */ |
235 |
FVECT norm; |
236 |
register RAY *r; |
237 |
{ |
238 |
double newdot; |
239 |
register int i; |
240 |
|
241 |
/* The perturbation is added to the surface normal to obtain |
242 |
* the new normal. If the new normal would affect the surface |
243 |
* orientation wrt. the ray, a correction is made. The method is |
244 |
* still fraught with problems since reflected rays and similar |
245 |
* directions calculated from the surface normal may spawn rays behind |
246 |
* the surface. The only solution is to curb textures at high |
247 |
* incidence (namely, keep DOT(rdir,pert) < Rdot). |
248 |
*/ |
249 |
|
250 |
for (i = 0; i < 3; i++) |
251 |
norm[i] = r->ron[i] + r->pert[i]; |
252 |
|
253 |
if (normalize(norm) == 0.0) { |
254 |
objerror(r->ro, WARNING, "illegal normal perturbation"); |
255 |
VCOPY(norm, r->ron); |
256 |
return(r->rod); |
257 |
} |
258 |
newdot = -DOT(norm, r->rdir); |
259 |
if ((newdot > 0.0) != (r->rod > 0.0)) { /* fix orientation */ |
260 |
for (i = 0; i < 3; i++) |
261 |
norm[i] += 2.0*newdot*r->rdir[i]; |
262 |
newdot = -newdot; |
263 |
} |
264 |
return(newdot); |
265 |
} |
266 |
|
267 |
|
268 |
newrayxf(r) /* get new tranformation matrix for ray */ |
269 |
RAY *r; |
270 |
{ |
271 |
static struct xfn { |
272 |
struct xfn *next; |
273 |
FULLXF xf; |
274 |
} xfseed = { &xfseed }, *xflast = &xfseed; |
275 |
register struct xfn *xp; |
276 |
register RAY *rp; |
277 |
|
278 |
/* |
279 |
* Search for transform in circular list that |
280 |
* has no associated ray in the tree. |
281 |
*/ |
282 |
xp = xflast; |
283 |
for (rp = r->parent; rp != NULL; rp = rp->parent) |
284 |
if (rp->rox == &xp->xf) { /* xp in use */ |
285 |
xp = xp->next; /* move to next */ |
286 |
if (xp == xflast) { /* need new one */ |
287 |
xp = (struct xfn *)bmalloc(sizeof(struct xfn)); |
288 |
if (xp == NULL) |
289 |
error(SYSTEM, |
290 |
"out of memory in newrayxf"); |
291 |
/* insert in list */ |
292 |
xp->next = xflast->next; |
293 |
xflast->next = xp; |
294 |
break; /* we're done */ |
295 |
} |
296 |
rp = r; /* start check over */ |
297 |
} |
298 |
/* got it */ |
299 |
r->rox = &xp->xf; |
300 |
xflast = xp; |
301 |
} |
302 |
|
303 |
|
304 |
flipsurface(r) /* reverse surface orientation */ |
305 |
register RAY *r; |
306 |
{ |
307 |
r->rod = -r->rod; |
308 |
r->ron[0] = -r->ron[0]; |
309 |
r->ron[1] = -r->ron[1]; |
310 |
r->ron[2] = -r->ron[2]; |
311 |
r->pert[0] = -r->pert[0]; |
312 |
r->pert[1] = -r->pert[1]; |
313 |
r->pert[2] = -r->pert[2]; |
314 |
} |
315 |
|
316 |
|
317 |
localhit(r, scene) /* check for hit in the octree */ |
318 |
register RAY *r; |
319 |
register CUBE *scene; |
320 |
{ |
321 |
OBJECT cxset[MAXCSET+1]; /* set of checked objects */ |
322 |
FVECT curpos; /* current cube position */ |
323 |
int sflags; /* sign flags */ |
324 |
double t, dt; |
325 |
register int i; |
326 |
|
327 |
nrays++; /* increment trace counter */ |
328 |
sflags = 0; |
329 |
for (i = 0; i < 3; i++) { |
330 |
curpos[i] = r->rorg[i]; |
331 |
if (r->rdir[i] > FTINY) |
332 |
sflags |= 1 << i; |
333 |
else if (r->rdir[i] < -FTINY) |
334 |
sflags |= 0x10 << i; |
335 |
} |
336 |
if (sflags == 0) |
337 |
error(CONSISTENCY, "zero ray direction in localhit"); |
338 |
t = 0.0; |
339 |
if (!incube(scene, curpos)) { |
340 |
/* find distance to entry */ |
341 |
for (i = 0; i < 3; i++) { |
342 |
/* plane in our direction */ |
343 |
if (sflags & 1<<i) |
344 |
dt = scene->cuorg[i]; |
345 |
else if (sflags & 0x10<<i) |
346 |
dt = scene->cuorg[i] + scene->cusize; |
347 |
else |
348 |
continue; |
349 |
/* distance to the plane */ |
350 |
dt = (dt - r->rorg[i])/r->rdir[i]; |
351 |
if (dt > t) |
352 |
t = dt; /* farthest face is the one */ |
353 |
} |
354 |
t += FTINY; /* fudge to get inside cube */ |
355 |
/* advance position */ |
356 |
for (i = 0; i < 3; i++) |
357 |
curpos[i] += r->rdir[i]*t; |
358 |
|
359 |
if (!incube(scene, curpos)) /* non-intersecting ray */ |
360 |
return(0); |
361 |
} |
362 |
cxset[0] = 0; |
363 |
return(raymove(curpos, cxset, sflags, r, scene) == RAYHIT); |
364 |
} |
365 |
|
366 |
|
367 |
static int |
368 |
raymove(pos, cxs, dirf, r, cu) /* check for hit as we move */ |
369 |
FVECT pos; /* current position, modified herein */ |
370 |
OBJECT *cxs; /* checked objects, modified by checkhit */ |
371 |
int dirf; /* direction indicators to speed tests */ |
372 |
register RAY *r; |
373 |
register CUBE *cu; |
374 |
{ |
375 |
int ax; |
376 |
double dt, t; |
377 |
|
378 |
if (istree(cu->cutree)) { /* recurse on subcubes */ |
379 |
CUBE cukid; |
380 |
register int br, sgn; |
381 |
|
382 |
cukid.cusize = cu->cusize * 0.5; /* find subcube */ |
383 |
VCOPY(cukid.cuorg, cu->cuorg); |
384 |
br = 0; |
385 |
if (pos[0] >= cukid.cuorg[0]+cukid.cusize) { |
386 |
cukid.cuorg[0] += cukid.cusize; |
387 |
br |= 1; |
388 |
} |
389 |
if (pos[1] >= cukid.cuorg[1]+cukid.cusize) { |
390 |
cukid.cuorg[1] += cukid.cusize; |
391 |
br |= 2; |
392 |
} |
393 |
if (pos[2] >= cukid.cuorg[2]+cukid.cusize) { |
394 |
cukid.cuorg[2] += cukid.cusize; |
395 |
br |= 4; |
396 |
} |
397 |
for ( ; ; ) { |
398 |
cukid.cutree = octkid(cu->cutree, br); |
399 |
if ((ax = raymove(pos,cxs,dirf,r,&cukid)) == RAYHIT) |
400 |
return(RAYHIT); |
401 |
sgn = 1 << ax; |
402 |
if (sgn & dirf) /* positive axis? */ |
403 |
if (sgn & br) |
404 |
return(ax); /* overflow */ |
405 |
else { |
406 |
cukid.cuorg[ax] += cukid.cusize; |
407 |
br |= sgn; |
408 |
} |
409 |
else |
410 |
if (sgn & br) { |
411 |
cukid.cuorg[ax] -= cukid.cusize; |
412 |
br &= ~sgn; |
413 |
} else |
414 |
return(ax); /* underflow */ |
415 |
} |
416 |
/*NOTREACHED*/ |
417 |
} |
418 |
if (isfull(cu->cutree) && checkhit(r, cu, cxs)) |
419 |
return(RAYHIT); |
420 |
/* advance to next cube */ |
421 |
if (dirf&0x11) { |
422 |
dt = dirf&1 ? cu->cuorg[0] + cu->cusize : cu->cuorg[0]; |
423 |
t = (dt - pos[0])/r->rdir[0]; |
424 |
ax = 0; |
425 |
} else |
426 |
t = FHUGE; |
427 |
if (dirf&0x22) { |
428 |
dt = dirf&2 ? cu->cuorg[1] + cu->cusize : cu->cuorg[1]; |
429 |
dt = (dt - pos[1])/r->rdir[1]; |
430 |
if (dt < t) { |
431 |
t = dt; |
432 |
ax = 1; |
433 |
} |
434 |
} |
435 |
if (dirf&0x44) { |
436 |
dt = dirf&4 ? cu->cuorg[2] + cu->cusize : cu->cuorg[2]; |
437 |
dt = (dt - pos[2])/r->rdir[2]; |
438 |
if (dt < t) { |
439 |
t = dt; |
440 |
ax = 2; |
441 |
} |
442 |
} |
443 |
pos[0] += r->rdir[0]*t; |
444 |
pos[1] += r->rdir[1]*t; |
445 |
pos[2] += r->rdir[2]*t; |
446 |
return(ax); |
447 |
} |
448 |
|
449 |
|
450 |
static |
451 |
checkhit(r, cu, cxs) /* check for hit in full cube */ |
452 |
register RAY *r; |
453 |
CUBE *cu; |
454 |
OBJECT *cxs; |
455 |
{ |
456 |
OBJECT oset[MAXSET+1]; |
457 |
register OBJREC *o; |
458 |
register int i; |
459 |
|
460 |
objset(oset, cu->cutree); |
461 |
checkset(oset, cxs); /* eliminate double-checking */ |
462 |
for (i = oset[0]; i > 0; i--) { |
463 |
o = objptr(oset[i]); |
464 |
(*ofun[o->otype].funp)(o, r); |
465 |
} |
466 |
if (r->ro == NULL) |
467 |
return(0); /* no scores yet */ |
468 |
|
469 |
return(incube(cu, r->rop)); /* hit OK if in current cube */ |
470 |
} |
471 |
|
472 |
|
473 |
static |
474 |
checkset(os, cs) /* modify checked set and set to check */ |
475 |
register OBJECT *os; /* os' = os - cs */ |
476 |
register OBJECT *cs; /* cs' = cs + os */ |
477 |
{ |
478 |
OBJECT cset[MAXCSET+MAXSET+1]; |
479 |
register int i, j; |
480 |
int k; |
481 |
/* copy os in place, cset <- cs */ |
482 |
cset[0] = 0; |
483 |
k = 0; |
484 |
for (i = j = 1; i <= os[0]; i++) { |
485 |
while (j <= cs[0] && cs[j] < os[i]) |
486 |
cset[++cset[0]] = cs[j++]; |
487 |
if (j > cs[0] || os[i] != cs[j]) { /* object to check */ |
488 |
os[++k] = os[i]; |
489 |
cset[++cset[0]] = os[i]; |
490 |
} |
491 |
} |
492 |
if (!(os[0] = k)) /* new "to check" set size */ |
493 |
return; /* special case */ |
494 |
while (j <= cs[0]) /* get the rest of cs */ |
495 |
cset[++cset[0]] = cs[j++]; |
496 |
if (cset[0] > MAXCSET) /* truncate "checked" set if nec. */ |
497 |
cset[0] = MAXCSET; |
498 |
/* setcopy(cs, cset); */ /* copy cset back to cs */ |
499 |
os = cset; |
500 |
for (i = os[0]; i-- >= 0; ) |
501 |
*cs++ = *os++; |
502 |
} |