20 |
|
|
21 |
|
#include "random.h" |
22 |
|
|
23 |
+ |
extern double specthresh; /* specular sampling threshold */ |
24 |
+ |
extern double specjitter; /* specular sampling jitter */ |
25 |
+ |
|
26 |
|
/* |
27 |
|
* This routine uses portions of the reflection |
28 |
|
* model described by Cook and Torrance. |
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 */ |
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 */ |
108 |
|
/* worth using? */ |
109 |
|
if (dtmp > FTINY) { |
110 |
|
copycolor(ctmp, np->scolor); |
111 |
< |
dtmp *= omega / np->pdot; |
111 |
> |
dtmp *= omega * sqrt(ldot/np->pdot); |
112 |
|
scalecolor(ctmp, dtmp); |
113 |
|
addcolor(cval, ctmp); |
114 |
|
} |
128 |
|
* is always modified by material color. |
129 |
|
*/ |
130 |
|
/* roughness + source */ |
131 |
< |
dtmp = np->alpha2 + omega/(2.0*PI); |
131 |
> |
dtmp = np->alpha2/2.0 + omega/(2.0*PI); |
132 |
|
/* gaussian */ |
133 |
|
dtmp = exp((DOT(np->prdir,ldir)-1.)/dtmp)/(2.*PI)/dtmp; |
134 |
|
/* worth using? */ |
135 |
|
if (dtmp > FTINY) { |
136 |
|
copycolor(ctmp, np->mcolor); |
137 |
< |
dtmp *= np->tspec * omega / np->pdot; |
137 |
> |
dtmp *= np->tspec * omega * sqrt(ldot/np->pdot); |
138 |
|
scalecolor(ctmp, dtmp); |
139 |
|
addcolor(cval, ctmp); |
140 |
|
} |
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 (!(nd.specfl & SP_PURE) && |
196 |
+ |
specthresh > FTINY && |
197 |
+ |
(specthresh >= 1.-FTINY || |
198 |
+ |
specthresh > 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; |
221 |
|
nd.tdiff = nd.trans - nd.tspec; |
222 |
|
if (nd.tspec > FTINY) { |
223 |
|
nd.specfl |= SP_TRAN; |
224 |
+ |
/* check threshold */ |
225 |
+ |
if (!(nd.specfl & SP_PURE) && specthresh > FTINY && |
226 |
+ |
(specthresh >= 1.-FTINY || |
227 |
+ |
specthresh > nd.tspec)) |
228 |
+ |
nd.specfl |= SP_TBLT; |
229 |
|
if (r->crtype & SHADOW || |
230 |
|
DOT(r->pert,r->pert) <= FTINY*FTINY) { |
231 |
|
VCOPY(nd.prdir, r->rdir); |
233 |
|
} else { |
234 |
|
for (i = 0; i < 3; i++) /* perturb */ |
235 |
|
nd.prdir[i] = r->rdir[i] - |
236 |
< |
.75*r->pert[i]; |
237 |
< |
normalize(nd.prdir); |
236 |
> |
0.5*r->pert[i]; |
237 |
> |
if (DOT(nd.prdir, r->ron) < -FTINY) |
238 |
> |
normalize(nd.prdir); /* OK */ |
239 |
> |
else |
240 |
> |
VCOPY(nd.prdir, r->rdir); |
241 |
|
} |
242 |
|
} |
243 |
|
} else |
254 |
|
transtest *= bright(lr.rcol); |
255 |
|
transdist = r->rot + lr.rt; |
256 |
|
} |
257 |
< |
} |
257 |
> |
} else |
258 |
> |
transtest = 0; |
259 |
|
|
260 |
|
if (r->crtype & SHADOW) /* the rest is shadow */ |
261 |
|
return; |
265 |
|
if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY) |
266 |
|
return; /* 100% pure specular */ |
267 |
|
|
268 |
< |
if (r->ro->otype == OBJ_FACE || r->ro->otype == OBJ_RING) |
268 |
> |
if (r->ro != NULL && (r->ro->otype == OBJ_FACE || |
269 |
> |
r->ro->otype == OBJ_RING)) |
270 |
|
nd.specfl |= SP_FLAT; |
271 |
|
|
272 |
|
if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_PURE)) |
274 |
|
|
275 |
|
if (nd.rdiff > FTINY) { /* ambient from this side */ |
276 |
|
ambient(ctmp, r); |
277 |
< |
scalecolor(ctmp, nd.rdiff); |
277 |
> |
if (nd.specfl & SP_RBLT) |
278 |
> |
scalecolor(ctmp, 1.0-nd.trans); |
279 |
> |
else |
280 |
> |
scalecolor(ctmp, nd.rdiff); |
281 |
|
multcolor(ctmp, nd.mcolor); /* modified by material color */ |
282 |
|
addcolor(r->rcol, ctmp); /* add to returned color */ |
283 |
|
} |
284 |
|
if (nd.tdiff > FTINY) { /* ambient from other side */ |
285 |
|
flipsurface(r); |
286 |
|
ambient(ctmp, r); |
287 |
< |
scalecolor(ctmp, nd.tdiff); |
287 |
> |
if (nd.specfl & SP_TBLT) |
288 |
> |
scalecolor(ctmp, nd.trans); |
289 |
> |
else |
290 |
> |
scalecolor(ctmp, nd.tdiff); |
291 |
|
multcolor(ctmp, nd.mcolor); /* modified by color */ |
292 |
|
addcolor(r->rcol, ctmp); |
293 |
|
flipsurface(r); |
309 |
|
FVECT u, v, h; |
310 |
|
double rv[2]; |
311 |
|
double d, sinp, cosp; |
282 |
– |
int ntries; |
312 |
|
register int i; |
313 |
+ |
/* quick test */ |
314 |
+ |
if ((np->specfl & (SP_REFL|SP_RBLT)) != SP_REFL && |
315 |
+ |
(np->specfl & (SP_TRAN|SP_TBLT)) != SP_TRAN) |
316 |
+ |
return; |
317 |
|
/* set up sample coordinates */ |
318 |
|
v[0] = v[1] = v[2] = 0.0; |
319 |
|
for (i = 0; i < 3; i++) |
324 |
|
normalize(u); |
325 |
|
fcross(v, np->pnorm, u); |
326 |
|
/* compute reflection */ |
327 |
< |
if (np->specfl & SP_REFL && |
327 |
> |
if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL && |
328 |
|
rayorigin(&sr, r, SPECULAR, np->rspec) == 0) { |
329 |
|
dimlist[ndims++] = (int)np->mp; |
330 |
< |
for (ntries = 0; ntries < 10; ntries++) { |
331 |
< |
dimlist[ndims] = ntries * 8912; |
332 |
< |
d = urand(ilhash(dimlist,ndims+1)+samplendx); |
333 |
< |
multisamp(rv, 2, d); |
334 |
< |
d = 2.0*PI * rv[0]; |
335 |
< |
cosp = cos(d); |
336 |
< |
sinp = sin(d); |
337 |
< |
if (rv[1] <= FTINY) |
338 |
< |
d = 1.0; |
339 |
< |
else |
340 |
< |
d = sqrt( np->alpha2 * -log(rv[1]) ); |
341 |
< |
for (i = 0; i < 3; i++) |
342 |
< |
h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*v[i]); |
343 |
< |
d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d); |
344 |
< |
for (i = 0; i < 3; i++) |
345 |
< |
sr.rdir[i] = r->rdir[i] + d*h[i]; |
346 |
< |
if (DOT(sr.rdir, r->ron) > FTINY) { |
347 |
< |
rayvalue(&sr); |
348 |
< |
multcolor(sr.rcol, np->scolor); |
349 |
< |
addcolor(r->rcol, sr.rcol); |
317 |
< |
break; |
318 |
< |
} |
319 |
< |
} |
330 |
> |
d = urand(ilhash(dimlist,ndims)+samplendx); |
331 |
> |
multisamp(rv, 2, d); |
332 |
> |
d = 2.0*PI * rv[0]; |
333 |
> |
cosp = cos(d); |
334 |
> |
sinp = sin(d); |
335 |
> |
rv[1] = 1.0 - specjitter*rv[1]; |
336 |
> |
if (rv[1] <= FTINY) |
337 |
> |
d = 1.0; |
338 |
> |
else |
339 |
> |
d = sqrt( np->alpha2 * -log(rv[1]) ); |
340 |
> |
for (i = 0; i < 3; i++) |
341 |
> |
h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*v[i]); |
342 |
> |
d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d); |
343 |
> |
for (i = 0; i < 3; i++) |
344 |
> |
sr.rdir[i] = r->rdir[i] + d*h[i]; |
345 |
> |
if (DOT(sr.rdir, r->ron) <= FTINY) |
346 |
> |
VCOPY(sr.rdir, np->vrefl); /* jitter no good */ |
347 |
> |
rayvalue(&sr); |
348 |
> |
multcolor(sr.rcol, np->scolor); |
349 |
> |
addcolor(r->rcol, sr.rcol); |
350 |
|
ndims--; |
351 |
|
} |
352 |
|
/* compute transmission */ |
353 |
+ |
if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN && |
354 |
+ |
rayorigin(&sr, r, SPECULAR, np->tspec) == 0) { |
355 |
+ |
dimlist[ndims++] = (int)np->mp; |
356 |
+ |
d = urand(ilhash(dimlist,ndims)+1823+samplendx); |
357 |
+ |
multisamp(rv, 2, d); |
358 |
+ |
d = 2.0*PI * rv[0]; |
359 |
+ |
cosp = cos(d); |
360 |
+ |
sinp = sin(d); |
361 |
+ |
rv[1] = 1.0 - specjitter*rv[1]; |
362 |
+ |
if (rv[1] <= FTINY) |
363 |
+ |
d = 1.0; |
364 |
+ |
else |
365 |
+ |
d = sqrt( np->alpha2/4.0 * -log(rv[1]) ); |
366 |
+ |
for (i = 0; i < 3; i++) |
367 |
+ |
sr.rdir[i] = np->prdir[i] + d*(cosp*u[i] + sinp*v[i]); |
368 |
+ |
if (DOT(sr.rdir, r->ron) < -FTINY) |
369 |
+ |
normalize(sr.rdir); /* OK, normalize */ |
370 |
+ |
else |
371 |
+ |
VCOPY(sr.rdir, np->prdir); /* else no jitter */ |
372 |
+ |
rayvalue(&sr); |
373 |
+ |
scalecolor(sr.rcol, np->tspec); |
374 |
+ |
multcolor(sr.rcol, np->mcolor); /* modified by color */ |
375 |
+ |
addcolor(r->rcol, sr.rcol); |
376 |
+ |
ndims--; |
377 |
+ |
} |
378 |
|
} |