--- ray/src/rt/normal.c 2004/09/20 17:32:04 2.48 +++ 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.48 2004/09/20 17:32:04 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. @@ -25,6 +25,7 @@ static const char RCSid[] = "$Id: normal.c,v 2.48 2004 #endif /* estimate of Fresnel function */ #define FRESNE(ci) (exp(-5.85*(ci)) - 0.00287989916) +#define FRESTHRESH 0.017999 /* minimum specularity for approx. */ /* @@ -78,8 +79,8 @@ dirnorm( /* compute source contribution */ { register NORMDAT *np = nnp; double ldot; - double ldiff; - double dtmp, d2; + double lrdiff, ltdiff; + double dtmp, d2, d3, d4; FVECT vtmp; COLOR ctmp; @@ -91,25 +92,30 @@ dirnorm( /* compute source contribution */ return; /* wrong side */ /* Fresnel estimate */ - ldiff = np->rdiff; - if (np->specfl & SP_PURE && (np->rspec > FTINY) & (ldiff > FTINY)) - ldiff *= 1. - FRESNE(fabs(ldot)); + lrdiff = np->rdiff; + ltdiff = np->tdiff; + if (np->specfl & SP_PURE && np->rspec >= FRESTHRESH && + (lrdiff > FTINY) | (ltdiff > FTINY)) { + dtmp = 1. - FRESNE(fabs(ldot)); + lrdiff *= dtmp; + ltdiff *= dtmp; + } - if (ldot > FTINY && ldiff > FTINY) { + if (ldot > FTINY && lrdiff > FTINY) { /* * Compute and add diffuse reflected component to returned * color. The diffuse reflected component will always be * modified by the color of the material. */ copycolor(ctmp, np->mcolor); - dtmp = ldot * omega * ldiff * (1.0/PI); + dtmp = ldot * omega * lrdiff * (1.0/PI); scalecolor(ctmp, dtmp); addcolor(cval, ctmp); } 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; @@ -122,23 +128,24 @@ 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); } } - if (ldot < -FTINY && np->tdiff > FTINY) { + if (ldot < -FTINY && ltdiff > FTINY) { /* * Compute diffuse transmission. */ copycolor(ctmp, np->mcolor); - dtmp = -ldot * omega * np->tdiff * (1.0/PI); + dtmp = -ldot * omega * ltdiff * (1.0/PI); scalecolor(ctmp, dtmp); addcolor(cval, ctmp); } @@ -149,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); } @@ -220,7 +226,7 @@ m_normal( /* color a ray that hit something normal * mirdist = transdist = r->rot; nd.rspec = m->oargs.farg[3]; /* compute Fresnel approx. */ - if (nd.specfl & SP_PURE && nd.rspec > FTINY) { + if (nd.specfl & SP_PURE && nd.rspec >= FRESTHRESH) { fest = FRESNE(r->rod); nd.rspec += fest*(1. - nd.rspec); } else @@ -253,11 +259,12 @@ m_normal( /* color a ray that hit something normal * /* transmitted ray */ if ((nd.specfl&(SP_TRAN|SP_PURE|SP_TBLT)) == (SP_TRAN|SP_PURE)) { RAY lr; - if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) { + copycolor(lr.rcoef, nd.mcolor); /* modified by color */ + scalecolor(lr.rcoef, nd.tspec); + if (rayorigin(&lr, TRANS, r, lr.rcoef) == 0) { VCOPY(lr.rdir, nd.prdir); rayvalue(&lr); - scalecolor(lr.rcol, nd.tspec); - multcolor(lr.rcol, nd.mcolor); /* modified by color */ + multcolor(lr.rcol, lr.rcoef); addcolor(r->rcol, lr.rcol); transtest *= bright(lr.rcol); transdist = r->rot + lr.rt; @@ -287,20 +294,19 @@ 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)) { RAY lr; - if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) { + if (rayorigin(&lr, REFLECTED, r, nd.scolor) == 0) { VCOPY(lr.rdir, nd.vrefl); rayvalue(&lr); - multcolor(lr.rcol, nd.scolor); + multcolor(lr.rcol, lr.rcoef); addcolor(r->rcol, lr.rcol); if (!hastexture && nd.specfl & SP_FLAT) { mirtest = 2.*bright(lr.rcol); @@ -318,29 +324,29 @@ m_normal( /* color a ray that hit something normal * gaussamp(r, &nd); /* checks *BLT flags */ if (nd.rdiff > FTINY) { /* ambient from this side */ - ambient(ctmp, r, hastexture?nd.pnorm:r->ron); + copycolor(ctmp, nd.mcolor); /* modified by material color */ if (nd.specfl & SP_RBLT) scalecolor(ctmp, 1.0-nd.trans); else scalecolor(ctmp, nd.rdiff); - multcolor(ctmp, nd.mcolor); /* modified by material color */ + multambient(ctmp, r, hastexture ? nd.pnorm : r->ron); addcolor(r->rcol, ctmp); /* add to returned color */ } if (nd.tdiff > FTINY) { /* ambient from other side */ + copycolor(ctmp, nd.mcolor); /* modified by color */ + if (nd.specfl & SP_TBLT) + scalecolor(ctmp, nd.trans); + else + scalecolor(ctmp, nd.tdiff); flipsurface(r); if (hastexture) { FVECT bnorm; bnorm[0] = -nd.pnorm[0]; bnorm[1] = -nd.pnorm[1]; bnorm[2] = -nd.pnorm[2]; - ambient(ctmp, r, bnorm); + multambient(ctmp, r, bnorm); } else - ambient(ctmp, r, r->ron); - if (nd.specfl & SP_TBLT) - scalecolor(ctmp, nd.trans); - else - scalecolor(ctmp, nd.tdiff); - multcolor(ctmp, nd.mcolor); /* modified by color */ + multambient(ctmp, r, r->ron); addcolor(r->rcol, ctmp); flipsurface(r); } @@ -358,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 ) @@ -367,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 && @@ -384,10 +391,23 @@ gaussamp( /* sample gaussian specular */ fcross(v, np->pnorm, u); /* compute reflection */ if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL && - rayorigin(&sr, r, SPECULAR, np->rspec) == 0) { + 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); @@ -395,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 @@ -403,23 +424,45 @@ 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, np->scolor); - 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--; } /* compute transmission */ + copycolor(sr.rcoef, np->mcolor); /* modified by color */ + scalecolor(sr.rcoef, np->tspec); if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN && - rayorigin(&sr, r, SPECULAR, np->tspec) == 0) { + 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); @@ -427,21 +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); - scalecolor(sr.rcol, np->tspec); - multcolor(sr.rcol, np->mcolor); /* modified */ - 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--; }