--- ray/src/rt/normal.c 1995/09/15 15:47:30 2.29 +++ ray/src/rt/normal.c 2003/06/17 21:49:57 2.44 @@ -1,9 +1,6 @@ -/* Copyright (c) 1992 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: normal.c,v 2.44 2003/06/17 21:49:57 greg Exp $"; #endif - /* * normal.c - shading function for normal materials. * @@ -14,19 +11,22 @@ static char SCCSid[] = "$SunId$ LBL"; * Later changes described in delta comments. */ +#include "copyright.h" + #include "ray.h" #include "otypes.h" #include "random.h" -extern double specthresh; /* specular sampling threshold */ -extern double specjitter; /* specular sampling jitter */ +#ifndef MAXITER +#define MAXITER 10 /* maximum # specular ray attempts */ +#endif + /* estimate of Fresnel function */ +#define FRESNE(ci) (exp(-5.85*(ci)) - 0.00287989916) -extern int backvis; /* back faces visible? */ +static void gaussamp(); -static gaussamp(); - /* * This routine implements the isotropic Gaussian * model described by Ward in Siggraph `92 article. @@ -65,6 +65,7 @@ typedef struct { } NORMDAT; /* normal material data */ +static void dirnorm(cval, np, ldir, omega) /* compute source contribution */ COLOR cval; /* returned coefficient */ register NORMDAT *np; /* material data */ @@ -72,6 +73,7 @@ FVECT ldir; /* light source direction */ double omega; /* light source size */ { double ldot; + double ldiff; double dtmp, d2; FVECT vtmp; COLOR ctmp; @@ -83,14 +85,19 @@ double omega; /* light source size */ if (ldot < 0.0 ? np->trans <= FTINY : np->trans >= 1.0-FTINY) return; /* wrong side */ - if (ldot > FTINY && np->rdiff > FTINY) { + /* Fresnel estimate */ + ldiff = np->rdiff; + if (np->specfl & SP_PURE && (np->rspec > FTINY & ldiff > FTINY)) + ldiff *= 1. - FRESNE(fabs(ldot)); + + if (ldot > FTINY && ldiff > FTINY) { /* * Compute and add diffuse reflected component to returned * color. The diffuse reflected component will always be * modified by the color of the material. */ copycolor(ctmp, np->mcolor); - dtmp = ldot * omega * np->rdiff / PI; + dtmp = ldot * omega * ldiff / PI; scalecolor(ctmp, dtmp); addcolor(cval, ctmp); } @@ -150,11 +157,13 @@ double omega; /* light source size */ } +int m_normal(m, r) /* color a ray that hit something normal */ register OBJREC *m; register RAY *r; { NORMDAT nd; + double fest; double transtest, transdist; double mirtest, mirdist; int hastexture; @@ -173,8 +182,10 @@ register RAY *r; raytrans(r); return(1); } + raytexture(r, m->omod); flipsurface(r); /* reorient if backvis */ - } + } else + raytexture(r, m->omod); nd.mp = m; nd.rp = r; /* get material color */ @@ -186,55 +197,27 @@ register RAY *r; nd.alpha2 = m->oargs.farg[4]; if ((nd.alpha2 *= nd.alpha2) <= FTINY) nd.specfl |= SP_PURE; - if (r->ro != NULL && isflat(r->ro->otype)) - nd.specfl |= SP_FLAT; - /* get modifiers */ - raytexture(r, m->omod); - if (hastexture = DOT(r->pert,r->pert) > FTINY*FTINY) + + if (hastexture = (DOT(r->pert,r->pert) > FTINY*FTINY)) { nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ - else { + } else { VCOPY(nd.pnorm, r->ron); nd.pdot = r->rod; } + if (r->ro != NULL && isflat(r->ro->otype)) + nd.specfl |= SP_FLAT; if (nd.pdot < .001) nd.pdot = .001; /* non-zero for dirnorm() */ multcolor(nd.mcolor, r->pcol); /* modify material color */ mirtest = transtest = 0; mirdist = transdist = r->rot; - /* 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); - /* 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; - } - } - } - } + nd.rspec = m->oargs.farg[3]; + /* compute Fresnel approx. */ + if (nd.specfl & SP_PURE && nd.rspec > FTINY) { + fest = FRESNE(r->rod); + nd.rspec += fest*(1. - nd.rspec); + } else + fest = 0.; /* compute transmission */ if (m->otype == MAT_TRANS) { nd.trans = m->oargs.farg[5]*(1.0 - nd.rspec); @@ -261,7 +244,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|SP_PURE|SP_TBLT)) == (SP_TRAN|SP_PURE)) { RAY lr; if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) { VCOPY(lr.rdir, nd.prdir); @@ -279,17 +262,56 @@ register RAY *r; r->rt = transdist; return(1); } + /* get specular reflection */ + if (nd.rspec > FTINY) { + nd.specfl |= SP_REFL; + /* compute specular color */ + if (m->otype != MAT_METAL) { + setcolor(nd.scolor, nd.rspec, nd.rspec, nd.rspec); + } else if (fest > FTINY) { + d = nd.rspec*(1. - fest); + for (i = 0; i < 3; i++) + nd.scolor[i] = fest + nd.mcolor[i]*d; + } else { + copycolor(nd.scolor, nd.mcolor); + 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]; + } + /* reflected ray */ + if ((nd.specfl&(SP_REFL|SP_PURE|SP_RBLT)) == (SP_REFL|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(1); /* 100% pure specular */ - if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_PURE)) - gaussamp(r, &nd); + if (!(nd.specfl & SP_PURE)) + gaussamp(r, &nd); /* checks *BLT flags */ 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 @@ -299,7 +321,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 @@ -321,7 +350,7 @@ register RAY *r; } -static +static void gaussamp(r, np) /* sample gaussian specular */ RAY *r; register NORMDAT *np; @@ -330,6 +359,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 && @@ -348,52 +378,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 = tcos(d); + sinp = tsin(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( -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); + 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 = tcos(d); + sinp = tsin(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++) + 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--; } }