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