| 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 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 |
+ |
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; |
| 72 |
– |
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)(); |
| 105 |
|
raycont(r) /* check for clipped object and continue */ |
| 106 |
|
register RAY *r; |
| 107 |
|
{ |
| 108 |
< |
if (r->clipset != NULL && inset(r->clipset, r->ro->omod)) |
| 108 |
> |
if ((r->clipset != NULL && inset(r->clipset, r->ro->omod)) || |
| 109 |
> |
r->ro->omod == OVOID) |
| 110 |
|
raytrans(r); |
| 111 |
|
else |
| 112 |
|
rayshade(r, r->ro->omod); |
| 136 |
|
/* check for infinite loop */ |
| 137 |
|
if (depth++ >= MAXLOOP) |
| 138 |
|
objerror(r->ro, USER, "possible modifier loop"); |
| 139 |
+ |
r->rt = r->rot; /* set effective ray length */ |
| 140 |
|
for ( ; mod != OVOID; mod = m->omod) { |
| 141 |
|
m = objptr(mod); |
| 142 |
|
/****** unnecessary test since modifier() is always called |
| 156 |
|
m = &Lamb; |
| 157 |
|
} |
| 158 |
|
(*ofun[m->otype].funp)(m, r); /* execute function */ |
| 143 |
– |
m->lastrno = r->rno; |
| 159 |
|
if (ismaterial(m->otype)) { /* materials call raytexture */ |
| 160 |
|
depth--; |
| 161 |
|
return; /* we're done */ |
| 182 |
|
error(USER, errmsg); |
| 183 |
|
} |
| 184 |
|
(*ofun[m->otype].funp)(m, r); |
| 170 |
– |
m->lastrno = r->rno; |
| 185 |
|
} |
| 186 |
|
depth--; /* end here */ |
| 187 |
|
} |
| 321 |
|
register RAY *r; |
| 322 |
|
register CUBE *scene; |
| 323 |
|
{ |
| 324 |
+ |
OBJECT cxset[MAXCSET+1]; /* set of checked objects */ |
| 325 |
|
FVECT curpos; /* current cube position */ |
| 326 |
|
int sflags; /* sign flags */ |
| 327 |
|
double t, dt; |
| 328 |
|
register int i; |
| 329 |
|
|
| 330 |
|
nrays++; /* increment trace counter */ |
| 316 |
– |
|
| 331 |
|
sflags = 0; |
| 332 |
|
for (i = 0; i < 3; i++) { |
| 333 |
|
curpos[i] = r->rorg[i]; |
| 334 |
< |
if (r->rdir[i] > FTINY) |
| 334 |
> |
if (r->rdir[i] > 1e-7) |
| 335 |
|
sflags |= 1 << i; |
| 336 |
< |
else if (r->rdir[i] < -FTINY) |
| 336 |
> |
else if (r->rdir[i] < -1e-7) |
| 337 |
|
sflags |= 0x10 << i; |
| 338 |
|
} |
| 339 |
|
if (sflags == 0) |
| 362 |
|
if (!incube(scene, curpos)) /* non-intersecting ray */ |
| 363 |
|
return(0); |
| 364 |
|
} |
| 365 |
< |
return(raymove(curpos, sflags, r, scene) == RAYHIT); |
| 365 |
> |
cxset[0] = 0; |
| 366 |
> |
return(raymove(curpos, cxset, sflags, r, scene) == RAYHIT); |
| 367 |
|
} |
| 368 |
|
|
| 369 |
|
|
| 370 |
|
static int |
| 371 |
< |
raymove(pos, dirf, r, cu) /* check for hit as we move */ |
| 372 |
< |
FVECT pos; /* modified */ |
| 371 |
> |
raymove(pos, cxs, dirf, r, cu) /* check for hit as we move */ |
| 372 |
> |
FVECT pos; /* current position, modified herein */ |
| 373 |
> |
OBJECT *cxs; /* checked objects, modified by checkhit */ |
| 374 |
|
int dirf; /* direction indicators to speed tests */ |
| 375 |
|
register RAY *r; |
| 376 |
|
register CUBE *cu; |
| 399 |
|
} |
| 400 |
|
for ( ; ; ) { |
| 401 |
|
cukid.cutree = octkid(cu->cutree, br); |
| 402 |
< |
if ((ax = raymove(pos,dirf,r,&cukid)) == RAYHIT) |
| 402 |
> |
if ((ax = raymove(pos,cxs,dirf,r,&cukid)) == RAYHIT) |
| 403 |
|
return(RAYHIT); |
| 404 |
|
sgn = 1 << ax; |
| 405 |
|
if (sgn & dirf) /* positive axis? */ |
| 418 |
|
} |
| 419 |
|
/*NOTREACHED*/ |
| 420 |
|
} |
| 421 |
< |
if (isfull(cu->cutree) && checkhit(r, cu)) |
| 421 |
> |
if (isfull(cu->cutree) && checkhit(r, cu, cxs)) |
| 422 |
|
return(RAYHIT); |
| 423 |
|
/* advance to next cube */ |
| 424 |
|
if (dirf&0x11) { |
| 451 |
|
|
| 452 |
|
|
| 453 |
|
static |
| 454 |
< |
checkhit(r, cu) /* check for hit in full cube */ |
| 454 |
> |
checkhit(r, cu, cxs) /* check for hit in full cube */ |
| 455 |
|
register RAY *r; |
| 456 |
|
CUBE *cu; |
| 457 |
+ |
OBJECT *cxs; |
| 458 |
|
{ |
| 459 |
|
OBJECT oset[MAXSET+1]; |
| 460 |
|
register OBJREC *o; |
| 461 |
|
register int i; |
| 462 |
|
|
| 463 |
|
objset(oset, cu->cutree); |
| 464 |
+ |
checkset(oset, cxs); /* eliminate double-checking */ |
| 465 |
|
for (i = oset[0]; i > 0; i--) { |
| 466 |
|
o = objptr(oset[i]); |
| 449 |
– |
if (o->lastrno == r->rno) /* checked already? */ |
| 450 |
– |
continue; |
| 467 |
|
(*ofun[o->otype].funp)(o, r); |
| 452 |
– |
o->lastrno = r->rno; |
| 468 |
|
} |
| 469 |
|
if (r->ro == NULL) |
| 470 |
|
return(0); /* no scores yet */ |
| 471 |
|
|
| 472 |
|
return(incube(cu, r->rop)); /* hit OK if in current cube */ |
| 473 |
+ |
} |
| 474 |
+ |
|
| 475 |
+ |
|
| 476 |
+ |
static |
| 477 |
+ |
checkset(os, cs) /* modify checked set and set to check */ |
| 478 |
+ |
register OBJECT *os; /* os' = os - cs */ |
| 479 |
+ |
register OBJECT *cs; /* cs' = cs + os */ |
| 480 |
+ |
{ |
| 481 |
+ |
OBJECT cset[MAXCSET+MAXSET+1]; |
| 482 |
+ |
register int i, j; |
| 483 |
+ |
int k; |
| 484 |
+ |
/* copy os in place, cset <- cs */ |
| 485 |
+ |
cset[0] = 0; |
| 486 |
+ |
k = 0; |
| 487 |
+ |
for (i = j = 1; i <= os[0]; i++) { |
| 488 |
+ |
while (j <= cs[0] && cs[j] < os[i]) |
| 489 |
+ |
cset[++cset[0]] = cs[j++]; |
| 490 |
+ |
if (j > cs[0] || os[i] != cs[j]) { /* object to check */ |
| 491 |
+ |
os[++k] = os[i]; |
| 492 |
+ |
cset[++cset[0]] = os[i]; |
| 493 |
+ |
} |
| 494 |
+ |
} |
| 495 |
+ |
if (!(os[0] = k)) /* new "to check" set size */ |
| 496 |
+ |
return; /* special case */ |
| 497 |
+ |
while (j <= cs[0]) /* get the rest of cs */ |
| 498 |
+ |
cset[++cset[0]] = cs[j++]; |
| 499 |
+ |
if (cset[0] > MAXCSET) /* truncate "checked" set if nec. */ |
| 500 |
+ |
cset[0] = MAXCSET; |
| 501 |
+ |
/* setcopy(cs, cset); */ /* copy cset back to cs */ |
| 502 |
+ |
os = cset; |
| 503 |
+ |
for (i = os[0]; i-- >= 0; ) |
| 504 |
+ |
*cs++ = *os++; |
| 505 |
|
} |