--- ray/src/rt/aniso.c 1992/01/15 11:41:47 2.6 +++ ray/src/rt/aniso.c 1995/11/22 09:27:51 2.31 @@ -19,9 +19,13 @@ static char SCCSid[] = "$SunId$ LBL"; extern double specthresh; /* specular sampling threshold */ extern double specjitter; /* specular sampling jitter */ +extern int backvis; /* back faces visible? */ + +static agaussamp(), getacoords(); + /* - * This anisotropic reflection model uses a variant on the - * exponential Gaussian used in normal.c. + * This routine implements the anisotropic Gaussian + * model described by Ward in Siggraph `92 article. * We orient the surface towards the incoming ray, so a single * surface can be used to represent an infinitely thin object. * @@ -34,16 +38,13 @@ extern double specjitter; /* specular sampling jitte * 8 red grn blu rspec u-rough v-rough trans tspec */ -#define BSPEC(m) (6.0) /* specularity parameter b */ - /* specularity flags */ #define SP_REFL 01 /* has reflected specular component */ #define SP_TRAN 02 /* has transmitted specular */ -#define SP_PURE 010 /* purely specular (zero roughness) */ -#define SP_FLAT 020 /* reflecting surface is flat */ -#define SP_RBLT 040 /* reflection below sample threshold */ -#define SP_TBLT 0100 /* transmission below threshold */ -#define SP_BADU 0200 /* bad u direction calculation */ +#define SP_FLAT 04 /* reflecting surface is flat */ +#define SP_RBLT 010 /* reflection below sample threshold */ +#define SP_TBLT 020 /* transmission below threshold */ +#define SP_BADU 040 /* bad u direction calculation */ typedef struct { OBJREC *mp; /* material pointer */ @@ -71,7 +72,7 @@ FVECT ldir; /* light source direction */ double omega; /* light source size */ { double ldot; - double dtmp, dtmp2; + double dtmp, dtmp1, dtmp2; FVECT h; double au2, av2; COLOR ctmp; @@ -94,7 +95,7 @@ double omega; /* light source size */ scalecolor(ctmp, dtmp); addcolor(cval, ctmp); } - if (ldot > FTINY && (np->specfl&(SP_REFL|SP_PURE|SP_BADU)) == SP_REFL) { + if (ldot > FTINY && (np->specfl&(SP_REFL|SP_BADU)) == SP_REFL) { /* * Compute specular reflection coefficient using * anisotropic gaussian distribution model. @@ -104,25 +105,26 @@ double omega; /* light source size */ au2 = av2 = omega/(4.0*PI); else au2 = av2 = 0.0; - au2 += np->u_alpha * np->u_alpha; - av2 += np->v_alpha * np->v_alpha; + 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]; - normalize(h); /* ellipse */ - dtmp = DOT(np->u, h); - dtmp *= dtmp / au2; + dtmp1 = DOT(np->u, h); + dtmp1 *= dtmp1 / au2; dtmp2 = DOT(np->v, h); dtmp2 *= dtmp2 / av2; /* gaussian */ - dtmp = (dtmp + dtmp2) / (1.0 + DOT(np->pnorm, h)); - dtmp = exp(-2.0*dtmp) / (4.0*PI * sqrt(au2*av2)); + dtmp = DOT(np->pnorm, h); + dtmp = (dtmp1 + dtmp2) / (dtmp*dtmp); + dtmp = exp(-dtmp) * (0.25/PI) + * sqrt(ldot/(np->pdot*au2*av2)); /* worth using? */ if (dtmp > FTINY) { copycolor(ctmp, np->scolor); - dtmp *= omega / np->pdot; + dtmp *= omega; scalecolor(ctmp, dtmp); addcolor(cval, ctmp); } @@ -136,18 +138,39 @@ double omega; /* light source size */ scalecolor(ctmp, dtmp); addcolor(cval, ctmp); } - if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_PURE|SP_BADU)) == SP_TRAN) { + if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_BADU)) == SP_TRAN) { /* * Compute specular transmission. Specular transmission * is always modified by material color. */ /* roughness + source */ + au2 = av2 = omega / PI; + 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]; + dtmp = DOT(h,h); + if (dtmp > FTINY*FTINY) { + dtmp1 = DOT(h,np->pnorm); + dtmp = 1.0 - dtmp1*dtmp1/dtmp; + if (dtmp > FTINY*FTINY) { + dtmp1 = DOT(h,np->u); + dtmp1 *= dtmp1 / au2; + dtmp2 = DOT(h,np->v); + dtmp2 *= dtmp2 / av2; + dtmp = (dtmp1 + dtmp2) / dtmp; + } + } else + dtmp = 0.0; /* gaussian */ - dtmp = 0.0; + dtmp = exp(-dtmp) * (1.0/PI) + * sqrt(-ldot/(np->pdot*au2*av2)); /* worth using? */ if (dtmp > FTINY) { copycolor(ctmp, np->mcolor); - dtmp *= np->tspec * omega / np->pdot; + dtmp *= np->tspec * omega; scalecolor(ctmp, dtmp); addcolor(cval, ctmp); } @@ -160,13 +183,11 @@ register OBJREC *m; register RAY *r; { ANISODAT nd; - double transtest, transdist; - double dtmp; COLOR ctmp; register int i; /* easy shadow test */ - if (r->crtype & SHADOW && m->otype != MAT_TRANS2) - return; + if (r->crtype & SHADOW) + return(1); if (m->oargs.nfargs != (m->otype == MAT_TRANS2 ? 8 : 6)) objerror(m, USER, "bad number of real arguments"); @@ -180,18 +201,22 @@ 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) - nd.specfl |= SP_PURE; - /* reorient if necessary */ - if (r->rod < 0.0) - flipsurface(r); + if (nd.u_alpha < FTINY || nd.v_alpha <= FTINY) + objerror(m, USER, "roughness too small"); + /* check for back side */ + if (r->rod < 0.0) { + if (!backvis && m->otype != MAT_TRANS2) { + raytrans(r); + return(1); + } + flipsurface(r); /* reorient if backvis */ + } /* get modifiers */ raytexture(r, m->omod); nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ if (nd.pdot < .001) nd.pdot = .001; /* non-zero for diraniso() */ multcolor(nd.mcolor, r->pcol); /* modify material color */ - transtest = 0; /* get specular component */ if ((nd.rspec = m->oargs.farg[3]) > FTINY) { nd.specfl |= SP_REFL; @@ -201,16 +226,8 @@ register RAY *r; else setcolor(nd.scolor, 1.0, 1.0, 1.0); scalecolor(nd.scolor, nd.rspec); - /* improved model */ - dtmp = exp(-BSPEC(m)*nd.pdot); - for (i = 0; i < 3; i++) - colval(nd.scolor,i) += (1.0-colval(nd.scolor,i))*dtmp; - nd.rspec += (1.0-nd.rspec)*dtmp; /* check threshold */ - if (specthresh > FTINY && - ((specthresh >= 1.-FTINY || - specthresh + (.1 - .2*urand(8199+samplendx)) - > nd.rspec))) + if (specthresh >= nd.rspec-FTINY) nd.specfl |= SP_RBLT; /* compute refl. direction */ for (i = 0; i < 3; i++) @@ -218,39 +235,22 @@ register RAY *r; 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]; - - if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) { - RAY lr; - if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) { - VCOPY(lr.rdir, nd.vrefl); - rayvalue(&lr); - multcolor(lr.rcol, nd.scolor); - addcolor(r->rcol, lr.rcol); - } - } } /* compute transmission */ - if (m->otype == MAT_TRANS) { + if (m->otype == MAT_TRANS2) { nd.trans = m->oargs.farg[6]*(1.0 - nd.rspec); nd.tspec = nd.trans * m->oargs.farg[7]; nd.tdiff = nd.trans - nd.tspec; if (nd.tspec > FTINY) { nd.specfl |= SP_TRAN; /* check threshold */ - if (specthresh > FTINY && - ((specthresh >= 1.-FTINY || - specthresh + - (.1 - .2*urand(7241+samplendx)) - > nd.tspec))) + if (specthresh >= nd.tspec-FTINY) nd.specfl |= SP_TBLT; - if (r->crtype & SHADOW || - DOT(r->pert,r->pert) <= FTINY*FTINY) { + if (DOT(r->pert,r->pert) <= FTINY*FTINY) { VCOPY(nd.prdir, r->rdir); - transtest = 2; } else { for (i = 0; i < 3; i++) /* perturb */ - nd.prdir[i] = r->rdir[i] - - .75*r->pert[i]; + nd.prdir[i] = r->rdir[i] - r->pert[i]; if (DOT(nd.prdir, r->ron) < -FTINY) normalize(nd.prdir); /* OK */ else @@ -259,38 +259,20 @@ register RAY *r; } } else nd.tdiff = nd.tspec = nd.trans = 0.0; - /* transmitted ray */ - if ((nd.specfl&(SP_TRAN|SP_PURE)) == (SP_TRAN|SP_PURE)) { - RAY lr; - if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) { - VCOPY(lr.rdir, nd.prdir); - rayvalue(&lr); - scalecolor(lr.rcol, nd.tspec); - multcolor(lr.rcol, nd.mcolor); /* modified by color */ - addcolor(r->rcol, lr.rcol); - transtest *= bright(lr.rcol); - transdist = r->rot + lr.rt; - } - } - if (r->crtype & SHADOW) /* the rest is shadow */ - return; /* diffuse reflection */ nd.rdiff = 1.0 - nd.trans - nd.rspec; - if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY) - return; /* 100% pure specular */ - - if (r->ro->otype == OBJ_FACE || r->ro->otype == OBJ_RING) + if (r->ro != NULL && isflat(r->ro->otype)) nd.specfl |= SP_FLAT; getacoords(r, &nd); /* set up coordinates */ - if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & (SP_PURE|SP_BADU))) + if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_BADU)) agaussamp(r, &nd); if (nd.rdiff > FTINY) { /* ambient from this side */ - ambient(ctmp, r); + ambient(ctmp, r, nd.pnorm); if (nd.specfl & SP_RBLT) scalecolor(ctmp, 1.0-nd.trans); else @@ -299,8 +281,13 @@ register RAY *r; addcolor(r->rcol, ctmp); /* add to returned color */ } if (nd.tdiff > FTINY) { /* ambient from other side */ + FVECT bnorm; + flipsurface(r); - ambient(ctmp, r); + bnorm[0] = -nd.pnorm[0]; + bnorm[1] = -nd.pnorm[1]; + bnorm[2] = -nd.pnorm[2]; + ambient(ctmp, r, bnorm); if (nd.specfl & SP_TBLT) scalecolor(ctmp, nd.trans); else @@ -311,9 +298,8 @@ register RAY *r; } /* add direct component */ direct(r, diraniso, &nd); - /* check distance */ - if (transtest > bright(r->rcol)) - r->rt = transdist; + + return(1); } @@ -335,7 +321,8 @@ register ANISODAT *np; np->specfl |= SP_BADU; return; } - multv3(np->u, np->u, mf->f->xfm); + if (mf->f != &unitxf) + multv3(np->u, np->u, mf->f->xfm); fcross(np->v, np->pnorm, np->u); if (normalize(np->v) == 0.0) { objerror(np->mp, WARNING, "illegal orientation vector"); @@ -363,8 +350,8 @@ register ANISODAT *np; d = urand(ilhash(dimlist,ndims)+samplendx); multisamp(rv, 2, d); d = 2.0*PI * rv[0]; - cosp = np->u_alpha * cos(d); - sinp = np->v_alpha * sin(d); + cosp = cos(d) * np->u_alpha; + sinp = sin(d) * np->v_alpha; d = sqrt(cosp*cosp + sinp*sinp); cosp /= d; sinp /= d; @@ -389,4 +376,35 @@ register ANISODAT *np; ndims--; } /* compute transmission */ + if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN && + rayorigin(&sr, r, SPECULAR, np->tspec) == 0) { + dimlist[ndims++] = (int)np->mp; + d = urand(ilhash(dimlist,ndims)+1823+samplendx); + multisamp(rv, 2, d); + d = 2.0*PI * rv[0]; + cosp = cos(d) * np->u_alpha; + sinp = sin(d) * np->v_alpha; + d = sqrt(cosp*cosp + sinp*sinp); + cosp /= d; + sinp /= d; + rv[1] = 1.0 - specjitter*rv[1]; + if (rv[1] <= FTINY) + d = 1.0; + else + d = sqrt(-log(rv[1]) / + (cosp*cosp/(np->u_alpha*np->u_alpha) + + sinp*sinp/(np->v_alpha*np->u_alpha))); + 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 */ + else + VCOPY(sr.rdir, np->prdir); /* else no jitter */ + rayvalue(&sr); + scalecolor(sr.rcol, np->tspec); + multcolor(sr.rcol, np->mcolor); /* modify by color */ + addcolor(r->rcol, sr.rcol); + ndims--; + } }