--- ray/src/rt/normal.c 1992/02/21 14:53:16 2.11 +++ ray/src/rt/normal.c 1992/05/14 11:32:07 2.17 @@ -50,6 +50,7 @@ extern double specjitter; /* specular sampling jitte 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 +72,9 @@ FVECT ldir; /* light source direction */ double omega; /* light source size */ { double ldot; - double dtmp; - int i; + double dtmp, d2; + FVECT vtmp; + register int i; COLOR ctmp; setcolor(cval, 0.0, 0.0, 0.0); @@ -99,16 +101,21 @@ 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); + /* delta */ + for (i = 0; i < 3; i++) + vtmp[i] = ldir[i] - np->rp->rdir[i]; + d2 = DOT(vtmp, np->pnorm); + d2 = 2.0 - 2.0*d2/sqrt(DOT(vtmp,vtmp)); /* 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); } @@ -134,7 +141,7 @@ double omega; /* light source size */ /* 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); } @@ -158,6 +165,7 @@ register RAY *r; 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], @@ -192,9 +200,10 @@ register RAY *r; 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))) + 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++) @@ -221,10 +230,9 @@ register RAY *r; if (nd.tspec > FTINY) { nd.specfl |= SP_TRAN; /* check threshold */ - if (specthresh > FTINY && - ((specthresh >= 1.-FTINY || - specthresh + - (.05 - .1*frandom()) > nd.tspec))) + if (!(nd.specfl & SP_PURE) && 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) { @@ -265,7 +273,8 @@ 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) + 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)) @@ -309,6 +318,10 @@ register NORMDAT *np; double rv[2]; double d, sinp, cosp; 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++)