--- ray/src/rt/normal.c 2005/12/08 17:56:38 2.51 +++ ray/src/rt/normal.c 2010/10/10 22:31:46 2.57 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: normal.c,v 2.51 2005/12/08 17:56:38 greg Exp $"; +static const char RCSid[] = "$Id: normal.c,v 2.57 2010/10/10 22:31:46 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,13 +156,12 @@ dirnorm( /* compute source contribution */ */ /* roughness + source */ dtmp = np->alpha2 + omega*(1.0/PI); - /* gaussian */ - dtmp = exp((2.*DOT(np->prdir,ldir)-2.)/dtmp) / - (PI*np->pdot*dtmp); + /* Gaussian */ + dtmp = exp((2.*DOT(np->prdir,ldir)-2.)/dtmp)/(PI*dtmp); /* worth using? */ if (dtmp > FTINY) { copycolor(ctmp, np->mcolor); - dtmp *= np->tspec * omega; + dtmp *= np->tspec * omega * sqrt(-ldot/np->pdot); scalecolor(ctmp, dtmp); addcolor(cval, ctmp); } @@ -294,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)) { @@ -365,7 +364,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 ) @@ -374,7 +373,8 @@ gaussamp( /* sample gaussian specular */ FVECT u, v, h; double rv[2]; double d, sinp, cosp; - int niter; + COLOR scol; + int niter, ns2go; register int i; /* quick test */ if ((np->specfl & (SP_REFL|SP_RBLT)) != SP_REFL && @@ -392,9 +392,22 @@ gaussamp( /* sample gaussian specular */ /* compute reflection */ if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL && rayorigin(&sr, SPECULAR, r, np->scolor) == 0) { + copycolor(scol, np->scolor); + ns2go = 1; + if (specjitter > 1.5) { /* multiple samples? */ + ns2go = specjitter*r->rweight + .5; + if (sr.rweight <= minweight*ns2go) + ns2go = sr.rweight/minweight; + if (ns2go > 1) { + d = 1./ns2go; + scalecolor(scol, d); + sr.rweight *= d; + } else + ns2go = 1; + } dimlist[ndims++] = (int)np->mp; - for (niter = 0; niter < MAXITER; niter++) { - if (niter) + for (niter = ns2go*MAXITER; (ns2go > 0) & (niter > 0); niter--) { + if (specjitter > 1.5) d = frandom(); else d = urand(ilhash(dimlist,ndims)+samplendx); @@ -402,7 +415,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 @@ -410,14 +424,22 @@ 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) { - rayvalue(&sr); - multcolor(sr.rcol, sr.rcoef); - addcolor(r->rcol, sr.rcol); - break; + if (d <= np->pdot + FTINY) + continue; + VSUM(sr.rdir, r->rdir, h, d); + if (DOT(sr.rdir, r->ron) <= FTINY) + continue; + checknorm(sr.rdir); + if (specjitter > 1.5) { /* adjusted W-G-M-D weight */ + d = 2.*(1. - np->pdot/d); + copycolor(sr.rcoef, scol); + scalecolor(sr.rcoef, d); + rayclear(&sr); } + rayvalue(&sr); + multcolor(sr.rcol, sr.rcoef); + addcolor(r->rcol, sr.rcol); + --ns2go; } ndims--; } @@ -426,9 +448,21 @@ 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) { + ns2go = 1; + if (specjitter > 1.5) { /* multiple samples? */ + ns2go = specjitter*r->rweight + .5; + if (sr.rweight <= minweight*ns2go) + ns2go = sr.rweight/minweight; + if (ns2go > 1) { + d = 1./ns2go; + scalecolor(sr.rcoef, d); + sr.rweight *= d; + } else + ns2go = 1; + } dimlist[ndims++] = (int)np->mp; - for (niter = 0; niter < MAXITER; niter++) { - if (niter) + for (niter = ns2go*MAXITER; (ns2go > 0) & (niter > 0); niter--) { + if (specjitter > 1.5) d = frandom(); else d = urand(ilhash(dimlist,ndims)+1823+samplendx); @@ -436,20 +470,23 @@ 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 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; - } + if (DOT(sr.rdir, r->ron) >= -FTINY) + continue; + normalize(sr.rdir); /* OK, normalize */ + if (specjitter > 1.5) /* multi-sampling */ + rayclear(&sr); + rayvalue(&sr); + multcolor(sr.rcol, sr.rcoef); + addcolor(r->rcol, sr.rcol); + --ns2go; } ndims--; }