--- ray/src/rt/normal.c 2003/08/28 03:22:16 2.46 +++ ray/src/rt/normal.c 2012/07/29 19:01:39 2.62 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: normal.c,v 2.46 2003/08/28 03:22:16 greg Exp $"; +static const char RCSid[] = "$Id: normal.c,v 2.62 2012/07/29 19:01:39 greg Exp $"; #endif /* * normal.c - shading function for normal materials. @@ -14,11 +14,10 @@ static const char RCSid[] = "$Id: normal.c,v 2.46 2003 #include "copyright.h" #include "ray.h" - #include "ambient.h" - +#include "source.h" #include "otypes.h" - +#include "rtotypes.h" #include "random.h" #ifndef MAXITER @@ -26,8 +25,8 @@ static const char RCSid[] = "$Id: normal.c,v 2.46 2003 #endif /* estimate of Fresnel function */ #define FRESNE(ci) (exp(-5.85*(ci)) - 0.00287989916) +#define FRESTHRESH 0.017999 /* minimum specularity for approx. */ -static void gaussamp(); /* * This routine implements the isotropic Gaussian @@ -66,17 +65,21 @@ typedef struct { double pdot; /* perturbed dot product */ } NORMDAT; /* normal material data */ +static void gaussamp(RAY *r, NORMDAT *np); + static void -dirnorm(cval, np, ldir, omega) /* compute source contribution */ -COLOR cval; /* returned coefficient */ -register NORMDAT *np; /* material data */ -FVECT ldir; /* light source direction */ -double omega; /* light source size */ +dirnorm( /* compute source contribution */ + COLOR cval, /* returned coefficient */ + void *nnp, /* material data */ + FVECT ldir, /* light source direction */ + double omega /* light source size */ +) { + NORMDAT *np = nnp; double ldot; - double ldiff; - double dtmp, d2; + double lrdiff, ltdiff; + double dtmp, d2, d3, d4; FVECT vtmp; COLOR ctmp; @@ -88,54 +91,58 @@ double omega; /* light source size */ 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 / 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; /* + source if flat */ if (np->specfl & SP_FLAT) - dtmp += omega/(4.0*PI); + dtmp += omega * (0.25/PI); /* half vector */ - vtmp[0] = ldir[0] - np->rp->rdir[0]; - vtmp[1] = ldir[1] - np->rp->rdir[1]; - vtmp[2] = ldir[2] - np->rp->rdir[2]; + VSUB(vtmp, ldir, np->rp->rdir); d2 = DOT(vtmp, np->pnorm); d2 *= d2; - d2 = (DOT(vtmp,vtmp) - d2) / d2; - /* gaussian */ - dtmp = exp(-d2/dtmp)/(4.*PI*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 * sqrt(ldot/np->pdot); + 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 / PI; + dtmp = -ldot * omega * ltdiff * (1.0/PI); scalecolor(ctmp, dtmp); addcolor(cval, ctmp); } @@ -145,8 +152,8 @@ double omega; /* light source size */ * is always modified by material color. */ /* roughness + source */ - dtmp = np->alpha2 + omega/PI; - /* gaussian */ + dtmp = np->alpha2 + omega*(1.0/PI); + /* Gaussian */ dtmp = exp((2.*DOT(np->prdir,ldir)-2.)/dtmp)/(PI*dtmp); /* worth using? */ if (dtmp > FTINY) { @@ -160,9 +167,10 @@ double omega; /* light source size */ int -m_normal(m, r) /* color a ray that hit something normal */ -register OBJREC *m; -register RAY *r; +m_normal( /* color a ray that hit something normal */ + OBJREC *m, + RAY *r +) { NORMDAT nd; double fest; @@ -171,7 +179,7 @@ register RAY *r; int hastexture; double d; COLOR ctmp; - register int i; + int i; /* easy shadow test */ if (r->crtype & SHADOW && m->otype != MAT_TRANS) return(1); @@ -215,8 +223,8 @@ register RAY *r; mirdist = transdist = r->rot; nd.rspec = m->oargs.farg[3]; /* compute Fresnel approx. */ - if (nd.specfl & SP_PURE && nd.rspec > FTINY) { - fest = FRESNE(r->rod); + if (nd.specfl & SP_PURE && nd.rspec >= FRESTHRESH) { + fest = FRESNE(nd.pdot); nd.rspec += fest*(1. - nd.rspec); } else fest = 0.; @@ -248,11 +256,12 @@ register RAY *r; /* 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; @@ -282,20 +291,19 @@ register RAY *r; 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); @@ -313,29 +321,28 @@ register RAY *r; gaussamp(r, &nd); /* checks *BLT flags */ if (nd.rdiff > FTINY) { /* ambient from this side */ - ambient(ctmp, r, hastexture?nd.pnorm:r->ron); - if (nd.specfl & SP_RBLT) - scalecolor(ctmp, 1.0-nd.trans); - else - scalecolor(ctmp, nd.rdiff); - multcolor(ctmp, nd.mcolor); /* modified by material color */ + copycolor(ctmp, nd.mcolor); /* modified by material color */ + 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 */ } 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); } @@ -353,16 +360,18 @@ register RAY *r; static void -gaussamp(r, np) /* sample gaussian specular */ -RAY *r; -register NORMDAT *np; +gaussamp( /* sample Gaussian specular */ + RAY *r, + NORMDAT *np +) { RAY sr; FVECT u, v, h; double rv[2]; double d, sinp, cosp; - int niter; - register int i; + COLOR scol; + int maxiter, ntrials, nstarget, nstaken; + int i; /* quick test */ if ((np->specfl & (SP_REFL|SP_RBLT)) != SP_REFL && (np->specfl & (SP_TRAN|SP_TBLT)) != SP_TRAN) @@ -378,10 +387,25 @@ register NORMDAT *np; fcross(v, np->pnorm, u); /* compute reflection */ if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL && - rayorigin(&sr, r, SPECULAR, np->rspec) == 0) { - dimlist[ndims++] = (int)np->mp; - for (niter = 0; niter < MAXITER; niter++) { - if (niter) + rayorigin(&sr, SPECULAR, r, np->scolor) == 0) { + 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); @@ -389,7 +413,8 @@ register NORMDAT *np; 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 @@ -397,45 +422,79 @@ register NORMDAT *np; 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); - multcolor(sr.rcol, np->scolor); + 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 */ + 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) { - dimlist[ndims++] = (int)np->mp; - for (niter = 0; niter < MAXITER; niter++) { - if (niter) + rayorigin(&sr, SPECULAR, r, sr.rcoef) == 0) { + 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); - scalecolor(sr.rcol, np->tspec); - multcolor(sr.rcol, np->mcolor); /* modified */ - 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--; }