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