1 |
< |
/* Copyright (c) 1986 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1990 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 */ |
27 |
|
|
28 |
< |
#define MAXLOOP 32 /* modifier loop detection */ |
28 |
> |
static double Lambfa[5] = {PI, PI, PI, 0.0, 0.0}; |
29 |
> |
OBJREC Lamb = { |
30 |
> |
OVOID, MAT_PLASTIC, "Lambertian", |
31 |
> |
{0, 5, NULL, Lambfa}, NULL, -1, |
32 |
> |
}; /* a Lambertian surface */ |
33 |
|
|
34 |
+ |
#define MAXLOOP 128 /* modifier loop detection */ |
35 |
+ |
|
36 |
|
#define RAYHIT (-1) /* return value for intercepted ray */ |
37 |
|
|
38 |
|
|
68 |
|
r->pert[0] = r->pert[1] = r->pert[2] = 0.0; |
69 |
|
setcolor(r->pcol, 1.0, 1.0, 1.0); |
70 |
|
setcolor(r->rcol, 0.0, 0.0, 0.0); |
71 |
+ |
r->rt = 0.0; |
72 |
|
return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1); |
73 |
|
} |
74 |
|
|
75 |
|
|
76 |
|
rayvalue(r) /* compute a ray's value */ |
77 |
< |
register RAY *r; |
77 |
> |
RAY *r; |
78 |
|
{ |
79 |
|
extern int (*trace)(); |
80 |
|
|
81 |
|
if (localhit(r, &thescene)) |
82 |
< |
if (r->clipset != NULL && inset(r->clipset, r->ro->omod)) |
73 |
< |
raytrans(r); /* object is clipped */ |
74 |
< |
else |
75 |
< |
rayshade(r, r->ro->omod); |
82 |
> |
raycont(r); |
83 |
|
else if (sourcehit(r)) |
84 |
|
rayshade(r, r->ro->omod); |
85 |
|
|
88 |
|
} |
89 |
|
|
90 |
|
|
91 |
+ |
raycont(r) /* check for clipped object and continue */ |
92 |
+ |
register RAY *r; |
93 |
+ |
{ |
94 |
+ |
if (r->clipset != NULL && inset(r->clipset, r->ro->omod)) |
95 |
+ |
raytrans(r); |
96 |
+ |
else |
97 |
+ |
rayshade(r, r->ro->omod); |
98 |
+ |
} |
99 |
+ |
|
100 |
+ |
|
101 |
|
raytrans(r) /* transmit ray as is */ |
102 |
< |
RAY *r; |
102 |
> |
register RAY *r; |
103 |
|
{ |
104 |
|
RAY tr; |
105 |
|
|
107 |
|
VCOPY(tr.rdir, r->rdir); |
108 |
|
rayvalue(&tr); |
109 |
|
copycolor(r->rcol, tr.rcol); |
110 |
+ |
r->rt = r->rot + tr.rt; |
111 |
|
} |
112 |
|
} |
113 |
|
|
120 |
|
register OBJREC *m; |
121 |
|
/* check for infinite loop */ |
122 |
|
if (depth++ >= MAXLOOP) |
123 |
< |
objerror(r->ro, USER, "material loop"); |
123 |
> |
objerror(r->ro, USER, "possible modifier loop"); |
124 |
|
for ( ; mod != OVOID; mod = m->omod) { |
125 |
|
m = objptr(mod); |
126 |
+ |
/****** unnecessary test since modifier() is always called |
127 |
|
if (!ismodifier(m->otype)) { |
128 |
|
sprintf(errmsg, "illegal modifier \"%s\"", m->oname); |
129 |
|
error(USER, errmsg); |
130 |
|
} |
131 |
+ |
******/ |
132 |
+ |
/* hack for irradiance calculation */ |
133 |
+ |
if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS))) { |
134 |
+ |
if (irr_ignore(m->otype)) { |
135 |
+ |
depth--; |
136 |
+ |
raytrans(r); |
137 |
+ |
return; |
138 |
+ |
} |
139 |
+ |
if (m->otype != MAT_ILLUM) |
140 |
+ |
m = &Lamb; |
141 |
+ |
} |
142 |
|
(*ofun[m->otype].funp)(m, r); /* execute function */ |
143 |
|
m->lastrno = r->rno; |
144 |
|
if (ismaterial(m->otype)) { /* materials call raytexture */ |
233 |
|
* still fraught with problems since reflected rays and similar |
234 |
|
* directions calculated from the surface normal may spawn rays behind |
235 |
|
* the surface. The only solution is to curb textures at high |
236 |
< |
* incidence (Rdot << 1). |
236 |
> |
* incidence (namely, keep DOT(rdir,pert) < Rdot). |
237 |
|
*/ |
238 |
|
|
239 |
|
for (i = 0; i < 3; i++) |
254 |
|
} |
255 |
|
|
256 |
|
|
257 |
+ |
newrayxf(r) /* get new tranformation matrix for ray */ |
258 |
+ |
RAY *r; |
259 |
+ |
{ |
260 |
+ |
static struct xfn { |
261 |
+ |
struct xfn *next; |
262 |
+ |
FULLXF xf; |
263 |
+ |
} xfseed = { &xfseed }, *xflast = &xfseed; |
264 |
+ |
register struct xfn *xp; |
265 |
+ |
register RAY *rp; |
266 |
+ |
|
267 |
+ |
/* |
268 |
+ |
* Search for transform in circular list that |
269 |
+ |
* has no associated ray in the tree. |
270 |
+ |
*/ |
271 |
+ |
xp = xflast; |
272 |
+ |
for (rp = r->parent; rp != NULL; rp = rp->parent) |
273 |
+ |
if (rp->rox == &xp->xf) { /* xp in use */ |
274 |
+ |
xp = xp->next; /* move to next */ |
275 |
+ |
if (xp == xflast) { /* need new one */ |
276 |
+ |
xp = (struct xfn *)bmalloc(sizeof(struct xfn)); |
277 |
+ |
if (xp == NULL) |
278 |
+ |
error(SYSTEM, |
279 |
+ |
"out of memory in newrayxf"); |
280 |
+ |
/* insert in list */ |
281 |
+ |
xp->next = xflast->next; |
282 |
+ |
xflast->next = xp; |
283 |
+ |
break; /* we're done */ |
284 |
+ |
} |
285 |
+ |
rp = r; /* start check over */ |
286 |
+ |
} |
287 |
+ |
/* got it */ |
288 |
+ |
r->rox = &xp->xf; |
289 |
+ |
xflast = xp; |
290 |
+ |
} |
291 |
+ |
|
292 |
+ |
|
293 |
|
flipsurface(r) /* reverse surface orientation */ |
294 |
|
register RAY *r; |
295 |
|
{ |
308 |
|
register CUBE *scene; |
309 |
|
{ |
310 |
|
FVECT curpos; /* current cube position */ |
311 |
< |
int mpos, mneg; /* sign flags */ |
311 |
> |
int sflags; /* sign flags */ |
312 |
|
double t, dt; |
313 |
|
register int i; |
314 |
|
|
315 |
|
nrays++; /* increment trace counter */ |
316 |
|
|
317 |
< |
mpos = mneg = 0; |
317 |
> |
sflags = 0; |
318 |
|
for (i = 0; i < 3; i++) { |
319 |
|
curpos[i] = r->rorg[i]; |
320 |
|
if (r->rdir[i] > FTINY) |
321 |
< |
mpos |= 1 << i; |
321 |
> |
sflags |= 1 << i; |
322 |
|
else if (r->rdir[i] < -FTINY) |
323 |
< |
mneg |= 1 << i; |
323 |
> |
sflags |= 0x10 << i; |
324 |
|
} |
325 |
|
t = 0.0; |
326 |
|
if (!incube(scene, curpos)) { |
327 |
|
/* find distance to entry */ |
328 |
|
for (i = 0; i < 3; i++) { |
329 |
|
/* plane in our direction */ |
330 |
< |
if (mpos & 1<<i) |
330 |
> |
if (sflags & 1<<i) |
331 |
|
dt = scene->cuorg[i]; |
332 |
< |
else if (mneg & 1<<i) |
332 |
> |
else if (sflags & 0x10<<i) |
333 |
|
dt = scene->cuorg[i] + scene->cusize; |
334 |
|
else |
335 |
|
continue; |
346 |
|
if (!incube(scene, curpos)) /* non-intersecting ray */ |
347 |
|
return(0); |
348 |
|
} |
349 |
< |
return(raymove(curpos, mpos, mneg, r, scene) == RAYHIT); |
349 |
> |
return(raymove(curpos, sflags, r, scene) == RAYHIT); |
350 |
|
} |
351 |
|
|
352 |
|
|
353 |
|
static int |
354 |
< |
raymove(pos, plus, minus, r, cu) /* check for hit as we move */ |
354 |
> |
raymove(pos, dirf, r, cu) /* check for hit as we move */ |
355 |
|
FVECT pos; /* modified */ |
356 |
< |
int plus, minus; /* direction indicators to speed tests */ |
356 |
> |
int dirf; /* direction indicators to speed tests */ |
357 |
|
register RAY *r; |
358 |
|
register CUBE *cu; |
359 |
|
{ |
360 |
|
int ax; |
361 |
|
double dt, t; |
296 |
– |
register int sgn; |
362 |
|
|
363 |
|
if (istree(cu->cutree)) { /* recurse on subcubes */ |
364 |
|
CUBE cukid; |
365 |
< |
register int br; |
365 |
> |
register int br, sgn; |
366 |
|
|
367 |
|
cukid.cusize = cu->cusize * 0.5; /* find subcube */ |
368 |
|
VCOPY(cukid.cuorg, cu->cuorg); |
381 |
|
} |
382 |
|
for ( ; ; ) { |
383 |
|
cukid.cutree = octkid(cu->cutree, br); |
384 |
< |
if ((ax = raymove(pos,plus,minus,r,&cukid)) == RAYHIT) |
384 |
> |
if ((ax = raymove(pos,dirf,r,&cukid)) == RAYHIT) |
385 |
|
return(RAYHIT); |
386 |
|
sgn = 1 << ax; |
387 |
< |
if (sgn & minus) /* negative axis? */ |
323 |
< |
if (sgn & br) { |
324 |
< |
cukid.cuorg[ax] -= cukid.cusize; |
325 |
< |
br &= ~sgn; |
326 |
< |
} else |
327 |
< |
return(ax); /* underflow */ |
328 |
< |
else |
387 |
> |
if (sgn & dirf) /* positive axis? */ |
388 |
|
if (sgn & br) |
389 |
|
return(ax); /* overflow */ |
390 |
|
else { |
391 |
|
cukid.cuorg[ax] += cukid.cusize; |
392 |
|
br |= sgn; |
393 |
|
} |
394 |
+ |
else |
395 |
+ |
if (sgn & br) { |
396 |
+ |
cukid.cuorg[ax] -= cukid.cusize; |
397 |
+ |
br &= ~sgn; |
398 |
+ |
} else |
399 |
+ |
return(ax); /* underflow */ |
400 |
|
} |
401 |
|
/*NOTREACHED*/ |
402 |
|
} |
403 |
|
if (isfull(cu->cutree) && checkhit(r, cu)) |
404 |
|
return(RAYHIT); |
405 |
|
/* advance to next cube */ |
406 |
< |
sgn = plus | minus; |
407 |
< |
if (sgn&1) { |
343 |
< |
dt = plus&1 ? cu->cuorg[0] + cu->cusize : cu->cuorg[0]; |
406 |
> |
if (dirf&0x11) { |
407 |
> |
dt = dirf&1 ? cu->cuorg[0] + cu->cusize : cu->cuorg[0]; |
408 |
|
t = (dt - pos[0])/r->rdir[0]; |
409 |
|
ax = 0; |
410 |
|
} else |
411 |
|
t = FHUGE; |
412 |
< |
if (sgn&2) { |
413 |
< |
dt = plus&2 ? cu->cuorg[1] + cu->cusize : cu->cuorg[1]; |
412 |
> |
if (dirf&0x22) { |
413 |
> |
dt = dirf&2 ? cu->cuorg[1] + cu->cusize : cu->cuorg[1]; |
414 |
|
dt = (dt - pos[1])/r->rdir[1]; |
415 |
|
if (dt < t) { |
416 |
|
t = dt; |
417 |
|
ax = 1; |
418 |
|
} |
419 |
|
} |
420 |
< |
if (sgn&4) { |
421 |
< |
dt = plus&4 ? cu->cuorg[2] + cu->cusize : cu->cuorg[2]; |
420 |
> |
if (dirf&0x44) { |
421 |
> |
dt = dirf&4 ? cu->cuorg[2] + cu->cusize : cu->cuorg[2]; |
422 |
|
dt = (dt - pos[2])/r->rdir[2]; |
423 |
|
if (dt < t) { |
424 |
|
t = dt; |