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