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 */ |
74 |
|
double ldot; |
75 |
|
double dtmp, d2; |
76 |
|
FVECT vtmp; |
77 |
– |
register int i; |
77 |
|
COLOR ctmp; |
78 |
|
|
79 |
|
setcolor(cval, 0.0, 0.0, 0.0); |
104 |
|
/* + source if flat */ |
105 |
|
if (np->specfl & SP_FLAT) |
106 |
|
dtmp += omega/(4.0*PI); |
107 |
< |
/* delta */ |
108 |
< |
for (i = 0; i < 3; i++) |
109 |
< |
vtmp[i] = ldir[i] - np->rp->rdir[i]; |
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? */ |
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 * sqrt(ldot/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 */ |
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) |
180 |
< |
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; |
202 |
> |
mirtest = transtest = 0; |
203 |
> |
mirdist = transdist = r->rot; |
204 |
|
/* get specular component */ |
205 |
|
if ((nd.rspec = m->oargs.farg[3]) > FTINY) { |
206 |
|
nd.specfl |= SP_REFL; |
210 |
|
else |
211 |
|
setcolor(nd.scolor, 1.0, 1.0, 1.0); |
212 |
|
scalecolor(nd.scolor, nd.rspec); |
197 |
– |
/* improved model */ |
198 |
– |
dtmp = exp(-BSPEC(m)*nd.pdot); |
199 |
– |
for (i = 0; i < 3; i++) |
200 |
– |
colval(nd.scolor,i) += (1.0-colval(nd.scolor,i))*dtmp; |
201 |
– |
nd.rspec += (1.0-nd.rspec)*dtmp; |
213 |
|
/* check threshold */ |
214 |
< |
if (!(nd.specfl & SP_PURE) && |
204 |
< |
specthresh > FTINY && |
205 |
< |
(specthresh >= 1.-FTINY || |
206 |
< |
specthresh > nd.rspec)) |
214 |
> |
if (!(nd.specfl & SP_PURE) && specthresh >= nd.rspec-FTINY) |
215 |
|
nd.specfl |= SP_RBLT; |
216 |
|
/* compute reflected ray */ |
217 |
|
for (i = 0; i < 3; i++) |
218 |
< |
nd.vrefl[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i]; |
219 |
< |
if (DOT(nd.vrefl, r->ron) <= FTINY) /* penetration? */ |
218 |
> |
nd.vrefl[i] = r->rdir[i] + 2.*nd.pdot*nd.pnorm[i]; |
219 |
> |
/* penetration? */ |
220 |
> |
if (hastexture && DOT(nd.vrefl, r->ron) <= FTINY) |
221 |
|
for (i = 0; i < 3; i++) /* safety measure */ |
222 |
|
nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i]; |
223 |
|
|
228 |
|
rayvalue(&lr); |
229 |
|
multcolor(lr.rcol, nd.scolor); |
230 |
|
addcolor(r->rcol, lr.rcol); |
231 |
+ |
if (!hastexture && nd.specfl & SP_FLAT) { |
232 |
+ |
mirtest = 2.*bright(lr.rcol); |
233 |
+ |
mirdist = r->rot + lr.rt; |
234 |
+ |
} |
235 |
|
} |
236 |
|
} |
237 |
|
} |
243 |
|
if (nd.tspec > FTINY) { |
244 |
|
nd.specfl |= SP_TRAN; |
245 |
|
/* check threshold */ |
246 |
< |
if (!(nd.specfl & SP_PURE) && specthresh > FTINY && |
247 |
< |
(specthresh >= 1.-FTINY || |
235 |
< |
specthresh > nd.tspec)) |
246 |
> |
if (!(nd.specfl & SP_PURE) && |
247 |
> |
specthresh >= nd.tspec-FTINY) |
248 |
|
nd.specfl |= SP_TBLT; |
249 |
< |
if (r->crtype & SHADOW || |
238 |
< |
DOT(r->pert,r->pert) <= FTINY*FTINY) { |
249 |
> |
if (!hastexture || r->crtype & SHADOW) { |
250 |
|
VCOPY(nd.prdir, r->rdir); |
251 |
|
transtest = 2; |
252 |
|
} else { |
253 |
|
for (i = 0; i < 3; i++) /* perturb */ |
254 |
< |
nd.prdir[i] = r->rdir[i] - |
244 |
< |
0.5*r->pert[i]; |
254 |
> |
nd.prdir[i] = r->rdir[i] - r->pert[i]; |
255 |
|
if (DOT(nd.prdir, r->ron) < -FTINY) |
256 |
|
normalize(nd.prdir); /* OK */ |
257 |
|
else |
275 |
|
} else |
276 |
|
transtest = 0; |
277 |
|
|
278 |
< |
if (r->crtype & SHADOW) /* the rest is shadow */ |
279 |
< |
return; |
278 |
> |
if (r->crtype & SHADOW) { /* the rest is shadow */ |
279 |
> |
r->rt = transdist; |
280 |
> |
return(1); |
281 |
> |
} |
282 |
|
/* diffuse reflection */ |
283 |
|
nd.rdiff = 1.0 - nd.trans - nd.rspec; |
284 |
|
|
285 |
|
if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY) |
286 |
< |
return; /* 100% pure specular */ |
286 |
> |
return(1); /* 100% pure specular */ |
287 |
|
|
276 |
– |
if (r->ro != NULL && (r->ro->otype == OBJ_FACE || |
277 |
– |
r->ro->otype == OBJ_RING)) |
278 |
– |
nd.specfl |= SP_FLAT; |
279 |
– |
|
288 |
|
if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_PURE)) |
289 |
|
gaussamp(r, &nd); |
290 |
|
|
311 |
|
/* add direct component */ |
312 |
|
direct(r, dirnorm, &nd); |
313 |
|
/* check distance */ |
314 |
< |
if (transtest > bright(r->rcol)) |
314 |
> |
d = bright(r->rcol); |
315 |
> |
if (transtest > d) |
316 |
|
r->rt = transdist; |
317 |
+ |
else if (mirtest > d) |
318 |
+ |
r->rt = mirdist; |
319 |
+ |
|
320 |
+ |
return(1); |
321 |
|
} |
322 |
|
|
323 |
|
|
383 |
|
if (rv[1] <= FTINY) |
384 |
|
d = 1.0; |
385 |
|
else |
386 |
< |
d = sqrt( np->alpha2/4.0 * -log(rv[1]) ); |
386 |
> |
d = sqrt( -log(rv[1]) * np->alpha2 ); |
387 |
|
for (i = 0; i < 3; i++) |
388 |
|
sr.rdir[i] = np->prdir[i] + d*(cosp*u[i] + sinp*v[i]); |
389 |
|
if (DOT(sr.rdir, r->ron) < -FTINY) |