--- ray/src/rt/normal.c 1992/01/04 19:53:53 2.2 +++ ray/src/rt/normal.c 1992/02/21 14:53:16 2.11 @@ -20,6 +20,9 @@ static char SCCSid[] = "$SunId$ LBL"; #include "random.h" +extern double specthresh; /* specular sampling threshold */ +extern double specjitter; /* specular sampling jitter */ + /* * This routine uses portions of the reflection * model described by Cook and Torrance. @@ -40,7 +43,10 @@ static char SCCSid[] = "$SunId$ LBL"; /* 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_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 */ @@ -66,6 +72,7 @@ double omega; /* light source size */ { double ldot; double dtmp; + int i; COLOR ctmp; setcolor(cval, 0.0, 0.0, 0.0); @@ -91,8 +98,11 @@ double omega; /* light source size */ * Compute specular reflection coefficient using * gaussian distribution model. */ - /* roughness + source */ - dtmp = 2.0*np->alpha2 + omega/(2.0*PI); + /* roughness */ + dtmp = 2.0*np->alpha2; + /* + source if flat */ + if (np->specfl & SP_FLAT) + dtmp += omega/(2.0*PI); /* gaussian */ dtmp = exp((DOT(np->vrefl,ldir)-1.)/dtmp)/(2.*PI)/dtmp; /* worth using? */ @@ -118,7 +128,7 @@ double omega; /* light source size */ * is always modified by material color. */ /* roughness + source */ - dtmp = np->alpha2 + omega/(2.0*PI); + dtmp = np->alpha2/2.0 + omega/(2.0*PI); /* gaussian */ dtmp = exp((DOT(np->prdir,ldir)-1.)/dtmp)/(2.*PI)/dtmp; /* worth using? */ @@ -181,9 +191,17 @@ register RAY *r; 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 + (.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; @@ -202,6 +220,12 @@ register RAY *r; nd.tdiff = nd.trans - nd.tspec; if (nd.tspec > FTINY) { nd.specfl |= SP_TRAN; + /* check threshold */ + 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) { VCOPY(nd.prdir, r->rdir); @@ -209,8 +233,11 @@ register RAY *r; } 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 @@ -227,7 +254,8 @@ register RAY *r; transtest *= bright(lr.rcol); transdist = r->rot + lr.rt; } - } + } else + transtest = 0; if (r->crtype & SHADOW) /* the rest is shadow */ return; @@ -237,19 +265,28 @@ register RAY *r; 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) + 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); - scalecolor(ctmp, nd.rdiff); + if (nd.specfl & SP_RBLT) + scalecolor(ctmp, 1.0-nd.trans); + else + scalecolor(ctmp, nd.rdiff); multcolor(ctmp, nd.mcolor); /* modified by material color */ addcolor(r->rcol, ctmp); /* add to returned color */ } if (nd.tdiff > FTINY) { /* ambient from other side */ flipsurface(r); ambient(ctmp, r); - scalecolor(ctmp, nd.tdiff); + if (nd.specfl & SP_TBLT) + scalecolor(ctmp, nd.trans); + else + scalecolor(ctmp, nd.tdiff); multcolor(ctmp, nd.mcolor); /* modified by color */ addcolor(r->rcol, ctmp); flipsurface(r); @@ -271,7 +308,6 @@ register NORMDAT *np; FVECT u, v, h; double rv[2]; double d, sinp, cosp; - int confuse; register int i; /* set up sample coordinates */ v[0] = v[1] = v[2] = 0.0; @@ -283,17 +319,15 @@ register NORMDAT *np; normalize(u); fcross(v, np->pnorm, u); /* compute reflection */ - if (np->specfl & SP_REFL && + if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL && rayorigin(&sr, r, SPECULAR, np->rspec) == 0) { - confuse = 0; dimlist[ndims++] = (int)np->mp; - refagain: - dimlist[ndims] = confuse += 3601; - d = urand(ilhash(dimlist,ndims+1)+samplendx); + 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 @@ -303,12 +337,37 @@ register NORMDAT *np; 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) /* oops! */ - goto refagain; + 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( 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); + ndims--; + } }