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