--- ray/src/rt/normal.c 2010/09/26 15:51:15 2.53 +++ ray/src/rt/normal.c 2012/08/01 15:26:36 2.65 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: normal.c,v 2.53 2010/09/26 15:51:15 greg Exp $"; +static const char RCSid[] = "$Id: normal.c,v 2.65 2012/08/01 15:26:36 greg Exp $"; #endif /* * normal.c - shading function for normal materials. @@ -65,22 +65,21 @@ typedef struct { double pdot; /* perturbed dot product */ } NORMDAT; /* normal material data */ -static srcdirf_t dirnorm; -static void gaussamp(RAY *r, NORMDAT *np); +static void gaussamp(NORMDAT *np); static void dirnorm( /* compute source contribution */ COLOR cval, /* returned coefficient */ - void *nnp, /* material data */ + void *nnp, /* material data */ FVECT ldir, /* light source direction */ double omega /* light source size */ ) { - register NORMDAT *np = nnp; + NORMDAT *np = nnp; double ldot; double lrdiff, ltdiff; - double dtmp, d2; + double dtmp, d2, d3, d4; FVECT vtmp; COLOR ctmp; @@ -115,7 +114,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; @@ -123,18 +122,17 @@ dirnorm( /* compute source contribution */ if (np->specfl & SP_FLAT) 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 * 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,7 +153,7 @@ dirnorm( /* compute source contribution */ */ /* roughness + source */ dtmp = np->alpha2 + omega*(1.0/PI); - /* gaussian */ + /* Gaussian */ dtmp = exp((2.*DOT(np->prdir,ldir)-2.)/dtmp)/(PI*dtmp); /* worth using? */ if (dtmp > FTINY) { @@ -168,10 +166,10 @@ dirnorm( /* compute source contribution */ } -extern int +int m_normal( /* color a ray that hit something normal */ - register OBJREC *m, - register RAY *r + OBJREC *m, + RAY *r ) { NORMDAT nd; @@ -181,7 +179,7 @@ m_normal( /* color a ray that hit something normal * int hastexture; double d; COLOR ctmp; - register int i; + int i; /* easy shadow test */ if (r->crtype & SHADOW && m->otype != MAT_TRANS) return(1); @@ -226,7 +224,7 @@ m_normal( /* color a ray that hit something normal * nd.rspec = m->oargs.farg[3]; /* compute Fresnel approx. */ if (nd.specfl & SP_PURE && nd.rspec >= FRESTHRESH) { - fest = FRESNE(r->rod); + fest = FRESNE(nd.pdot); nd.rspec += fest*(1. - nd.rspec); } else fest = 0.; @@ -282,9 +280,10 @@ m_normal( /* color a ray that hit something normal * if (m->otype != MAT_METAL) { setcolor(nd.scolor, nd.rspec, nd.rspec, nd.rspec); } else if (fest > FTINY) { - d = nd.rspec*(1. - fest); + d = m->oargs.farg[3]*(1. - fest); for (i = 0; i < 3; i++) - nd.scolor[i] = fest + nd.mcolor[i]*d; + colval(nd.scolor,i) = fest + + colval(nd.mcolor,i)*d; } else { copycolor(nd.scolor, nd.mcolor); scalecolor(nd.scolor, nd.rspec); @@ -293,12 +292,10 @@ 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 */ @@ -322,14 +319,13 @@ m_normal( /* color a ray that hit something normal * return(1); /* 100% pure specular */ if (!(nd.specfl & SP_PURE)) - gaussamp(r, &nd); /* checks *BLT flags */ + gaussamp(&nd); /* checks *BLT flags */ 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 */ } @@ -365,17 +361,17 @@ m_normal( /* color a ray that hit something normal * static void -gaussamp( /* sample gaussian specular */ - RAY *r, - register NORMDAT *np +gaussamp( /* sample Gaussian specular */ + 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) @@ -391,10 +387,25 @@ gaussamp( /* sample gaussian specular */ fcross(v, np->pnorm, u); /* 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) + rayorigin(&sr, SPECULAR, np->rp, np->scolor) == 0) { + nstarget = 1; + if (specjitter > 1.5) { /* multiple samples? */ + nstarget = specjitter*np->rp->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); @@ -402,54 +413,88 @@ 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++) 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) { + d = -2.0 * DOT(h, np->rp->rdir) / (1.0 + d*d); + VSUM(sr.rdir, np->rp->rdir, h, d); + /* sample rejection test */ + if ((d = DOT(sr.rdir, np->rp->ron)) <= FTINY) + continue; + checknorm(sr.rdir); + if (nstarget > 1) { /* W-G-M-D adjustment */ + if (nstaken) rayclear(&sr); rayvalue(&sr); + d = 2./(1. + np->rp->rod/d); + scalecolor(sr.rcol, d); + addcolor(scol, sr.rcol); + } else { + rayvalue(&sr); multcolor(sr.rcol, sr.rcoef); - addcolor(r->rcol, sr.rcol); - break; + addcolor(np->rp->rcol, sr.rcol); } + ++nstaken; } + if (nstarget > 1) { /* final W-G-M-D weighting */ + multcolor(scol, sr.rcoef); + d = (double)nstarget/ntrials; + scalecolor(scol, d); + addcolor(np->rp->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, SPECULAR, r, sr.rcoef) == 0) { - dimlist[ndims++] = (int)np->mp; - for (niter = 0; niter < MAXITER; niter++) { - if (niter) + rayorigin(&sr, SPECULAR, np->rp, sr.rcoef) == 0) { + nstarget = 1; + if (specjitter > 1.5) { /* multiple samples? */ + nstarget = specjitter*np->rp->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, np->rp->ron) >= -FTINY) + continue; + normalize(sr.rdir); /* OK, normalize */ + if (nstaken) /* multi-sampling */ + rayclear(&sr); + rayvalue(&sr); + multcolor(sr.rcol, sr.rcoef); + addcolor(np->rp->rcol, sr.rcol); + ++nstaken; } ndims--; }