--- ray/src/rt/normal.c 1992/05/15 13:07:55 2.18 +++ ray/src/rt/normal.c 1996/04/24 15:47:27 2.34 @@ -1,4 +1,4 @@ -/* Copyright (c) 1992 Regents of the University of California */ +/* Copyright (c) 1996 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; @@ -23,11 +23,17 @@ static char SCCSid[] = "$SunId$ LBL"; extern double specthresh; /* specular sampling threshold */ extern double specjitter; /* specular sampling jitter */ +extern int backvis; /* back faces visible? */ + +#ifndef MAXITER +#define MAXITER 10 /* maximum # specular ray attempts */ +#endif + +static gaussamp(); + /* - * This routine uses portions of the reflection - * model described by Cook and Torrance. - * The computation of specular components has been simplified by - * numerous approximations and ommisions to improve speed. + * This routine implements the isotropic 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. * @@ -38,8 +44,6 @@ extern double specjitter; /* specular sampling jitte * red grn blu rspec 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 */ @@ -104,12 +108,13 @@ double omega; /* light source size */ /* + source if flat */ if (np->specfl & SP_FLAT) dtmp += omega/(4.0*PI); - /* delta */ + /* half vector */ vtmp[0] = ldir[0] - np->rp->rdir[0]; vtmp[1] = ldir[1] - np->rp->rdir[1]; vtmp[2] = ldir[2] - np->rp->rdir[2]; d2 = DOT(vtmp, np->pnorm); - d2 = 2.0 - 2.0*d2/sqrt(DOT(vtmp,vtmp)); + d2 *= d2; + d2 = (DOT(vtmp,vtmp) - d2) / d2; /* gaussian */ dtmp = exp(-d2/dtmp)/(4.*PI*dtmp); /* worth using? */ @@ -135,9 +140,9 @@ double omega; /* light source size */ * is always modified by material color. */ /* roughness + source */ - dtmp = np->alpha2/4.0 + omega/PI; + dtmp = np->alpha2 + omega/PI; /* gaussian */ - dtmp = exp((2.*DOT(np->prdir,ldir)-2.)/dtmp)/(4.*PI*dtmp); + dtmp = exp((2.*DOT(np->prdir,ldir)-2.)/dtmp)/(PI*dtmp); /* worth using? */ if (dtmp > FTINY) { copycolor(ctmp, np->mcolor); @@ -155,15 +160,25 @@ register RAY *r; { NORMDAT nd; double transtest, transdist; - double dtmp; + double mirtest, mirdist; + int hastexture; + double d; COLOR ctmp; register int i; /* easy shadow test */ if (r->crtype & SHADOW && m->otype != MAT_TRANS) - return; + return(1); if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5)) objerror(m, USER, "bad number of arguments"); + /* check for back side */ + if (r->rod < 0.0) { + if (!backvis && m->otype != MAT_TRANS) { + raytrans(r); + return(1); + } + flipsurface(r); /* reorient if backvis */ + } nd.mp = m; nd.rp = r; /* get material color */ @@ -175,53 +190,22 @@ register RAY *r; nd.alpha2 = m->oargs.farg[4]; if ((nd.alpha2 *= nd.alpha2) <= FTINY) nd.specfl |= SP_PURE; - /* reorient if necessary */ - if (r->rod < 0.0) - flipsurface(r); + if (r->ro != NULL && isflat(r->ro->otype)) + nd.specfl |= SP_FLAT; /* get modifiers */ raytexture(r, m->omod); - nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ + if (hastexture = DOT(r->pert,r->pert) > FTINY*FTINY) + nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ + else { + VCOPY(nd.pnorm, r->ron); + nd.pdot = r->rod; + } if (nd.pdot < .001) nd.pdot = .001; /* non-zero for dirnorm() */ 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; - /* compute specular color */ - if (m->otype == MAT_METAL) - copycolor(nd.scolor, nd.mcolor); - 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 (!(nd.specfl & SP_PURE) && - specthresh > FTINY && - (specthresh >= 1.-FTINY || - specthresh + .05 - .1*frandom() > nd.rspec)) - nd.specfl |= SP_RBLT; - /* compute reflected ray */ - 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]; - - 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); - } - } - } + mirtest = transtest = 0; + mirdist = transdist = r->rot; + nd.rspec = m->oargs.farg[3]; /* compute transmission */ if (m->otype == MAT_TRANS) { nd.trans = m->oargs.farg[5]*(1.0 - nd.rspec); @@ -230,18 +214,15 @@ register RAY *r; if (nd.tspec > FTINY) { nd.specfl |= SP_TRAN; /* check threshold */ - if (!(nd.specfl & SP_PURE) && specthresh > FTINY && - (specthresh >= 1.-FTINY || - specthresh + .05 - .1*frandom() > nd.tspec)) + if (!(nd.specfl & SP_PURE) && + specthresh >= nd.tspec-FTINY) nd.specfl |= SP_TBLT; - if (r->crtype & SHADOW || - DOT(r->pert,r->pert) <= FTINY*FTINY) { + if (!hastexture || r->crtype & SHADOW) { VCOPY(nd.prdir, r->rdir); transtest = 2; } else { for (i = 0; i < 3; i++) /* perturb */ - nd.prdir[i] = r->rdir[i] - - 0.5*r->pert[i]; + nd.prdir[i] = r->rdir[i] - r->pert[i]; if (DOT(nd.prdir, r->ron) < -FTINY) normalize(nd.prdir); /* OK */ else @@ -251,7 +232,7 @@ 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)) { + if (nd.specfl&SP_TRAN && (nd.specfl&SP_PURE || r->crtype&SHADOW)) { RAY lr; if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) { VCOPY(lr.rdir, nd.prdir); @@ -265,23 +246,55 @@ register RAY *r; } else transtest = 0; - if (r->crtype & SHADOW) /* the rest is shadow */ - return; + if (r->crtype & SHADOW) { /* the rest is shadow */ + r->rt = transdist; + return(1); + } + /* get specular reflection */ + if (nd.rspec > FTINY) { + nd.specfl |= SP_REFL; + /* compute specular color */ + if (m->otype == MAT_METAL) + copycolor(nd.scolor, nd.mcolor); + else + setcolor(nd.scolor, 1.0, 1.0, 1.0); + scalecolor(nd.scolor, nd.rspec); + /* check threshold */ + if (!(nd.specfl & SP_PURE) && specthresh >= nd.rspec-FTINY) + nd.specfl |= SP_RBLT; + /* compute reflected ray */ + for (i = 0; i < 3; i++) + nd.vrefl[i] = r->rdir[i] + 2.*nd.pdot*nd.pnorm[i]; + /* penetration? */ + if (hastexture && DOT(nd.vrefl, r->ron) <= FTINY) + 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); + if (!hastexture && nd.specfl & SP_FLAT) { + mirtest = 2.*bright(lr.rcol); + mirdist = r->rot + lr.rt; + } + } + } + } /* 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 */ + return(1); /* 100% pure specular */ - if (r->ro != NULL && (r->ro->otype == OBJ_FACE || - r->ro->otype == OBJ_RING)) - nd.specfl |= SP_FLAT; - if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_PURE)) gaussamp(r, &nd); if (nd.rdiff > FTINY) { /* ambient from this side */ - ambient(ctmp, r); + ambient(ctmp, r, hastexture?nd.pnorm:r->ron); if (nd.specfl & SP_RBLT) scalecolor(ctmp, 1.0-nd.trans); else @@ -291,7 +304,14 @@ register RAY *r; } if (nd.tdiff > FTINY) { /* ambient from other side */ flipsurface(r); - ambient(ctmp, r); + if (hastexture) { + FVECT bnorm; + bnorm[0] = -nd.pnorm[0]; + bnorm[1] = -nd.pnorm[1]; + bnorm[2] = -nd.pnorm[2]; + ambient(ctmp, r, bnorm); + } else + ambient(ctmp, r, r->ron); if (nd.specfl & SP_TBLT) scalecolor(ctmp, nd.trans); else @@ -303,8 +323,13 @@ register RAY *r; /* add direct component */ direct(r, dirnorm, &nd); /* check distance */ - if (transtest > bright(r->rcol)) + d = bright(r->rcol); + if (transtest > d) r->rt = transdist; + else if (mirtest > d) + r->rt = mirdist; + + return(1); } @@ -317,6 +342,7 @@ register NORMDAT *np; FVECT u, v, h; double rv[2]; double d, sinp, cosp; + int niter; register int i; /* quick test */ if ((np->specfl & (SP_REFL|SP_RBLT)) != SP_REFL && @@ -335,52 +361,63 @@ register NORMDAT *np; if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL && rayorigin(&sr, r, SPECULAR, np->rspec) == 0) { dimlist[ndims++] = (int)np->mp; - d = urand(ilhash(dimlist,ndims)+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( np->alpha2 * -log(rv[1]) ); - for (i = 0; i < 3; i++) - h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*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) - VCOPY(sr.rdir, np->vrefl); /* jitter no good */ - rayvalue(&sr); - multcolor(sr.rcol, np->scolor); - addcolor(r->rcol, sr.rcol); + for (niter = 0; niter < MAXITER; niter++) { + if (niter) + d = frandom(); + else + d = urand(ilhash(dimlist,ndims)+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( np->alpha2 * -log(rv[1]) ); + for (i = 0; i < 3; i++) + h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*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; + } + } 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( np->alpha2/4.0 * -log(rv[1]) ); - for (i = 0; i < 3; i++) - sr.rdir[i] = np->prdir[i] + d*(cosp*u[i] + sinp*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); /* modified by color */ - addcolor(r->rcol, sr.rcol); + for (niter = 0; niter < MAXITER; niter++) { + if (niter) + d = frandom(); + else + 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]) * np->alpha2 ); + for (i = 0; i < 3; i++) + sr.rdir[i] = np->prdir[i] + d*(cosp*u[i] + sinp*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); /* modified */ + addcolor(r->rcol, sr.rcol); + break; + } + } ndims--; } }