--- ray/src/rt/aniso.c 1992/01/14 16:16:48 2.4 +++ ray/src/rt/aniso.c 1992/03/03 16:20:00 2.11 @@ -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 */ @@ -51,6 +50,7 @@ typedef struct { short specfl; /* specularity flags, defined above */ COLOR mcolor; /* color of this material */ COLOR scolor; /* color of specular component */ + 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 */ @@ -93,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. @@ -135,7 +135,7 @@ 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. @@ -159,12 +159,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)) @@ -179,8 +178,8 @@ 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; + if (nd.u_alpha < 1e-6 || nd.v_alpha <= 1e-6) + objerror(m, USER, "roughness too small"); /* reorient if necessary */ if (r->rod < 0.0) flipsurface(r); @@ -190,7 +189,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; @@ -206,20 +204,16 @@ register RAY *r; colval(nd.scolor,i) += (1.0-colval(nd.scolor,i))*dtmp; nd.rspec += (1.0-nd.rspec)*dtmp; /* check threshold */ - if (nd.rspec <= specthresh+FTINY) + if (specthresh > FTINY && + ((specthresh >= 1.-FTINY || + specthresh + (.05 - .1*frandom()) > nd.rspec))) nd.specfl |= SP_RBLT; - - if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) { - RAY lr; - if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) { - for (i = 0; i < 3; i++) - lr.rdir[i] = r->rdir[i] + - 2.0*nd.pdot*nd.pnorm[i]; - rayvalue(&lr); - multcolor(lr.rcol, nd.scolor); - addcolor(r->rcol, lr.rcol); - } - } + /* compute refl. direction */ + for (i = 0; i < 3; i++) + nd.vrefl[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i]; + 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]; } /* compute transmission */ if (m->otype == MAT_TRANS) { @@ -229,49 +223,36 @@ register RAY *r; if (nd.tspec > FTINY) { nd.specfl |= SP_TRAN; /* check threshold */ - if (nd.tspec <= specthresh+FTINY) + if (specthresh > FTINY && + ((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] - - .75*r->pert[i]; - normalize(nd.prdir); + 0.5*r->pert[i]; + if (DOT(nd.prdir, r->ron) < -FTINY) + normalize(nd.prdir); /* OK */ + else + VCOPY(nd.prdir, r->rdir); } } } 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 */ @@ -296,9 +277,6 @@ register RAY *r; } /* add direct component */ direct(r, diraniso, &nd); - /* check distance */ - if (transtest > bright(r->rcol)) - r->rt = transdist; } @@ -340,43 +318,66 @@ register ANISODAT *np; FVECT h; double rv[2]; double d, sinp, cosp; - int ntries; 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 (ntries = 0; ntries < 10; ntries++) { - dimlist[ndims] = ntries * 3601; - d = urand(ilhash(dimlist,ndims+1)+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 /= 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->v_alpha))); - for (i = 0; i < 3; i++) - 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; - } - } + 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 /= 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->v_alpha))); + for (i = 0; i < 3; i++) + 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) /* penetration? */ + VCOPY(sr.rdir, np->vrefl); /* jitter no good */ + rayvalue(&sr); + multcolor(sr.rcol, np->scolor); + addcolor(r->rcol, sr.rcol); 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); + sinp = sin(d); + rv[1] = 1.0 - specjitter*rv[1]; + if (rv[1] <= FTINY) + 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))); + 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--; + } }