| 1 |
< |
/* Copyright (c) 1995 Regents of the University of California */ |
| 1 |
> |
/* Copyright (c) 1998 Silicon Graphics, Inc. */ |
| 2 |
|
|
| 3 |
|
#ifndef lint |
| 4 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
| 4 |
> |
static char SCCSid[] = "$SunId$ SGI"; |
| 5 |
|
#endif |
| 6 |
|
|
| 7 |
|
/* |
| 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 |
|
|
| 54 |
|
int rt; |
| 55 |
|
double rw; |
| 56 |
|
{ |
| 57 |
+ |
double re; |
| 58 |
+ |
|
| 59 |
|
if ((r->parent = ro) == NULL) { /* primary ray */ |
| 60 |
|
r->rlvl = 0; |
| 61 |
|
r->rweight = rw; |
| 63 |
|
r->rsrc = -1; |
| 64 |
|
r->clipset = NULL; |
| 65 |
|
r->revf = raytrace; |
| 66 |
+ |
copycolor(r->cext, cextinction); |
| 67 |
+ |
copycolor(r->albedo, salbedo); |
| 68 |
+ |
r->gecc = seccg; |
| 69 |
+ |
r->slights = NULL; |
| 70 |
|
} else { /* spawned ray */ |
| 71 |
|
r->rlvl = ro->rlvl; |
| 72 |
|
if (rt & RAYREFL) { |
| 73 |
|
r->rlvl++; |
| 74 |
|
r->rsrc = -1; |
| 75 |
|
r->clipset = ro->clipset; |
| 76 |
+ |
r->rmax = 0.0; |
| 77 |
|
} else { |
| 78 |
|
r->rsrc = ro->rsrc; |
| 79 |
|
r->clipset = ro->newcset; |
| 80 |
+ |
r->rmax = ro->rmax <= FTINY ? 0.0 : ro->rmax - ro->rot; |
| 81 |
|
} |
| 82 |
|
r->revf = ro->revf; |
| 83 |
< |
r->rweight = ro->rweight * rw; |
| 83 |
> |
copycolor(r->cext, ro->cext); |
| 84 |
> |
copycolor(r->albedo, ro->albedo); |
| 85 |
> |
r->gecc = ro->gecc; |
| 86 |
> |
r->slights = ro->slights; |
| 87 |
|
r->crtype = ro->crtype | (r->rtype = rt); |
| 88 |
|
VCOPY(r->rorg, ro->rop); |
| 89 |
< |
r->rmax = 0.0; |
| 89 |
> |
r->rweight = ro->rweight * rw; |
| 90 |
> |
/* estimate absorption */ |
| 91 |
> |
re = colval(ro->cext,RED) < colval(ro->cext,GRN) ? |
| 92 |
> |
colval(ro->cext,RED) : colval(ro->cext,GRN); |
| 93 |
> |
if (colval(ro->cext,BLU) < re) re = colval(ro->cext,BLU); |
| 94 |
> |
if (re > 0.) |
| 95 |
> |
r->rweight *= exp(-re*ro->rot); |
| 96 |
|
} |
| 97 |
|
rayclear(r); |
| 98 |
|
return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1); |
| 104 |
|
{ |
| 105 |
|
r->rno = raynum++; |
| 106 |
|
r->newcset = r->clipset; |
| 107 |
+ |
r->robj = OVOID; |
| 108 |
|
r->ro = NULL; |
| 109 |
< |
r->rot = FHUGE; |
| 109 |
> |
r->rt = r->rot = FHUGE; |
| 110 |
|
r->pert[0] = r->pert[1] = r->pert[2] = 0.0; |
| 111 |
|
setcolor(r->pcol, 1.0, 1.0, 1.0); |
| 112 |
|
setcolor(r->rcol, 0.0, 0.0, 0.0); |
| 89 |
– |
r->rt = 0.0; |
| 113 |
|
} |
| 114 |
|
|
| 115 |
|
|
| 117 |
|
RAY *r; |
| 118 |
|
{ |
| 119 |
|
extern int (*trace)(); |
| 97 |
– |
int gotmat; |
| 120 |
|
|
| 121 |
|
if (localhit(r, &thescene)) |
| 122 |
< |
gotmat = raycont(r); |
| 122 |
> |
raycont(r); /* hit local surface, evaluate */ |
| 123 |
|
else if (r->ro == &Aftplane) { |
| 124 |
< |
r->ro = NULL; |
| 124 |
> |
r->ro = NULL; /* hit aft clipping plane */ |
| 125 |
|
r->rot = FHUGE; |
| 126 |
|
} else if (sourcehit(r)) |
| 127 |
< |
gotmat = rayshade(r, r->ro->omod); |
| 127 |
> |
rayshade(r, r->ro->omod); /* distant source */ |
| 128 |
|
|
| 129 |
< |
if (r->ro != NULL && !gotmat) |
| 108 |
< |
objerror(r->ro, USER, "material not found"); |
| 129 |
> |
rayparticipate(r); /* for participating medium */ |
| 130 |
|
|
| 131 |
|
if (trace != NULL) |
| 132 |
|
(*trace)(r); /* trace execution */ |
| 137 |
|
register RAY *r; |
| 138 |
|
{ |
| 139 |
|
if ((r->clipset != NULL && inset(r->clipset, r->ro->omod)) || |
| 140 |
< |
r->ro->omod == OVOID) { |
| 140 |
> |
!rayshade(r, r->ro->omod)) |
| 141 |
|
raytrans(r); |
| 121 |
– |
return(1); |
| 122 |
– |
} |
| 123 |
– |
return(rayshade(r, r->ro->omod)); |
| 142 |
|
} |
| 143 |
|
|
| 144 |
|
|
| 149 |
|
|
| 150 |
|
if (rayorigin(&tr, r, TRANS, 1.0) == 0) { |
| 151 |
|
VCOPY(tr.rdir, r->rdir); |
| 134 |
– |
if (r->rmax > FTINY) |
| 135 |
– |
tr.rmax = r->rmax - r->rot; |
| 152 |
|
rayvalue(&tr); |
| 153 |
|
copycolor(r->rcol, tr.rcol); |
| 154 |
|
r->rt = r->rot + tr.rt; |
| 193 |
|
} |
| 194 |
|
|
| 195 |
|
|
| 196 |
+ |
rayparticipate(r) /* compute ray medium participation */ |
| 197 |
+ |
register RAY *r; |
| 198 |
+ |
{ |
| 199 |
+ |
COLOR ce, ca; |
| 200 |
+ |
double re, ge, be; |
| 201 |
+ |
|
| 202 |
+ |
if (intens(r->cext) <= 1./FHUGE) |
| 203 |
+ |
return; /* no medium */ |
| 204 |
+ |
re = r->rot*colval(r->cext,RED); |
| 205 |
+ |
ge = r->rot*colval(r->cext,GRN); |
| 206 |
+ |
be = r->rot*colval(r->cext,BLU); |
| 207 |
+ |
if (r->crtype & SHADOW) { /* no scattering for sources */ |
| 208 |
+ |
re *= 1. - colval(r->albedo,RED); |
| 209 |
+ |
ge *= 1. - colval(r->albedo,GRN); |
| 210 |
+ |
be *= 1. - colval(r->albedo,BLU); |
| 211 |
+ |
} |
| 212 |
+ |
setcolor(ce, re<=0. ? 1. : re>92. ? 0. : exp(-re), |
| 213 |
+ |
ge<=0. ? 1. : ge>92. ? 0. : exp(-ge), |
| 214 |
+ |
be<=0. ? 1. : be>92. ? 0. : exp(-be)); |
| 215 |
+ |
multcolor(r->rcol, ce); /* path absorption */ |
| 216 |
+ |
if (r->crtype & SHADOW || intens(r->albedo) <= FTINY) |
| 217 |
+ |
return; /* no scattering */ |
| 218 |
+ |
setcolor(ca, |
| 219 |
+ |
colval(r->albedo,RED)*colval(ambval,RED)*(1.-colval(ce,RED)), |
| 220 |
+ |
colval(r->albedo,GRN)*colval(ambval,GRN)*(1.-colval(ce,GRN)), |
| 221 |
+ |
colval(r->albedo,BLU)*colval(ambval,BLU)*(1.-colval(ce,BLU))); |
| 222 |
+ |
addcolor(r->rcol, ca); /* ambient in scattering */ |
| 223 |
+ |
srcscatter(r); /* source in scattering */ |
| 224 |
+ |
} |
| 225 |
+ |
|
| 226 |
+ |
|
| 227 |
|
raytexture(r, mod) /* get material modifiers */ |
| 228 |
|
RAY *r; |
| 229 |
|
int mod; |
| 242 |
|
error(USER, errmsg); |
| 243 |
|
} |
| 244 |
|
******/ |
| 245 |
< |
if ((*ofun[m->otype].funp)(m, r)) |
| 246 |
< |
objerror(r->ro, USER, "conflicting materials"); |
| 245 |
> |
if ((*ofun[m->otype].funp)(m, r)) { |
| 246 |
> |
sprintf(errmsg, "conflicting material \"%s\"", |
| 247 |
> |
m->oname); |
| 248 |
> |
objerror(r->ro, USER, errmsg); |
| 249 |
> |
} |
| 250 |
|
} |
| 251 |
|
depth--; /* end here */ |
| 252 |
|
} |
| 260 |
|
RAY fr, br; |
| 261 |
|
int foremat, backmat; |
| 262 |
|
register int i; |
| 263 |
< |
/* clip coefficient */ |
| 263 |
> |
/* bound coefficient */ |
| 264 |
|
if (coef > 1.0) |
| 265 |
|
coef = 1.0; |
| 266 |
|
else if (coef < 0.0) |
| 267 |
|
coef = 0.0; |
| 268 |
|
/* compute foreground and background */ |
| 269 |
< |
foremat = backmat = -1; |
| 269 |
> |
foremat = backmat = 0; |
| 270 |
|
/* foreground */ |
| 271 |
|
copystruct(&fr, r); |
| 272 |
< |
if (fore != OVOID && coef > FTINY) |
| 272 |
> |
if (coef > FTINY) |
| 273 |
|
foremat = rayshade(&fr, fore); |
| 274 |
|
/* background */ |
| 275 |
|
copystruct(&br, r); |
| 276 |
< |
if (back != OVOID && coef < 1.0-FTINY) |
| 276 |
> |
if (coef < 1.0-FTINY) |
| 277 |
|
backmat = rayshade(&br, back); |
| 278 |
< |
/* check */ |
| 279 |
< |
if (foremat < 0) |
| 280 |
< |
if (backmat < 0) |
| 281 |
< |
foremat = backmat = 0; |
| 282 |
< |
else |
| 283 |
< |
foremat = backmat; |
| 234 |
< |
else if (backmat < 0) |
| 235 |
< |
backmat = foremat; |
| 236 |
< |
if ((foremat==0) != (backmat==0)) |
| 237 |
< |
objerror(r->ro, USER, "mixing material with non-material"); |
| 278 |
> |
/* check for transparency */ |
| 279 |
> |
if (backmat ^ foremat) |
| 280 |
> |
if (backmat && coef > FTINY) |
| 281 |
> |
raytrans(&fr); |
| 282 |
> |
else if (foremat && coef < 1.0-FTINY) |
| 283 |
> |
raytrans(&br); |
| 284 |
|
/* mix perturbations */ |
| 285 |
|
for (i = 0; i < 3; i++) |
| 286 |
|
r->pert[i] = coef*fr.pert[i] + (1.0-coef)*br.pert[i]; |
| 289 |
|
scalecolor(br.pcol, 1.0-coef); |
| 290 |
|
copycolor(r->pcol, fr.pcol); |
| 291 |
|
addcolor(r->pcol, br.pcol); |
| 292 |
+ |
/* return value tells if material */ |
| 293 |
+ |
if (!foremat & !backmat) |
| 294 |
+ |
return(0); |
| 295 |
|
/* mix returned ray values */ |
| 296 |
< |
if (foremat) { |
| 297 |
< |
scalecolor(fr.rcol, coef); |
| 298 |
< |
scalecolor(br.rcol, 1.0-coef); |
| 299 |
< |
copycolor(r->rcol, fr.rcol); |
| 300 |
< |
addcolor(r->rcol, br.rcol); |
| 301 |
< |
r->rt = bright(fr.rcol) > bright(br.rcol) ? fr.rt : br.rt; |
| 296 |
> |
scalecolor(fr.rcol, coef); |
| 297 |
> |
scalecolor(br.rcol, 1.0-coef); |
| 298 |
> |
copycolor(r->rcol, fr.rcol); |
| 299 |
> |
addcolor(r->rcol, br.rcol); |
| 300 |
> |
r->rt = bright(fr.rcol) > bright(br.rcol) ? fr.rt : br.rt; |
| 301 |
> |
return(1); |
| 302 |
> |
} |
| 303 |
> |
|
| 304 |
> |
|
| 305 |
> |
double |
| 306 |
> |
raydist(r, flags) /* compute (cumulative) ray distance */ |
| 307 |
> |
register RAY *r; |
| 308 |
> |
register int flags; |
| 309 |
> |
{ |
| 310 |
> |
double sum = 0.0; |
| 311 |
> |
|
| 312 |
> |
while (r != NULL && r->crtype&flags) { |
| 313 |
> |
sum += r->rot; |
| 314 |
> |
r = r->parent; |
| 315 |
|
} |
| 316 |
< |
/* return value tells if material */ |
| 255 |
< |
return(foremat); |
| 316 |
> |
return(sum); |
| 317 |
|
} |
| 318 |
|
|
| 319 |
|
|
| 562 |
|
checkset(oset, cxs); /* eliminate double-checking */ |
| 563 |
|
for (i = oset[0]; i > 0; i--) { |
| 564 |
|
o = objptr(oset[i]); |
| 565 |
< |
(*ofun[o->otype].funp)(o, r); |
| 565 |
> |
if ((*ofun[o->otype].funp)(o, r)) |
| 566 |
> |
r->robj = oset[i]; |
| 567 |
|
} |
| 568 |
|
if (r->ro == NULL) |
| 569 |
|
return(0); /* no scores yet */ |