1 |
< |
/* Copyright (c) 1986 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
16 |
|
|
17 |
|
#include "otypes.h" |
18 |
|
|
19 |
+ |
#include "otspecial.h" |
20 |
+ |
|
21 |
|
extern CUBE thescene; /* our scene */ |
22 |
|
extern int maxdepth; /* maximum recursion depth */ |
23 |
|
extern double minweight; /* minimum ray weight */ |
24 |
+ |
extern int do_irrad; /* compute irradiance? */ |
25 |
|
|
26 |
< |
long nrays = 0L; /* number of rays traced */ |
26 |
> |
long raynum = 0L; /* next unique ray number */ |
27 |
> |
long nrays = 0L; /* number of calls to localhit */ |
28 |
|
|
29 |
+ |
static FLOAT Lambfa[5] = {PI, PI, PI, 0.0, 0.0}; |
30 |
+ |
OBJREC Lamb = { |
31 |
+ |
OVOID, MAT_PLASTIC, "Lambertian", |
32 |
+ |
{0, 5, NULL, Lambfa}, NULL, -1, |
33 |
+ |
}; /* a Lambertian surface */ |
34 |
+ |
|
35 |
|
#define MAXLOOP 128 /* modifier loop detection */ |
36 |
|
|
37 |
|
#define RAYHIT (-1) /* return value for intercepted ray */ |
48 |
|
r->crtype = r->rtype = rt; |
49 |
|
r->rsrc = -1; |
50 |
|
r->clipset = NULL; |
51 |
+ |
r->revf = raytrace; |
52 |
|
} else { /* spawned ray */ |
53 |
|
r->rlvl = ro->rlvl; |
54 |
|
if (rt & RAYREFL) { |
59 |
|
r->rsrc = ro->rsrc; |
60 |
|
r->clipset = ro->newcset; |
61 |
|
} |
62 |
+ |
r->revf = ro->revf; |
63 |
|
r->rweight = ro->rweight * rw; |
64 |
|
r->crtype = ro->crtype | (r->rtype = rt); |
65 |
|
VCOPY(r->rorg, ro->rop); |
66 |
|
} |
67 |
< |
r->rno = nrays; |
67 |
> |
rayclear(r); |
68 |
> |
return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1); |
69 |
> |
} |
70 |
> |
|
71 |
> |
|
72 |
> |
rayclear(r) /* clear a ray for (re)evaluation */ |
73 |
> |
register RAY *r; |
74 |
> |
{ |
75 |
> |
r->rno = raynum++; |
76 |
|
r->newcset = r->clipset; |
77 |
|
r->ro = NULL; |
78 |
|
r->rot = FHUGE; |
79 |
|
r->pert[0] = r->pert[1] = r->pert[2] = 0.0; |
80 |
|
setcolor(r->pcol, 1.0, 1.0, 1.0); |
81 |
|
setcolor(r->rcol, 0.0, 0.0, 0.0); |
82 |
< |
return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1); |
82 |
> |
r->rt = 0.0; |
83 |
|
} |
84 |
|
|
85 |
|
|
86 |
< |
rayvalue(r) /* compute a ray's value */ |
86 |
> |
raytrace(r) /* trace a ray and compute its value */ |
87 |
|
RAY *r; |
88 |
|
{ |
89 |
|
extern int (*trace)(); |
90 |
|
|
91 |
< |
if (localhit(r, &thescene) || sourcehit(r)) |
91 |
> |
if (localhit(r, &thescene)) |
92 |
|
raycont(r); |
93 |
+ |
else if (sourcehit(r)) |
94 |
+ |
rayshade(r, r->ro->omod); |
95 |
|
|
96 |
|
if (trace != NULL) |
97 |
|
(*trace)(r); /* trace execution */ |
117 |
|
VCOPY(tr.rdir, r->rdir); |
118 |
|
rayvalue(&tr); |
119 |
|
copycolor(r->rcol, tr.rcol); |
120 |
+ |
r->rt = r->rot + tr.rt; |
121 |
|
} |
122 |
|
} |
123 |
|
|
131 |
|
/* check for infinite loop */ |
132 |
|
if (depth++ >= MAXLOOP) |
133 |
|
objerror(r->ro, USER, "possible modifier loop"); |
134 |
+ |
r->rt = r->rot; /* set effective ray length */ |
135 |
|
for ( ; mod != OVOID; mod = m->omod) { |
136 |
|
m = objptr(mod); |
137 |
|
/****** unnecessary test since modifier() is always called |
140 |
|
error(USER, errmsg); |
141 |
|
} |
142 |
|
******/ |
143 |
+ |
/* hack for irradiance calculation */ |
144 |
+ |
if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS))) { |
145 |
+ |
if (irr_ignore(m->otype)) { |
146 |
+ |
depth--; |
147 |
+ |
raytrans(r); |
148 |
+ |
return; |
149 |
+ |
} |
150 |
+ |
if (!islight(m->otype)) |
151 |
+ |
m = &Lamb; |
152 |
+ |
} |
153 |
|
(*ofun[m->otype].funp)(m, r); /* execute function */ |
154 |
|
m->lastrno = r->rno; |
155 |
|
if (ismaterial(m->otype)) { /* materials call raytexture */ |
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 (Rdot << 1). |
247 |
> |
* incidence (namely, keep DOT(rdir,pert) < Rdot). |
248 |
|
*/ |
249 |
|
|
250 |
|
for (i = 0; i < 3; i++) |
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 |
|
{ |
319 |
|
register CUBE *scene; |
320 |
|
{ |
321 |
|
FVECT curpos; /* current cube position */ |
322 |
< |
int mpos, mneg; /* sign flags */ |
322 |
> |
int sflags; /* sign flags */ |
323 |
|
double t, dt; |
324 |
|
register int i; |
325 |
|
|
326 |
|
nrays++; /* increment trace counter */ |
327 |
< |
|
258 |
< |
mpos = mneg = 0; |
327 |
> |
sflags = 0; |
328 |
|
for (i = 0; i < 3; i++) { |
329 |
|
curpos[i] = r->rorg[i]; |
330 |
|
if (r->rdir[i] > FTINY) |
331 |
< |
mpos |= 1 << i; |
331 |
> |
sflags |= 1 << i; |
332 |
|
else if (r->rdir[i] < -FTINY) |
333 |
< |
mneg |= 1 << i; |
333 |
> |
sflags |= 0x10 << i; |
334 |
|
} |
335 |
+ |
if (sflags == 0) |
336 |
+ |
error(CONSISTENCY, "zero ray direction in localhit"); |
337 |
|
t = 0.0; |
338 |
|
if (!incube(scene, curpos)) { |
339 |
|
/* find distance to entry */ |
340 |
|
for (i = 0; i < 3; i++) { |
341 |
|
/* plane in our direction */ |
342 |
< |
if (mpos & 1<<i) |
342 |
> |
if (sflags & 1<<i) |
343 |
|
dt = scene->cuorg[i]; |
344 |
< |
else if (mneg & 1<<i) |
344 |
> |
else if (sflags & 0x10<<i) |
345 |
|
dt = scene->cuorg[i] + scene->cusize; |
346 |
|
else |
347 |
|
continue; |
358 |
|
if (!incube(scene, curpos)) /* non-intersecting ray */ |
359 |
|
return(0); |
360 |
|
} |
361 |
< |
return(raymove(curpos, mpos, mneg, r, scene) == RAYHIT); |
361 |
> |
return(raymove(curpos, sflags, r, scene) == RAYHIT); |
362 |
|
} |
363 |
|
|
364 |
|
|
365 |
|
static int |
366 |
< |
raymove(pos, plus, minus, r, cu) /* check for hit as we move */ |
366 |
> |
raymove(pos, dirf, r, cu) /* check for hit as we move */ |
367 |
|
FVECT pos; /* modified */ |
368 |
< |
int plus, minus; /* direction indicators to speed tests */ |
368 |
> |
int dirf; /* direction indicators to speed tests */ |
369 |
|
register RAY *r; |
370 |
|
register CUBE *cu; |
371 |
|
{ |
372 |
|
int ax; |
373 |
|
double dt, t; |
303 |
– |
register int sgn; |
374 |
|
|
375 |
|
if (istree(cu->cutree)) { /* recurse on subcubes */ |
376 |
|
CUBE cukid; |
377 |
< |
register int br; |
377 |
> |
register int br, sgn; |
378 |
|
|
379 |
|
cukid.cusize = cu->cusize * 0.5; /* find subcube */ |
380 |
|
VCOPY(cukid.cuorg, cu->cuorg); |
393 |
|
} |
394 |
|
for ( ; ; ) { |
395 |
|
cukid.cutree = octkid(cu->cutree, br); |
396 |
< |
if ((ax = raymove(pos,plus,minus,r,&cukid)) == RAYHIT) |
396 |
> |
if ((ax = raymove(pos,dirf,r,&cukid)) == RAYHIT) |
397 |
|
return(RAYHIT); |
398 |
|
sgn = 1 << ax; |
399 |
< |
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 |
399 |
> |
if (sgn & dirf) /* positive axis? */ |
400 |
|
if (sgn & br) |
401 |
|
return(ax); /* overflow */ |
402 |
|
else { |
403 |
|
cukid.cuorg[ax] += cukid.cusize; |
404 |
|
br |= sgn; |
405 |
|
} |
406 |
+ |
else |
407 |
+ |
if (sgn & br) { |
408 |
+ |
cukid.cuorg[ax] -= cukid.cusize; |
409 |
+ |
br &= ~sgn; |
410 |
+ |
} else |
411 |
+ |
return(ax); /* underflow */ |
412 |
|
} |
413 |
|
/*NOTREACHED*/ |
414 |
|
} |
415 |
|
if (isfull(cu->cutree) && checkhit(r, cu)) |
416 |
|
return(RAYHIT); |
417 |
|
/* advance to next cube */ |
418 |
< |
sgn = plus | minus; |
419 |
< |
if (sgn&1) { |
350 |
< |
dt = plus&1 ? cu->cuorg[0] + cu->cusize : cu->cuorg[0]; |
418 |
> |
if (dirf&0x11) { |
419 |
> |
dt = dirf&1 ? cu->cuorg[0] + cu->cusize : cu->cuorg[0]; |
420 |
|
t = (dt - pos[0])/r->rdir[0]; |
421 |
|
ax = 0; |
422 |
|
} else |
423 |
|
t = FHUGE; |
424 |
< |
if (sgn&2) { |
425 |
< |
dt = plus&2 ? cu->cuorg[1] + cu->cusize : cu->cuorg[1]; |
424 |
> |
if (dirf&0x22) { |
425 |
> |
dt = dirf&2 ? cu->cuorg[1] + cu->cusize : cu->cuorg[1]; |
426 |
|
dt = (dt - pos[1])/r->rdir[1]; |
427 |
|
if (dt < t) { |
428 |
|
t = dt; |
429 |
|
ax = 1; |
430 |
|
} |
431 |
|
} |
432 |
< |
if (sgn&4) { |
433 |
< |
dt = plus&4 ? cu->cuorg[2] + cu->cusize : cu->cuorg[2]; |
432 |
> |
if (dirf&0x44) { |
433 |
> |
dt = dirf&4 ? cu->cuorg[2] + cu->cusize : cu->cuorg[2]; |
434 |
|
dt = (dt - pos[2])/r->rdir[2]; |
435 |
|
if (dt < t) { |
436 |
|
t = dt; |