--- ray/src/rt/normal.c 1992/01/14 16:16:45 2.5 +++ ray/src/rt/normal.c 1994/01/12 16:46:45 2.27 @@ -23,11 +23,11 @@ static char SCCSid[] = "$SunId$ LBL"; extern double specthresh; /* specular sampling threshold */ extern double specjitter; /* specular sampling jitter */ +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,18 +38,17 @@ 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 */ -#define SP_PURE 010 /* purely specular (zero roughness) */ -#define SP_FLAT 020 /* flat reflecting surface */ -#define SP_RBLT 040 /* reflection below sample threshold */ -#define SP_TBLT 0100 /* transmission below threshold */ +#define SP_PURE 04 /* purely specular (zero roughness) */ +#define SP_FLAT 010 /* flat reflecting surface */ +#define SP_RBLT 020 /* reflection below sample threshold */ +#define SP_TBLT 040 /* transmission below threshold */ typedef struct { OBJREC *mp; /* material pointer */ + RAY *rp; /* ray pointer */ short specfl; /* specularity flags, defined above */ COLOR mcolor; /* color of this material */ COLOR scolor; /* color of specular component */ @@ -71,8 +70,8 @@ FVECT ldir; /* light source direction */ double omega; /* light source size */ { double ldot; - double dtmp; - int i; + double dtmp, d2; + FVECT vtmp; COLOR ctmp; setcolor(cval, 0.0, 0.0, 0.0); @@ -99,16 +98,23 @@ double omega; /* light source size */ * gaussian distribution model. */ /* roughness */ - dtmp = 2.0*np->alpha2; + dtmp = np->alpha2; /* + source if flat */ if (np->specfl & SP_FLAT) - dtmp += omega/(2.0*PI); + dtmp += omega/(4.0*PI); + /* 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 *= d2; + d2 = (DOT(vtmp,vtmp) - d2) / d2; /* gaussian */ - dtmp = exp((DOT(np->vrefl,ldir)-1.)/dtmp)/(2.*PI)/dtmp; + dtmp = exp(-d2/dtmp)/(4.*PI*dtmp); /* worth using? */ if (dtmp > FTINY) { copycolor(ctmp, np->scolor); - dtmp *= omega / np->pdot; + dtmp *= omega * sqrt(ldot/np->pdot); scalecolor(ctmp, dtmp); addcolor(cval, ctmp); } @@ -128,13 +134,13 @@ double omega; /* light source size */ * is always modified by material color. */ /* roughness + source */ - dtmp = np->alpha2 + omega/(2.0*PI); + dtmp = np->alpha2 + omega/PI; /* gaussian */ - dtmp = exp((DOT(np->prdir,ldir)-1.)/dtmp)/(2.*PI)/dtmp; + dtmp = exp((2.*DOT(np->prdir,ldir)-2.)/dtmp)/(PI*dtmp); /* worth using? */ if (dtmp > FTINY) { copycolor(ctmp, np->mcolor); - dtmp *= np->tspec * omega / np->pdot; + dtmp *= np->tspec * omega * sqrt(-ldot/np->pdot); scalecolor(ctmp, dtmp); addcolor(cval, ctmp); } @@ -148,16 +154,16 @@ register RAY *r; { NORMDAT nd; double transtest, transdist; - double dtmp; 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"); nd.mp = m; + nd.rp = r; /* get material color */ setcolor(nd.mcolor, m->oargs.farg[0], m->oargs.farg[1], @@ -177,6 +183,7 @@ register RAY *r; nd.pdot = .001; /* non-zero for dirnorm() */ multcolor(nd.mcolor, r->pcol); /* modify material color */ transtest = 0; + transdist = r->rot; /* get specular component */ if ((nd.rspec = m->oargs.farg[3]) > FTINY) { nd.specfl |= SP_REFL; @@ -186,17 +193,15 @@ 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 (nd.rspec <= specthresh+FTINY) + 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.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; @@ -216,7 +221,8 @@ register RAY *r; if (nd.tspec > FTINY) { nd.specfl |= SP_TRAN; /* check threshold */ - if (nd.tspec <= specthresh+FTINY) + if (!(nd.specfl & SP_PURE) && + specthresh >= nd.tspec-FTINY) nd.specfl |= SP_TBLT; if (r->crtype & SHADOW || DOT(r->pert,r->pert) <= FTINY*FTINY) { @@ -224,9 +230,11 @@ register RAY *r; transtest = 2; } else { for (i = 0; i < 3; i++) /* perturb */ - nd.prdir[i] = r->rdir[i] - - .75*r->pert[i]; - normalize(nd.prdir); + nd.prdir[i] = r->rdir[i] - r->pert[i]; + if (DOT(nd.prdir, r->ron) < -FTINY) + normalize(nd.prdir); /* OK */ + else + VCOPY(nd.prdir, r->rdir); } } } else @@ -243,17 +251,19 @@ register RAY *r; transtest *= bright(lr.rcol); transdist = r->rot + lr.rt; } - } + } else + transtest = 0; if (r->crtype & SHADOW) /* the rest is shadow */ - return; + return(1); /* 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->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; if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_PURE)) @@ -284,6 +294,8 @@ register RAY *r; /* check distance */ if (transtest > bright(r->rcol)) r->rt = transdist; + + return(1); } @@ -296,8 +308,11 @@ register NORMDAT *np; FVECT u, v, h; double rv[2]; double d, sinp, cosp; - int ntries; register int i; + /* quick test */ + if ((np->specfl & (SP_REFL|SP_RBLT)) != SP_REFL && + (np->specfl & (SP_TRAN|SP_TBLT)) != SP_TRAN) + return; /* set up sample coordinates */ v[0] = v[1] = v[2] = 0.0; for (i = 0; i < 3; i++) @@ -311,31 +326,52 @@ register NORMDAT *np; 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 * 8912; - d = urand(ilhash(dimlist,ndims+1)+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; - } - } + 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); 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]) * 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 */ + 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); + ndims--; + } }