--- ray/src/rt/aniso.c 2010/10/10 19:49:17 2.48 +++ ray/src/rt/aniso.c 2012/07/29 19:01:39 2.54 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: aniso.c,v 2.48 2010/10/10 19:49:17 greg Exp $"; +static const char RCSid[] = "$Id: aniso.c,v 2.54 2012/07/29 19:01:39 greg Exp $"; #endif /* * Shading functions for anisotropic materials. @@ -21,14 +21,15 @@ static const char RCSid[] = "$Id: aniso.c,v 2.48 2010/ /* * This routine implements the anisotropic Gaussian - * model described by Ward in Siggraph `92 article. + * model described by Ward in Siggraph `92 article, updated with + * normalization and sampling adjustments due to Geisler-Moroder and Duer. * We orient the surface towards the incoming ray, so a single * surface can be used to represent an infinitely thin object. * * Arguments for MAT_PLASTIC2 and MAT_METAL2 are: * 4+ ux uy uz funcfile [transform...] * 0 - * 6 red grn blu specular-frac. u-facet-slope v-facet-slope + * 6 red grn blu specular-frac. u-rough v-rough * * Real arguments for MAT_TRANS2 are: * 8 red grn blu rspec u-rough v-rough trans tspec @@ -60,7 +61,6 @@ typedef struct { double pdot; /* perturbed dot product */ } ANISODAT; /* anisotropic material data */ -static srcdirf_t diraniso; static void getacoords(RAY *r, ANISODAT *np); static void agaussamp(RAY *r, ANISODAT *np); @@ -68,12 +68,12 @@ static void agaussamp(RAY *r, ANISODAT *np); static void diraniso( /* 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 ANISODAT *np = nnp; + ANISODAT *np = nnp; double ldot; double dtmp, dtmp1, dtmp2; FVECT h; @@ -87,7 +87,7 @@ diraniso( /* compute source contribution */ if (ldot < 0.0 ? np->trans <= FTINY : np->trans >= 1.0-FTINY) return; /* wrong side */ - if (ldot > FTINY && np->rdiff > FTINY) { + if ((ldot > FTINY) & (np->rdiff > FTINY)) { /* * Compute and add diffuse reflected component to returned * color. The diffuse reflected component will always be @@ -111,9 +111,7 @@ diraniso( /* compute source contribution */ au2 += np->u_alpha*np->u_alpha; av2 += np->v_alpha*np->v_alpha; /* half vector */ - h[0] = ldir[0] - np->rp->rdir[0]; - h[1] = ldir[1] - np->rp->rdir[1]; - h[2] = ldir[2] - np->rp->rdir[2]; + VSUB(h, ldir, np->rp->rdir); /* ellipse */ dtmp1 = DOT(np->u, h); dtmp1 *= dtmp1 / au2; @@ -133,7 +131,7 @@ diraniso( /* compute source contribution */ addcolor(cval, ctmp); } } - if (ldot < -FTINY && np->tdiff > FTINY) { + if ((ldot < -FTINY) & (np->tdiff > FTINY)) { /* * Compute diffuse transmission. */ @@ -152,9 +150,7 @@ diraniso( /* compute source contribution */ au2 += np->u_alpha*np->u_alpha; av2 += np->v_alpha*np->v_alpha; /* "half vector" */ - h[0] = ldir[0] - np->prdir[0]; - h[1] = ldir[1] - np->prdir[1]; - h[2] = ldir[2] - np->prdir[2]; + VSUB(h, ldir, np->prdir); dtmp = DOT(h,h); if (dtmp > FTINY*FTINY) { dtmp1 = DOT(h,np->pnorm); @@ -181,15 +177,15 @@ diraniso( /* compute source contribution */ } -extern int +int m_aniso( /* shade ray that hit something anisotropic */ - register OBJREC *m, - register RAY *r + OBJREC *m, + RAY *r ) { ANISODAT nd; COLOR ctmp; - register int i; + int i; /* easy shadow test */ if (r->crtype & SHADOW) return(1); @@ -216,7 +212,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 */ @@ -277,10 +273,9 @@ m_aniso( /* shade ray that hit something anisotropic 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, nd.pnorm); addcolor(r->rcol, ctmp); /* add to returned color */ } @@ -310,24 +305,24 @@ m_aniso( /* shade ray that hit something anisotropic static void getacoords( /* set up coordinate system */ RAY *r, - register ANISODAT *np + ANISODAT *np ) { - register MFUNC *mf; - register int i; + MFUNC *mf; + int i; mf = getfunc(np->mp, 3, 0x7, 1); setfunc(np->mp, r); errno = 0; for (i = 0; i < 3; i++) np->u[i] = evalue(mf->ep[i]); - if (errno == EDOM || errno == ERANGE) { + if ((errno == EDOM) | (errno == ERANGE)) { objerror(np->mp, WARNING, "compute error"); np->specfl |= SP_BADU; return; } - if (mf->f != &unitxf) - multv3(np->u, np->u, mf->f->xfm); + if (mf->fxp != &unitxf) + multv3(np->u, np->u, mf->fxp->xfm); fcross(np->v, np->pnorm, np->u); if (normalize(np->v) == 0.0) { objerror(np->mp, WARNING, "illegal orientation vector"); @@ -341,7 +336,7 @@ getacoords( /* set up coordinate system */ static void agaussamp( /* sample anisotropic Gaussian specular */ RAY *r, - register ANISODAT *np + ANISODAT *np ) { RAY sr; @@ -349,27 +344,29 @@ agaussamp( /* sample anisotropic Gaussian specular */ double rv[2]; double d, sinp, cosp; COLOR scol; - int niter, ns2go; - register int i; + int maxiter, ntrials, nstarget, nstaken; + int i; /* compute reflection */ if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL && rayorigin(&sr, SPECULAR, r, np->scolor) == 0) { - copycolor(scol, np->scolor); - ns2go = 1; + nstarget = 1; if (specjitter > 1.5) { /* multiple samples? */ - ns2go = specjitter*sr.rweight + .5; - if (sr.rweight <= minweight*ns2go) - ns2go = sr.rweight/minweight; - if (ns2go > 1) { - d = 1./ns2go; - scalecolor(scol, d); + 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 - ns2go = 1; + nstarget = 1; } - dimlist[ndims++] = (int)np->mp; - for (niter = ns2go*MAXITER; (ns2go > 0) & (niter > 0); niter--) { - if (specjitter > 1.5) + 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); @@ -392,23 +389,30 @@ 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); - if (d <= np->pdot + FTINY) - continue; VSUM(sr.rdir, r->rdir, h, d); - if (DOT(sr.rdir, r->ron) <= FTINY) + /* sample rejection test */ + if ((d = 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); + if (nstarget > 1) { /* W-G-M-D adjustment */ + if (nstaken) rayclear(&sr); + rayvalue(&sr); + 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); } - rayvalue(&sr); - multcolor(sr.rcol, sr.rcoef); - addcolor(r->rcol, sr.rcol); - --ns2go; + ++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 */ @@ -416,21 +420,23 @@ agaussamp( /* sample anisotropic 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; + nstarget = 1; if (specjitter > 1.5) { /* multiple samples? */ - ns2go = specjitter*sr.rweight + .5; - if (sr.rweight <= minweight*ns2go) - ns2go = sr.rweight/minweight; - if (ns2go > 1) { - d = 1./ns2go; + 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 - ns2go = 1; + nstarget = 1; } - dimlist[ndims++] = (int)np->mp; - for (niter = ns2go*MAXITER; (ns2go > 0) & (niter > 0); niter--) { - if (specjitter > 1.5) + 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); @@ -455,12 +461,12 @@ agaussamp( /* sample anisotropic Gaussian specular */ if (DOT(sr.rdir, r->ron) >= -FTINY) continue; normalize(sr.rdir); /* OK, normalize */ - if (specjitter > 1.5) /* multi-sampling */ + if (nstaken) /* multi-sampling */ rayclear(&sr); rayvalue(&sr); multcolor(sr.rcol, sr.rcoef); addcolor(r->rcol, sr.rcol); - --ns2go; + ++nstaken; } ndims--; }