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 */ |
205 |
|
for (i = 0; i < 3; i++) |
206 |
|
colval(nd.scolor,i) += (1.0-colval(nd.scolor,i))*dtmp; |
207 |
|
nd.rspec += (1.0-nd.rspec)*dtmp; |
208 |
+ |
/* check threshold */ |
209 |
+ |
if (nd.rspec <= specthresh+FTINY) |
210 |
+ |
nd.specfl |= SP_RBLT; |
211 |
|
|
212 |
|
if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) { |
213 |
|
RAY lr; |
228 |
|
nd.tdiff = nd.trans - nd.tspec; |
229 |
|
if (nd.tspec > FTINY) { |
230 |
|
nd.specfl |= SP_TRAN; |
231 |
+ |
/* check threshold */ |
232 |
+ |
if (nd.tspec <= specthresh+FTINY) |
233 |
+ |
nd.specfl |= SP_TBLT; |
234 |
|
if (r->crtype & SHADOW || |
235 |
|
DOT(r->pert,r->pert) <= FTINY*FTINY) { |
236 |
|
VCOPY(nd.prdir, r->rdir); |
266 |
|
if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY) |
267 |
|
return; /* 100% pure specular */ |
268 |
|
|
269 |
+ |
if (r->ro->otype == OBJ_FACE || r->ro->otype == OBJ_RING) |
270 |
+ |
nd.specfl |= SP_FLAT; |
271 |
+ |
|
272 |
|
getacoords(r, &nd); /* set up coordinates */ |
273 |
|
|
274 |
|
if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & (SP_PURE|SP_BADU))) |
276 |
|
|
277 |
|
if (nd.rdiff > FTINY) { /* ambient from this side */ |
278 |
|
ambient(ctmp, r); |
279 |
< |
scalecolor(ctmp, nd.rdiff); |
279 |
> |
if (nd.specfl & SP_RBLT) |
280 |
> |
scalecolor(ctmp, 1.0-nd.trans); |
281 |
> |
else |
282 |
> |
scalecolor(ctmp, nd.rdiff); |
283 |
|
multcolor(ctmp, nd.mcolor); /* modified by material color */ |
284 |
|
addcolor(r->rcol, ctmp); /* add to returned color */ |
285 |
|
} |
286 |
|
if (nd.tdiff > FTINY) { /* ambient from other side */ |
287 |
|
flipsurface(r); |
288 |
|
ambient(ctmp, r); |
289 |
< |
scalecolor(ctmp, nd.tdiff); |
289 |
> |
if (nd.specfl & SP_TBLT) |
290 |
> |
scalecolor(ctmp, nd.trans); |
291 |
> |
else |
292 |
> |
scalecolor(ctmp, nd.tdiff); |
293 |
|
multcolor(ctmp, nd.mcolor); /* modified by color */ |
294 |
|
addcolor(r->rcol, ctmp); |
295 |
|
flipsurface(r); |
340 |
|
FVECT h; |
341 |
|
double rv[2]; |
342 |
|
double d, sinp, cosp; |
343 |
< |
int confuse; |
343 |
> |
int ntries; |
344 |
|
register int i; |
345 |
|
/* compute reflection */ |
346 |
< |
if (np->specfl & SP_REFL && |
346 |
> |
if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL && |
347 |
|
rayorigin(&sr, r, SPECULAR, np->rspec) == 0) { |
328 |
– |
confuse = 0; |
348 |
|
dimlist[ndims++] = (int)np->mp; |
349 |
< |
refagain: |
350 |
< |
dimlist[ndims] = confuse += 3601; |
351 |
< |
d = urand(ilhash(dimlist,ndims+1)+samplendx); |
352 |
< |
multisamp(rv, 2, d); |
353 |
< |
d = 2.0*PI * rv[0]; |
354 |
< |
cosp = np->u_alpha * cos(d); |
355 |
< |
sinp = np->v_alpha * sin(d); |
356 |
< |
d = sqrt(cosp*cosp + sinp*sinp); |
357 |
< |
cosp /= d; |
358 |
< |
sinp /= d; |
359 |
< |
if (rv[1] <= FTINY) |
360 |
< |
d = 1.0; |
361 |
< |
else |
362 |
< |
d = sqrt( -log(rv[1]) / |
363 |
< |
(cosp*cosp/(np->u_alpha*np->u_alpha) + |
364 |
< |
sinp*sinp/(np->v_alpha*np->v_alpha)) ); |
365 |
< |
for (i = 0; i < 3; i++) |
366 |
< |
h[i] = np->pnorm[i] + |
349 |
> |
for (ntries = 0; ntries < 10; ntries++) { |
350 |
> |
dimlist[ndims] = ntries * 3601; |
351 |
> |
d = urand(ilhash(dimlist,ndims+1)+samplendx); |
352 |
> |
multisamp(rv, 2, d); |
353 |
> |
d = 2.0*PI * rv[0]; |
354 |
> |
cosp = np->u_alpha * cos(d); |
355 |
> |
sinp = np->v_alpha * sin(d); |
356 |
> |
d = sqrt(cosp*cosp + sinp*sinp); |
357 |
> |
cosp /= d; |
358 |
> |
sinp /= d; |
359 |
> |
rv[1] = 1.0 - specjitter*rv[1]; |
360 |
> |
if (rv[1] <= FTINY) |
361 |
> |
d = 1.0; |
362 |
> |
else |
363 |
> |
d = sqrt(-log(rv[1]) / |
364 |
> |
(cosp*cosp/(np->u_alpha*np->u_alpha) + |
365 |
> |
sinp*sinp/(np->v_alpha*np->v_alpha))); |
366 |
> |
for (i = 0; i < 3; i++) |
367 |
> |
h[i] = np->pnorm[i] + |
368 |
|
d*(cosp*np->u[i] + sinp*np->v[i]); |
369 |
< |
d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d); |
370 |
< |
for (i = 0; i < 3; i++) |
371 |
< |
sr.rdir[i] = r->rdir[i] + d*h[i]; |
372 |
< |
if (DOT(sr.rdir, r->ron) <= FTINY) /* oops! */ |
373 |
< |
goto refagain; |
374 |
< |
rayvalue(&sr); |
375 |
< |
multcolor(sr.rcol, np->scolor); |
376 |
< |
addcolor(r->rcol, sr.rcol); |
369 |
> |
d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d); |
370 |
> |
for (i = 0; i < 3; i++) |
371 |
> |
sr.rdir[i] = r->rdir[i] + d*h[i]; |
372 |
> |
if (DOT(sr.rdir, r->ron) > FTINY) { |
373 |
> |
rayvalue(&sr); |
374 |
> |
multcolor(sr.rcol, np->scolor); |
375 |
> |
addcolor(r->rcol, sr.rcol); |
376 |
> |
break; |
377 |
> |
} |
378 |
> |
} |
379 |
|
ndims--; |
380 |
|
} |
381 |
|
/* compute transmission */ |