1 |
< |
/* Copyright (c) 1990 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 |
+ |
#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 |
< |
long nrays = 0L; /* number of rays traced */ |
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 int raymove(), checkset(), checkhit(); |
38 |
+ |
|
39 |
|
#define MAXLOOP 128 /* modifier loop detection */ |
40 |
|
|
41 |
|
#define RAYHIT (-1) /* return value for intercepted ray */ |
52 |
|
r->crtype = r->rtype = rt; |
53 |
|
r->rsrc = -1; |
54 |
|
r->clipset = NULL; |
55 |
+ |
r->revf = raytrace; |
56 |
|
} else { /* spawned ray */ |
57 |
|
r->rlvl = ro->rlvl; |
58 |
|
if (rt & RAYREFL) { |
63 |
|
r->rsrc = ro->rsrc; |
64 |
|
r->clipset = ro->newcset; |
65 |
|
} |
66 |
+ |
r->revf = ro->revf; |
67 |
|
r->rweight = ro->rweight * rw; |
68 |
|
r->crtype = ro->crtype | (r->rtype = rt); |
69 |
|
VCOPY(r->rorg, ro->rop); |
70 |
|
} |
71 |
< |
r->rno = nrays; |
71 |
> |
rayclear(r); |
72 |
> |
return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1); |
73 |
> |
} |
74 |
> |
|
75 |
> |
|
76 |
> |
rayclear(r) /* clear a ray for (re)evaluation */ |
77 |
> |
register RAY *r; |
78 |
> |
{ |
79 |
> |
r->rno = raynum++; |
80 |
|
r->newcset = r->clipset; |
81 |
|
r->ro = NULL; |
82 |
|
r->rot = FHUGE; |
84 |
|
setcolor(r->pcol, 1.0, 1.0, 1.0); |
85 |
|
setcolor(r->rcol, 0.0, 0.0, 0.0); |
86 |
|
r->rt = 0.0; |
63 |
– |
return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1); |
87 |
|
} |
88 |
|
|
89 |
|
|
90 |
< |
rayvalue(r) /* compute a ray's value */ |
90 |
> |
raytrace(r) /* trace a ray and compute its value */ |
91 |
|
RAY *r; |
92 |
|
{ |
93 |
|
extern int (*trace)(); |
94 |
|
|
95 |
< |
if (localhit(r, &thescene) || sourcehit(r)) |
95 |
> |
if (localhit(r, &thescene)) |
96 |
|
raycont(r); |
97 |
+ |
else if (sourcehit(r)) |
98 |
+ |
rayshade(r, r->ro->omod); |
99 |
|
|
100 |
|
if (trace != NULL) |
101 |
|
(*trace)(r); /* trace execution */ |
135 |
|
/* check for infinite loop */ |
136 |
|
if (depth++ >= MAXLOOP) |
137 |
|
objerror(r->ro, USER, "possible modifier loop"); |
138 |
+ |
r->rt = r->rot; /* set effective ray length */ |
139 |
|
for ( ; mod != OVOID; mod = m->omod) { |
140 |
|
m = objptr(mod); |
141 |
|
/****** unnecessary test since modifier() is always called |
144 |
|
error(USER, errmsg); |
145 |
|
} |
146 |
|
******/ |
147 |
+ |
/* hack for irradiance calculation */ |
148 |
+ |
if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS))) { |
149 |
+ |
if (irr_ignore(m->otype)) { |
150 |
+ |
depth--; |
151 |
+ |
raytrans(r); |
152 |
+ |
return; |
153 |
+ |
} |
154 |
+ |
if (!islight(m->otype)) |
155 |
+ |
m = &Lamb; |
156 |
+ |
} |
157 |
|
(*ofun[m->otype].funp)(m, r); /* execute function */ |
122 |
– |
m->lastrno = r->rno; |
158 |
|
if (ismaterial(m->otype)) { /* materials call raytexture */ |
159 |
|
depth--; |
160 |
|
return; /* we're done */ |
181 |
|
error(USER, errmsg); |
182 |
|
} |
183 |
|
(*ofun[m->otype].funp)(m, r); |
149 |
– |
m->lastrno = r->rno; |
184 |
|
} |
185 |
|
depth--; /* end here */ |
186 |
|
} |
320 |
|
register RAY *r; |
321 |
|
register CUBE *scene; |
322 |
|
{ |
323 |
+ |
OBJECT cxset[MAXCSET+1]; /* set of checked objects */ |
324 |
|
FVECT curpos; /* current cube position */ |
325 |
|
int sflags; /* sign flags */ |
326 |
|
double t, dt; |
327 |
|
register int i; |
328 |
|
|
329 |
|
nrays++; /* increment trace counter */ |
295 |
– |
|
330 |
|
sflags = 0; |
331 |
|
for (i = 0; i < 3; i++) { |
332 |
|
curpos[i] = r->rorg[i]; |
335 |
|
else if (r->rdir[i] < -FTINY) |
336 |
|
sflags |= 0x10 << i; |
337 |
|
} |
338 |
+ |
if (sflags == 0) |
339 |
+ |
error(CONSISTENCY, "zero ray direction in localhit"); |
340 |
|
t = 0.0; |
341 |
|
if (!incube(scene, curpos)) { |
342 |
|
/* find distance to entry */ |
361 |
|
if (!incube(scene, curpos)) /* non-intersecting ray */ |
362 |
|
return(0); |
363 |
|
} |
364 |
< |
return(raymove(curpos, sflags, r, scene) == RAYHIT); |
364 |
> |
cxset[0] = 0; |
365 |
> |
return(raymove(curpos, cxset, sflags, r, scene) == RAYHIT); |
366 |
|
} |
367 |
|
|
368 |
|
|
369 |
|
static int |
370 |
< |
raymove(pos, dirf, r, cu) /* check for hit as we move */ |
371 |
< |
FVECT pos; /* modified */ |
370 |
> |
raymove(pos, cxs, dirf, r, cu) /* check for hit as we move */ |
371 |
> |
FVECT pos; /* current position, modified herein */ |
372 |
> |
OBJECT *cxs; /* checked objects, modified by checkhit */ |
373 |
|
int dirf; /* direction indicators to speed tests */ |
374 |
|
register RAY *r; |
375 |
|
register CUBE *cu; |
398 |
|
} |
399 |
|
for ( ; ; ) { |
400 |
|
cukid.cutree = octkid(cu->cutree, br); |
401 |
< |
if ((ax = raymove(pos,dirf,r,&cukid)) == RAYHIT) |
401 |
> |
if ((ax = raymove(pos,cxs,dirf,r,&cukid)) == RAYHIT) |
402 |
|
return(RAYHIT); |
403 |
|
sgn = 1 << ax; |
404 |
|
if (sgn & dirf) /* positive axis? */ |
417 |
|
} |
418 |
|
/*NOTREACHED*/ |
419 |
|
} |
420 |
< |
if (isfull(cu->cutree) && checkhit(r, cu)) |
420 |
> |
if (isfull(cu->cutree) && checkhit(r, cu, cxs)) |
421 |
|
return(RAYHIT); |
422 |
|
/* advance to next cube */ |
423 |
|
if (dirf&0x11) { |
450 |
|
|
451 |
|
|
452 |
|
static |
453 |
< |
checkhit(r, cu) /* check for hit in full cube */ |
453 |
> |
checkhit(r, cu, cxs) /* check for hit in full cube */ |
454 |
|
register RAY *r; |
455 |
|
CUBE *cu; |
456 |
+ |
OBJECT *cxs; |
457 |
|
{ |
458 |
|
OBJECT oset[MAXSET+1]; |
459 |
|
register OBJREC *o; |
460 |
|
register int i; |
461 |
|
|
462 |
|
objset(oset, cu->cutree); |
463 |
+ |
checkset(oset, cxs); /* eliminate double-checking */ |
464 |
|
for (i = oset[0]; i > 0; i--) { |
465 |
|
o = objptr(oset[i]); |
466 |
< |
if (o->lastrno == r->rno) /* checked already? */ |
467 |
< |
continue; |
466 |
> |
if (o->omod == OVOID && issurface(o->otype)) |
467 |
> |
continue; /* ignore void surfaces */ |
468 |
|
(*ofun[o->otype].funp)(o, r); |
429 |
– |
o->lastrno = r->rno; |
469 |
|
} |
470 |
|
if (r->ro == NULL) |
471 |
|
return(0); /* no scores yet */ |
472 |
|
|
473 |
|
return(incube(cu, r->rop)); /* hit OK if in current cube */ |
474 |
+ |
} |
475 |
+ |
|
476 |
+ |
|
477 |
+ |
static |
478 |
+ |
checkset(os, cs) /* modify checked set and set to check */ |
479 |
+ |
register OBJECT *os; /* os' = os - cs */ |
480 |
+ |
register OBJECT *cs; /* cs' = cs + os */ |
481 |
+ |
{ |
482 |
+ |
OBJECT cset[MAXCSET+MAXSET+1]; |
483 |
+ |
register int i, j; |
484 |
+ |
int k; |
485 |
+ |
/* copy os in place, cset <- cs */ |
486 |
+ |
cset[0] = 0; |
487 |
+ |
k = 0; |
488 |
+ |
for (i = j = 1; i <= os[0]; i++) { |
489 |
+ |
while (j <= cs[0] && cs[j] < os[i]) |
490 |
+ |
cset[++cset[0]] = cs[j++]; |
491 |
+ |
if (j > cs[0] || os[i] != cs[j]) { /* object to check */ |
492 |
+ |
os[++k] = os[i]; |
493 |
+ |
cset[++cset[0]] = os[i]; |
494 |
+ |
} |
495 |
+ |
} |
496 |
+ |
if (!(os[0] = k)) /* new "to check" set size */ |
497 |
+ |
return; /* special case */ |
498 |
+ |
while (j <= cs[0]) /* get the rest of cs */ |
499 |
+ |
cset[++cset[0]] = cs[j++]; |
500 |
+ |
if (cset[0] > MAXCSET) /* truncate "checked" set if nec. */ |
501 |
+ |
cset[0] = MAXCSET; |
502 |
+ |
/* setcopy(cs, cset); */ /* copy cset back to cs */ |
503 |
+ |
os = cset; |
504 |
+ |
for (i = os[0]; i-- >= 0; ) |
505 |
+ |
*cs++ = *os++; |
506 |
|
} |