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