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 |
> |
register int i; |
78 |
|
COLOR ctmp; |
79 |
|
|
80 |
|
setcolor(cval, 0.0, 0.0, 0.0); |
101 |
|
* gaussian distribution model. |
102 |
|
*/ |
103 |
|
/* roughness */ |
104 |
< |
dtmp = 2.0*np->alpha2; |
104 |
> |
dtmp = np->alpha2; |
105 |
|
/* + source if flat */ |
106 |
|
if (np->specfl & SP_FLAT) |
107 |
< |
dtmp += omega/(2.0*PI); |
107 |
> |
dtmp += omega/(4.0*PI); |
108 |
> |
/* delta */ |
109 |
> |
for (i = 0; i < 3; i++) |
110 |
> |
vtmp[i] = ldir[i] - np->rp->rdir[i]; |
111 |
> |
d2 = DOT(vtmp, np->pnorm); |
112 |
> |
d2 = 2.0 - 2.0*d2/sqrt(DOT(vtmp,vtmp)); |
113 |
|
/* gaussian */ |
114 |
< |
dtmp = exp((DOT(np->vrefl,ldir)-1.)/dtmp)/(2.*PI)/dtmp; |
114 |
> |
dtmp = exp(-d2/dtmp)/(4.*PI*dtmp); |
115 |
|
/* worth using? */ |
116 |
|
if (dtmp > FTINY) { |
117 |
|
copycolor(ctmp, np->scolor); |
118 |
< |
dtmp *= omega / np->pdot; |
118 |
> |
dtmp *= omega * sqrt(ldot/np->pdot); |
119 |
|
scalecolor(ctmp, dtmp); |
120 |
|
addcolor(cval, ctmp); |
121 |
|
} |
135 |
|
* is always modified by material color. |
136 |
|
*/ |
137 |
|
/* roughness + source */ |
138 |
< |
dtmp = np->alpha2 + omega/(2.0*PI); |
138 |
> |
dtmp = np->alpha2/2.0 + omega/(2.0*PI); |
139 |
|
/* gaussian */ |
140 |
|
dtmp = exp((DOT(np->prdir,ldir)-1.)/dtmp)/(2.*PI)/dtmp; |
141 |
|
/* worth using? */ |
142 |
|
if (dtmp > FTINY) { |
143 |
|
copycolor(ctmp, np->mcolor); |
144 |
< |
dtmp *= np->tspec * omega / np->pdot; |
144 |
> |
dtmp *= np->tspec * omega * sqrt(ldot/np->pdot); |
145 |
|
scalecolor(ctmp, dtmp); |
146 |
|
addcolor(cval, ctmp); |
147 |
|
} |
165 |
|
if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5)) |
166 |
|
objerror(m, USER, "bad number of arguments"); |
167 |
|
nd.mp = m; |
168 |
+ |
nd.rp = r; |
169 |
|
/* get material color */ |
170 |
|
setcolor(nd.mcolor, m->oargs.farg[0], |
171 |
|
m->oargs.farg[1], |
200 |
|
colval(nd.scolor,i) += (1.0-colval(nd.scolor,i))*dtmp; |
201 |
|
nd.rspec += (1.0-nd.rspec)*dtmp; |
202 |
|
/* check threshold */ |
203 |
< |
if (specthresh > FTINY && |
204 |
< |
((specthresh >= 1.-FTINY || |
205 |
< |
specthresh + (.1 - .2*urand(8199+samplendx)) |
206 |
< |
> nd.rspec))) |
203 |
> |
if (!(nd.specfl & SP_PURE) && |
204 |
> |
specthresh > FTINY && |
205 |
> |
(specthresh >= 1.-FTINY || |
206 |
> |
specthresh + .05 - .1*frandom() > nd.rspec)) |
207 |
|
nd.specfl |= SP_RBLT; |
208 |
|
/* compute reflected ray */ |
209 |
|
for (i = 0; i < 3; i++) |
230 |
|
if (nd.tspec > FTINY) { |
231 |
|
nd.specfl |= SP_TRAN; |
232 |
|
/* check threshold */ |
233 |
< |
if (specthresh > FTINY && |
234 |
< |
((specthresh >= 1.-FTINY || |
235 |
< |
specthresh + |
228 |
< |
(.1 - .2*urand(7241+samplendx)) |
229 |
< |
> nd.tspec))) |
233 |
> |
if (!(nd.specfl & SP_PURE) && specthresh > FTINY && |
234 |
> |
(specthresh >= 1.-FTINY || |
235 |
> |
specthresh + .05 - .1*frandom() > nd.tspec)) |
236 |
|
nd.specfl |= SP_TBLT; |
237 |
|
if (r->crtype & SHADOW || |
238 |
|
DOT(r->pert,r->pert) <= FTINY*FTINY) { |
241 |
|
} else { |
242 |
|
for (i = 0; i < 3; i++) /* perturb */ |
243 |
|
nd.prdir[i] = r->rdir[i] - |
244 |
< |
.75*r->pert[i]; |
244 |
> |
0.5*r->pert[i]; |
245 |
|
if (DOT(nd.prdir, r->ron) < -FTINY) |
246 |
|
normalize(nd.prdir); /* OK */ |
247 |
|
else |
262 |
|
transtest *= bright(lr.rcol); |
263 |
|
transdist = r->rot + lr.rt; |
264 |
|
} |
265 |
< |
} |
265 |
> |
} else |
266 |
> |
transtest = 0; |
267 |
|
|
268 |
|
if (r->crtype & SHADOW) /* the rest is shadow */ |
269 |
|
return; |
273 |
|
if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY) |
274 |
|
return; /* 100% pure specular */ |
275 |
|
|
276 |
< |
if (r->ro->otype == OBJ_FACE || r->ro->otype == OBJ_RING) |
276 |
> |
if (r->ro != NULL && (r->ro->otype == OBJ_FACE || |
277 |
> |
r->ro->otype == OBJ_RING)) |
278 |
|
nd.specfl |= SP_FLAT; |
279 |
|
|
280 |
|
if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_PURE)) |
318 |
|
double rv[2]; |
319 |
|
double d, sinp, cosp; |
320 |
|
register int i; |
321 |
+ |
/* quick test */ |
322 |
+ |
if ((np->specfl & (SP_REFL|SP_RBLT)) != SP_REFL && |
323 |
+ |
(np->specfl & (SP_TRAN|SP_TBLT)) != SP_TRAN) |
324 |
+ |
return; |
325 |
|
/* set up sample coordinates */ |
326 |
|
v[0] = v[1] = v[2] = 0.0; |
327 |
|
for (i = 0; i < 3; i++) |
358 |
|
ndims--; |
359 |
|
} |
360 |
|
/* compute transmission */ |
361 |
+ |
if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN && |
362 |
+ |
rayorigin(&sr, r, SPECULAR, np->tspec) == 0) { |
363 |
+ |
dimlist[ndims++] = (int)np->mp; |
364 |
+ |
d = urand(ilhash(dimlist,ndims)+1823+samplendx); |
365 |
+ |
multisamp(rv, 2, d); |
366 |
+ |
d = 2.0*PI * rv[0]; |
367 |
+ |
cosp = cos(d); |
368 |
+ |
sinp = sin(d); |
369 |
+ |
rv[1] = 1.0 - specjitter*rv[1]; |
370 |
+ |
if (rv[1] <= FTINY) |
371 |
+ |
d = 1.0; |
372 |
+ |
else |
373 |
+ |
d = sqrt( np->alpha2/4.0 * -log(rv[1]) ); |
374 |
+ |
for (i = 0; i < 3; i++) |
375 |
+ |
sr.rdir[i] = np->prdir[i] + d*(cosp*u[i] + sinp*v[i]); |
376 |
+ |
if (DOT(sr.rdir, r->ron) < -FTINY) |
377 |
+ |
normalize(sr.rdir); /* OK, normalize */ |
378 |
+ |
else |
379 |
+ |
VCOPY(sr.rdir, np->prdir); /* else no jitter */ |
380 |
+ |
rayvalue(&sr); |
381 |
+ |
scalecolor(sr.rcol, np->tspec); |
382 |
+ |
multcolor(sr.rcol, np->mcolor); /* modified by color */ |
383 |
+ |
addcolor(r->rcol, sr.rcol); |
384 |
+ |
ndims--; |
385 |
+ |
} |
386 |
|
} |