--- ray/src/rt/normal.c 2010/05/07 15:44:52 2.52 +++ ray/src/rt/normal.c 2011/10/26 03:44:56 2.61 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: normal.c,v 2.52 2010/05/07 15:44:52 greg Exp $"; +static const char RCSid[] = "$Id: normal.c,v 2.61 2011/10/26 03:44:56 greg Exp $"; #endif /* * normal.c - shading function for normal materials. @@ -80,7 +80,7 @@ dirnorm( /* compute source contribution */ register NORMDAT *np = nnp; double ldot; double lrdiff, ltdiff; - double dtmp, d2; + double dtmp, d2, d3, d4; FVECT vtmp; COLOR ctmp; @@ -115,7 +115,7 @@ dirnorm( /* compute source contribution */ if (ldot > FTINY && (np->specfl&(SP_REFL|SP_PURE)) == SP_REFL) { /* * Compute specular reflection coefficient using - * gaussian distribution model. + * Gaussian distribution model. */ /* roughness */ dtmp = np->alpha2; @@ -128,13 +128,14 @@ dirnorm( /* compute source contribution */ vtmp[2] = ldir[2] - np->rp->rdir[2]; d2 = DOT(vtmp, np->pnorm); d2 *= d2; - d2 = (DOT(vtmp,vtmp) - d2) / d2; - /* gaussian */ - dtmp = exp(-d2/dtmp)/(4.*PI * np->pdot * dtmp); + d3 = DOT(vtmp,vtmp); + d4 = (d3 - d2) / d2; + /* new W-G-M-D model */ + dtmp = exp(-d4/dtmp) * d3 / (PI * d2*d2 * dtmp); /* worth using? */ if (dtmp > FTINY) { copycolor(ctmp, np->scolor); - dtmp *= omega; + dtmp *= ldot * omega; scalecolor(ctmp, dtmp); addcolor(cval, ctmp); } @@ -155,8 +156,8 @@ dirnorm( /* compute source contribution */ */ /* roughness + source */ dtmp = np->alpha2 + omega*(1.0/PI); - /* gaussian */ - dtmp = exp((2.*DOT(np->prdir,ldir)-2.)/dtmp)/(PI*dtmp); + /* Gaussian */ + dtmp = exp((2.*DOT(np->prdir,ldir)-2.)/dtmp)/(PI*dtmp); /* worth using? */ if (dtmp > FTINY) { copycolor(ctmp, np->mcolor); @@ -293,12 +294,11 @@ m_normal( /* color a ray that hit something normal * if (!(nd.specfl & SP_PURE) && specthresh >= nd.rspec-FTINY) nd.specfl |= SP_RBLT; /* compute reflected ray */ - for (i = 0; i < 3; i++) - nd.vrefl[i] = r->rdir[i] + 2.*nd.pdot*nd.pnorm[i]; + VSUM(nd.vrefl, r->rdir, nd.pnorm, 2.*nd.pdot); /* penetration? */ if (hastexture && DOT(nd.vrefl, r->ron) <= FTINY) - for (i = 0; i < 3; i++) /* safety measure */ - nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i]; + VSUM(nd.vrefl, r->rdir, r->ron, 2.*r->rod); + checknorm(nd.vrefl); } /* reflected ray */ if ((nd.specfl&(SP_REFL|SP_PURE|SP_RBLT)) == (SP_REFL|SP_PURE)) { @@ -325,10 +325,9 @@ m_normal( /* color a ray that hit something normal * if (nd.rdiff > FTINY) { /* ambient from this side */ copycolor(ctmp, nd.mcolor); /* modified by material color */ - if (nd.specfl & SP_RBLT) - scalecolor(ctmp, 1.0-nd.trans); - else - scalecolor(ctmp, nd.rdiff); + scalecolor(ctmp, nd.rdiff); + if (nd.specfl & SP_RBLT) /* add in specular as well? */ + addcolor(ctmp, nd.scolor); multambient(ctmp, r, hastexture ? nd.pnorm : r->ron); addcolor(r->rcol, ctmp); /* add to returned color */ } @@ -364,7 +363,7 @@ m_normal( /* color a ray that hit something normal * static void -gaussamp( /* sample gaussian specular */ +gaussamp( /* sample Gaussian specular */ RAY *r, register NORMDAT *np ) @@ -373,7 +372,8 @@ gaussamp( /* sample gaussian specular */ FVECT u, v, h; double rv[2]; double d, sinp, cosp; - int niter; + COLOR scol; + int maxiter, ntrials, nstarget, nstaken; register int i; /* quick test */ if ((np->specfl & (SP_REFL|SP_RBLT)) != SP_REFL && @@ -391,9 +391,24 @@ gaussamp( /* sample gaussian specular */ /* compute reflection */ if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL && rayorigin(&sr, SPECULAR, r, np->scolor) == 0) { - dimlist[ndims++] = (int)np->mp; - for (niter = 0; niter < MAXITER; niter++) { - if (niter) + nstarget = 1; + if (specjitter > 1.5) { /* multiple samples? */ + nstarget = specjitter*r->rweight + .5; + if (sr.rweight <= minweight*nstarget) + nstarget = sr.rweight/minweight; + if (nstarget > 1) { + d = 1./nstarget; + scalecolor(sr.rcoef, d); + sr.rweight *= d; + } else + nstarget = 1; + } + setcolor(scol, 0., 0., 0.); + dimlist[ndims++] = (int)(size_t)np->mp; + maxiter = MAXITER*nstarget; + for (nstaken = ntrials = 0; nstaken < nstarget && + ntrials < maxiter; ntrials++) { + if (ntrials) d = frandom(); else d = urand(ilhash(dimlist,ndims)+samplendx); @@ -401,7 +416,8 @@ gaussamp( /* sample gaussian specular */ d = 2.0*PI * rv[0]; cosp = tcos(d); sinp = tsin(d); - rv[1] = 1.0 - specjitter*rv[1]; + if ((0. <= specjitter) & (specjitter < 1.)) + rv[1] = 1.0 - specjitter*rv[1]; if (rv[1] <= FTINY) d = 1.0; else @@ -409,15 +425,30 @@ gaussamp( /* sample gaussian specular */ for (i = 0; i < 3; i++) h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*v[i]); d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d); - for (i = 0; i < 3; i++) - sr.rdir[i] = r->rdir[i] + d*h[i]; - if (DOT(sr.rdir, r->ron) > FTINY) { + VSUM(sr.rdir, r->rdir, h, d); + /* sample rejection test */ + if ((d = DOT(sr.rdir, r->ron)) <= FTINY) + continue; + checknorm(sr.rdir); + if (nstarget > 1) { /* W-G-M-D adjustment */ + if (nstaken) rayclear(&sr); rayvalue(&sr); + d = 2./(1. + r->rod/d); + scalecolor(sr.rcol, d); + addcolor(scol, sr.rcol); + } else { + rayvalue(&sr); multcolor(sr.rcol, sr.rcoef); addcolor(r->rcol, sr.rcol); - break; } + ++nstaken; } + if (nstarget > 1) { /* final W-G-M-D weighting */ + multcolor(scol, sr.rcoef); + d = (double)nstarget/ntrials; + scalecolor(scol, d); + addcolor(r->rcol, scol); + } ndims--; } /* compute transmission */ @@ -425,30 +456,48 @@ gaussamp( /* sample gaussian specular */ scalecolor(sr.rcoef, np->tspec); if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN && rayorigin(&sr, SPECULAR, r, sr.rcoef) == 0) { - dimlist[ndims++] = (int)np->mp; - for (niter = 0; niter < MAXITER; niter++) { - if (niter) + nstarget = 1; + if (specjitter > 1.5) { /* multiple samples? */ + nstarget = specjitter*r->rweight + .5; + if (sr.rweight <= minweight*nstarget) + nstarget = sr.rweight/minweight; + if (nstarget > 1) { + d = 1./nstarget; + scalecolor(sr.rcoef, d); + sr.rweight *= d; + } else + nstarget = 1; + } + dimlist[ndims++] = (int)(size_t)np->mp; + maxiter = MAXITER*nstarget; + for (nstaken = ntrials = 0; nstaken < nstarget && + ntrials < maxiter; ntrials++) { + if (ntrials) d = frandom(); else - d = urand(ilhash(dimlist,ndims)+1823+samplendx); + d = urand(ilhash(dimlist,ndims)+samplendx); multisamp(rv, 2, d); d = 2.0*PI * rv[0]; cosp = tcos(d); sinp = tsin(d); - rv[1] = 1.0 - specjitter*rv[1]; + if ((0. <= specjitter) & (specjitter < 1.)) + rv[1] = 1.0 - specjitter*rv[1]; if (rv[1] <= FTINY) d = 1.0; else d = sqrt( np->alpha2 * -log(rv[1]) ); for (i = 0; i < 3; i++) sr.rdir[i] = np->prdir[i] + d*(cosp*u[i] + sinp*v[i]); - if (DOT(sr.rdir, r->ron) < -FTINY) { - normalize(sr.rdir); /* OK, normalize */ - rayvalue(&sr); - multcolor(sr.rcol, sr.rcoef); - addcolor(r->rcol, sr.rcol); - break; - } + /* sample rejection test */ + if (DOT(sr.rdir, r->ron) >= -FTINY) + continue; + normalize(sr.rdir); /* OK, normalize */ + if (nstaken) /* multi-sampling */ + rayclear(&sr); + rayvalue(&sr); + multcolor(sr.rcol, sr.rcoef); + addcolor(r->rcol, sr.rcol); + ++nstaken; } ndims--; }