24 |
|
extern double specjitter; /* specular sampling jitter */ |
25 |
|
|
26 |
|
/* |
27 |
< |
* This routine uses portions of the reflection |
28 |
< |
* model described by Cook and Torrance. |
29 |
< |
* The computation of specular components has been simplified by |
30 |
< |
* numerous approximations and ommisions to improve speed. |
27 |
> |
* This routine implements the isotropic Gaussian |
28 |
> |
* model described by Ward in Siggraph `92 article. |
29 |
|
* We orient the surface towards the incoming ray, so a single |
30 |
|
* surface can be used to represent an infinitely thin object. |
31 |
|
* |
41 |
|
/* specularity flags */ |
42 |
|
#define SP_REFL 01 /* has reflected specular component */ |
43 |
|
#define SP_TRAN 02 /* has transmitted specular */ |
44 |
< |
#define SP_PURE 010 /* purely specular (zero roughness) */ |
45 |
< |
#define SP_FLAT 020 /* flat reflecting surface */ |
46 |
< |
#define SP_RBLT 040 /* reflection below sample threshold */ |
47 |
< |
#define SP_TBLT 0100 /* transmission below threshold */ |
44 |
> |
#define SP_PURE 04 /* purely specular (zero roughness) */ |
45 |
> |
#define SP_FLAT 010 /* flat reflecting surface */ |
46 |
> |
#define SP_RBLT 020 /* reflection below sample threshold */ |
47 |
> |
#define SP_TBLT 040 /* transmission below threshold */ |
48 |
|
|
49 |
|
typedef struct { |
50 |
|
OBJREC *mp; /* material pointer */ |
51 |
+ |
RAY *rp; /* ray pointer */ |
52 |
|
short specfl; /* specularity flags, defined above */ |
53 |
|
COLOR mcolor; /* color of this material */ |
54 |
|
COLOR scolor; /* color of specular component */ |
70 |
|
double omega; /* light source size */ |
71 |
|
{ |
72 |
|
double ldot; |
73 |
< |
double dtmp; |
74 |
< |
int i; |
73 |
> |
double dtmp, d2; |
74 |
> |
FVECT vtmp; |
75 |
|
COLOR ctmp; |
76 |
|
|
77 |
|
setcolor(cval, 0.0, 0.0, 0.0); |
98 |
|
* gaussian distribution model. |
99 |
|
*/ |
100 |
|
/* roughness */ |
101 |
< |
dtmp = 2.0*np->alpha2; |
101 |
> |
dtmp = np->alpha2; |
102 |
|
/* + source if flat */ |
103 |
|
if (np->specfl & SP_FLAT) |
104 |
< |
dtmp += omega/(2.0*PI); |
104 |
> |
dtmp += omega/(4.0*PI); |
105 |
> |
/* delta */ |
106 |
> |
vtmp[0] = ldir[0] - np->rp->rdir[0]; |
107 |
> |
vtmp[1] = ldir[1] - np->rp->rdir[1]; |
108 |
> |
vtmp[2] = ldir[2] - np->rp->rdir[2]; |
109 |
> |
d2 = DOT(vtmp, np->pnorm); |
110 |
> |
d2 = 2.0 - 2.0*d2/sqrt(DOT(vtmp,vtmp)); |
111 |
|
/* gaussian */ |
112 |
< |
dtmp = exp((DOT(np->vrefl,ldir)-1.)/dtmp)/(2.*PI)/dtmp; |
112 |
> |
dtmp = exp(-d2/dtmp)/(4.*PI*dtmp); |
113 |
|
/* worth using? */ |
114 |
|
if (dtmp > FTINY) { |
115 |
|
copycolor(ctmp, np->scolor); |
116 |
< |
dtmp *= omega / np->pdot; |
116 |
> |
dtmp *= omega * sqrt(ldot/np->pdot); |
117 |
|
scalecolor(ctmp, dtmp); |
118 |
|
addcolor(cval, ctmp); |
119 |
|
} |
133 |
|
* is always modified by material color. |
134 |
|
*/ |
135 |
|
/* roughness + source */ |
136 |
< |
dtmp = np->alpha2 + omega/(2.0*PI); |
136 |
> |
dtmp = np->alpha2 + omega/PI; |
137 |
|
/* gaussian */ |
138 |
< |
dtmp = exp((DOT(np->prdir,ldir)-1.)/dtmp)/(2.*PI)/dtmp; |
138 |
> |
dtmp = exp((2.*DOT(np->prdir,ldir)-2.)/dtmp)/(PI*dtmp); |
139 |
|
/* worth using? */ |
140 |
|
if (dtmp > FTINY) { |
141 |
|
copycolor(ctmp, np->mcolor); |
142 |
< |
dtmp *= np->tspec * omega / np->pdot; |
142 |
> |
dtmp *= np->tspec * omega * sqrt(-ldot/np->pdot); |
143 |
|
scalecolor(ctmp, dtmp); |
144 |
|
addcolor(cval, ctmp); |
145 |
|
} |
163 |
|
if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5)) |
164 |
|
objerror(m, USER, "bad number of arguments"); |
165 |
|
nd.mp = m; |
166 |
+ |
nd.rp = r; |
167 |
|
/* get material color */ |
168 |
|
setcolor(nd.mcolor, m->oargs.farg[0], |
169 |
|
m->oargs.farg[1], |
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 (specthresh > FTINY && |
202 |
< |
((specthresh >= 1.-FTINY || |
203 |
< |
specthresh + (.1 - .2*urand(8199+samplendx)) |
204 |
< |
> nd.rspec))) |
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; |
228 |
|
if (nd.tspec > FTINY) { |
229 |
|
nd.specfl |= SP_TRAN; |
230 |
|
/* check threshold */ |
231 |
< |
if (specthresh > FTINY && |
232 |
< |
((specthresh >= 1.-FTINY || |
233 |
< |
specthresh + |
225 |
< |
(.1 - .2*urand(7241+samplendx)) |
226 |
< |
> nd.tspec))) |
231 |
> |
if (!(nd.specfl & SP_PURE) && specthresh > FTINY && |
232 |
> |
(specthresh >= 1.-FTINY || |
233 |
> |
specthresh + .05 - .1*frandom() > nd.tspec)) |
234 |
|
nd.specfl |= SP_TBLT; |
235 |
|
if (r->crtype & SHADOW || |
236 |
|
DOT(r->pert,r->pert) <= FTINY*FTINY) { |
238 |
|
transtest = 2; |
239 |
|
} else { |
240 |
|
for (i = 0; i < 3; i++) /* perturb */ |
241 |
< |
nd.prdir[i] = r->rdir[i] - |
242 |
< |
.75*r->pert[i]; |
243 |
< |
normalize(nd.prdir); |
241 |
> |
nd.prdir[i] = r->rdir[i] - r->pert[i]; |
242 |
> |
if (DOT(nd.prdir, r->ron) < -FTINY) |
243 |
> |
normalize(nd.prdir); /* OK */ |
244 |
> |
else |
245 |
> |
VCOPY(nd.prdir, r->rdir); |
246 |
|
} |
247 |
|
} |
248 |
|
} else |
259 |
|
transtest *= bright(lr.rcol); |
260 |
|
transdist = r->rot + lr.rt; |
261 |
|
} |
262 |
< |
} |
262 |
> |
} else |
263 |
> |
transtest = 0; |
264 |
|
|
265 |
|
if (r->crtype & SHADOW) /* the rest is shadow */ |
266 |
|
return; |
270 |
|
if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY) |
271 |
|
return; /* 100% pure specular */ |
272 |
|
|
273 |
< |
if (r->ro->otype == OBJ_FACE || r->ro->otype == OBJ_RING) |
273 |
> |
if (r->ro != NULL && (r->ro->otype == OBJ_FACE || |
274 |
> |
r->ro->otype == OBJ_RING)) |
275 |
|
nd.specfl |= SP_FLAT; |
276 |
|
|
277 |
|
if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_PURE)) |
314 |
|
FVECT u, v, h; |
315 |
|
double rv[2]; |
316 |
|
double d, sinp, cosp; |
306 |
– |
int ntries; |
317 |
|
register int i; |
318 |
+ |
/* quick test */ |
319 |
+ |
if ((np->specfl & (SP_REFL|SP_RBLT)) != SP_REFL && |
320 |
+ |
(np->specfl & (SP_TRAN|SP_TBLT)) != SP_TRAN) |
321 |
+ |
return; |
322 |
|
/* set up sample coordinates */ |
323 |
|
v[0] = v[1] = v[2] = 0.0; |
324 |
|
for (i = 0; i < 3; i++) |
332 |
|
if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL && |
333 |
|
rayorigin(&sr, r, SPECULAR, np->rspec) == 0) { |
334 |
|
dimlist[ndims++] = (int)np->mp; |
335 |
< |
for (ntries = 0; ntries < 10; ntries++) { |
336 |
< |
dimlist[ndims] = ntries * 8912; |
337 |
< |
d = urand(ilhash(dimlist,ndims+1)+samplendx); |
338 |
< |
multisamp(rv, 2, d); |
339 |
< |
d = 2.0*PI * rv[0]; |
340 |
< |
cosp = cos(d); |
341 |
< |
sinp = sin(d); |
342 |
< |
rv[1] = 1.0 - specjitter*rv[1]; |
343 |
< |
if (rv[1] <= FTINY) |
344 |
< |
d = 1.0; |
345 |
< |
else |
346 |
< |
d = sqrt( np->alpha2 * -log(rv[1]) ); |
347 |
< |
for (i = 0; i < 3; i++) |
348 |
< |
h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*v[i]); |
349 |
< |
d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d); |
350 |
< |
for (i = 0; i < 3; i++) |
351 |
< |
sr.rdir[i] = r->rdir[i] + d*h[i]; |
352 |
< |
if (DOT(sr.rdir, r->ron) > FTINY) { |
353 |
< |
rayvalue(&sr); |
354 |
< |
multcolor(sr.rcol, np->scolor); |
341 |
< |
addcolor(r->rcol, sr.rcol); |
342 |
< |
break; |
343 |
< |
} |
344 |
< |
} |
335 |
> |
d = urand(ilhash(dimlist,ndims)+samplendx); |
336 |
> |
multisamp(rv, 2, d); |
337 |
> |
d = 2.0*PI * rv[0]; |
338 |
> |
cosp = cos(d); |
339 |
> |
sinp = sin(d); |
340 |
> |
rv[1] = 1.0 - specjitter*rv[1]; |
341 |
> |
if (rv[1] <= FTINY) |
342 |
> |
d = 1.0; |
343 |
> |
else |
344 |
> |
d = sqrt( np->alpha2 * -log(rv[1]) ); |
345 |
> |
for (i = 0; i < 3; i++) |
346 |
> |
h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*v[i]); |
347 |
> |
d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d); |
348 |
> |
for (i = 0; i < 3; i++) |
349 |
> |
sr.rdir[i] = r->rdir[i] + d*h[i]; |
350 |
> |
if (DOT(sr.rdir, r->ron) <= FTINY) |
351 |
> |
VCOPY(sr.rdir, np->vrefl); /* jitter no good */ |
352 |
> |
rayvalue(&sr); |
353 |
> |
multcolor(sr.rcol, np->scolor); |
354 |
> |
addcolor(r->rcol, sr.rcol); |
355 |
|
ndims--; |
356 |
|
} |
357 |
|
/* compute transmission */ |
358 |
+ |
if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN && |
359 |
+ |
rayorigin(&sr, r, SPECULAR, np->tspec) == 0) { |
360 |
+ |
dimlist[ndims++] = (int)np->mp; |
361 |
+ |
d = urand(ilhash(dimlist,ndims)+1823+samplendx); |
362 |
+ |
multisamp(rv, 2, d); |
363 |
+ |
d = 2.0*PI * rv[0]; |
364 |
+ |
cosp = cos(d); |
365 |
+ |
sinp = sin(d); |
366 |
+ |
rv[1] = 1.0 - specjitter*rv[1]; |
367 |
+ |
if (rv[1] <= FTINY) |
368 |
+ |
d = 1.0; |
369 |
+ |
else |
370 |
+ |
d = sqrt( -log(rv[1]) * np->alpha2 ); |
371 |
+ |
for (i = 0; i < 3; i++) |
372 |
+ |
sr.rdir[i] = np->prdir[i] + d*(cosp*u[i] + sinp*v[i]); |
373 |
+ |
if (DOT(sr.rdir, r->ron) < -FTINY) |
374 |
+ |
normalize(sr.rdir); /* OK, normalize */ |
375 |
+ |
else |
376 |
+ |
VCOPY(sr.rdir, np->prdir); /* else no jitter */ |
377 |
+ |
rayvalue(&sr); |
378 |
+ |
scalecolor(sr.rcol, np->tspec); |
379 |
+ |
multcolor(sr.rcol, np->mcolor); /* modified by color */ |
380 |
+ |
addcolor(r->rcol, sr.rcol); |
381 |
+ |
ndims--; |
382 |
+ |
} |
383 |
|
} |