| 1 |
< |
/* Copyright (c) 1992 Regents of the University of California */ |
| 1 |
> |
/* Copyright (c) 1995 Regents of the University of California */ |
| 2 |
|
|
| 3 |
|
#ifndef lint |
| 4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
| 23 |
|
extern double specthresh; /* specular sampling threshold */ |
| 24 |
|
extern double specjitter; /* specular sampling jitter */ |
| 25 |
|
|
| 26 |
+ |
extern int backvis; /* back faces visible? */ |
| 27 |
+ |
|
| 28 |
+ |
static gaussamp(); |
| 29 |
+ |
|
| 30 |
|
/* |
| 31 |
|
* This routine implements the isotropic Gaussian |
| 32 |
|
* model described by Ward in Siggraph `92 article. |
| 40 |
|
* red grn blu rspec rough trans tspec |
| 41 |
|
*/ |
| 42 |
|
|
| 39 |
– |
#define BSPEC(m) (6.0) /* specularity parameter b */ |
| 40 |
– |
|
| 43 |
|
/* specularity flags */ |
| 44 |
|
#define SP_REFL 01 /* has reflected specular component */ |
| 45 |
|
#define SP_TRAN 02 /* has transmitted specular */ |
| 104 |
|
/* + source if flat */ |
| 105 |
|
if (np->specfl & SP_FLAT) |
| 106 |
|
dtmp += omega/(4.0*PI); |
| 107 |
< |
/* delta */ |
| 107 |
> |
/* half vector */ |
| 108 |
|
vtmp[0] = ldir[0] - np->rp->rdir[0]; |
| 109 |
|
vtmp[1] = ldir[1] - np->rp->rdir[1]; |
| 110 |
|
vtmp[2] = ldir[2] - np->rp->rdir[2]; |
| 111 |
|
d2 = DOT(vtmp, np->pnorm); |
| 112 |
< |
d2 = 2.0 - 2.0*d2/sqrt(DOT(vtmp,vtmp)); |
| 112 |
> |
d2 *= d2; |
| 113 |
> |
d2 = (DOT(vtmp,vtmp) - d2) / d2; |
| 114 |
|
/* gaussian */ |
| 115 |
|
dtmp = exp(-d2/dtmp)/(4.*PI*dtmp); |
| 116 |
|
/* worth using? */ |
| 156 |
|
{ |
| 157 |
|
NORMDAT nd; |
| 158 |
|
double transtest, transdist; |
| 159 |
< |
double dtmp; |
| 159 |
> |
double mirtest, mirdist; |
| 160 |
> |
int hastexture; |
| 161 |
> |
double d; |
| 162 |
|
COLOR ctmp; |
| 163 |
|
register int i; |
| 164 |
|
/* easy shadow test */ |
| 165 |
|
if (r->crtype & SHADOW && m->otype != MAT_TRANS) |
| 166 |
< |
return; |
| 166 |
> |
return(1); |
| 167 |
|
|
| 168 |
|
if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5)) |
| 169 |
|
objerror(m, USER, "bad number of arguments"); |
| 170 |
+ |
/* check for back side */ |
| 171 |
+ |
if (r->rod < 0.0) { |
| 172 |
+ |
if (!backvis && m->otype != MAT_TRANS) { |
| 173 |
+ |
raytrans(r); |
| 174 |
+ |
return(1); |
| 175 |
+ |
} |
| 176 |
+ |
flipsurface(r); /* reorient if backvis */ |
| 177 |
+ |
} |
| 178 |
|
nd.mp = m; |
| 179 |
|
nd.rp = r; |
| 180 |
|
/* get material color */ |
| 186 |
|
nd.alpha2 = m->oargs.farg[4]; |
| 187 |
|
if ((nd.alpha2 *= nd.alpha2) <= FTINY) |
| 188 |
|
nd.specfl |= SP_PURE; |
| 189 |
< |
/* reorient if necessary */ |
| 190 |
< |
if (r->rod < 0.0) |
| 178 |
< |
flipsurface(r); |
| 189 |
> |
if (r->ro != NULL && isflat(r->ro->otype)) |
| 190 |
> |
nd.specfl |= SP_FLAT; |
| 191 |
|
/* get modifiers */ |
| 192 |
|
raytexture(r, m->omod); |
| 193 |
< |
nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ |
| 193 |
> |
if (hastexture = DOT(r->pert,r->pert) > FTINY*FTINY) |
| 194 |
> |
nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ |
| 195 |
> |
else { |
| 196 |
> |
VCOPY(nd.pnorm, r->ron); |
| 197 |
> |
nd.pdot = r->rod; |
| 198 |
> |
} |
| 199 |
|
if (nd.pdot < .001) |
| 200 |
|
nd.pdot = .001; /* non-zero for dirnorm() */ |
| 201 |
|
multcolor(nd.mcolor, r->pcol); /* modify material color */ |
| 202 |
< |
transtest = 0; |
| 203 |
< |
/* get specular component */ |
| 204 |
< |
if ((nd.rspec = m->oargs.farg[3]) > FTINY) { |
| 188 |
< |
nd.specfl |= SP_REFL; |
| 189 |
< |
/* compute specular color */ |
| 190 |
< |
if (m->otype == MAT_METAL) |
| 191 |
< |
copycolor(nd.scolor, nd.mcolor); |
| 192 |
< |
else |
| 193 |
< |
setcolor(nd.scolor, 1.0, 1.0, 1.0); |
| 194 |
< |
scalecolor(nd.scolor, nd.rspec); |
| 195 |
< |
/* improved model */ |
| 196 |
< |
dtmp = exp(-BSPEC(m)*nd.pdot); |
| 197 |
< |
for (i = 0; i < 3; i++) |
| 198 |
< |
colval(nd.scolor,i) += (1.0-colval(nd.scolor,i))*dtmp; |
| 199 |
< |
nd.rspec += (1.0-nd.rspec)*dtmp; |
| 200 |
< |
/* check threshold */ |
| 201 |
< |
if (!(nd.specfl & SP_PURE) && |
| 202 |
< |
specthresh > FTINY && |
| 203 |
< |
(specthresh >= 1.-FTINY || |
| 204 |
< |
specthresh + .05 - .1*frandom() > nd.rspec)) |
| 205 |
< |
nd.specfl |= SP_RBLT; |
| 206 |
< |
/* compute reflected ray */ |
| 207 |
< |
for (i = 0; i < 3; i++) |
| 208 |
< |
nd.vrefl[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i]; |
| 209 |
< |
if (DOT(nd.vrefl, r->ron) <= FTINY) /* penetration? */ |
| 210 |
< |
for (i = 0; i < 3; i++) /* safety measure */ |
| 211 |
< |
nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i]; |
| 212 |
< |
|
| 213 |
< |
if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) { |
| 214 |
< |
RAY lr; |
| 215 |
< |
if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) { |
| 216 |
< |
VCOPY(lr.rdir, nd.vrefl); |
| 217 |
< |
rayvalue(&lr); |
| 218 |
< |
multcolor(lr.rcol, nd.scolor); |
| 219 |
< |
addcolor(r->rcol, lr.rcol); |
| 220 |
< |
} |
| 221 |
< |
} |
| 222 |
< |
} |
| 202 |
> |
mirtest = transtest = 0; |
| 203 |
> |
mirdist = transdist = r->rot; |
| 204 |
> |
nd.rspec = m->oargs.farg[3]; |
| 205 |
|
/* compute transmission */ |
| 206 |
|
if (m->otype == MAT_TRANS) { |
| 207 |
|
nd.trans = m->oargs.farg[5]*(1.0 - nd.rspec); |
| 210 |
|
if (nd.tspec > FTINY) { |
| 211 |
|
nd.specfl |= SP_TRAN; |
| 212 |
|
/* check threshold */ |
| 213 |
< |
if (!(nd.specfl & SP_PURE) && specthresh > FTINY && |
| 214 |
< |
(specthresh >= 1.-FTINY || |
| 233 |
< |
specthresh + .05 - .1*frandom() > nd.tspec)) |
| 213 |
> |
if (!(nd.specfl & SP_PURE) && |
| 214 |
> |
specthresh >= nd.tspec-FTINY) |
| 215 |
|
nd.specfl |= SP_TBLT; |
| 216 |
< |
if (r->crtype & SHADOW || |
| 236 |
< |
DOT(r->pert,r->pert) <= FTINY*FTINY) { |
| 216 |
> |
if (!hastexture || r->crtype & SHADOW) { |
| 217 |
|
VCOPY(nd.prdir, r->rdir); |
| 218 |
|
transtest = 2; |
| 219 |
|
} else { |
| 242 |
|
} else |
| 243 |
|
transtest = 0; |
| 244 |
|
|
| 245 |
< |
if (r->crtype & SHADOW) /* the rest is shadow */ |
| 246 |
< |
return; |
| 245 |
> |
if (r->crtype & SHADOW) { /* the rest is shadow */ |
| 246 |
> |
r->rt = transdist; |
| 247 |
> |
return(1); |
| 248 |
> |
} |
| 249 |
> |
/* get specular reflection */ |
| 250 |
> |
if (nd.rspec > FTINY) { |
| 251 |
> |
nd.specfl |= SP_REFL; |
| 252 |
> |
/* compute specular color */ |
| 253 |
> |
if (m->otype == MAT_METAL) |
| 254 |
> |
copycolor(nd.scolor, nd.mcolor); |
| 255 |
> |
else |
| 256 |
> |
setcolor(nd.scolor, 1.0, 1.0, 1.0); |
| 257 |
> |
scalecolor(nd.scolor, nd.rspec); |
| 258 |
> |
/* check threshold */ |
| 259 |
> |
if (!(nd.specfl & SP_PURE) && specthresh >= nd.rspec-FTINY) |
| 260 |
> |
nd.specfl |= SP_RBLT; |
| 261 |
> |
/* compute reflected ray */ |
| 262 |
> |
for (i = 0; i < 3; i++) |
| 263 |
> |
nd.vrefl[i] = r->rdir[i] + 2.*nd.pdot*nd.pnorm[i]; |
| 264 |
> |
/* penetration? */ |
| 265 |
> |
if (hastexture && DOT(nd.vrefl, r->ron) <= FTINY) |
| 266 |
> |
for (i = 0; i < 3; i++) /* safety measure */ |
| 267 |
> |
nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i]; |
| 268 |
> |
|
| 269 |
> |
if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) { |
| 270 |
> |
RAY lr; |
| 271 |
> |
if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) { |
| 272 |
> |
VCOPY(lr.rdir, nd.vrefl); |
| 273 |
> |
rayvalue(&lr); |
| 274 |
> |
multcolor(lr.rcol, nd.scolor); |
| 275 |
> |
addcolor(r->rcol, lr.rcol); |
| 276 |
> |
if (!hastexture && nd.specfl & SP_FLAT) { |
| 277 |
> |
mirtest = 2.*bright(lr.rcol); |
| 278 |
> |
mirdist = r->rot + lr.rt; |
| 279 |
> |
} |
| 280 |
> |
} |
| 281 |
> |
} |
| 282 |
> |
} |
| 283 |
|
/* diffuse reflection */ |
| 284 |
|
nd.rdiff = 1.0 - nd.trans - nd.rspec; |
| 285 |
|
|
| 286 |
|
if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY) |
| 287 |
< |
return; /* 100% pure specular */ |
| 287 |
> |
return(1); /* 100% pure specular */ |
| 288 |
|
|
| 273 |
– |
if (r->ro != NULL && (r->ro->otype == OBJ_FACE || |
| 274 |
– |
r->ro->otype == OBJ_RING)) |
| 275 |
– |
nd.specfl |= SP_FLAT; |
| 276 |
– |
|
| 289 |
|
if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_PURE)) |
| 290 |
|
gaussamp(r, &nd); |
| 291 |
|
|
| 292 |
|
if (nd.rdiff > FTINY) { /* ambient from this side */ |
| 293 |
< |
ambient(ctmp, r); |
| 293 |
> |
ambient(ctmp, r, hastexture?nd.pnorm:r->ron); |
| 294 |
|
if (nd.specfl & SP_RBLT) |
| 295 |
|
scalecolor(ctmp, 1.0-nd.trans); |
| 296 |
|
else |
| 300 |
|
} |
| 301 |
|
if (nd.tdiff > FTINY) { /* ambient from other side */ |
| 302 |
|
flipsurface(r); |
| 303 |
< |
ambient(ctmp, r); |
| 303 |
> |
if (hastexture) { |
| 304 |
> |
FVECT bnorm; |
| 305 |
> |
bnorm[0] = -nd.pnorm[0]; |
| 306 |
> |
bnorm[1] = -nd.pnorm[1]; |
| 307 |
> |
bnorm[2] = -nd.pnorm[2]; |
| 308 |
> |
ambient(ctmp, r, bnorm); |
| 309 |
> |
} else |
| 310 |
> |
ambient(ctmp, r, r->ron); |
| 311 |
|
if (nd.specfl & SP_TBLT) |
| 312 |
|
scalecolor(ctmp, nd.trans); |
| 313 |
|
else |
| 319 |
|
/* add direct component */ |
| 320 |
|
direct(r, dirnorm, &nd); |
| 321 |
|
/* check distance */ |
| 322 |
< |
if (transtest > bright(r->rcol)) |
| 322 |
> |
d = bright(r->rcol); |
| 323 |
> |
if (transtest > d) |
| 324 |
|
r->rt = transdist; |
| 325 |
+ |
else if (mirtest > d) |
| 326 |
+ |
r->rt = mirdist; |
| 327 |
+ |
|
| 328 |
+ |
return(1); |
| 329 |
|
} |
| 330 |
|
|
| 331 |
|
|