| 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 uses portions of the reflection |
| 32 |
< |
* model described by Cook and Torrance. |
| 29 |
< |
* The computation of specular components has been simplified by |
| 30 |
< |
* numerous approximations and ommisions to improve speed. |
| 31 |
> |
* This routine implements the isotropic Gaussian |
| 32 |
> |
* model described by Ward in Siggraph `92 article. |
| 33 |
|
* We orient the surface towards the incoming ray, so a single |
| 34 |
|
* surface can be used to represent an infinitely thin object. |
| 35 |
|
* |
| 40 |
|
* red grn blu rspec rough trans tspec |
| 41 |
|
*/ |
| 42 |
|
|
| 41 |
– |
#define BSPEC(m) (6.0) /* specularity parameter b */ |
| 42 |
– |
|
| 43 |
|
/* specularity flags */ |
| 44 |
|
#define SP_REFL 01 /* has reflected specular component */ |
| 45 |
|
#define SP_TRAN 02 /* has transmitted specular */ |
| 46 |
< |
#define SP_PURE 010 /* purely specular (zero roughness) */ |
| 47 |
< |
#define SP_FLAT 020 /* flat reflecting surface */ |
| 48 |
< |
#define SP_RBLT 040 /* reflection below sample threshold */ |
| 49 |
< |
#define SP_TBLT 0100 /* transmission below threshold */ |
| 46 |
> |
#define SP_PURE 04 /* purely specular (zero roughness) */ |
| 47 |
> |
#define SP_FLAT 010 /* flat reflecting surface */ |
| 48 |
> |
#define SP_RBLT 020 /* reflection below sample threshold */ |
| 49 |
> |
#define SP_TBLT 040 /* transmission below threshold */ |
| 50 |
|
|
| 51 |
|
typedef struct { |
| 52 |
|
OBJREC *mp; /* material pointer */ |
| 53 |
+ |
RAY *rp; /* ray pointer */ |
| 54 |
|
short specfl; /* specularity flags, defined above */ |
| 55 |
|
COLOR mcolor; /* color of this material */ |
| 56 |
|
COLOR scolor; /* color of specular component */ |
| 72 |
|
double omega; /* light source size */ |
| 73 |
|
{ |
| 74 |
|
double ldot; |
| 75 |
< |
double dtmp; |
| 76 |
< |
int i; |
| 75 |
> |
double dtmp, d2; |
| 76 |
> |
FVECT vtmp; |
| 77 |
|
COLOR ctmp; |
| 78 |
|
|
| 79 |
|
setcolor(cval, 0.0, 0.0, 0.0); |
| 100 |
|
* gaussian distribution model. |
| 101 |
|
*/ |
| 102 |
|
/* roughness */ |
| 103 |
< |
dtmp = 2.0*np->alpha2; |
| 103 |
> |
dtmp = np->alpha2; |
| 104 |
|
/* + source if flat */ |
| 105 |
|
if (np->specfl & SP_FLAT) |
| 106 |
< |
dtmp += omega/(2.0*PI); |
| 106 |
> |
dtmp += omega/(4.0*PI); |
| 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 *= d2; |
| 113 |
> |
d2 = (DOT(vtmp,vtmp) - d2) / d2; |
| 114 |
|
/* gaussian */ |
| 115 |
< |
dtmp = exp((DOT(np->vrefl,ldir)-1.)/dtmp)/(2.*PI)/dtmp; |
| 115 |
> |
dtmp = exp(-d2/dtmp)/(4.*PI*dtmp); |
| 116 |
|
/* worth using? */ |
| 117 |
|
if (dtmp > FTINY) { |
| 118 |
|
copycolor(ctmp, np->scolor); |
| 119 |
< |
dtmp *= omega / np->pdot; |
| 119 |
> |
dtmp *= omega * sqrt(ldot/np->pdot); |
| 120 |
|
scalecolor(ctmp, dtmp); |
| 121 |
|
addcolor(cval, ctmp); |
| 122 |
|
} |
| 136 |
|
* is always modified by material color. |
| 137 |
|
*/ |
| 138 |
|
/* roughness + source */ |
| 139 |
< |
dtmp = np->alpha2/2.0 + omega/(2.0*PI); |
| 139 |
> |
dtmp = np->alpha2 + omega/PI; |
| 140 |
|
/* gaussian */ |
| 141 |
< |
dtmp = exp((DOT(np->prdir,ldir)-1.)/dtmp)/(2.*PI)/dtmp; |
| 141 |
> |
dtmp = exp((2.*DOT(np->prdir,ldir)-2.)/dtmp)/(PI*dtmp); |
| 142 |
|
/* worth using? */ |
| 143 |
|
if (dtmp > FTINY) { |
| 144 |
|
copycolor(ctmp, np->mcolor); |
| 145 |
< |
dtmp *= np->tspec * omega / np->pdot; |
| 145 |
> |
dtmp *= np->tspec * omega * sqrt(-ldot/np->pdot); |
| 146 |
|
scalecolor(ctmp, dtmp); |
| 147 |
|
addcolor(cval, ctmp); |
| 148 |
|
} |
| 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 */ |
| 181 |
|
setcolor(nd.mcolor, m->oargs.farg[0], |
| 182 |
|
m->oargs.farg[1], |
| 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) |
| 172 |
< |
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) { |
| 182 |
< |
nd.specfl |= SP_REFL; |
| 183 |
< |
/* compute specular color */ |
| 184 |
< |
if (m->otype == MAT_METAL) |
| 185 |
< |
copycolor(nd.scolor, nd.mcolor); |
| 186 |
< |
else |
| 187 |
< |
setcolor(nd.scolor, 1.0, 1.0, 1.0); |
| 188 |
< |
scalecolor(nd.scolor, nd.rspec); |
| 189 |
< |
/* improved model */ |
| 190 |
< |
dtmp = exp(-BSPEC(m)*nd.pdot); |
| 191 |
< |
for (i = 0; i < 3; i++) |
| 192 |
< |
colval(nd.scolor,i) += (1.0-colval(nd.scolor,i))*dtmp; |
| 193 |
< |
nd.rspec += (1.0-nd.rspec)*dtmp; |
| 194 |
< |
/* check threshold */ |
| 195 |
< |
if (specthresh > FTINY && |
| 196 |
< |
((specthresh >= 1.-FTINY || |
| 197 |
< |
specthresh + (.05 - .1*urand(8199+samplendx)) |
| 198 |
< |
> nd.rspec))) |
| 199 |
< |
nd.specfl |= SP_RBLT; |
| 200 |
< |
/* compute reflected ray */ |
| 201 |
< |
for (i = 0; i < 3; i++) |
| 202 |
< |
nd.vrefl[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i]; |
| 203 |
< |
if (DOT(nd.vrefl, r->ron) <= FTINY) /* penetration? */ |
| 204 |
< |
for (i = 0; i < 3; i++) /* safety measure */ |
| 205 |
< |
nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i]; |
| 206 |
< |
|
| 207 |
< |
if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) { |
| 208 |
< |
RAY lr; |
| 209 |
< |
if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) { |
| 210 |
< |
VCOPY(lr.rdir, nd.vrefl); |
| 211 |
< |
rayvalue(&lr); |
| 212 |
< |
multcolor(lr.rcol, nd.scolor); |
| 213 |
< |
addcolor(r->rcol, lr.rcol); |
| 214 |
< |
} |
| 215 |
< |
} |
| 216 |
< |
} |
| 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 (specthresh > FTINY && |
| 214 |
< |
((specthresh >= 1.-FTINY || |
| 227 |
< |
specthresh + |
| 228 |
< |
(.05 - .1*urand(7241+samplendx)) |
| 229 |
< |
> nd.tspec))) |
| 213 |
> |
if (!(nd.specfl & SP_PURE) && |
| 214 |
> |
specthresh >= nd.tspec-FTINY) |
| 215 |
|
nd.specfl |= SP_TBLT; |
| 216 |
< |
if (r->crtype & SHADOW || |
| 232 |
< |
DOT(r->pert,r->pert) <= FTINY*FTINY) { |
| 216 |
> |
if (!hastexture || r->crtype & SHADOW) { |
| 217 |
|
VCOPY(nd.prdir, r->rdir); |
| 218 |
|
transtest = 2; |
| 219 |
|
} else { |
| 220 |
|
for (i = 0; i < 3; i++) /* perturb */ |
| 221 |
< |
nd.prdir[i] = r->rdir[i] - |
| 238 |
< |
0.5*r->pert[i]; |
| 221 |
> |
nd.prdir[i] = r->rdir[i] - r->pert[i]; |
| 222 |
|
if (DOT(nd.prdir, r->ron) < -FTINY) |
| 223 |
|
normalize(nd.prdir); /* OK */ |
| 224 |
|
else |
| 239 |
|
transtest *= bright(lr.rcol); |
| 240 |
|
transdist = r->rot + lr.rt; |
| 241 |
|
} |
| 242 |
+ |
} else |
| 243 |
+ |
transtest = 0; |
| 244 |
+ |
|
| 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) /* the rest is shadow */ |
| 270 |
< |
return; |
| 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 |
|
|
| 269 |
– |
if (r->ro->otype == OBJ_FACE || r->ro->otype == OBJ_RING) |
| 270 |
– |
nd.specfl |= SP_FLAT; |
| 271 |
– |
|
| 289 |
|
if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_PURE)) |
| 290 |
|
gaussamp(r, &nd); |
| 291 |
|
|
| 312 |
|
/* add direct component */ |
| 313 |
|
direct(r, dirnorm, &nd); |
| 314 |
|
/* check distance */ |
| 315 |
< |
if (transtest > bright(r->rcol)) |
| 315 |
> |
d = bright(r->rcol); |
| 316 |
> |
if (transtest > d) |
| 317 |
|
r->rt = transdist; |
| 318 |
+ |
else if (mirtest > d) |
| 319 |
+ |
r->rt = mirdist; |
| 320 |
+ |
|
| 321 |
+ |
return(1); |
| 322 |
|
} |
| 323 |
|
|
| 324 |
|
|
| 332 |
|
double rv[2]; |
| 333 |
|
double d, sinp, cosp; |
| 334 |
|
register int i; |
| 335 |
+ |
/* quick test */ |
| 336 |
+ |
if ((np->specfl & (SP_REFL|SP_RBLT)) != SP_REFL && |
| 337 |
+ |
(np->specfl & (SP_TRAN|SP_TBLT)) != SP_TRAN) |
| 338 |
+ |
return; |
| 339 |
|
/* set up sample coordinates */ |
| 340 |
|
v[0] = v[1] = v[2] = 0.0; |
| 341 |
|
for (i = 0; i < 3; i++) |
| 384 |
|
if (rv[1] <= FTINY) |
| 385 |
|
d = 1.0; |
| 386 |
|
else |
| 387 |
< |
d = sqrt( np->alpha2/4.0 * -log(rv[1]) ); |
| 387 |
> |
d = sqrt( -log(rv[1]) * np->alpha2 ); |
| 388 |
|
for (i = 0; i < 3; i++) |
| 389 |
|
sr.rdir[i] = np->prdir[i] + d*(cosp*u[i] + sinp*v[i]); |
| 390 |
|
if (DOT(sr.rdir, r->ron) < -FTINY) |
| 392 |
|
else |
| 393 |
|
VCOPY(sr.rdir, np->prdir); /* else no jitter */ |
| 394 |
|
rayvalue(&sr); |
| 395 |
< |
multcolor(sr.rcol, np->scolor); |
| 395 |
> |
scalecolor(sr.rcol, np->tspec); |
| 396 |
> |
multcolor(sr.rcol, np->mcolor); /* modified by color */ |
| 397 |
|
addcolor(r->rcol, sr.rcol); |
| 398 |
|
ndims--; |
| 399 |
|
} |