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