--- ray/src/rt/aniso.c 2004/09/20 17:32:04 2.42 +++ ray/src/rt/aniso.c 2011/02/18 00:40:25 2.51 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: aniso.c,v 2.42 2004/09/20 17:32:04 greg Exp $"; +static const char RCSid[] = "$Id: aniso.c,v 2.51 2011/02/18 00:40:25 greg Exp $"; #endif /* * Shading functions for anisotropic materials. @@ -101,7 +101,7 @@ diraniso( /* compute source contribution */ if (ldot > FTINY && (np->specfl&(SP_REFL|SP_BADU)) == SP_REFL) { /* * Compute specular reflection coefficient using - * anisotropic gaussian distribution model. + * anisotropic Gaussian distribution model. */ /* add source width if flat */ if (np->specfl & SP_FLAT) @@ -119,14 +119,16 @@ diraniso( /* compute source contribution */ dtmp1 *= dtmp1 / au2; dtmp2 = DOT(np->v, h); dtmp2 *= dtmp2 / av2; - /* gaussian */ + /* new W-G-M-D model */ dtmp = DOT(np->pnorm, h); - dtmp = (dtmp1 + dtmp2) / (dtmp*dtmp); - dtmp = exp(-dtmp) / (4.0*PI * np->pdot * sqrt(au2*av2)); + dtmp *= dtmp; + dtmp1 = (dtmp1 + dtmp2) / dtmp; + dtmp = exp(-dtmp1) * DOT(h,h) / + (PI * dtmp*dtmp * sqrt(au2*av2)); /* worth using? */ if (dtmp > FTINY) { copycolor(ctmp, np->scolor); - dtmp *= omega; + dtmp *= ldot * omega; scalecolor(ctmp, dtmp); addcolor(cval, ctmp); } @@ -166,8 +168,8 @@ diraniso( /* compute source contribution */ } } else dtmp = 0.0; - /* gaussian */ - dtmp = exp(-dtmp) / (PI * np->pdot * sqrt(au2*av2)); + /* Gaussian */ + dtmp = exp(-dtmp) * (1.0/PI) * sqrt(-ldot/(np->pdot*au2*av2)); /* worth using? */ if (dtmp > FTINY) { copycolor(ctmp, np->mcolor); @@ -214,7 +216,7 @@ m_aniso( /* shade ray that hit something anisotropic nd.specfl = 0; nd.u_alpha = m->oargs.farg[4]; nd.v_alpha = m->oargs.farg[5]; - if (nd.u_alpha < FTINY || nd.v_alpha <= FTINY) + if (nd.u_alpha <= FTINY || nd.v_alpha <= FTINY) objerror(m, USER, "roughness too small"); nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ @@ -234,11 +236,9 @@ m_aniso( /* shade ray that hit something anisotropic if (specthresh >= nd.rspec-FTINY) nd.specfl |= SP_RBLT; /* compute refl. direction */ - for (i = 0; i < 3; i++) - nd.vrefl[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i]; + VSUM(nd.vrefl, r->rdir, nd.pnorm, 2.0*nd.pdot); if (DOT(nd.vrefl, r->ron) <= FTINY) /* penetration? */ - 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.0*r->rod); } /* compute transmission */ if (m->otype == MAT_TRANS2) { @@ -276,12 +276,12 @@ m_aniso( /* shade ray that hit something anisotropic agaussamp(r, &nd); if (nd.rdiff > FTINY) { /* ambient from this side */ - ambient(ctmp, r, nd.pnorm); + 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, nd.pnorm); addcolor(r->rcol, ctmp); /* add to returned color */ } if (nd.tdiff > FTINY) { /* ambient from other side */ @@ -291,12 +291,12 @@ m_aniso( /* shade ray that hit something anisotropic bnorm[0] = -nd.pnorm[0]; bnorm[1] = -nd.pnorm[1]; bnorm[2] = -nd.pnorm[2]; - ambient(ctmp, r, bnorm); + copycolor(ctmp, nd.mcolor); /* modified by color */ if (nd.specfl & SP_TBLT) scalecolor(ctmp, nd.trans); else scalecolor(ctmp, nd.tdiff); - multcolor(ctmp, nd.mcolor); /* modified by color */ + multambient(ctmp, r, bnorm); addcolor(r->rcol, ctmp); flipsurface(r); } @@ -339,7 +339,7 @@ getacoords( /* set up coordinate system */ static void -agaussamp( /* sample anisotropic gaussian specular */ +agaussamp( /* sample anisotropic Gaussian specular */ RAY *r, register ANISODAT *np ) @@ -348,14 +348,30 @@ agaussamp( /* sample anisotropic gaussian specular */ FVECT h; double rv[2]; double d, sinp, cosp; - int niter; + COLOR scol; + int maxiter, ntrials, nstarget, nstaken; register int i; /* 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); @@ -363,10 +379,11 @@ agaussamp( /* sample anisotropic gaussian specular */ d = 2.0*PI * rv[0]; cosp = tcos(d) * np->u_alpha; sinp = tsin(d) * np->v_alpha; - d = sqrt(cosp*cosp + sinp*sinp); - cosp /= d; - sinp /= d; - rv[1] = 1.0 - specjitter*rv[1]; + d = 1./sqrt(cosp*cosp + sinp*sinp); + cosp *= d; + sinp *= d; + if ((0. <= specjitter) & (specjitter < 1.)) + rv[1] = 1.0 - specjitter*rv[1]; if (rv[1] <= FTINY) d = 1.0; else @@ -377,23 +394,54 @@ agaussamp( /* sample anisotropic gaussian specular */ h[i] = np->pnorm[i] + d*(cosp*np->u[i] + sinp*np->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); /* modify by material 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); @@ -401,10 +449,11 @@ agaussamp( /* sample anisotropic gaussian specular */ d = 2.0*PI * rv[0]; cosp = tcos(d) * np->u_alpha; sinp = tsin(d) * np->v_alpha; - d = sqrt(cosp*cosp + sinp*sinp); - cosp /= d; - sinp /= d; - rv[1] = 1.0 - specjitter*rv[1]; + d = 1./sqrt(cosp*cosp + sinp*sinp); + cosp *= d; + sinp *= d; + if ((0. <= specjitter) & (specjitter < 1.)) + rv[1] = 1.0 - specjitter*rv[1]; if (rv[1] <= FTINY) d = 1.0; else @@ -414,14 +463,15 @@ agaussamp( /* sample anisotropic gaussian specular */ for (i = 0; i < 3; i++) sr.rdir[i] = np->prdir[i] + d*(cosp*np->u[i] + sinp*np->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); /* modify */ - addcolor(r->rcol, sr.rcol); - break; - } + 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--; }