16 |
|
|
17 |
|
#include "random.h" |
18 |
|
|
19 |
+ |
extern double specthresh; /* specular sampling threshold */ |
20 |
+ |
extern double specjitter; /* specular sampling jitter */ |
21 |
+ |
|
22 |
|
/* |
23 |
|
* This anisotropic reflection model uses a variant on the |
24 |
|
* exponential Gaussian used in normal.c. |
40 |
|
#define SP_REFL 01 /* has reflected specular component */ |
41 |
|
#define SP_TRAN 02 /* has transmitted specular */ |
42 |
|
#define SP_PURE 010 /* purely specular (zero roughness) */ |
43 |
< |
#define SP_BADU 020 /* bad u direction calculation */ |
44 |
< |
#define SP_FLAT 040 /* reflecting surface is flat */ |
43 |
> |
#define SP_FLAT 020 /* reflecting surface is flat */ |
44 |
> |
#define SP_RBLT 040 /* reflection below sample threshold */ |
45 |
> |
#define SP_TBLT 0100 /* transmission below threshold */ |
46 |
> |
#define SP_BADU 0200 /* bad u direction calculation */ |
47 |
|
|
48 |
|
typedef struct { |
49 |
|
OBJREC *mp; /* material pointer */ |
51 |
|
short specfl; /* specularity flags, defined above */ |
52 |
|
COLOR mcolor; /* color of this material */ |
53 |
|
COLOR scolor; /* color of specular component */ |
54 |
+ |
FVECT vrefl; /* vector in reflected direction */ |
55 |
|
FVECT prdir; /* vector in transmitted direction */ |
56 |
|
FVECT u, v; /* u and v vectors orienting anisotropy */ |
57 |
|
double u_alpha; /* u roughness */ |
206 |
|
for (i = 0; i < 3; i++) |
207 |
|
colval(nd.scolor,i) += (1.0-colval(nd.scolor,i))*dtmp; |
208 |
|
nd.rspec += (1.0-nd.rspec)*dtmp; |
209 |
+ |
/* check threshold */ |
210 |
+ |
if (specthresh > FTINY && |
211 |
+ |
((specthresh >= 1.-FTINY || |
212 |
+ |
specthresh + (.05 - .1*frandom()) > nd.rspec))) |
213 |
+ |
nd.specfl |= SP_RBLT; |
214 |
+ |
/* compute refl. direction */ |
215 |
+ |
for (i = 0; i < 3; i++) |
216 |
+ |
nd.vrefl[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i]; |
217 |
+ |
if (DOT(nd.vrefl, r->ron) <= FTINY) /* penetration? */ |
218 |
+ |
for (i = 0; i < 3; i++) /* safety measure */ |
219 |
+ |
nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i]; |
220 |
|
|
221 |
|
if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) { |
222 |
|
RAY lr; |
223 |
|
if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) { |
224 |
< |
for (i = 0; i < 3; i++) |
208 |
< |
lr.rdir[i] = r->rdir[i] + |
209 |
< |
2.0*nd.pdot*nd.pnorm[i]; |
224 |
> |
VCOPY(lr.rdir, nd.vrefl); |
225 |
|
rayvalue(&lr); |
226 |
|
multcolor(lr.rcol, nd.scolor); |
227 |
|
addcolor(r->rcol, lr.rcol); |
235 |
|
nd.tdiff = nd.trans - nd.tspec; |
236 |
|
if (nd.tspec > FTINY) { |
237 |
|
nd.specfl |= SP_TRAN; |
238 |
+ |
/* check threshold */ |
239 |
+ |
if (specthresh > FTINY && |
240 |
+ |
((specthresh >= 1.-FTINY || |
241 |
+ |
specthresh + |
242 |
+ |
(.05 - .1*frandom()) > nd.tspec))) |
243 |
+ |
nd.specfl |= SP_TBLT; |
244 |
|
if (r->crtype & SHADOW || |
245 |
|
DOT(r->pert,r->pert) <= FTINY*FTINY) { |
246 |
|
VCOPY(nd.prdir, r->rdir); |
248 |
|
} else { |
249 |
|
for (i = 0; i < 3; i++) /* perturb */ |
250 |
|
nd.prdir[i] = r->rdir[i] - |
251 |
< |
.75*r->pert[i]; |
252 |
< |
normalize(nd.prdir); |
251 |
> |
0.5*r->pert[i]; |
252 |
> |
if (DOT(nd.prdir, r->ron) < -FTINY) |
253 |
> |
normalize(nd.prdir); /* OK */ |
254 |
> |
else |
255 |
> |
VCOPY(nd.prdir, r->rdir); |
256 |
|
} |
257 |
|
} |
258 |
|
} else |
279 |
|
if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY) |
280 |
|
return; /* 100% pure specular */ |
281 |
|
|
282 |
+ |
if (r->ro->otype == OBJ_FACE || r->ro->otype == OBJ_RING) |
283 |
+ |
nd.specfl |= SP_FLAT; |
284 |
+ |
|
285 |
|
getacoords(r, &nd); /* set up coordinates */ |
286 |
|
|
287 |
|
if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & (SP_PURE|SP_BADU))) |
289 |
|
|
290 |
|
if (nd.rdiff > FTINY) { /* ambient from this side */ |
291 |
|
ambient(ctmp, r); |
292 |
< |
scalecolor(ctmp, nd.rdiff); |
292 |
> |
if (nd.specfl & SP_RBLT) |
293 |
> |
scalecolor(ctmp, 1.0-nd.trans); |
294 |
> |
else |
295 |
> |
scalecolor(ctmp, nd.rdiff); |
296 |
|
multcolor(ctmp, nd.mcolor); /* modified by material color */ |
297 |
|
addcolor(r->rcol, ctmp); /* add to returned color */ |
298 |
|
} |
299 |
|
if (nd.tdiff > FTINY) { /* ambient from other side */ |
300 |
|
flipsurface(r); |
301 |
|
ambient(ctmp, r); |
302 |
< |
scalecolor(ctmp, nd.tdiff); |
302 |
> |
if (nd.specfl & SP_TBLT) |
303 |
> |
scalecolor(ctmp, nd.trans); |
304 |
> |
else |
305 |
> |
scalecolor(ctmp, nd.tdiff); |
306 |
|
multcolor(ctmp, nd.mcolor); /* modified by color */ |
307 |
|
addcolor(r->rcol, ctmp); |
308 |
|
flipsurface(r); |
353 |
|
FVECT h; |
354 |
|
double rv[2]; |
355 |
|
double d, sinp, cosp; |
323 |
– |
int confuse; |
356 |
|
register int i; |
357 |
|
/* compute reflection */ |
358 |
< |
if (np->specfl & SP_REFL && |
358 |
> |
if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL && |
359 |
|
rayorigin(&sr, r, SPECULAR, np->rspec) == 0) { |
328 |
– |
confuse = 0; |
360 |
|
dimlist[ndims++] = (int)np->mp; |
361 |
< |
refagain: |
331 |
< |
dimlist[ndims] = confuse += 3601; |
332 |
< |
d = urand(ilhash(dimlist,ndims+1)+samplendx); |
361 |
> |
d = urand(ilhash(dimlist,ndims)+samplendx); |
362 |
|
multisamp(rv, 2, d); |
363 |
|
d = 2.0*PI * rv[0]; |
364 |
|
cosp = np->u_alpha * cos(d); |
366 |
|
d = sqrt(cosp*cosp + sinp*sinp); |
367 |
|
cosp /= d; |
368 |
|
sinp /= d; |
369 |
+ |
rv[1] = 1.0 - specjitter*rv[1]; |
370 |
|
if (rv[1] <= FTINY) |
371 |
|
d = 1.0; |
372 |
|
else |
373 |
< |
d = sqrt( -log(rv[1]) / |
373 |
> |
d = sqrt(-log(rv[1]) / |
374 |
|
(cosp*cosp/(np->u_alpha*np->u_alpha) + |
375 |
< |
sinp*sinp/(np->v_alpha*np->v_alpha)) ); |
375 |
> |
sinp*sinp/(np->v_alpha*np->v_alpha))); |
376 |
|
for (i = 0; i < 3; i++) |
377 |
|
h[i] = np->pnorm[i] + |
378 |
< |
d*(cosp*np->u[i] + sinp*np->v[i]); |
378 |
> |
d*(cosp*np->u[i] + sinp*np->v[i]); |
379 |
|
d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d); |
380 |
|
for (i = 0; i < 3; i++) |
381 |
|
sr.rdir[i] = r->rdir[i] + d*h[i]; |
382 |
< |
if (DOT(sr.rdir, r->ron) <= FTINY) /* oops! */ |
383 |
< |
goto refagain; |
382 |
> |
if (DOT(sr.rdir, r->ron) <= FTINY) /* penetration? */ |
383 |
> |
VCOPY(sr.rdir, np->vrefl); /* jitter no good */ |
384 |
|
rayvalue(&sr); |
385 |
|
multcolor(sr.rcol, np->scolor); |
386 |
|
addcolor(r->rcol, sr.rcol); |
387 |
|
ndims--; |
388 |
|
} |
389 |
|
/* compute transmission */ |
390 |
+ |
if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN && |
391 |
+ |
rayorigin(&sr, r, SPECULAR, np->tspec) == 0) { |
392 |
+ |
dimlist[ndims++] = (int)np->mp; |
393 |
+ |
d = urand(ilhash(dimlist,ndims)+1823+samplendx); |
394 |
+ |
multisamp(rv, 2, d); |
395 |
+ |
d = 2.0*PI * rv[0]; |
396 |
+ |
cosp = cos(d); |
397 |
+ |
sinp = sin(d); |
398 |
+ |
rv[1] = 1.0 - specjitter*rv[1]; |
399 |
+ |
if (rv[1] <= FTINY) |
400 |
+ |
d = 1.0; |
401 |
+ |
else |
402 |
+ |
d = sqrt(-log(rv[1]) / |
403 |
+ |
(cosp*cosp*4./(np->u_alpha*np->u_alpha) + |
404 |
+ |
sinp*sinp*4./(np->v_alpha*np->v_alpha))); |
405 |
+ |
for (i = 0; i < 3; i++) |
406 |
+ |
sr.rdir[i] = np->prdir[i] + |
407 |
+ |
d*(cosp*np->u[i] + sinp*np->v[i]); |
408 |
+ |
if (DOT(sr.rdir, r->ron) < -FTINY) |
409 |
+ |
normalize(sr.rdir); /* OK, normalize */ |
410 |
+ |
else |
411 |
+ |
VCOPY(sr.rdir, np->prdir); /* else no jitter */ |
412 |
+ |
rayvalue(&sr); |
413 |
+ |
multcolor(sr.rcol, np->scolor); |
414 |
+ |
addcolor(r->rcol, sr.rcol); |
415 |
+ |
ndims--; |
416 |
+ |
} |
417 |
|
} |