| 1 |
– |
/* Copyright (c) 1998 Silicon Graphics, Inc. */ |
| 2 |
– |
|
| 1 |
|
#ifndef lint |
| 2 |
< |
static char SCCSid[] = "$SunId$ SGI"; |
| 2 |
> |
static const char RCSid[] = "$Id$"; |
| 3 |
|
#endif |
| 6 |
– |
|
| 4 |
|
/* |
| 5 |
|
* raytrace.c - routines for tracing and shading rays. |
| 6 |
|
* |
| 7 |
< |
* 8/7/85 |
| 7 |
> |
* External symbols declared in ray.h |
| 8 |
|
*/ |
| 9 |
|
|
| 10 |
+ |
#include "copyright.h" |
| 11 |
+ |
|
| 12 |
|
#include "ray.h" |
| 13 |
|
|
| 15 |
– |
#include "octree.h" |
| 16 |
– |
|
| 14 |
|
#include "otypes.h" |
| 15 |
|
|
| 16 |
|
#include "otspecial.h" |
| 17 |
|
|
| 18 |
|
#define MAXCSET ((MAXSET+1)*2-1) /* maximum check set size */ |
| 19 |
|
|
| 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 |
– |
|
| 20 |
|
unsigned long raynum = 0; /* next unique ray number */ |
| 21 |
|
unsigned long nrays = 0; /* number of calls to localhit */ |
| 22 |
|
|
| 28 |
|
|
| 29 |
|
OBJREC Aftplane; /* aft clipping plane object */ |
| 30 |
|
|
| 31 |
< |
static int raymove(), checkset(), checkhit(); |
| 31 |
> |
static int raymove(), checkhit(); |
| 32 |
> |
static void checkset(); |
| 33 |
|
|
| 34 |
< |
#define MAXLOOP 128 /* modifier loop detection */ |
| 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; |
| 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 |
|
{ |
| 119 |
– |
extern int (*trace)(); |
| 120 |
– |
|
| 114 |
|
if (localhit(r, &thescene)) |
| 115 |
|
raycont(r); /* hit local surface, evaluate */ |
| 116 |
|
else if (r->ro == &Aftplane) { |
| 126 |
|
} |
| 127 |
|
|
| 128 |
|
|
| 129 |
+ |
void |
| 130 |
|
raycont(r) /* check for clipped object and continue */ |
| 131 |
|
register RAY *r; |
| 132 |
|
{ |
| 136 |
|
} |
| 137 |
|
|
| 138 |
|
|
| 139 |
+ |
void |
| 140 |
|
raytrans(r) /* transmit ray as is */ |
| 141 |
|
register RAY *r; |
| 142 |
|
{ |
| 151 |
|
} |
| 152 |
|
|
| 153 |
|
|
| 154 |
+ |
int |
| 155 |
|
rayshade(r, mod) /* shade ray r with material mod */ |
| 156 |
|
register RAY *r; |
| 157 |
|
int mod; |
| 158 |
|
{ |
| 163 |
– |
static int depth = 0; |
| 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); |
| 174 |
|
} |
| 175 |
|
******/ |
| 176 |
|
/* hack for irradiance calculation */ |
| 177 |
< |
if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS))) { |
| 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 |
|
} |
| 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 |
|
{ |
| 232 |
|
RAY *r; |
| 233 |
|
int mod; |
| 234 |
|
{ |
| 231 |
– |
static int depth = 0; |
| 235 |
|
register OBJREC *m; |
| 236 |
+ |
#if MAXLOOP |
| 237 |
+ |
static int depth = 0; |
| 238 |
|
/* check for infinite loop */ |
| 239 |
|
if (depth++ >= MAXLOOP) |
| 240 |
|
objerror(r->ro, USER, "modifier loop"); |
| 241 |
+ |
#endif |
| 242 |
|
/* execute textures and patterns */ |
| 243 |
|
for ( ; mod != OVOID; mod = m->omod) { |
| 244 |
|
m = objptr(mod); |
| 254 |
|
objerror(r->ro, USER, errmsg); |
| 255 |
|
} |
| 256 |
|
} |
| 257 |
+ |
#if MAXLOOP |
| 258 |
|
depth--; /* end here */ |
| 259 |
+ |
#endif |
| 260 |
|
} |
| 261 |
|
|
| 262 |
|
|
| 263 |
+ |
int |
| 264 |
|
raymixture(r, fore, back, coef) /* mix modifiers */ |
| 265 |
|
register RAY *r; |
| 266 |
|
OBJECT fore, back; |
| 286 |
|
backmat = rayshade(&br, back); |
| 287 |
|
/* check for transparency */ |
| 288 |
|
if (backmat ^ foremat) |
| 289 |
< |
if (!foremat && coef > FTINY) |
| 289 |
> |
if (backmat && coef > FTINY) |
| 290 |
|
raytrans(&fr); |
| 291 |
< |
else if (!backmat && coef < 1.0-FTINY) |
| 291 |
> |
else if (foremat && coef < 1.0-FTINY) |
| 292 |
|
raytrans(&br); |
| 293 |
|
/* mix perturbations */ |
| 294 |
|
for (i = 0; i < 3; i++) |
| 361 |
|
} |
| 362 |
|
|
| 363 |
|
|
| 364 |
+ |
void |
| 365 |
|
newrayxf(r) /* get new tranformation matrix for ray */ |
| 366 |
|
RAY *r; |
| 367 |
|
{ |
| 381 |
|
if (rp->rox == &xp->xf) { /* xp in use */ |
| 382 |
|
xp = xp->next; /* move to next */ |
| 383 |
|
if (xp == xflast) { /* need new one */ |
| 384 |
< |
xp = (struct xfn *)bmalloc(sizeof(struct xfn)); |
| 384 |
> |
xp = (struct xfn *)malloc(sizeof(struct xfn)); |
| 385 |
|
if (xp == NULL) |
| 386 |
|
error(SYSTEM, |
| 387 |
|
"out of memory in newrayxf"); |
| 398 |
|
} |
| 399 |
|
|
| 400 |
|
|
| 401 |
+ |
void |
| 402 |
|
flipsurface(r) /* reverse surface orientation */ |
| 403 |
|
register RAY *r; |
| 404 |
|
{ |
| 412 |
|
} |
| 413 |
|
|
| 414 |
|
|
| 415 |
+ |
void |
| 416 |
+ |
rayhit(oset, r) /* standard ray hit test */ |
| 417 |
+ |
OBJECT *oset; |
| 418 |
+ |
RAY *r; |
| 419 |
+ |
{ |
| 420 |
+ |
OBJREC *o; |
| 421 |
+ |
int i; |
| 422 |
+ |
|
| 423 |
+ |
for (i = oset[0]; i > 0; i--) { |
| 424 |
+ |
o = objptr(oset[i]); |
| 425 |
+ |
if ((*ofun[o->otype].funp)(o, r)) |
| 426 |
+ |
r->robj = oset[i]; |
| 427 |
+ |
} |
| 428 |
+ |
} |
| 429 |
+ |
|
| 430 |
+ |
|
| 431 |
+ |
int |
| 432 |
|
localhit(r, scene) /* check for hit in the octree */ |
| 433 |
|
register RAY *r; |
| 434 |
|
register CUBE *scene; |
| 576 |
|
} |
| 577 |
|
|
| 578 |
|
|
| 579 |
< |
static |
| 579 |
> |
static int |
| 580 |
|
checkhit(r, cu, cxs) /* check for hit in full cube */ |
| 581 |
|
register RAY *r; |
| 582 |
|
CUBE *cu; |
| 583 |
|
OBJECT *cxs; |
| 584 |
|
{ |
| 585 |
|
OBJECT oset[MAXSET+1]; |
| 558 |
– |
register OBJREC *o; |
| 559 |
– |
register int i; |
| 586 |
|
|
| 587 |
|
objset(oset, cu->cutree); |
| 588 |
< |
checkset(oset, cxs); /* eliminate double-checking */ |
| 589 |
< |
for (i = oset[0]; i > 0; i--) { |
| 590 |
< |
o = objptr(oset[i]); |
| 591 |
< |
if ((*ofun[o->otype].funp)(o, r)) |
| 592 |
< |
r->robj = oset[i]; |
| 567 |
< |
} |
| 568 |
< |
if (r->ro == NULL) |
| 588 |
> |
checkset(oset, cxs); /* avoid double-checking */ |
| 589 |
> |
|
| 590 |
> |
(*r->hitf)(oset, r); /* test for hit in set */ |
| 591 |
> |
|
| 592 |
> |
if (r->robj == OVOID) |
| 593 |
|
return(0); /* no scores yet */ |
| 594 |
|
|
| 595 |
|
return(incube(cu, r->rop)); /* hit OK if in current cube */ |
| 596 |
|
} |
| 597 |
|
|
| 598 |
|
|
| 599 |
< |
static |
| 599 |
> |
static void |
| 600 |
|
checkset(os, cs) /* modify checked set and set to check */ |
| 601 |
|
register OBJECT *os; /* os' = os - cs */ |
| 602 |
|
register OBJECT *cs; /* cs' = cs + os */ |