--- ray/src/rt/aniso.c 1992/01/15 16:59:55 2.7 +++ ray/src/rt/aniso.c 1992/05/15 13:07:58 2.16 @@ -39,11 +39,10 @@ extern double specjitter; /* specular sampling jitte /* 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 */ @@ -54,8 +53,8 @@ typedef struct { FVECT vrefl; /* vector in reflected direction */ FVECT prdir; /* vector in transmitted direction */ FVECT u, v; /* u and v vectors orienting anisotropy */ - double u_alpha; /* u roughness */ - double v_alpha; /* v roughness */ + double u_alpha2; /* u roughness squared */ + double v_alpha2; /* v roughness squared */ double rdiff, rspec; /* reflected specular, diffuse */ double trans; /* transmissivity */ double tdiff, tspec; /* transmitted specular, diffuse */ @@ -71,7 +70,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 +93,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 +103,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_alpha2; + av2 += np->v_alpha2; /* 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 = (dtmp1 + dtmp2) / (1.0 + DOT(np->pnorm, h)); + dtmp = exp(-2.0*dtmp) * 1.0/(4.0*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 +136,37 @@ 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 += .25 * np->u_alpha2; + av2 += .25 * np->v_alpha2; + /* "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,np->pnorm); + dtmp = DOT(h,h) - dtmp*dtmp; + if (dtmp > FTINY*FTINY) { + dtmp1 = DOT(h,np->u); + dtmp1 = dtmp1*dtmp1 / (au2*dtmp); + dtmp2 = DOT(h,np->v); + dtmp2 = dtmp2*dtmp2 / (av2*dtmp); + dtmp = 2. - 2.*DOT(ldir,np->prdir); + dtmp *= dtmp1 + dtmp2; + } else + dtmp = 0.0; /* gaussian */ - dtmp = 0.0; + dtmp = exp(-dtmp) * 1.0/(4.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,12 +179,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) + if (r->crtype & SHADOW) return; if (m->oargs.nfargs != (m->otype == MAT_TRANS2 ? 8 : 6)) @@ -178,10 +196,12 @@ register RAY *r; m->oargs.farg[2]); /* get roughness */ 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; + nd.u_alpha2 = m->oargs.farg[4]; + nd.u_alpha2 *= nd.u_alpha2; + nd.v_alpha2 = m->oargs.farg[5]; + nd.v_alpha2 *= nd.v_alpha2; + if (nd.u_alpha2 < FTINY*FTINY || nd.v_alpha2 <= FTINY*FTINY) + objerror(m, USER, "roughness too small"); /* reorient if necessary */ if (r->rod < 0.0) flipsurface(r); @@ -191,7 +211,6 @@ register RAY *r; 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; @@ -208,9 +227,8 @@ register RAY *r; nd.rspec += (1.0-nd.rspec)*dtmp; /* check threshold */ if (specthresh > FTINY && - ((specthresh >= 1.-FTINY || - specthresh + (.1 - .2*urand(8199+samplendx)) - > nd.rspec))) + (specthresh >= 1.-FTINY || + specthresh + .05 - .1*frandom() > nd.rspec)) nd.specfl |= SP_RBLT; /* compute refl. direction */ for (i = 0; i < 3; i++) @@ -218,19 +236,9 @@ 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; @@ -238,15 +246,11 @@ register RAY *r; nd.specfl |= SP_TRAN; /* check threshold */ if (specthresh > FTINY && - ((specthresh >= 1.-FTINY || - specthresh + - (.1 - .2*urand(7241+samplendx)) - > nd.tspec))) + (specthresh >= 1.-FTINY || + specthresh + .05 - .1*frandom() > nd.tspec)) 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] - @@ -259,34 +263,17 @@ 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 && (r->ro->otype == OBJ_FACE || + r->ro->otype == OBJ_RING)) 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 */ @@ -311,9 +298,6 @@ register RAY *r; } /* add direct component */ direct(r, diraniso, &nd); - /* check distance */ - if (transtest > bright(r->rcol)) - r->rt = transdist; } @@ -335,7 +319,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,9 +348,9 @@ 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); - d = sqrt(cosp*cosp + sinp*sinp); + cosp = cos(d); + sinp = sin(d); + d = sqrt(np->u_alpha2*cosp*cosp + np->v_alpha2*sinp*sinp); cosp /= d; sinp /= d; rv[1] = 1.0 - specjitter*rv[1]; @@ -373,8 +358,8 @@ register ANISODAT *np; d = 1.0; else d = sqrt(-log(rv[1]) / - (cosp*cosp/(np->u_alpha*np->u_alpha) + - sinp*sinp/(np->v_alpha*np->v_alpha))); + (cosp*cosp/np->u_alpha2 + + sinp*sinp/np->v_alpha2)); for (i = 0; i < 3; i++) h[i] = np->pnorm[i] + d*(cosp*np->u[i] + sinp*np->v[i]); @@ -402,8 +387,8 @@ register ANISODAT *np; d = 1.0; else d = sqrt(-log(rv[1]) / - (cosp*cosp*4./(np->u_alpha*np->u_alpha) + - sinp*sinp*4./(np->v_alpha*np->v_alpha))); + (cosp*cosp*4./np->u_alpha2 + + sinp*sinp*4./np->v_alpha2)); for (i = 0; i < 3; i++) sr.rdir[i] = np->prdir[i] + d*(cosp*np->u[i] + sinp*np->v[i]); @@ -412,7 +397,8 @@ register ANISODAT *np; else VCOPY(sr.rdir, np->prdir); /* else no jitter */ rayvalue(&sr); - multcolor(sr.rcol, np->scolor); + scalecolor(sr.rcol, np->tspec); + multcolor(sr.rcol, np->mcolor); /* modify by color */ addcolor(r->rcol, sr.rcol); ndims--; }