| 1 |
greg |
2.9 |
/* Copyright (c) 1994 Regents of the University of California */ |
| 2 |
greg |
1.1 |
|
| 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 |
greg |
1.15 |
#include "otspecial.h" |
| 20 |
|
|
|
| 21 |
greg |
2.3 |
#define MAXCSET ((MAXSET+1)*2-1) /* maximum check set size */ |
| 22 |
|
|
|
| 23 |
greg |
1.1 |
extern CUBE thescene; /* our scene */ |
| 24 |
|
|
extern int maxdepth; /* maximum recursion depth */ |
| 25 |
|
|
extern double minweight; /* minimum ray weight */ |
| 26 |
greg |
1.15 |
extern int do_irrad; /* compute irradiance? */ |
| 27 |
greg |
1.1 |
|
| 28 |
greg |
2.6 |
unsigned long raynum = 0; /* next unique ray number */ |
| 29 |
|
|
unsigned long nrays = 0; /* number of calls to localhit */ |
| 30 |
greg |
1.1 |
|
| 31 |
greg |
1.23 |
static FLOAT Lambfa[5] = {PI, PI, PI, 0.0, 0.0}; |
| 32 |
greg |
1.15 |
OBJREC Lamb = { |
| 33 |
|
|
OVOID, MAT_PLASTIC, "Lambertian", |
| 34 |
greg |
2.2 |
{0, 5, NULL, Lambfa}, NULL, |
| 35 |
greg |
1.15 |
}; /* a Lambertian surface */ |
| 36 |
|
|
|
| 37 |
greg |
2.5 |
static int raymove(), checkset(), checkhit(); |
| 38 |
|
|
|
| 39 |
greg |
1.6 |
#define MAXLOOP 128 /* modifier loop detection */ |
| 40 |
greg |
1.1 |
|
| 41 |
|
|
#define RAYHIT (-1) /* return value for intercepted ray */ |
| 42 |
|
|
|
| 43 |
|
|
|
| 44 |
|
|
rayorigin(r, ro, rt, rw) /* start new ray from old one */ |
| 45 |
|
|
register RAY *r, *ro; |
| 46 |
|
|
int rt; |
| 47 |
|
|
double rw; |
| 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 |
greg |
1.21 |
r->revf = raytrace; |
| 56 |
greg |
1.1 |
} else { /* spawned ray */ |
| 57 |
|
|
r->rlvl = ro->rlvl; |
| 58 |
|
|
if (rt & RAYREFL) { |
| 59 |
|
|
r->rlvl++; |
| 60 |
|
|
r->rsrc = -1; |
| 61 |
|
|
r->clipset = ro->clipset; |
| 62 |
|
|
} else { |
| 63 |
|
|
r->rsrc = ro->rsrc; |
| 64 |
|
|
r->clipset = ro->newcset; |
| 65 |
|
|
} |
| 66 |
greg |
1.21 |
r->revf = ro->revf; |
| 67 |
greg |
1.1 |
r->rweight = ro->rweight * rw; |
| 68 |
|
|
r->crtype = ro->crtype | (r->rtype = rt); |
| 69 |
|
|
VCOPY(r->rorg, ro->rop); |
| 70 |
|
|
} |
| 71 |
greg |
1.22 |
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 |
greg |
1.20 |
r->rno = raynum++; |
| 80 |
greg |
1.1 |
r->newcset = r->clipset; |
| 81 |
|
|
r->ro = NULL; |
| 82 |
|
|
r->rot = FHUGE; |
| 83 |
|
|
r->pert[0] = r->pert[1] = r->pert[2] = 0.0; |
| 84 |
|
|
setcolor(r->pcol, 1.0, 1.0, 1.0); |
| 85 |
|
|
setcolor(r->rcol, 0.0, 0.0, 0.0); |
| 86 |
greg |
1.10 |
r->rt = 0.0; |
| 87 |
greg |
1.1 |
} |
| 88 |
|
|
|
| 89 |
|
|
|
| 90 |
greg |
1.21 |
raytrace(r) /* trace a ray and compute its value */ |
| 91 |
greg |
1.8 |
RAY *r; |
| 92 |
greg |
1.1 |
{ |
| 93 |
|
|
extern int (*trace)(); |
| 94 |
greg |
2.9 |
int gotmat; |
| 95 |
greg |
1.1 |
|
| 96 |
greg |
1.15 |
if (localhit(r, &thescene)) |
| 97 |
greg |
2.9 |
gotmat = raycont(r); |
| 98 |
greg |
1.15 |
else if (sourcehit(r)) |
| 99 |
greg |
2.9 |
gotmat = rayshade(r, r->ro->omod); |
| 100 |
greg |
1.1 |
|
| 101 |
greg |
2.9 |
if (!gotmat) |
| 102 |
|
|
objerror(r->ro, USER, "material not found"); |
| 103 |
|
|
|
| 104 |
greg |
1.1 |
if (trace != NULL) |
| 105 |
|
|
(*trace)(r); /* trace execution */ |
| 106 |
|
|
} |
| 107 |
|
|
|
| 108 |
|
|
|
| 109 |
greg |
1.8 |
raycont(r) /* check for clipped object and continue */ |
| 110 |
|
|
register RAY *r; |
| 111 |
|
|
{ |
| 112 |
greg |
2.7 |
if ((r->clipset != NULL && inset(r->clipset, r->ro->omod)) || |
| 113 |
greg |
2.9 |
r->ro->omod == OVOID) { |
| 114 |
greg |
1.8 |
raytrans(r); |
| 115 |
greg |
2.9 |
return(1); |
| 116 |
|
|
} |
| 117 |
|
|
return(rayshade(r, r->ro->omod)); |
| 118 |
greg |
1.8 |
} |
| 119 |
|
|
|
| 120 |
|
|
|
| 121 |
greg |
1.1 |
raytrans(r) /* transmit ray as is */ |
| 122 |
greg |
1.8 |
register RAY *r; |
| 123 |
greg |
1.1 |
{ |
| 124 |
|
|
RAY tr; |
| 125 |
|
|
|
| 126 |
|
|
if (rayorigin(&tr, r, TRANS, 1.0) == 0) { |
| 127 |
|
|
VCOPY(tr.rdir, r->rdir); |
| 128 |
|
|
rayvalue(&tr); |
| 129 |
|
|
copycolor(r->rcol, tr.rcol); |
| 130 |
greg |
1.10 |
r->rt = r->rot + tr.rt; |
| 131 |
greg |
1.1 |
} |
| 132 |
|
|
} |
| 133 |
|
|
|
| 134 |
|
|
|
| 135 |
|
|
rayshade(r, mod) /* shade ray r with material mod */ |
| 136 |
|
|
register RAY *r; |
| 137 |
|
|
int mod; |
| 138 |
|
|
{ |
| 139 |
|
|
static int depth = 0; |
| 140 |
greg |
2.9 |
int gotmat; |
| 141 |
greg |
1.1 |
register OBJREC *m; |
| 142 |
|
|
/* check for infinite loop */ |
| 143 |
|
|
if (depth++ >= MAXLOOP) |
| 144 |
greg |
1.4 |
objerror(r->ro, USER, "possible modifier loop"); |
| 145 |
greg |
1.19 |
r->rt = r->rot; /* set effective ray length */ |
| 146 |
greg |
2.9 |
for (gotmat = 0; !gotmat && mod != OVOID; mod = m->omod) { |
| 147 |
greg |
1.1 |
m = objptr(mod); |
| 148 |
greg |
1.4 |
/****** unnecessary test since modifier() is always called |
| 149 |
greg |
1.1 |
if (!ismodifier(m->otype)) { |
| 150 |
|
|
sprintf(errmsg, "illegal modifier \"%s\"", m->oname); |
| 151 |
|
|
error(USER, errmsg); |
| 152 |
|
|
} |
| 153 |
greg |
1.4 |
******/ |
| 154 |
greg |
1.16 |
/* hack for irradiance calculation */ |
| 155 |
|
|
if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS))) { |
| 156 |
|
|
if (irr_ignore(m->otype)) { |
| 157 |
|
|
depth--; |
| 158 |
|
|
raytrans(r); |
| 159 |
|
|
return; |
| 160 |
|
|
} |
| 161 |
greg |
1.18 |
if (!islight(m->otype)) |
| 162 |
greg |
1.16 |
m = &Lamb; |
| 163 |
|
|
} |
| 164 |
greg |
2.9 |
/* materials call raytexture */ |
| 165 |
|
|
gotmat = (*ofun[m->otype].funp)(m, r); |
| 166 |
greg |
1.1 |
} |
| 167 |
greg |
2.9 |
depth--; |
| 168 |
|
|
return(gotmat); |
| 169 |
greg |
1.1 |
} |
| 170 |
|
|
|
| 171 |
|
|
|
| 172 |
|
|
raytexture(r, mod) /* get material modifiers */ |
| 173 |
|
|
RAY *r; |
| 174 |
|
|
int mod; |
| 175 |
|
|
{ |
| 176 |
|
|
static int depth = 0; |
| 177 |
|
|
register OBJREC *m; |
| 178 |
|
|
/* check for infinite loop */ |
| 179 |
|
|
if (depth++ >= MAXLOOP) |
| 180 |
|
|
objerror(r->ro, USER, "modifier loop"); |
| 181 |
|
|
/* execute textures and patterns */ |
| 182 |
|
|
for ( ; mod != OVOID; mod = m->omod) { |
| 183 |
|
|
m = objptr(mod); |
| 184 |
greg |
2.9 |
/****** unnecessary test since modifier() is always called |
| 185 |
|
|
if (!ismodifier(m->otype)) { |
| 186 |
greg |
1.1 |
sprintf(errmsg, "illegal modifier \"%s\"", m->oname); |
| 187 |
|
|
error(USER, errmsg); |
| 188 |
|
|
} |
| 189 |
greg |
2.9 |
******/ |
| 190 |
|
|
if ((*ofun[m->otype].funp)(m, r)) |
| 191 |
|
|
objerror(USER, r->ro, "conflicting materials"); |
| 192 |
greg |
1.1 |
} |
| 193 |
|
|
depth--; /* end here */ |
| 194 |
|
|
} |
| 195 |
|
|
|
| 196 |
|
|
|
| 197 |
|
|
raymixture(r, fore, back, coef) /* mix modifiers */ |
| 198 |
|
|
register RAY *r; |
| 199 |
|
|
OBJECT fore, back; |
| 200 |
|
|
double coef; |
| 201 |
|
|
{ |
| 202 |
greg |
2.9 |
RAY fr, br; |
| 203 |
|
|
COLOR ctmp; |
| 204 |
|
|
int foremat, backmat; |
| 205 |
greg |
1.1 |
register int i; |
| 206 |
|
|
/* clip coefficient */ |
| 207 |
|
|
if (coef > 1.0) |
| 208 |
|
|
coef = 1.0; |
| 209 |
|
|
else if (coef < 0.0) |
| 210 |
|
|
coef = 0.0; |
| 211 |
greg |
2.9 |
/* foreground */ |
| 212 |
|
|
copystruct(&fr, r); |
| 213 |
|
|
fr.pert[0] = fr.pert[1] = fr.pert[2] = 0.0; |
| 214 |
|
|
setcolor(fr.pcol, 1.0, 1.0, 1.0); |
| 215 |
|
|
setcolor(fr.rcol, 0.0, 0.0, 0.0); |
| 216 |
greg |
1.1 |
if (fore != OVOID && coef > FTINY) |
| 217 |
greg |
2.9 |
foremat = rayshade(&fr, fore); |
| 218 |
|
|
else |
| 219 |
|
|
foremat = 0; |
| 220 |
|
|
/* background */ |
| 221 |
|
|
copystruct(&br, r); |
| 222 |
|
|
br.pert[0] = br.pert[1] = br.pert[2] = 0.0; |
| 223 |
|
|
setcolor(br.pcol, 1.0, 1.0, 1.0); |
| 224 |
|
|
setcolor(br.rcol, 0.0, 0.0, 0.0); |
| 225 |
greg |
1.1 |
if (back != OVOID && coef < 1.0-FTINY) |
| 226 |
greg |
2.9 |
backmat = rayshade(&br, back); |
| 227 |
|
|
else |
| 228 |
|
|
backmat = foremat; |
| 229 |
|
|
/* check */ |
| 230 |
|
|
if (backmat != foremat) |
| 231 |
|
|
objerror(USER, r->ro, "mixing material with non-material"); |
| 232 |
greg |
1.1 |
/* sum perturbations */ |
| 233 |
|
|
for (i = 0; i < 3; i++) |
| 234 |
greg |
2.9 |
r->pert[i] += coef*fr.pert[i] + (1.0-coef)*br.pert[i]; |
| 235 |
|
|
/* multiply pattern colors */ |
| 236 |
|
|
scalecolor(fr.pcol, coef); |
| 237 |
|
|
scalecolor(br.pcol, 1.0-coef); |
| 238 |
|
|
copycolor(ctmp, fr.pcol); |
| 239 |
|
|
addcolor(ctmp, br.pcol); |
| 240 |
|
|
multcolor(r->pcol, ctmp); |
| 241 |
|
|
/* sum returned ray values */ |
| 242 |
|
|
scalecolor(fr.rcol, coef); |
| 243 |
|
|
scalecolor(br.rcol, 1.0-coef); |
| 244 |
|
|
addcolor(r->rcol, fr.rcol); |
| 245 |
|
|
addcolor(r->rcol, br.rcol); |
| 246 |
|
|
/* return value tells if material */ |
| 247 |
|
|
return(foremat); |
| 248 |
greg |
1.1 |
} |
| 249 |
|
|
|
| 250 |
|
|
|
| 251 |
|
|
double |
| 252 |
|
|
raynormal(norm, r) /* compute perturbed normal for ray */ |
| 253 |
|
|
FVECT norm; |
| 254 |
|
|
register RAY *r; |
| 255 |
|
|
{ |
| 256 |
|
|
double newdot; |
| 257 |
|
|
register int i; |
| 258 |
|
|
|
| 259 |
|
|
/* The perturbation is added to the surface normal to obtain |
| 260 |
|
|
* the new normal. If the new normal would affect the surface |
| 261 |
|
|
* orientation wrt. the ray, a correction is made. The method is |
| 262 |
|
|
* still fraught with problems since reflected rays and similar |
| 263 |
|
|
* directions calculated from the surface normal may spawn rays behind |
| 264 |
|
|
* the surface. The only solution is to curb textures at high |
| 265 |
greg |
1.9 |
* incidence (namely, keep DOT(rdir,pert) < Rdot). |
| 266 |
greg |
1.1 |
*/ |
| 267 |
|
|
|
| 268 |
|
|
for (i = 0; i < 3; i++) |
| 269 |
|
|
norm[i] = r->ron[i] + r->pert[i]; |
| 270 |
|
|
|
| 271 |
|
|
if (normalize(norm) == 0.0) { |
| 272 |
|
|
objerror(r->ro, WARNING, "illegal normal perturbation"); |
| 273 |
|
|
VCOPY(norm, r->ron); |
| 274 |
|
|
return(r->rod); |
| 275 |
|
|
} |
| 276 |
|
|
newdot = -DOT(norm, r->rdir); |
| 277 |
|
|
if ((newdot > 0.0) != (r->rod > 0.0)) { /* fix orientation */ |
| 278 |
|
|
for (i = 0; i < 3; i++) |
| 279 |
|
|
norm[i] += 2.0*newdot*r->rdir[i]; |
| 280 |
|
|
newdot = -newdot; |
| 281 |
|
|
} |
| 282 |
|
|
return(newdot); |
| 283 |
greg |
1.12 |
} |
| 284 |
|
|
|
| 285 |
|
|
|
| 286 |
|
|
newrayxf(r) /* get new tranformation matrix for ray */ |
| 287 |
|
|
RAY *r; |
| 288 |
|
|
{ |
| 289 |
|
|
static struct xfn { |
| 290 |
|
|
struct xfn *next; |
| 291 |
|
|
FULLXF xf; |
| 292 |
|
|
} xfseed = { &xfseed }, *xflast = &xfseed; |
| 293 |
|
|
register struct xfn *xp; |
| 294 |
|
|
register RAY *rp; |
| 295 |
|
|
|
| 296 |
|
|
/* |
| 297 |
|
|
* Search for transform in circular list that |
| 298 |
|
|
* has no associated ray in the tree. |
| 299 |
|
|
*/ |
| 300 |
|
|
xp = xflast; |
| 301 |
|
|
for (rp = r->parent; rp != NULL; rp = rp->parent) |
| 302 |
|
|
if (rp->rox == &xp->xf) { /* xp in use */ |
| 303 |
|
|
xp = xp->next; /* move to next */ |
| 304 |
|
|
if (xp == xflast) { /* need new one */ |
| 305 |
greg |
1.14 |
xp = (struct xfn *)bmalloc(sizeof(struct xfn)); |
| 306 |
greg |
1.12 |
if (xp == NULL) |
| 307 |
|
|
error(SYSTEM, |
| 308 |
|
|
"out of memory in newrayxf"); |
| 309 |
|
|
/* insert in list */ |
| 310 |
|
|
xp->next = xflast->next; |
| 311 |
|
|
xflast->next = xp; |
| 312 |
|
|
break; /* we're done */ |
| 313 |
|
|
} |
| 314 |
|
|
rp = r; /* start check over */ |
| 315 |
|
|
} |
| 316 |
|
|
/* got it */ |
| 317 |
|
|
r->rox = &xp->xf; |
| 318 |
|
|
xflast = xp; |
| 319 |
greg |
1.1 |
} |
| 320 |
|
|
|
| 321 |
|
|
|
| 322 |
|
|
flipsurface(r) /* reverse surface orientation */ |
| 323 |
|
|
register RAY *r; |
| 324 |
|
|
{ |
| 325 |
|
|
r->rod = -r->rod; |
| 326 |
|
|
r->ron[0] = -r->ron[0]; |
| 327 |
|
|
r->ron[1] = -r->ron[1]; |
| 328 |
|
|
r->ron[2] = -r->ron[2]; |
| 329 |
|
|
r->pert[0] = -r->pert[0]; |
| 330 |
|
|
r->pert[1] = -r->pert[1]; |
| 331 |
|
|
r->pert[2] = -r->pert[2]; |
| 332 |
|
|
} |
| 333 |
|
|
|
| 334 |
|
|
|
| 335 |
|
|
localhit(r, scene) /* check for hit in the octree */ |
| 336 |
|
|
register RAY *r; |
| 337 |
|
|
register CUBE *scene; |
| 338 |
|
|
{ |
| 339 |
greg |
2.3 |
OBJECT cxset[MAXCSET+1]; /* set of checked objects */ |
| 340 |
greg |
1.1 |
FVECT curpos; /* current cube position */ |
| 341 |
greg |
1.11 |
int sflags; /* sign flags */ |
| 342 |
greg |
1.1 |
double t, dt; |
| 343 |
|
|
register int i; |
| 344 |
|
|
|
| 345 |
greg |
1.21 |
nrays++; /* increment trace counter */ |
| 346 |
greg |
1.11 |
sflags = 0; |
| 347 |
greg |
1.1 |
for (i = 0; i < 3; i++) { |
| 348 |
|
|
curpos[i] = r->rorg[i]; |
| 349 |
greg |
2.8 |
if (r->rdir[i] > 1e-7) |
| 350 |
greg |
1.11 |
sflags |= 1 << i; |
| 351 |
greg |
2.8 |
else if (r->rdir[i] < -1e-7) |
| 352 |
greg |
1.11 |
sflags |= 0x10 << i; |
| 353 |
greg |
1.1 |
} |
| 354 |
greg |
1.17 |
if (sflags == 0) |
| 355 |
|
|
error(CONSISTENCY, "zero ray direction in localhit"); |
| 356 |
greg |
1.1 |
t = 0.0; |
| 357 |
|
|
if (!incube(scene, curpos)) { |
| 358 |
|
|
/* find distance to entry */ |
| 359 |
|
|
for (i = 0; i < 3; i++) { |
| 360 |
|
|
/* plane in our direction */ |
| 361 |
greg |
1.11 |
if (sflags & 1<<i) |
| 362 |
greg |
1.1 |
dt = scene->cuorg[i]; |
| 363 |
greg |
1.11 |
else if (sflags & 0x10<<i) |
| 364 |
greg |
1.1 |
dt = scene->cuorg[i] + scene->cusize; |
| 365 |
|
|
else |
| 366 |
|
|
continue; |
| 367 |
|
|
/* distance to the plane */ |
| 368 |
|
|
dt = (dt - r->rorg[i])/r->rdir[i]; |
| 369 |
|
|
if (dt > t) |
| 370 |
|
|
t = dt; /* farthest face is the one */ |
| 371 |
|
|
} |
| 372 |
|
|
t += FTINY; /* fudge to get inside cube */ |
| 373 |
|
|
/* advance position */ |
| 374 |
|
|
for (i = 0; i < 3; i++) |
| 375 |
|
|
curpos[i] += r->rdir[i]*t; |
| 376 |
|
|
|
| 377 |
|
|
if (!incube(scene, curpos)) /* non-intersecting ray */ |
| 378 |
|
|
return(0); |
| 379 |
|
|
} |
| 380 |
greg |
2.3 |
cxset[0] = 0; |
| 381 |
|
|
return(raymove(curpos, cxset, sflags, r, scene) == RAYHIT); |
| 382 |
greg |
1.1 |
} |
| 383 |
|
|
|
| 384 |
|
|
|
| 385 |
|
|
static int |
| 386 |
greg |
2.3 |
raymove(pos, cxs, dirf, r, cu) /* check for hit as we move */ |
| 387 |
|
|
FVECT pos; /* current position, modified herein */ |
| 388 |
|
|
OBJECT *cxs; /* checked objects, modified by checkhit */ |
| 389 |
greg |
1.11 |
int dirf; /* direction indicators to speed tests */ |
| 390 |
greg |
1.1 |
register RAY *r; |
| 391 |
|
|
register CUBE *cu; |
| 392 |
|
|
{ |
| 393 |
|
|
int ax; |
| 394 |
|
|
double dt, t; |
| 395 |
|
|
|
| 396 |
|
|
if (istree(cu->cutree)) { /* recurse on subcubes */ |
| 397 |
|
|
CUBE cukid; |
| 398 |
greg |
1.11 |
register int br, sgn; |
| 399 |
greg |
1.1 |
|
| 400 |
|
|
cukid.cusize = cu->cusize * 0.5; /* find subcube */ |
| 401 |
|
|
VCOPY(cukid.cuorg, cu->cuorg); |
| 402 |
|
|
br = 0; |
| 403 |
|
|
if (pos[0] >= cukid.cuorg[0]+cukid.cusize) { |
| 404 |
|
|
cukid.cuorg[0] += cukid.cusize; |
| 405 |
|
|
br |= 1; |
| 406 |
|
|
} |
| 407 |
|
|
if (pos[1] >= cukid.cuorg[1]+cukid.cusize) { |
| 408 |
|
|
cukid.cuorg[1] += cukid.cusize; |
| 409 |
|
|
br |= 2; |
| 410 |
|
|
} |
| 411 |
|
|
if (pos[2] >= cukid.cuorg[2]+cukid.cusize) { |
| 412 |
|
|
cukid.cuorg[2] += cukid.cusize; |
| 413 |
|
|
br |= 4; |
| 414 |
|
|
} |
| 415 |
|
|
for ( ; ; ) { |
| 416 |
|
|
cukid.cutree = octkid(cu->cutree, br); |
| 417 |
greg |
2.3 |
if ((ax = raymove(pos,cxs,dirf,r,&cukid)) == RAYHIT) |
| 418 |
greg |
1.1 |
return(RAYHIT); |
| 419 |
|
|
sgn = 1 << ax; |
| 420 |
greg |
1.11 |
if (sgn & dirf) /* positive axis? */ |
| 421 |
greg |
1.1 |
if (sgn & br) |
| 422 |
|
|
return(ax); /* overflow */ |
| 423 |
|
|
else { |
| 424 |
|
|
cukid.cuorg[ax] += cukid.cusize; |
| 425 |
|
|
br |= sgn; |
| 426 |
|
|
} |
| 427 |
greg |
1.11 |
else |
| 428 |
|
|
if (sgn & br) { |
| 429 |
|
|
cukid.cuorg[ax] -= cukid.cusize; |
| 430 |
|
|
br &= ~sgn; |
| 431 |
|
|
} else |
| 432 |
|
|
return(ax); /* underflow */ |
| 433 |
greg |
1.1 |
} |
| 434 |
|
|
/*NOTREACHED*/ |
| 435 |
|
|
} |
| 436 |
greg |
2.3 |
if (isfull(cu->cutree) && checkhit(r, cu, cxs)) |
| 437 |
greg |
1.1 |
return(RAYHIT); |
| 438 |
|
|
/* advance to next cube */ |
| 439 |
greg |
1.11 |
if (dirf&0x11) { |
| 440 |
|
|
dt = dirf&1 ? cu->cuorg[0] + cu->cusize : cu->cuorg[0]; |
| 441 |
greg |
1.1 |
t = (dt - pos[0])/r->rdir[0]; |
| 442 |
|
|
ax = 0; |
| 443 |
|
|
} else |
| 444 |
|
|
t = FHUGE; |
| 445 |
greg |
1.11 |
if (dirf&0x22) { |
| 446 |
|
|
dt = dirf&2 ? cu->cuorg[1] + cu->cusize : cu->cuorg[1]; |
| 447 |
greg |
1.1 |
dt = (dt - pos[1])/r->rdir[1]; |
| 448 |
|
|
if (dt < t) { |
| 449 |
|
|
t = dt; |
| 450 |
|
|
ax = 1; |
| 451 |
|
|
} |
| 452 |
|
|
} |
| 453 |
greg |
1.11 |
if (dirf&0x44) { |
| 454 |
|
|
dt = dirf&4 ? cu->cuorg[2] + cu->cusize : cu->cuorg[2]; |
| 455 |
greg |
1.1 |
dt = (dt - pos[2])/r->rdir[2]; |
| 456 |
|
|
if (dt < t) { |
| 457 |
|
|
t = dt; |
| 458 |
|
|
ax = 2; |
| 459 |
|
|
} |
| 460 |
|
|
} |
| 461 |
|
|
pos[0] += r->rdir[0]*t; |
| 462 |
|
|
pos[1] += r->rdir[1]*t; |
| 463 |
|
|
pos[2] += r->rdir[2]*t; |
| 464 |
|
|
return(ax); |
| 465 |
|
|
} |
| 466 |
|
|
|
| 467 |
|
|
|
| 468 |
|
|
static |
| 469 |
greg |
2.3 |
checkhit(r, cu, cxs) /* check for hit in full cube */ |
| 470 |
greg |
1.1 |
register RAY *r; |
| 471 |
|
|
CUBE *cu; |
| 472 |
greg |
2.3 |
OBJECT *cxs; |
| 473 |
greg |
1.1 |
{ |
| 474 |
|
|
OBJECT oset[MAXSET+1]; |
| 475 |
|
|
register OBJREC *o; |
| 476 |
|
|
register int i; |
| 477 |
|
|
|
| 478 |
|
|
objset(oset, cu->cutree); |
| 479 |
greg |
2.3 |
checkset(oset, cxs); /* eliminate double-checking */ |
| 480 |
greg |
1.1 |
for (i = oset[0]; i > 0; i--) { |
| 481 |
|
|
o = objptr(oset[i]); |
| 482 |
|
|
(*ofun[o->otype].funp)(o, r); |
| 483 |
|
|
} |
| 484 |
|
|
if (r->ro == NULL) |
| 485 |
|
|
return(0); /* no scores yet */ |
| 486 |
|
|
|
| 487 |
|
|
return(incube(cu, r->rop)); /* hit OK if in current cube */ |
| 488 |
greg |
2.2 |
} |
| 489 |
|
|
|
| 490 |
|
|
|
| 491 |
|
|
static |
| 492 |
|
|
checkset(os, cs) /* modify checked set and set to check */ |
| 493 |
greg |
2.3 |
register OBJECT *os; /* os' = os - cs */ |
| 494 |
|
|
register OBJECT *cs; /* cs' = cs + os */ |
| 495 |
greg |
2.2 |
{ |
| 496 |
|
|
OBJECT cset[MAXCSET+MAXSET+1]; |
| 497 |
greg |
2.3 |
register int i, j; |
| 498 |
|
|
int k; |
| 499 |
greg |
2.2 |
/* copy os in place, cset <- cs */ |
| 500 |
|
|
cset[0] = 0; |
| 501 |
|
|
k = 0; |
| 502 |
|
|
for (i = j = 1; i <= os[0]; i++) { |
| 503 |
|
|
while (j <= cs[0] && cs[j] < os[i]) |
| 504 |
|
|
cset[++cset[0]] = cs[j++]; |
| 505 |
|
|
if (j > cs[0] || os[i] != cs[j]) { /* object to check */ |
| 506 |
|
|
os[++k] = os[i]; |
| 507 |
|
|
cset[++cset[0]] = os[i]; |
| 508 |
|
|
} |
| 509 |
|
|
} |
| 510 |
greg |
2.3 |
if (!(os[0] = k)) /* new "to check" set size */ |
| 511 |
|
|
return; /* special case */ |
| 512 |
greg |
2.2 |
while (j <= cs[0]) /* get the rest of cs */ |
| 513 |
|
|
cset[++cset[0]] = cs[j++]; |
| 514 |
greg |
2.3 |
if (cset[0] > MAXCSET) /* truncate "checked" set if nec. */ |
| 515 |
greg |
2.2 |
cset[0] = MAXCSET; |
| 516 |
greg |
2.3 |
/* setcopy(cs, cset); */ /* copy cset back to cs */ |
| 517 |
|
|
os = cset; |
| 518 |
|
|
for (i = os[0]; i-- >= 0; ) |
| 519 |
|
|
*cs++ = *os++; |
| 520 |
greg |
1.1 |
} |