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