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 */ |
27 |
|
|
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 */ |
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) || sourcehit(r)) |
82 |
< |
/* check for clipped object */ |
83 |
< |
if (r->clipset != NULL && inset(r->clipset, r->ro->omod)) |
84 |
< |
raytrans(r); |
75 |
< |
else |
76 |
< |
rayshade(r, r->ro->omod); |
81 |
> |
if (localhit(r, &thescene)) |
82 |
> |
raycont(r); |
83 |
> |
else if (sourcehit(r)) |
84 |
> |
rayshade(r, r->ro->omod); |
85 |
|
|
86 |
|
if (trace != NULL) |
87 |
|
(*trace)(r); /* trace execution */ |
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 |
|
|
121 |
|
/* check for infinite loop */ |
122 |
|
if (depth++ >= MAXLOOP) |
123 |
|
objerror(r->ro, USER, "possible modifier loop"); |
124 |
+ |
r->rt = r->rot; /* set effective ray length */ |
125 |
|
for ( ; mod != OVOID; mod = m->omod) { |
126 |
|
m = objptr(mod); |
127 |
|
/****** unnecessary test since modifier() is always called |
130 |
|
error(USER, errmsg); |
131 |
|
} |
132 |
|
******/ |
133 |
+ |
/* hack for irradiance calculation */ |
134 |
+ |
if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS))) { |
135 |
+ |
if (irr_ignore(m->otype)) { |
136 |
+ |
depth--; |
137 |
+ |
raytrans(r); |
138 |
+ |
return; |
139 |
+ |
} |
140 |
+ |
if (!islight(m->otype)) |
141 |
+ |
m = &Lamb; |
142 |
+ |
} |
143 |
|
(*ofun[m->otype].funp)(m, r); /* execute function */ |
144 |
|
m->lastrno = r->rno; |
145 |
|
if (ismaterial(m->otype)) { /* materials call raytexture */ |
234 |
|
* still fraught with problems since reflected rays and similar |
235 |
|
* directions calculated from the surface normal may spawn rays behind |
236 |
|
* the surface. The only solution is to curb textures at high |
237 |
< |
* incidence (Rdot << 1). |
237 |
> |
* incidence (namely, keep DOT(rdir,pert) < Rdot). |
238 |
|
*/ |
239 |
|
|
240 |
|
for (i = 0; i < 3; i++) |
255 |
|
} |
256 |
|
|
257 |
|
|
258 |
+ |
newrayxf(r) /* get new tranformation matrix for ray */ |
259 |
+ |
RAY *r; |
260 |
+ |
{ |
261 |
+ |
static struct xfn { |
262 |
+ |
struct xfn *next; |
263 |
+ |
FULLXF xf; |
264 |
+ |
} xfseed = { &xfseed }, *xflast = &xfseed; |
265 |
+ |
register struct xfn *xp; |
266 |
+ |
register RAY *rp; |
267 |
+ |
|
268 |
+ |
/* |
269 |
+ |
* Search for transform in circular list that |
270 |
+ |
* has no associated ray in the tree. |
271 |
+ |
*/ |
272 |
+ |
xp = xflast; |
273 |
+ |
for (rp = r->parent; rp != NULL; rp = rp->parent) |
274 |
+ |
if (rp->rox == &xp->xf) { /* xp in use */ |
275 |
+ |
xp = xp->next; /* move to next */ |
276 |
+ |
if (xp == xflast) { /* need new one */ |
277 |
+ |
xp = (struct xfn *)bmalloc(sizeof(struct xfn)); |
278 |
+ |
if (xp == NULL) |
279 |
+ |
error(SYSTEM, |
280 |
+ |
"out of memory in newrayxf"); |
281 |
+ |
/* insert in list */ |
282 |
+ |
xp->next = xflast->next; |
283 |
+ |
xflast->next = xp; |
284 |
+ |
break; /* we're done */ |
285 |
+ |
} |
286 |
+ |
rp = r; /* start check over */ |
287 |
+ |
} |
288 |
+ |
/* got it */ |
289 |
+ |
r->rox = &xp->xf; |
290 |
+ |
xflast = xp; |
291 |
+ |
} |
292 |
+ |
|
293 |
+ |
|
294 |
|
flipsurface(r) /* reverse surface orientation */ |
295 |
|
register RAY *r; |
296 |
|
{ |
309 |
|
register CUBE *scene; |
310 |
|
{ |
311 |
|
FVECT curpos; /* current cube position */ |
312 |
< |
int mpos, mneg; /* sign flags */ |
312 |
> |
int sflags; /* sign flags */ |
313 |
|
double t, dt; |
314 |
|
register int i; |
315 |
|
|
316 |
|
nrays++; /* increment trace counter */ |
317 |
|
|
318 |
< |
mpos = mneg = 0; |
318 |
> |
sflags = 0; |
319 |
|
for (i = 0; i < 3; i++) { |
320 |
|
curpos[i] = r->rorg[i]; |
321 |
|
if (r->rdir[i] > FTINY) |
322 |
< |
mpos |= 1 << i; |
322 |
> |
sflags |= 1 << i; |
323 |
|
else if (r->rdir[i] < -FTINY) |
324 |
< |
mneg |= 1 << i; |
324 |
> |
sflags |= 0x10 << i; |
325 |
|
} |
326 |
+ |
if (sflags == 0) |
327 |
+ |
error(CONSISTENCY, "zero ray direction in localhit"); |
328 |
|
t = 0.0; |
329 |
|
if (!incube(scene, curpos)) { |
330 |
|
/* find distance to entry */ |
331 |
|
for (i = 0; i < 3; i++) { |
332 |
|
/* plane in our direction */ |
333 |
< |
if (mpos & 1<<i) |
333 |
> |
if (sflags & 1<<i) |
334 |
|
dt = scene->cuorg[i]; |
335 |
< |
else if (mneg & 1<<i) |
335 |
> |
else if (sflags & 0x10<<i) |
336 |
|
dt = scene->cuorg[i] + scene->cusize; |
337 |
|
else |
338 |
|
continue; |
349 |
|
if (!incube(scene, curpos)) /* non-intersecting ray */ |
350 |
|
return(0); |
351 |
|
} |
352 |
< |
return(raymove(curpos, mpos, mneg, r, scene) == RAYHIT); |
352 |
> |
return(raymove(curpos, sflags, r, scene) == RAYHIT); |
353 |
|
} |
354 |
|
|
355 |
|
|
356 |
|
static int |
357 |
< |
raymove(pos, plus, minus, r, cu) /* check for hit as we move */ |
357 |
> |
raymove(pos, dirf, r, cu) /* check for hit as we move */ |
358 |
|
FVECT pos; /* modified */ |
359 |
< |
int plus, minus; /* direction indicators to speed tests */ |
359 |
> |
int dirf; /* direction indicators to speed tests */ |
360 |
|
register RAY *r; |
361 |
|
register CUBE *cu; |
362 |
|
{ |
363 |
|
int ax; |
364 |
|
double dt, t; |
297 |
– |
register int sgn; |
365 |
|
|
366 |
|
if (istree(cu->cutree)) { /* recurse on subcubes */ |
367 |
|
CUBE cukid; |
368 |
< |
register int br; |
368 |
> |
register int br, sgn; |
369 |
|
|
370 |
|
cukid.cusize = cu->cusize * 0.5; /* find subcube */ |
371 |
|
VCOPY(cukid.cuorg, cu->cuorg); |
384 |
|
} |
385 |
|
for ( ; ; ) { |
386 |
|
cukid.cutree = octkid(cu->cutree, br); |
387 |
< |
if ((ax = raymove(pos,plus,minus,r,&cukid)) == RAYHIT) |
387 |
> |
if ((ax = raymove(pos,dirf,r,&cukid)) == RAYHIT) |
388 |
|
return(RAYHIT); |
389 |
|
sgn = 1 << ax; |
390 |
< |
if (sgn & minus) /* negative axis? */ |
324 |
< |
if (sgn & br) { |
325 |
< |
cukid.cuorg[ax] -= cukid.cusize; |
326 |
< |
br &= ~sgn; |
327 |
< |
} else |
328 |
< |
return(ax); /* underflow */ |
329 |
< |
else |
390 |
> |
if (sgn & dirf) /* positive axis? */ |
391 |
|
if (sgn & br) |
392 |
|
return(ax); /* overflow */ |
393 |
|
else { |
394 |
|
cukid.cuorg[ax] += cukid.cusize; |
395 |
|
br |= sgn; |
396 |
|
} |
397 |
+ |
else |
398 |
+ |
if (sgn & br) { |
399 |
+ |
cukid.cuorg[ax] -= cukid.cusize; |
400 |
+ |
br &= ~sgn; |
401 |
+ |
} else |
402 |
+ |
return(ax); /* underflow */ |
403 |
|
} |
404 |
|
/*NOTREACHED*/ |
405 |
|
} |
406 |
|
if (isfull(cu->cutree) && checkhit(r, cu)) |
407 |
|
return(RAYHIT); |
408 |
|
/* advance to next cube */ |
409 |
< |
sgn = plus | minus; |
410 |
< |
if (sgn&1) { |
344 |
< |
dt = plus&1 ? cu->cuorg[0] + cu->cusize : cu->cuorg[0]; |
409 |
> |
if (dirf&0x11) { |
410 |
> |
dt = dirf&1 ? cu->cuorg[0] + cu->cusize : cu->cuorg[0]; |
411 |
|
t = (dt - pos[0])/r->rdir[0]; |
412 |
|
ax = 0; |
413 |
|
} else |
414 |
|
t = FHUGE; |
415 |
< |
if (sgn&2) { |
416 |
< |
dt = plus&2 ? cu->cuorg[1] + cu->cusize : cu->cuorg[1]; |
415 |
> |
if (dirf&0x22) { |
416 |
> |
dt = dirf&2 ? cu->cuorg[1] + cu->cusize : cu->cuorg[1]; |
417 |
|
dt = (dt - pos[1])/r->rdir[1]; |
418 |
|
if (dt < t) { |
419 |
|
t = dt; |
420 |
|
ax = 1; |
421 |
|
} |
422 |
|
} |
423 |
< |
if (sgn&4) { |
424 |
< |
dt = plus&4 ? cu->cuorg[2] + cu->cusize : cu->cuorg[2]; |
423 |
> |
if (dirf&0x44) { |
424 |
> |
dt = dirf&4 ? cu->cuorg[2] + cu->cusize : cu->cuorg[2]; |
425 |
|
dt = (dt - pos[2])/r->rdir[2]; |
426 |
|
if (dt < t) { |
427 |
|
t = dt; |