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