23 |
|
extern double specthresh; /* specular sampling threshold */ |
24 |
|
extern double specjitter; /* specular sampling jitter */ |
25 |
|
|
26 |
+ |
static gaussamp(); |
27 |
+ |
|
28 |
|
/* |
29 |
< |
* This routine uses portions of the reflection |
30 |
< |
* model described by Cook and Torrance. |
29 |
< |
* The computation of specular components has been simplified by |
30 |
< |
* numerous approximations and ommisions to improve speed. |
29 |
> |
* This routine implements the isotropic Gaussian |
30 |
> |
* model described by Ward in Siggraph `92 article. |
31 |
|
* We orient the surface towards the incoming ray, so a single |
32 |
|
* surface can be used to represent an infinitely thin object. |
33 |
|
* |
38 |
|
* red grn blu rspec rough trans tspec |
39 |
|
*/ |
40 |
|
|
41 |
– |
#define BSPEC(m) (6.0) /* specularity parameter b */ |
42 |
– |
|
41 |
|
/* specularity flags */ |
42 |
|
#define SP_REFL 01 /* has reflected specular component */ |
43 |
|
#define SP_TRAN 02 /* has transmitted specular */ |
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 |
> |
/* half vector */ |
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 *= d2; |
111 |
> |
d2 = (DOT(vtmp,vtmp) - d2) / d2; |
112 |
|
/* gaussian */ |
113 |
< |
dtmp = exp((DOT(np->vrefl,ldir)-1.)/dtmp)/(2.*PI)/dtmp; |
113 |
> |
dtmp = exp(-d2/dtmp)/(4.*PI*dtmp); |
114 |
|
/* worth using? */ |
115 |
|
if (dtmp > FTINY) { |
116 |
|
copycolor(ctmp, np->scolor); |
134 |
|
* is always modified by material color. |
135 |
|
*/ |
136 |
|
/* roughness + source */ |
137 |
< |
dtmp = np->alpha2/2.0 + omega/(2.0*PI); |
137 |
> |
dtmp = np->alpha2 + omega/PI; |
138 |
|
/* gaussian */ |
139 |
< |
dtmp = exp((DOT(np->prdir,ldir)-1.)/dtmp)/(2.*PI)/dtmp; |
139 |
> |
dtmp = exp((2.*DOT(np->prdir,ldir)-2.)/dtmp)/(PI*dtmp); |
140 |
|
/* worth using? */ |
141 |
|
if (dtmp > FTINY) { |
142 |
|
copycolor(ctmp, np->mcolor); |
143 |
< |
dtmp *= np->tspec * omega * sqrt(ldot/np->pdot); |
143 |
> |
dtmp *= np->tspec * omega * sqrt(-ldot/np->pdot); |
144 |
|
scalecolor(ctmp, dtmp); |
145 |
|
addcolor(cval, ctmp); |
146 |
|
} |
154 |
|
{ |
155 |
|
NORMDAT nd; |
156 |
|
double transtest, transdist; |
151 |
– |
double dtmp; |
157 |
|
COLOR ctmp; |
158 |
|
register int i; |
159 |
|
/* easy shadow test */ |
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], |
183 |
|
nd.pdot = .001; /* non-zero for dirnorm() */ |
184 |
|
multcolor(nd.mcolor, r->pcol); /* modify material color */ |
185 |
|
transtest = 0; |
186 |
+ |
transdist = r->rot; |
187 |
|
/* get specular component */ |
188 |
|
if ((nd.rspec = m->oargs.farg[3]) > FTINY) { |
189 |
|
nd.specfl |= SP_REFL; |
193 |
|
else |
194 |
|
setcolor(nd.scolor, 1.0, 1.0, 1.0); |
195 |
|
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; |
196 |
|
/* check threshold */ |
197 |
< |
if (!(nd.specfl & SP_PURE) && |
196 |
< |
specthresh > FTINY && |
197 |
< |
(specthresh >= 1.-FTINY || |
198 |
< |
specthresh > nd.rspec)) |
197 |
> |
if (!(nd.specfl & SP_PURE) && specthresh >= nd.rspec-FTINY) |
198 |
|
nd.specfl |= SP_RBLT; |
199 |
|
/* compute reflected ray */ |
200 |
|
for (i = 0; i < 3; i++) |
221 |
|
if (nd.tspec > FTINY) { |
222 |
|
nd.specfl |= SP_TRAN; |
223 |
|
/* check threshold */ |
224 |
< |
if (!(nd.specfl & SP_PURE) && specthresh > FTINY && |
225 |
< |
(specthresh >= 1.-FTINY || |
227 |
< |
specthresh > nd.tspec)) |
224 |
> |
if (!(nd.specfl & SP_PURE) && |
225 |
> |
specthresh >= nd.tspec-FTINY) |
226 |
|
nd.specfl |= SP_TBLT; |
227 |
|
if (r->crtype & SHADOW || |
228 |
|
DOT(r->pert,r->pert) <= FTINY*FTINY) { |
230 |
|
transtest = 2; |
231 |
|
} else { |
232 |
|
for (i = 0; i < 3; i++) /* perturb */ |
233 |
< |
nd.prdir[i] = r->rdir[i] - |
236 |
< |
0.5*r->pert[i]; |
233 |
> |
nd.prdir[i] = r->rdir[i] - r->pert[i]; |
234 |
|
if (DOT(nd.prdir, r->ron) < -FTINY) |
235 |
|
normalize(nd.prdir); /* OK */ |
236 |
|
else |
359 |
|
if (rv[1] <= FTINY) |
360 |
|
d = 1.0; |
361 |
|
else |
362 |
< |
d = sqrt( np->alpha2/4.0 * -log(rv[1]) ); |
362 |
> |
d = sqrt( -log(rv[1]) * np->alpha2 ); |
363 |
|
for (i = 0; i < 3; i++) |
364 |
|
sr.rdir[i] = np->prdir[i] + d*(cosp*u[i] + sinp*v[i]); |
365 |
|
if (DOT(sr.rdir, r->ron) < -FTINY) |