| 1 |
#ifndef lint
|
| 2 |
static const char RCSid[] = "$Id$";
|
| 3 |
#endif
|
| 4 |
/*
|
| 5 |
* raytrace.c - routines for tracing and shading rays.
|
| 6 |
*
|
| 7 |
* External symbols declared in ray.h
|
| 8 |
*/
|
| 9 |
|
| 10 |
#include "copyright.h"
|
| 11 |
|
| 12 |
#include "ray.h"
|
| 13 |
|
| 14 |
#include "otypes.h"
|
| 15 |
|
| 16 |
#include "otspecial.h"
|
| 17 |
|
| 18 |
#define MAXCSET ((MAXSET+1)*2-1) /* maximum check set size */
|
| 19 |
|
| 20 |
unsigned long raynum = 0; /* next unique ray number */
|
| 21 |
unsigned long nrays = 0; /* number of calls to localhit */
|
| 22 |
|
| 23 |
static FLOAT Lambfa[5] = {PI, PI, PI, 0.0, 0.0};
|
| 24 |
OBJREC Lamb = {
|
| 25 |
OVOID, MAT_PLASTIC, "Lambertian",
|
| 26 |
{0, 5, NULL, Lambfa}, NULL,
|
| 27 |
}; /* a Lambertian surface */
|
| 28 |
|
| 29 |
OBJREC Aftplane; /* aft clipping plane object */
|
| 30 |
|
| 31 |
static int raymove(), checkhit();
|
| 32 |
static void checkset();
|
| 33 |
|
| 34 |
#ifndef MAXLOOP
|
| 35 |
#define MAXLOOP 0 /* modifier loop detection */
|
| 36 |
#endif
|
| 37 |
|
| 38 |
#define RAYHIT (-1) /* return value for intercepted ray */
|
| 39 |
|
| 40 |
|
| 41 |
int
|
| 42 |
rayorigin(r, ro, rt, rw) /* start new ray from old one */
|
| 43 |
register RAY *r, *ro;
|
| 44 |
int rt;
|
| 45 |
double rw;
|
| 46 |
{
|
| 47 |
double re;
|
| 48 |
|
| 49 |
if ((r->parent = ro) == NULL) { /* primary ray */
|
| 50 |
r->rlvl = 0;
|
| 51 |
r->rweight = rw;
|
| 52 |
r->crtype = r->rtype = rt;
|
| 53 |
r->rsrc = -1;
|
| 54 |
r->clipset = NULL;
|
| 55 |
r->revf = raytrace;
|
| 56 |
copycolor(r->cext, cextinction);
|
| 57 |
copycolor(r->albedo, salbedo);
|
| 58 |
r->gecc = seccg;
|
| 59 |
r->slights = NULL;
|
| 60 |
} else { /* spawned ray */
|
| 61 |
r->rlvl = ro->rlvl;
|
| 62 |
if (rt & RAYREFL) {
|
| 63 |
r->rlvl++;
|
| 64 |
r->rsrc = -1;
|
| 65 |
r->clipset = ro->clipset;
|
| 66 |
r->rmax = 0.0;
|
| 67 |
} else {
|
| 68 |
r->rsrc = ro->rsrc;
|
| 69 |
r->clipset = ro->newcset;
|
| 70 |
r->rmax = ro->rmax <= FTINY ? 0.0 : ro->rmax - ro->rot;
|
| 71 |
}
|
| 72 |
r->revf = ro->revf;
|
| 73 |
copycolor(r->cext, ro->cext);
|
| 74 |
copycolor(r->albedo, ro->albedo);
|
| 75 |
r->gecc = ro->gecc;
|
| 76 |
r->slights = ro->slights;
|
| 77 |
r->crtype = ro->crtype | (r->rtype = rt);
|
| 78 |
VCOPY(r->rorg, ro->rop);
|
| 79 |
r->rweight = ro->rweight * rw;
|
| 80 |
/* estimate absorption */
|
| 81 |
re = colval(ro->cext,RED) < colval(ro->cext,GRN) ?
|
| 82 |
colval(ro->cext,RED) : colval(ro->cext,GRN);
|
| 83 |
if (colval(ro->cext,BLU) < re) re = colval(ro->cext,BLU);
|
| 84 |
if (re > 0.)
|
| 85 |
r->rweight *= exp(-re*ro->rot);
|
| 86 |
}
|
| 87 |
rayclear(r);
|
| 88 |
return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1);
|
| 89 |
}
|
| 90 |
|
| 91 |
|
| 92 |
void
|
| 93 |
rayclear(r) /* clear a ray for (re)evaluation */
|
| 94 |
register RAY *r;
|
| 95 |
{
|
| 96 |
r->rno = raynum++;
|
| 97 |
r->newcset = r->clipset;
|
| 98 |
r->hitf = rayhit;
|
| 99 |
r->robj = OVOID;
|
| 100 |
r->ro = NULL;
|
| 101 |
r->rox = NULL;
|
| 102 |
r->rt = r->rot = FHUGE;
|
| 103 |
r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
|
| 104 |
setcolor(r->pcol, 1.0, 1.0, 1.0);
|
| 105 |
setcolor(r->rcol, 0.0, 0.0, 0.0);
|
| 106 |
}
|
| 107 |
|
| 108 |
|
| 109 |
void
|
| 110 |
raytrace(r) /* trace a ray and compute its value */
|
| 111 |
RAY *r;
|
| 112 |
{
|
| 113 |
if (localhit(r, &thescene))
|
| 114 |
raycont(r); /* hit local surface, evaluate */
|
| 115 |
else if (r->ro == &Aftplane) {
|
| 116 |
r->ro = NULL; /* hit aft clipping plane */
|
| 117 |
r->rot = FHUGE;
|
| 118 |
} else if (sourcehit(r))
|
| 119 |
rayshade(r, r->ro->omod); /* distant source */
|
| 120 |
|
| 121 |
rayparticipate(r); /* for participating medium */
|
| 122 |
|
| 123 |
if (trace != NULL)
|
| 124 |
(*trace)(r); /* trace execution */
|
| 125 |
}
|
| 126 |
|
| 127 |
|
| 128 |
void
|
| 129 |
raycont(r) /* check for clipped object and continue */
|
| 130 |
register RAY *r;
|
| 131 |
{
|
| 132 |
if ((r->clipset != NULL && inset(r->clipset, r->ro->omod)) ||
|
| 133 |
!rayshade(r, r->ro->omod))
|
| 134 |
raytrans(r);
|
| 135 |
}
|
| 136 |
|
| 137 |
|
| 138 |
void
|
| 139 |
raytrans(r) /* transmit ray as is */
|
| 140 |
register RAY *r;
|
| 141 |
{
|
| 142 |
RAY tr;
|
| 143 |
|
| 144 |
if (rayorigin(&tr, r, TRANS, 1.0) == 0) {
|
| 145 |
VCOPY(tr.rdir, r->rdir);
|
| 146 |
rayvalue(&tr);
|
| 147 |
copycolor(r->rcol, tr.rcol);
|
| 148 |
r->rt = r->rot + tr.rt;
|
| 149 |
}
|
| 150 |
}
|
| 151 |
|
| 152 |
|
| 153 |
int
|
| 154 |
rayshade(r, mod) /* shade ray r with material mod */
|
| 155 |
register RAY *r;
|
| 156 |
int mod;
|
| 157 |
{
|
| 158 |
int gotmat;
|
| 159 |
register OBJREC *m;
|
| 160 |
#if MAXLOOP
|
| 161 |
static int depth = 0;
|
| 162 |
/* check for infinite loop */
|
| 163 |
if (depth++ >= MAXLOOP)
|
| 164 |
objerror(r->ro, USER, "possible modifier loop");
|
| 165 |
#endif
|
| 166 |
r->rt = r->rot; /* set effective ray length */
|
| 167 |
for (gotmat = 0; !gotmat && mod != OVOID; mod = m->omod) {
|
| 168 |
m = objptr(mod);
|
| 169 |
/****** unnecessary test since modifier() is always called
|
| 170 |
if (!ismodifier(m->otype)) {
|
| 171 |
sprintf(errmsg, "illegal modifier \"%s\"", m->oname);
|
| 172 |
error(USER, errmsg);
|
| 173 |
}
|
| 174 |
******/
|
| 175 |
/* hack for irradiance calculation */
|
| 176 |
if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS))) {
|
| 177 |
if (irr_ignore(m->otype)) {
|
| 178 |
#if MAXLOOP
|
| 179 |
depth--;
|
| 180 |
#endif
|
| 181 |
raytrans(r);
|
| 182 |
return(1);
|
| 183 |
}
|
| 184 |
if (!islight(m->otype))
|
| 185 |
m = &Lamb;
|
| 186 |
}
|
| 187 |
/* materials call raytexture */
|
| 188 |
gotmat = (*ofun[m->otype].funp)(m, r);
|
| 189 |
}
|
| 190 |
#if MAXLOOP
|
| 191 |
depth--;
|
| 192 |
#endif
|
| 193 |
return(gotmat);
|
| 194 |
}
|
| 195 |
|
| 196 |
|
| 197 |
void
|
| 198 |
rayparticipate(r) /* compute ray medium participation */
|
| 199 |
register RAY *r;
|
| 200 |
{
|
| 201 |
COLOR ce, ca;
|
| 202 |
double re, ge, be;
|
| 203 |
|
| 204 |
if (intens(r->cext) <= 1./FHUGE)
|
| 205 |
return; /* no medium */
|
| 206 |
re = r->rot*colval(r->cext,RED);
|
| 207 |
ge = r->rot*colval(r->cext,GRN);
|
| 208 |
be = r->rot*colval(r->cext,BLU);
|
| 209 |
if (r->crtype & SHADOW) { /* no scattering for sources */
|
| 210 |
re *= 1. - colval(r->albedo,RED);
|
| 211 |
ge *= 1. - colval(r->albedo,GRN);
|
| 212 |
be *= 1. - colval(r->albedo,BLU);
|
| 213 |
}
|
| 214 |
setcolor(ce, re<=0. ? 1. : re>92. ? 0. : exp(-re),
|
| 215 |
ge<=0. ? 1. : ge>92. ? 0. : exp(-ge),
|
| 216 |
be<=0. ? 1. : be>92. ? 0. : exp(-be));
|
| 217 |
multcolor(r->rcol, ce); /* path absorption */
|
| 218 |
if (r->crtype & SHADOW || intens(r->albedo) <= FTINY)
|
| 219 |
return; /* no scattering */
|
| 220 |
setcolor(ca,
|
| 221 |
colval(r->albedo,RED)*colval(ambval,RED)*(1.-colval(ce,RED)),
|
| 222 |
colval(r->albedo,GRN)*colval(ambval,GRN)*(1.-colval(ce,GRN)),
|
| 223 |
colval(r->albedo,BLU)*colval(ambval,BLU)*(1.-colval(ce,BLU)));
|
| 224 |
addcolor(r->rcol, ca); /* ambient in scattering */
|
| 225 |
srcscatter(r); /* source in scattering */
|
| 226 |
}
|
| 227 |
|
| 228 |
|
| 229 |
raytexture(r, mod) /* get material modifiers */
|
| 230 |
RAY *r;
|
| 231 |
int mod;
|
| 232 |
{
|
| 233 |
register OBJREC *m;
|
| 234 |
#if MAXLOOP
|
| 235 |
static int depth = 0;
|
| 236 |
/* check for infinite loop */
|
| 237 |
if (depth++ >= MAXLOOP)
|
| 238 |
objerror(r->ro, USER, "modifier loop");
|
| 239 |
#endif
|
| 240 |
/* execute textures and patterns */
|
| 241 |
for ( ; mod != OVOID; mod = m->omod) {
|
| 242 |
m = objptr(mod);
|
| 243 |
/****** unnecessary test since modifier() is always called
|
| 244 |
if (!ismodifier(m->otype)) {
|
| 245 |
sprintf(errmsg, "illegal modifier \"%s\"", m->oname);
|
| 246 |
error(USER, errmsg);
|
| 247 |
}
|
| 248 |
******/
|
| 249 |
if ((*ofun[m->otype].funp)(m, r)) {
|
| 250 |
sprintf(errmsg, "conflicting material \"%s\"",
|
| 251 |
m->oname);
|
| 252 |
objerror(r->ro, USER, errmsg);
|
| 253 |
}
|
| 254 |
}
|
| 255 |
#if MAXLOOP
|
| 256 |
depth--; /* end here */
|
| 257 |
#endif
|
| 258 |
}
|
| 259 |
|
| 260 |
|
| 261 |
int
|
| 262 |
raymixture(r, fore, back, coef) /* mix modifiers */
|
| 263 |
register RAY *r;
|
| 264 |
OBJECT fore, back;
|
| 265 |
double coef;
|
| 266 |
{
|
| 267 |
RAY fr, br;
|
| 268 |
int foremat, backmat;
|
| 269 |
register int i;
|
| 270 |
/* bound coefficient */
|
| 271 |
if (coef > 1.0)
|
| 272 |
coef = 1.0;
|
| 273 |
else if (coef < 0.0)
|
| 274 |
coef = 0.0;
|
| 275 |
/* compute foreground and background */
|
| 276 |
foremat = backmat = 0;
|
| 277 |
/* foreground */
|
| 278 |
copystruct(&fr, r);
|
| 279 |
if (coef > FTINY)
|
| 280 |
foremat = rayshade(&fr, fore);
|
| 281 |
/* background */
|
| 282 |
copystruct(&br, r);
|
| 283 |
if (coef < 1.0-FTINY)
|
| 284 |
backmat = rayshade(&br, back);
|
| 285 |
/* check for transparency */
|
| 286 |
if (backmat ^ foremat)
|
| 287 |
if (backmat && coef > FTINY)
|
| 288 |
raytrans(&fr);
|
| 289 |
else if (foremat && coef < 1.0-FTINY)
|
| 290 |
raytrans(&br);
|
| 291 |
/* mix perturbations */
|
| 292 |
for (i = 0; i < 3; i++)
|
| 293 |
r->pert[i] = coef*fr.pert[i] + (1.0-coef)*br.pert[i];
|
| 294 |
/* mix pattern colors */
|
| 295 |
scalecolor(fr.pcol, coef);
|
| 296 |
scalecolor(br.pcol, 1.0-coef);
|
| 297 |
copycolor(r->pcol, fr.pcol);
|
| 298 |
addcolor(r->pcol, br.pcol);
|
| 299 |
/* return value tells if material */
|
| 300 |
if (!foremat & !backmat)
|
| 301 |
return(0);
|
| 302 |
/* mix returned ray values */
|
| 303 |
scalecolor(fr.rcol, coef);
|
| 304 |
scalecolor(br.rcol, 1.0-coef);
|
| 305 |
copycolor(r->rcol, fr.rcol);
|
| 306 |
addcolor(r->rcol, br.rcol);
|
| 307 |
r->rt = bright(fr.rcol) > bright(br.rcol) ? fr.rt : br.rt;
|
| 308 |
return(1);
|
| 309 |
}
|
| 310 |
|
| 311 |
|
| 312 |
double
|
| 313 |
raydist(r, flags) /* compute (cumulative) ray distance */
|
| 314 |
register RAY *r;
|
| 315 |
register int flags;
|
| 316 |
{
|
| 317 |
double sum = 0.0;
|
| 318 |
|
| 319 |
while (r != NULL && r->crtype&flags) {
|
| 320 |
sum += r->rot;
|
| 321 |
r = r->parent;
|
| 322 |
}
|
| 323 |
return(sum);
|
| 324 |
}
|
| 325 |
|
| 326 |
|
| 327 |
double
|
| 328 |
raynormal(norm, r) /* compute perturbed normal for ray */
|
| 329 |
FVECT norm;
|
| 330 |
register RAY *r;
|
| 331 |
{
|
| 332 |
double newdot;
|
| 333 |
register int i;
|
| 334 |
|
| 335 |
/* The perturbation is added to the surface normal to obtain
|
| 336 |
* the new normal. If the new normal would affect the surface
|
| 337 |
* orientation wrt. the ray, a correction is made. The method is
|
| 338 |
* still fraught with problems since reflected rays and similar
|
| 339 |
* directions calculated from the surface normal may spawn rays behind
|
| 340 |
* the surface. The only solution is to curb textures at high
|
| 341 |
* incidence (namely, keep DOT(rdir,pert) < Rdot).
|
| 342 |
*/
|
| 343 |
|
| 344 |
for (i = 0; i < 3; i++)
|
| 345 |
norm[i] = r->ron[i] + r->pert[i];
|
| 346 |
|
| 347 |
if (normalize(norm) == 0.0) {
|
| 348 |
objerror(r->ro, WARNING, "illegal normal perturbation");
|
| 349 |
VCOPY(norm, r->ron);
|
| 350 |
return(r->rod);
|
| 351 |
}
|
| 352 |
newdot = -DOT(norm, r->rdir);
|
| 353 |
if ((newdot > 0.0) != (r->rod > 0.0)) { /* fix orientation */
|
| 354 |
for (i = 0; i < 3; i++)
|
| 355 |
norm[i] += 2.0*newdot*r->rdir[i];
|
| 356 |
newdot = -newdot;
|
| 357 |
}
|
| 358 |
return(newdot);
|
| 359 |
}
|
| 360 |
|
| 361 |
|
| 362 |
void
|
| 363 |
newrayxf(r) /* get new tranformation matrix for ray */
|
| 364 |
RAY *r;
|
| 365 |
{
|
| 366 |
static struct xfn {
|
| 367 |
struct xfn *next;
|
| 368 |
FULLXF xf;
|
| 369 |
} xfseed = { &xfseed }, *xflast = &xfseed;
|
| 370 |
register struct xfn *xp;
|
| 371 |
register RAY *rp;
|
| 372 |
|
| 373 |
/*
|
| 374 |
* Search for transform in circular list that
|
| 375 |
* has no associated ray in the tree.
|
| 376 |
*/
|
| 377 |
xp = xflast;
|
| 378 |
for (rp = r->parent; rp != NULL; rp = rp->parent)
|
| 379 |
if (rp->rox == &xp->xf) { /* xp in use */
|
| 380 |
xp = xp->next; /* move to next */
|
| 381 |
if (xp == xflast) { /* need new one */
|
| 382 |
xp = (struct xfn *)malloc(sizeof(struct xfn));
|
| 383 |
if (xp == NULL)
|
| 384 |
error(SYSTEM,
|
| 385 |
"out of memory in newrayxf");
|
| 386 |
/* insert in list */
|
| 387 |
xp->next = xflast->next;
|
| 388 |
xflast->next = xp;
|
| 389 |
break; /* we're done */
|
| 390 |
}
|
| 391 |
rp = r; /* start check over */
|
| 392 |
}
|
| 393 |
/* got it */
|
| 394 |
r->rox = &xp->xf;
|
| 395 |
xflast = xp;
|
| 396 |
}
|
| 397 |
|
| 398 |
|
| 399 |
void
|
| 400 |
flipsurface(r) /* reverse surface orientation */
|
| 401 |
register RAY *r;
|
| 402 |
{
|
| 403 |
r->rod = -r->rod;
|
| 404 |
r->ron[0] = -r->ron[0];
|
| 405 |
r->ron[1] = -r->ron[1];
|
| 406 |
r->ron[2] = -r->ron[2];
|
| 407 |
r->pert[0] = -r->pert[0];
|
| 408 |
r->pert[1] = -r->pert[1];
|
| 409 |
r->pert[2] = -r->pert[2];
|
| 410 |
}
|
| 411 |
|
| 412 |
|
| 413 |
void
|
| 414 |
rayhit(oset, r) /* standard ray hit test */
|
| 415 |
OBJECT *oset;
|
| 416 |
RAY *r;
|
| 417 |
{
|
| 418 |
OBJREC *o;
|
| 419 |
int i;
|
| 420 |
|
| 421 |
for (i = oset[0]; i > 0; i--) {
|
| 422 |
o = objptr(oset[i]);
|
| 423 |
if ((*ofun[o->otype].funp)(o, r))
|
| 424 |
r->robj = oset[i];
|
| 425 |
}
|
| 426 |
}
|
| 427 |
|
| 428 |
|
| 429 |
int
|
| 430 |
localhit(r, scene) /* check for hit in the octree */
|
| 431 |
register RAY *r;
|
| 432 |
register CUBE *scene;
|
| 433 |
{
|
| 434 |
OBJECT cxset[MAXCSET+1]; /* set of checked objects */
|
| 435 |
FVECT curpos; /* current cube position */
|
| 436 |
int sflags; /* sign flags */
|
| 437 |
double t, dt;
|
| 438 |
register int i;
|
| 439 |
|
| 440 |
nrays++; /* increment trace counter */
|
| 441 |
sflags = 0;
|
| 442 |
for (i = 0; i < 3; i++) {
|
| 443 |
curpos[i] = r->rorg[i];
|
| 444 |
if (r->rdir[i] > 1e-7)
|
| 445 |
sflags |= 1 << i;
|
| 446 |
else if (r->rdir[i] < -1e-7)
|
| 447 |
sflags |= 0x10 << i;
|
| 448 |
}
|
| 449 |
if (sflags == 0)
|
| 450 |
error(CONSISTENCY, "zero ray direction in localhit");
|
| 451 |
/* start off assuming nothing hit */
|
| 452 |
if (r->rmax > FTINY) { /* except aft plane if one */
|
| 453 |
r->ro = &Aftplane;
|
| 454 |
r->rot = r->rmax;
|
| 455 |
for (i = 0; i < 3; i++)
|
| 456 |
r->rop[i] = r->rorg[i] + r->rot*r->rdir[i];
|
| 457 |
}
|
| 458 |
/* find global cube entrance point */
|
| 459 |
t = 0.0;
|
| 460 |
if (!incube(scene, curpos)) {
|
| 461 |
/* find distance to entry */
|
| 462 |
for (i = 0; i < 3; i++) {
|
| 463 |
/* plane in our direction */
|
| 464 |
if (sflags & 1<<i)
|
| 465 |
dt = scene->cuorg[i];
|
| 466 |
else if (sflags & 0x10<<i)
|
| 467 |
dt = scene->cuorg[i] + scene->cusize;
|
| 468 |
else
|
| 469 |
continue;
|
| 470 |
/* distance to the plane */
|
| 471 |
dt = (dt - r->rorg[i])/r->rdir[i];
|
| 472 |
if (dt > t)
|
| 473 |
t = dt; /* farthest face is the one */
|
| 474 |
}
|
| 475 |
t += FTINY; /* fudge to get inside cube */
|
| 476 |
if (t >= r->rot) /* clipped already */
|
| 477 |
return(0);
|
| 478 |
/* advance position */
|
| 479 |
for (i = 0; i < 3; i++)
|
| 480 |
curpos[i] += r->rdir[i]*t;
|
| 481 |
|
| 482 |
if (!incube(scene, curpos)) /* non-intersecting ray */
|
| 483 |
return(0);
|
| 484 |
}
|
| 485 |
cxset[0] = 0;
|
| 486 |
raymove(curpos, cxset, sflags, r, scene);
|
| 487 |
return(r->ro != NULL & r->ro != &Aftplane);
|
| 488 |
}
|
| 489 |
|
| 490 |
|
| 491 |
static int
|
| 492 |
raymove(pos, cxs, dirf, r, cu) /* check for hit as we move */
|
| 493 |
FVECT pos; /* current position, modified herein */
|
| 494 |
OBJECT *cxs; /* checked objects, modified by checkhit */
|
| 495 |
int dirf; /* direction indicators to speed tests */
|
| 496 |
register RAY *r;
|
| 497 |
register CUBE *cu;
|
| 498 |
{
|
| 499 |
int ax;
|
| 500 |
double dt, t;
|
| 501 |
|
| 502 |
if (istree(cu->cutree)) { /* recurse on subcubes */
|
| 503 |
CUBE cukid;
|
| 504 |
register int br, sgn;
|
| 505 |
|
| 506 |
cukid.cusize = cu->cusize * 0.5; /* find subcube */
|
| 507 |
VCOPY(cukid.cuorg, cu->cuorg);
|
| 508 |
br = 0;
|
| 509 |
if (pos[0] >= cukid.cuorg[0]+cukid.cusize) {
|
| 510 |
cukid.cuorg[0] += cukid.cusize;
|
| 511 |
br |= 1;
|
| 512 |
}
|
| 513 |
if (pos[1] >= cukid.cuorg[1]+cukid.cusize) {
|
| 514 |
cukid.cuorg[1] += cukid.cusize;
|
| 515 |
br |= 2;
|
| 516 |
}
|
| 517 |
if (pos[2] >= cukid.cuorg[2]+cukid.cusize) {
|
| 518 |
cukid.cuorg[2] += cukid.cusize;
|
| 519 |
br |= 4;
|
| 520 |
}
|
| 521 |
for ( ; ; ) {
|
| 522 |
cukid.cutree = octkid(cu->cutree, br);
|
| 523 |
if ((ax = raymove(pos,cxs,dirf,r,&cukid)) == RAYHIT)
|
| 524 |
return(RAYHIT);
|
| 525 |
sgn = 1 << ax;
|
| 526 |
if (sgn & dirf) /* positive axis? */
|
| 527 |
if (sgn & br)
|
| 528 |
return(ax); /* overflow */
|
| 529 |
else {
|
| 530 |
cukid.cuorg[ax] += cukid.cusize;
|
| 531 |
br |= sgn;
|
| 532 |
}
|
| 533 |
else
|
| 534 |
if (sgn & br) {
|
| 535 |
cukid.cuorg[ax] -= cukid.cusize;
|
| 536 |
br &= ~sgn;
|
| 537 |
} else
|
| 538 |
return(ax); /* underflow */
|
| 539 |
}
|
| 540 |
/*NOTREACHED*/
|
| 541 |
}
|
| 542 |
if (isfull(cu->cutree)) {
|
| 543 |
if (checkhit(r, cu, cxs))
|
| 544 |
return(RAYHIT);
|
| 545 |
} else if (r->ro == &Aftplane && incube(cu, r->rop))
|
| 546 |
return(RAYHIT);
|
| 547 |
/* advance to next cube */
|
| 548 |
if (dirf&0x11) {
|
| 549 |
dt = dirf&1 ? cu->cuorg[0] + cu->cusize : cu->cuorg[0];
|
| 550 |
t = (dt - pos[0])/r->rdir[0];
|
| 551 |
ax = 0;
|
| 552 |
} else
|
| 553 |
t = FHUGE;
|
| 554 |
if (dirf&0x22) {
|
| 555 |
dt = dirf&2 ? cu->cuorg[1] + cu->cusize : cu->cuorg[1];
|
| 556 |
dt = (dt - pos[1])/r->rdir[1];
|
| 557 |
if (dt < t) {
|
| 558 |
t = dt;
|
| 559 |
ax = 1;
|
| 560 |
}
|
| 561 |
}
|
| 562 |
if (dirf&0x44) {
|
| 563 |
dt = dirf&4 ? cu->cuorg[2] + cu->cusize : cu->cuorg[2];
|
| 564 |
dt = (dt - pos[2])/r->rdir[2];
|
| 565 |
if (dt < t) {
|
| 566 |
t = dt;
|
| 567 |
ax = 2;
|
| 568 |
}
|
| 569 |
}
|
| 570 |
pos[0] += r->rdir[0]*t;
|
| 571 |
pos[1] += r->rdir[1]*t;
|
| 572 |
pos[2] += r->rdir[2]*t;
|
| 573 |
return(ax);
|
| 574 |
}
|
| 575 |
|
| 576 |
|
| 577 |
static int
|
| 578 |
checkhit(r, cu, cxs) /* check for hit in full cube */
|
| 579 |
register RAY *r;
|
| 580 |
CUBE *cu;
|
| 581 |
OBJECT *cxs;
|
| 582 |
{
|
| 583 |
OBJECT oset[MAXSET+1];
|
| 584 |
|
| 585 |
objset(oset, cu->cutree);
|
| 586 |
checkset(oset, cxs); /* avoid double-checking */
|
| 587 |
|
| 588 |
(*r->hitf)(oset, r); /* test for hit in set */
|
| 589 |
|
| 590 |
if (r->robj == OVOID)
|
| 591 |
return(0); /* no scores yet */
|
| 592 |
|
| 593 |
return(incube(cu, r->rop)); /* hit OK if in current cube */
|
| 594 |
}
|
| 595 |
|
| 596 |
|
| 597 |
static void
|
| 598 |
checkset(os, cs) /* modify checked set and set to check */
|
| 599 |
register OBJECT *os; /* os' = os - cs */
|
| 600 |
register OBJECT *cs; /* cs' = cs + os */
|
| 601 |
{
|
| 602 |
OBJECT cset[MAXCSET+MAXSET+1];
|
| 603 |
register int i, j;
|
| 604 |
int k;
|
| 605 |
/* copy os in place, cset <- cs */
|
| 606 |
cset[0] = 0;
|
| 607 |
k = 0;
|
| 608 |
for (i = j = 1; i <= os[0]; i++) {
|
| 609 |
while (j <= cs[0] && cs[j] < os[i])
|
| 610 |
cset[++cset[0]] = cs[j++];
|
| 611 |
if (j > cs[0] || os[i] != cs[j]) { /* object to check */
|
| 612 |
os[++k] = os[i];
|
| 613 |
cset[++cset[0]] = os[i];
|
| 614 |
}
|
| 615 |
}
|
| 616 |
if (!(os[0] = k)) /* new "to check" set size */
|
| 617 |
return; /* special case */
|
| 618 |
while (j <= cs[0]) /* get the rest of cs */
|
| 619 |
cset[++cset[0]] = cs[j++];
|
| 620 |
if (cset[0] > MAXCSET) /* truncate "checked" set if nec. */
|
| 621 |
cset[0] = MAXCSET;
|
| 622 |
/* setcopy(cs, cset); */ /* copy cset back to cs */
|
| 623 |
os = cset;
|
| 624 |
for (i = os[0]; i-- >= 0; )
|
| 625 |
*cs++ = *os++;
|
| 626 |
}
|