--- ray/src/rt/aniso.c 2003/03/03 00:10:51 2.36 +++ ray/src/rt/aniso.c 2010/10/10 22:31:45 2.49 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: aniso.c,v 2.36 2003/03/03 00:10:51 greg Exp $"; +static const char RCSid[] = "$Id: aniso.c,v 2.49 2010/10/10 22:31:45 greg Exp $"; #endif /* * Shading functions for anisotropic materials. @@ -8,11 +8,11 @@ static const char RCSid[] = "$Id: aniso.c,v 2.36 2003/ #include "copyright.h" #include "ray.h" - +#include "ambient.h" #include "otypes.h" - +#include "rtotypes.h" +#include "source.h" #include "func.h" - #include "random.h" #ifndef MAXITER @@ -60,17 +60,20 @@ typedef struct { double pdot; /* perturbed dot product */ } ANISODAT; /* anisotropic material data */ -static void getacoords(); -static void agaussamp(); +static srcdirf_t diraniso; +static void getacoords(RAY *r, ANISODAT *np); +static void agaussamp(RAY *r, ANISODAT *np); static void -diraniso(cval, np, ldir, omega) /* compute source contribution */ -COLOR cval; /* returned coefficient */ -register ANISODAT *np; /* material data */ -FVECT ldir; /* light source direction */ -double omega; /* light source size */ +diraniso( /* compute source contribution */ + COLOR cval, /* returned coefficient */ + void *nnp, /* material data */ + FVECT ldir, /* light source direction */ + double omega /* light source size */ +) { + register ANISODAT *np = nnp; double ldot; double dtmp, dtmp1, dtmp2; FVECT h; @@ -91,18 +94,18 @@ double omega; /* light source size */ * modified by the color of the material. */ copycolor(ctmp, np->mcolor); - dtmp = ldot * omega * np->rdiff / PI; + dtmp = ldot * omega * np->rdiff * (1.0/PI); scalecolor(ctmp, dtmp); addcolor(cval, ctmp); } 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) - au2 = av2 = omega/(4.0*PI); + au2 = av2 = omega * (0.25/PI); else au2 = av2 = 0.0; au2 += np->u_alpha*np->u_alpha; @@ -116,15 +119,16 @@ double omega; /* light source size */ 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) * (0.25/PI) - * sqrt(ldot/(np->pdot*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); } @@ -134,7 +138,7 @@ double omega; /* light source size */ * Compute diffuse transmission. */ copycolor(ctmp, np->mcolor); - dtmp = -ldot * omega * np->tdiff / PI; + dtmp = -ldot * omega * np->tdiff * (1.0/PI); scalecolor(ctmp, dtmp); addcolor(cval, ctmp); } @@ -144,7 +148,7 @@ double omega; /* light source size */ * is always modified by material color. */ /* roughness + source */ - au2 = av2 = omega / PI; + au2 = av2 = omega * (1.0/PI); au2 += np->u_alpha*np->u_alpha; av2 += np->v_alpha*np->v_alpha; /* "half vector" */ @@ -164,9 +168,8 @@ double omega; /* light source size */ } } else dtmp = 0.0; - /* gaussian */ - dtmp = exp(-dtmp) * (1.0/PI) - * sqrt(-ldot/(np->pdot*au2*av2)); + /* Gaussian */ + dtmp = exp(-dtmp) * (1.0/PI) * sqrt(-ldot/(np->pdot*au2*av2)); /* worth using? */ if (dtmp > FTINY) { copycolor(ctmp, np->mcolor); @@ -178,10 +181,11 @@ double omega; /* light source size */ } -int -m_aniso(m, r) /* shade ray that hit something anisotropic */ -register OBJREC *m; -register RAY *r; +extern int +m_aniso( /* shade ray that hit something anisotropic */ + register OBJREC *m, + register RAY *r +) { ANISODAT nd; COLOR ctmp; @@ -212,7 +216,7 @@ register RAY *r; 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 */ @@ -232,11 +236,9 @@ register RAY *r; 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) { @@ -274,12 +276,12 @@ register RAY *r; 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 */ @@ -289,12 +291,12 @@ register RAY *r; 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); } @@ -306,9 +308,10 @@ register RAY *r; static void -getacoords(r, np) /* set up coordinate system */ -RAY *r; -register ANISODAT *np; +getacoords( /* set up coordinate system */ + RAY *r, + register ANISODAT *np +) { register MFUNC *mf; register int i; @@ -318,7 +321,7 @@ register ANISODAT *np; errno = 0; for (i = 0; i < 3; i++) np->u[i] = evalue(mf->ep[i]); - if (errno) { + if (errno == EDOM || errno == ERANGE) { objerror(np->mp, WARNING, "compute error"); np->specfl |= SP_BADU; return; @@ -336,22 +339,37 @@ register ANISODAT *np; static void -agaussamp(r, np) /* sample anisotropic gaussian specular */ -RAY *r; -register ANISODAT *np; +agaussamp( /* sample anisotropic Gaussian specular */ + RAY *r, + register ANISODAT *np +) { RAY sr; FVECT h; double rv[2]; double d, sinp, cosp; - int niter; + COLOR scol; + int niter, ns2go; register int i; /* 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); @@ -359,10 +377,11 @@ register ANISODAT *np; 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 @@ -373,23 +392,45 @@ register ANISODAT *np; 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) { - 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); /* 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) { + 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); @@ -397,10 +438,11 @@ register ANISODAT *np; 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 @@ -410,14 +452,15 @@ register ANISODAT *np; 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 (specjitter > 1.5) /* multi-sampling */ + rayclear(&sr); + rayvalue(&sr); + multcolor(sr.rcol, sr.rcoef); + addcolor(r->rcol, sr.rcol); + --ns2go; } ndims--; }