80 |
|
register NORMDAT *np = nnp; |
81 |
|
double ldot; |
82 |
|
double lrdiff, ltdiff; |
83 |
< |
double dtmp, d2; |
83 |
> |
double dtmp, d2, d3, d4; |
84 |
|
FVECT vtmp; |
85 |
|
COLOR ctmp; |
86 |
|
|
115 |
|
if (ldot > FTINY && (np->specfl&(SP_REFL|SP_PURE)) == SP_REFL) { |
116 |
|
/* |
117 |
|
* Compute specular reflection coefficient using |
118 |
< |
* gaussian distribution model. |
118 |
> |
* Gaussian distribution model. |
119 |
|
*/ |
120 |
|
/* roughness */ |
121 |
|
dtmp = np->alpha2; |
128 |
|
vtmp[2] = ldir[2] - np->rp->rdir[2]; |
129 |
|
d2 = DOT(vtmp, np->pnorm); |
130 |
|
d2 *= d2; |
131 |
< |
d2 = (DOT(vtmp,vtmp) - d2) / d2; |
132 |
< |
/* gaussian */ |
133 |
< |
dtmp = exp(-d2/dtmp)/(4.*PI * np->pdot * dtmp); |
131 |
> |
d3 = DOT(vtmp,vtmp); |
132 |
> |
d4 = (d3 - d2) / d2; |
133 |
> |
/* new W-G-M-D model */ |
134 |
> |
dtmp = exp(-d4/dtmp) * d3 / (PI * d2*d2 * dtmp); |
135 |
|
/* worth using? */ |
136 |
|
if (dtmp > FTINY) { |
137 |
|
copycolor(ctmp, np->scolor); |
138 |
< |
dtmp *= omega; |
138 |
> |
dtmp *= ldot * omega; |
139 |
|
scalecolor(ctmp, dtmp); |
140 |
|
addcolor(cval, ctmp); |
141 |
|
} |
156 |
|
*/ |
157 |
|
/* roughness + source */ |
158 |
|
dtmp = np->alpha2 + omega*(1.0/PI); |
159 |
< |
/* gaussian */ |
159 |
> |
/* Gaussian */ |
160 |
|
dtmp = exp((2.*DOT(np->prdir,ldir)-2.)/dtmp)/(PI*dtmp); |
161 |
|
/* worth using? */ |
162 |
|
if (dtmp > FTINY) { |
294 |
|
if (!(nd.specfl & SP_PURE) && specthresh >= nd.rspec-FTINY) |
295 |
|
nd.specfl |= SP_RBLT; |
296 |
|
/* compute reflected ray */ |
297 |
< |
for (i = 0; i < 3; i++) |
297 |
< |
nd.vrefl[i] = r->rdir[i] + 2.*nd.pdot*nd.pnorm[i]; |
297 |
> |
VSUM(nd.vrefl, r->rdir, nd.pnorm, 2.*nd.pdot); |
298 |
|
/* penetration? */ |
299 |
|
if (hastexture && DOT(nd.vrefl, r->ron) <= FTINY) |
300 |
< |
for (i = 0; i < 3; i++) /* safety measure */ |
301 |
< |
nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i]; |
300 |
> |
VSUM(nd.vrefl, r->rdir, r->ron, 2.*r->rod); |
301 |
|
checknorm(nd.vrefl); |
302 |
|
} |
303 |
|
/* reflected ray */ |
325 |
|
|
326 |
|
if (nd.rdiff > FTINY) { /* ambient from this side */ |
327 |
|
copycolor(ctmp, nd.mcolor); /* modified by material color */ |
328 |
< |
if (nd.specfl & SP_RBLT) |
329 |
< |
scalecolor(ctmp, 1.0-nd.trans); |
330 |
< |
else |
332 |
< |
scalecolor(ctmp, nd.rdiff); |
328 |
> |
scalecolor(ctmp, nd.rdiff); |
329 |
> |
if (nd.specfl & SP_RBLT) /* add in specular as well? */ |
330 |
> |
addcolor(ctmp, nd.scolor); |
331 |
|
multambient(ctmp, r, hastexture ? nd.pnorm : r->ron); |
332 |
|
addcolor(r->rcol, ctmp); /* add to returned color */ |
333 |
|
} |
363 |
|
|
364 |
|
|
365 |
|
static void |
366 |
< |
gaussamp( /* sample gaussian specular */ |
366 |
> |
gaussamp( /* sample Gaussian specular */ |
367 |
|
RAY *r, |
368 |
|
register NORMDAT *np |
369 |
|
) |
372 |
|
FVECT u, v, h; |
373 |
|
double rv[2]; |
374 |
|
double d, sinp, cosp; |
375 |
< |
int niter; |
375 |
> |
COLOR scol; |
376 |
> |
int maxiter, ntrials, nstarget, nstaken; |
377 |
|
register int i; |
378 |
|
/* quick test */ |
379 |
|
if ((np->specfl & (SP_REFL|SP_RBLT)) != SP_REFL && |
391 |
|
/* compute reflection */ |
392 |
|
if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL && |
393 |
|
rayorigin(&sr, SPECULAR, r, np->scolor) == 0) { |
394 |
< |
dimlist[ndims++] = (int)np->mp; |
395 |
< |
for (niter = 0; niter < MAXITER; niter++) { |
396 |
< |
if (niter) |
394 |
> |
nstarget = 1; |
395 |
> |
if (specjitter > 1.5) { /* multiple samples? */ |
396 |
> |
nstarget = specjitter*r->rweight + .5; |
397 |
> |
if (sr.rweight <= minweight*nstarget) |
398 |
> |
nstarget = sr.rweight/minweight; |
399 |
> |
if (nstarget > 1) { |
400 |
> |
d = 1./nstarget; |
401 |
> |
scalecolor(sr.rcoef, d); |
402 |
> |
sr.rweight *= d; |
403 |
> |
} else |
404 |
> |
nstarget = 1; |
405 |
> |
} |
406 |
> |
setcolor(scol, 0., 0., 0.); |
407 |
> |
dimlist[ndims++] = (int)(size_t)np->mp; |
408 |
> |
maxiter = MAXITER*nstarget; |
409 |
> |
for (nstaken = ntrials = 0; nstaken < nstarget && |
410 |
> |
ntrials < maxiter; ntrials++) { |
411 |
> |
if (ntrials) |
412 |
|
d = frandom(); |
413 |
|
else |
414 |
|
d = urand(ilhash(dimlist,ndims)+samplendx); |
416 |
|
d = 2.0*PI * rv[0]; |
417 |
|
cosp = tcos(d); |
418 |
|
sinp = tsin(d); |
419 |
< |
rv[1] = 1.0 - specjitter*rv[1]; |
419 |
> |
if ((0. <= specjitter) & (specjitter < 1.)) |
420 |
> |
rv[1] = 1.0 - specjitter*rv[1]; |
421 |
|
if (rv[1] <= FTINY) |
422 |
|
d = 1.0; |
423 |
|
else |
425 |
|
for (i = 0; i < 3; i++) |
426 |
|
h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*v[i]); |
427 |
|
d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d); |
428 |
< |
for (i = 0; i < 3; i++) |
429 |
< |
sr.rdir[i] = r->rdir[i] + d*h[i]; |
430 |
< |
if (DOT(sr.rdir, r->ron) > FTINY) { |
428 |
> |
VSUM(sr.rdir, r->rdir, h, d); |
429 |
> |
/* sample rejection test */ |
430 |
> |
if ((d = DOT(sr.rdir, r->ron)) <= FTINY) |
431 |
> |
continue; |
432 |
> |
checknorm(sr.rdir); |
433 |
> |
if (nstarget > 1) { /* W-G-M-D adjustment */ |
434 |
> |
if (nstaken) rayclear(&sr); |
435 |
|
rayvalue(&sr); |
436 |
+ |
d = 2./(1. + r->rod/d); |
437 |
+ |
scalecolor(sr.rcol, d); |
438 |
+ |
addcolor(scol, sr.rcol); |
439 |
+ |
} else { |
440 |
+ |
rayvalue(&sr); |
441 |
|
multcolor(sr.rcol, sr.rcoef); |
442 |
|
addcolor(r->rcol, sr.rcol); |
419 |
– |
break; |
443 |
|
} |
444 |
+ |
++nstaken; |
445 |
|
} |
446 |
+ |
if (nstarget > 1) { /* final W-G-M-D weighting */ |
447 |
+ |
multcolor(scol, sr.rcoef); |
448 |
+ |
d = (double)nstarget/ntrials; |
449 |
+ |
scalecolor(scol, d); |
450 |
+ |
addcolor(r->rcol, scol); |
451 |
+ |
} |
452 |
|
ndims--; |
453 |
|
} |
454 |
|
/* compute transmission */ |
456 |
|
scalecolor(sr.rcoef, np->tspec); |
457 |
|
if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN && |
458 |
|
rayorigin(&sr, SPECULAR, r, sr.rcoef) == 0) { |
459 |
< |
dimlist[ndims++] = (int)np->mp; |
460 |
< |
for (niter = 0; niter < MAXITER; niter++) { |
461 |
< |
if (niter) |
459 |
> |
nstarget = 1; |
460 |
> |
if (specjitter > 1.5) { /* multiple samples? */ |
461 |
> |
nstarget = specjitter*r->rweight + .5; |
462 |
> |
if (sr.rweight <= minweight*nstarget) |
463 |
> |
nstarget = sr.rweight/minweight; |
464 |
> |
if (nstarget > 1) { |
465 |
> |
d = 1./nstarget; |
466 |
> |
scalecolor(sr.rcoef, d); |
467 |
> |
sr.rweight *= d; |
468 |
> |
} else |
469 |
> |
nstarget = 1; |
470 |
> |
} |
471 |
> |
dimlist[ndims++] = (int)(size_t)np->mp; |
472 |
> |
maxiter = MAXITER*nstarget; |
473 |
> |
for (nstaken = ntrials = 0; nstaken < nstarget && |
474 |
> |
ntrials < maxiter; ntrials++) { |
475 |
> |
if (ntrials) |
476 |
|
d = frandom(); |
477 |
|
else |
478 |
< |
d = urand(ilhash(dimlist,ndims)+1823+samplendx); |
478 |
> |
d = urand(ilhash(dimlist,ndims)+samplendx); |
479 |
|
multisamp(rv, 2, d); |
480 |
|
d = 2.0*PI * rv[0]; |
481 |
|
cosp = tcos(d); |
482 |
|
sinp = tsin(d); |
483 |
< |
rv[1] = 1.0 - specjitter*rv[1]; |
483 |
> |
if ((0. <= specjitter) & (specjitter < 1.)) |
484 |
> |
rv[1] = 1.0 - specjitter*rv[1]; |
485 |
|
if (rv[1] <= FTINY) |
486 |
|
d = 1.0; |
487 |
|
else |
488 |
|
d = sqrt( np->alpha2 * -log(rv[1]) ); |
489 |
|
for (i = 0; i < 3; i++) |
490 |
|
sr.rdir[i] = np->prdir[i] + d*(cosp*u[i] + sinp*v[i]); |
491 |
< |
if (DOT(sr.rdir, r->ron) < -FTINY) { |
492 |
< |
normalize(sr.rdir); /* OK, normalize */ |
493 |
< |
rayvalue(&sr); |
494 |
< |
multcolor(sr.rcol, sr.rcoef); |
495 |
< |
addcolor(r->rcol, sr.rcol); |
496 |
< |
break; |
497 |
< |
} |
491 |
> |
/* sample rejection test */ |
492 |
> |
if (DOT(sr.rdir, r->ron) >= -FTINY) |
493 |
> |
continue; |
494 |
> |
normalize(sr.rdir); /* OK, normalize */ |
495 |
> |
if (nstaken) /* multi-sampling */ |
496 |
> |
rayclear(&sr); |
497 |
> |
rayvalue(&sr); |
498 |
> |
multcolor(sr.rcol, sr.rcoef); |
499 |
> |
addcolor(r->rcol, sr.rcol); |
500 |
> |
++nstaken; |
501 |
|
} |
502 |
|
ndims--; |
503 |
|
} |