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"; |
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 |
> |
long raynum = 0L; /* next unique ray number */ |
29 |
> |
long nrays = 0L; /* number of calls to localhit */ |
30 |
|
|
31 |
< |
static double Lambfa[5] = {PI, PI, PI, 0.0, 0.0}; |
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, -1, |
34 |
> |
{0, 5, NULL, Lambfa}, NULL, |
35 |
|
}; /* a Lambertian surface */ |
36 |
|
|
37 |
|
#define MAXLOOP 128 /* modifier loop detection */ |
50 |
|
r->crtype = r->rtype = rt; |
51 |
|
r->rsrc = -1; |
52 |
|
r->clipset = NULL; |
53 |
+ |
r->revf = raytrace; |
54 |
|
} else { /* spawned ray */ |
55 |
|
r->rlvl = ro->rlvl; |
56 |
|
if (rt & RAYREFL) { |
61 |
|
r->rsrc = ro->rsrc; |
62 |
|
r->clipset = ro->newcset; |
63 |
|
} |
64 |
+ |
r->revf = ro->revf; |
65 |
|
r->rweight = ro->rweight * rw; |
66 |
|
r->crtype = ro->crtype | (r->rtype = rt); |
67 |
|
VCOPY(r->rorg, ro->rop); |
68 |
|
} |
69 |
< |
r->rno = nrays; |
69 |
> |
rayclear(r); |
70 |
> |
return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1); |
71 |
> |
} |
72 |
> |
|
73 |
> |
|
74 |
> |
rayclear(r) /* clear a ray for (re)evaluation */ |
75 |
> |
register RAY *r; |
76 |
> |
{ |
77 |
> |
r->rno = raynum++; |
78 |
|
r->newcset = r->clipset; |
79 |
|
r->ro = NULL; |
80 |
|
r->rot = FHUGE; |
82 |
|
setcolor(r->pcol, 1.0, 1.0, 1.0); |
83 |
|
setcolor(r->rcol, 0.0, 0.0, 0.0); |
84 |
|
r->rt = 0.0; |
72 |
– |
return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1); |
85 |
|
} |
86 |
|
|
87 |
|
|
88 |
< |
rayvalue(r) /* compute a ray's value */ |
88 |
> |
raytrace(r) /* trace a ray and compute its value */ |
89 |
|
RAY *r; |
90 |
|
{ |
91 |
|
extern int (*trace)(); |
133 |
|
/* check for infinite loop */ |
134 |
|
if (depth++ >= MAXLOOP) |
135 |
|
objerror(r->ro, USER, "possible modifier loop"); |
136 |
+ |
r->rt = r->rot; /* set effective ray length */ |
137 |
|
for ( ; mod != OVOID; mod = m->omod) { |
138 |
|
m = objptr(mod); |
139 |
|
/****** unnecessary test since modifier() is always called |
149 |
|
raytrans(r); |
150 |
|
return; |
151 |
|
} |
152 |
< |
if (m->otype != MAT_ILLUM) |
152 |
> |
if (!islight(m->otype)) |
153 |
|
m = &Lamb; |
154 |
|
} |
155 |
|
(*ofun[m->otype].funp)(m, r); /* execute function */ |
143 |
– |
m->lastrno = r->rno; |
156 |
|
if (ismaterial(m->otype)) { /* materials call raytexture */ |
157 |
|
depth--; |
158 |
|
return; /* we're done */ |
179 |
|
error(USER, errmsg); |
180 |
|
} |
181 |
|
(*ofun[m->otype].funp)(m, r); |
170 |
– |
m->lastrno = r->rno; |
182 |
|
} |
183 |
|
depth--; /* end here */ |
184 |
|
} |
318 |
|
register RAY *r; |
319 |
|
register CUBE *scene; |
320 |
|
{ |
321 |
+ |
OBJECT cxset[MAXCSET+1]; /* set of checked objects */ |
322 |
|
FVECT curpos; /* current cube position */ |
323 |
|
int sflags; /* sign flags */ |
324 |
|
double t, dt; |
325 |
|
register int i; |
326 |
|
|
327 |
|
nrays++; /* increment trace counter */ |
316 |
– |
|
328 |
|
sflags = 0; |
329 |
|
for (i = 0; i < 3; i++) { |
330 |
|
curpos[i] = r->rorg[i]; |
333 |
|
else if (r->rdir[i] < -FTINY) |
334 |
|
sflags |= 0x10 << i; |
335 |
|
} |
336 |
+ |
if (sflags == 0) |
337 |
+ |
error(CONSISTENCY, "zero ray direction in localhit"); |
338 |
|
t = 0.0; |
339 |
|
if (!incube(scene, curpos)) { |
340 |
|
/* find distance to entry */ |
359 |
|
if (!incube(scene, curpos)) /* non-intersecting ray */ |
360 |
|
return(0); |
361 |
|
} |
362 |
< |
return(raymove(curpos, sflags, r, scene) == RAYHIT); |
362 |
> |
cxset[0] = 0; |
363 |
> |
return(raymove(curpos, cxset, sflags, r, scene) == RAYHIT); |
364 |
|
} |
365 |
|
|
366 |
|
|
367 |
|
static int |
368 |
< |
raymove(pos, dirf, r, cu) /* check for hit as we move */ |
369 |
< |
FVECT pos; /* modified */ |
368 |
> |
raymove(pos, cxs, dirf, r, cu) /* check for hit as we move */ |
369 |
> |
FVECT pos; /* current position, modified herein */ |
370 |
> |
OBJECT *cxs; /* checked objects, modified by checkhit */ |
371 |
|
int dirf; /* direction indicators to speed tests */ |
372 |
|
register RAY *r; |
373 |
|
register CUBE *cu; |
396 |
|
} |
397 |
|
for ( ; ; ) { |
398 |
|
cukid.cutree = octkid(cu->cutree, br); |
399 |
< |
if ((ax = raymove(pos,dirf,r,&cukid)) == RAYHIT) |
399 |
> |
if ((ax = raymove(pos,cxs,dirf,r,&cukid)) == RAYHIT) |
400 |
|
return(RAYHIT); |
401 |
|
sgn = 1 << ax; |
402 |
|
if (sgn & dirf) /* positive axis? */ |
415 |
|
} |
416 |
|
/*NOTREACHED*/ |
417 |
|
} |
418 |
< |
if (isfull(cu->cutree) && checkhit(r, cu)) |
418 |
> |
if (isfull(cu->cutree) && checkhit(r, cu, cxs)) |
419 |
|
return(RAYHIT); |
420 |
|
/* advance to next cube */ |
421 |
|
if (dirf&0x11) { |
448 |
|
|
449 |
|
|
450 |
|
static |
451 |
< |
checkhit(r, cu) /* check for hit in full cube */ |
451 |
> |
checkhit(r, cu, cxs) /* check for hit in full cube */ |
452 |
|
register RAY *r; |
453 |
|
CUBE *cu; |
454 |
+ |
OBJECT *cxs; |
455 |
|
{ |
456 |
|
OBJECT oset[MAXSET+1]; |
457 |
|
register OBJREC *o; |
458 |
|
register int i; |
459 |
|
|
460 |
|
objset(oset, cu->cutree); |
461 |
+ |
checkset(oset, cxs); /* eliminate double-checking */ |
462 |
|
for (i = oset[0]; i > 0; i--) { |
463 |
|
o = objptr(oset[i]); |
447 |
– |
if (o->lastrno == r->rno) /* checked already? */ |
448 |
– |
continue; |
464 |
|
(*ofun[o->otype].funp)(o, r); |
450 |
– |
o->lastrno = r->rno; |
465 |
|
} |
466 |
|
if (r->ro == NULL) |
467 |
|
return(0); /* no scores yet */ |
468 |
|
|
469 |
|
return(incube(cu, r->rop)); /* hit OK if in current cube */ |
470 |
+ |
} |
471 |
+ |
|
472 |
+ |
|
473 |
+ |
static |
474 |
+ |
checkset(os, cs) /* modify checked set and set to check */ |
475 |
+ |
register OBJECT *os; /* os' = os - cs */ |
476 |
+ |
register OBJECT *cs; /* cs' = cs + os */ |
477 |
+ |
{ |
478 |
+ |
OBJECT cset[MAXCSET+MAXSET+1]; |
479 |
+ |
register int i, j; |
480 |
+ |
int k; |
481 |
+ |
/* copy os in place, cset <- cs */ |
482 |
+ |
cset[0] = 0; |
483 |
+ |
k = 0; |
484 |
+ |
for (i = j = 1; i <= os[0]; i++) { |
485 |
+ |
while (j <= cs[0] && cs[j] < os[i]) |
486 |
+ |
cset[++cset[0]] = cs[j++]; |
487 |
+ |
if (j > cs[0] || os[i] != cs[j]) { /* object to check */ |
488 |
+ |
os[++k] = os[i]; |
489 |
+ |
cset[++cset[0]] = os[i]; |
490 |
+ |
} |
491 |
+ |
} |
492 |
+ |
if (!(os[0] = k)) /* new "to check" set size */ |
493 |
+ |
return; /* special case */ |
494 |
+ |
while (j <= cs[0]) /* get the rest of cs */ |
495 |
+ |
cset[++cset[0]] = cs[j++]; |
496 |
+ |
if (cset[0] > MAXCSET) /* truncate "checked" set if nec. */ |
497 |
+ |
cset[0] = MAXCSET; |
498 |
+ |
/* setcopy(cs, cset); */ /* copy cset back to cs */ |
499 |
+ |
os = cset; |
500 |
+ |
for (i = os[0]; i-- >= 0; ) |
501 |
+ |
*cs++ = *os++; |
502 |
|
} |