--- ray/src/rt/normal.c 1998/12/16 18:14:58 2.37 +++ ray/src/rt/normal.c 2010/10/10 22:31:46 2.57 @@ -1,9 +1,6 @@ -/* Copyright (c) 1998 Silicon Graphics, Inc. */ - #ifndef lint -static char SCCSid[] = "$SunId$ SGI"; +static const char RCSid[] = "$Id: normal.c,v 2.57 2010/10/10 22:31:46 greg Exp $"; #endif - /* * normal.c - shading function for normal materials. * @@ -14,22 +11,22 @@ static char SCCSid[] = "$SunId$ SGI"; * Later changes described in delta comments. */ -#include "ray.h" +#include "copyright.h" +#include "ray.h" +#include "ambient.h" +#include "source.h" #include "otypes.h" - +#include "rtotypes.h" #include "random.h" -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 + /* estimate of Fresnel function */ +#define FRESNE(ci) (exp(-5.85*(ci)) - 0.00287989916) +#define FRESTHRESH 0.017999 /* minimum specularity for approx. */ -static gaussamp(); /* * This routine implements the isotropic Gaussian @@ -68,15 +65,22 @@ typedef struct { double pdot; /* perturbed dot product */ } NORMDAT; /* normal material data */ +static srcdirf_t dirnorm; +static void gaussamp(RAY *r, NORMDAT *np); -dirnorm(cval, np, ldir, omega) /* compute source contribution */ -COLOR cval; /* returned coefficient */ -register NORMDAT *np; /* material data */ -FVECT ldir; /* light source direction */ -double omega; /* light source size */ + +static void +dirnorm( /* compute source contribution */ + COLOR cval, /* returned coefficient */ + void *nnp, /* material data */ + FVECT ldir, /* light source direction */ + double omega /* light source size */ +) { + register NORMDAT *np = nnp; double ldot; - double dtmp, d2; + double lrdiff, ltdiff; + double dtmp, d2, d3, d4; FVECT vtmp; COLOR ctmp; @@ -87,50 +91,61 @@ 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 */ + lrdiff = np->rdiff; + ltdiff = np->tdiff; + if (np->specfl & SP_PURE && np->rspec >= FRESTHRESH && + (lrdiff > FTINY) | (ltdiff > FTINY)) { + dtmp = 1. - FRESNE(fabs(ldot)); + lrdiff *= dtmp; + ltdiff *= dtmp; + } + + if (ldot > FTINY && lrdiff > 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 * lrdiff * (1.0/PI); scalecolor(ctmp, dtmp); addcolor(cval, ctmp); } if (ldot > FTINY && (np->specfl&(SP_REFL|SP_PURE)) == SP_REFL) { /* * Compute specular reflection coefficient using - * gaussian distribution model. + * Gaussian distribution model. */ /* roughness */ dtmp = np->alpha2; /* + source if flat */ if (np->specfl & SP_FLAT) - dtmp += omega/(4.0*PI); + dtmp += omega * (0.25/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(-d2/dtmp)/(4.*PI*dtmp); + d3 = DOT(vtmp,vtmp); + d4 = (d3 - d2) / d2; + /* new W-G-M-D model */ + dtmp = exp(-d4/dtmp) * d3 / (PI * d2*d2 * dtmp); /* worth using? */ if (dtmp > FTINY) { copycolor(ctmp, np->scolor); - dtmp *= omega * sqrt(ldot/np->pdot); + dtmp *= ldot * omega; scalecolor(ctmp, dtmp); addcolor(cval, ctmp); } } - if (ldot < -FTINY && np->tdiff > FTINY) { + if (ldot < -FTINY && ltdiff > FTINY) { /* * Compute diffuse transmission. */ copycolor(ctmp, np->mcolor); - dtmp = -ldot * omega * np->tdiff / PI; + dtmp = -ldot * omega * ltdiff * (1.0/PI); scalecolor(ctmp, dtmp); addcolor(cval, ctmp); } @@ -140,8 +155,8 @@ double omega; /* light source size */ * is always modified by material color. */ /* roughness + source */ - dtmp = np->alpha2 + omega/PI; - /* gaussian */ + dtmp = np->alpha2 + omega*(1.0/PI); + /* Gaussian */ dtmp = exp((2.*DOT(np->prdir,ldir)-2.)/dtmp)/(PI*dtmp); /* worth using? */ if (dtmp > FTINY) { @@ -154,11 +169,14 @@ double omega; /* light source size */ } -m_normal(m, r) /* color a ray that hit something normal */ -register OBJREC *m; -register RAY *r; +extern int +m_normal( /* 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; @@ -177,8 +195,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 */ @@ -190,22 +210,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; nd.rspec = m->oargs.farg[3]; + /* compute Fresnel approx. */ + if (nd.specfl & SP_PURE && nd.rspec >= FRESTHRESH) { + 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); @@ -234,11 +259,12 @@ register RAY *r; /* transmitted ray */ if ((nd.specfl&(SP_TRAN|SP_PURE|SP_TBLT)) == (SP_TRAN|SP_PURE)) { RAY lr; - if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) { + copycolor(lr.rcoef, nd.mcolor); /* modified by color */ + scalecolor(lr.rcoef, nd.tspec); + if (rayorigin(&lr, TRANS, r, lr.rcoef) == 0) { VCOPY(lr.rdir, nd.prdir); rayvalue(&lr); - scalecolor(lr.rcol, nd.tspec); - multcolor(lr.rcol, nd.mcolor); /* modified by color */ + multcolor(lr.rcol, lr.rcoef); addcolor(r->rcol, lr.rcol); transtest *= bright(lr.rcol); transdist = r->rot + lr.rt; @@ -254,29 +280,33 @@ register RAY *r; if (nd.rspec > FTINY) { nd.specfl |= SP_REFL; /* compute specular color */ - if (m->otype == MAT_METAL) + 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); - else - setcolor(nd.scolor, 1.0, 1.0, 1.0); - scalecolor(nd.scolor, nd.rspec); + 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]; + VSUM(nd.vrefl, r->rdir, nd.pnorm, 2.*nd.pdot); /* 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]; + VSUM(nd.vrefl, r->rdir, r->ron, 2.*r->rod); + checknorm(nd.vrefl); } /* 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) { + if (rayorigin(&lr, REFLECTED, r, nd.scolor) == 0) { VCOPY(lr.rdir, nd.vrefl); rayvalue(&lr); - multcolor(lr.rcol, nd.scolor); + multcolor(lr.rcol, lr.rcoef); addcolor(r->rcol, lr.rcol); if (!hastexture && nd.specfl & SP_FLAT) { mirtest = 2.*bright(lr.rcol); @@ -294,29 +324,29 @@ register RAY *r; gaussamp(r, &nd); /* checks *BLT flags */ if (nd.rdiff > FTINY) { /* ambient from this side */ - ambient(ctmp, r, hastexture?nd.pnorm:r->ron); + copycolor(ctmp, nd.mcolor); /* modified by material color */ if (nd.specfl & SP_RBLT) scalecolor(ctmp, 1.0-nd.trans); else scalecolor(ctmp, nd.rdiff); - multcolor(ctmp, nd.mcolor); /* modified by material color */ + multambient(ctmp, r, hastexture ? nd.pnorm : r->ron); addcolor(r->rcol, ctmp); /* add to returned color */ } if (nd.tdiff > FTINY) { /* ambient from other side */ + copycolor(ctmp, nd.mcolor); /* modified by color */ + if (nd.specfl & SP_TBLT) + scalecolor(ctmp, nd.trans); + else + scalecolor(ctmp, nd.tdiff); flipsurface(r); if (hastexture) { FVECT bnorm; bnorm[0] = -nd.pnorm[0]; bnorm[1] = -nd.pnorm[1]; bnorm[2] = -nd.pnorm[2]; - ambient(ctmp, r, bnorm); + multambient(ctmp, r, bnorm); } else - ambient(ctmp, r, r->ron); - if (nd.specfl & SP_TBLT) - scalecolor(ctmp, nd.trans); - else - scalecolor(ctmp, nd.tdiff); - multcolor(ctmp, nd.mcolor); /* modified by color */ + multambient(ctmp, r, r->ron); addcolor(r->rcol, ctmp); flipsurface(r); } @@ -333,16 +363,18 @@ register RAY *r; } -static -gaussamp(r, np) /* sample gaussian specular */ -RAY *r; -register NORMDAT *np; +static void +gaussamp( /* sample Gaussian specular */ + RAY *r, + register NORMDAT *np +) { RAY sr; FVECT u, v, h; double rv[2]; double d, sinp, cosp; - int niter; + COLOR scol; + int niter, ns2go; register int i; /* quick test */ if ((np->specfl & (SP_REFL|SP_RBLT)) != SP_REFL && @@ -359,10 +391,23 @@ register NORMDAT *np; fcross(v, np->pnorm, u); /* compute reflection */ if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL && - rayorigin(&sr, r, SPECULAR, np->rspec) == 0) { + rayorigin(&sr, SPECULAR, r, np->scolor) == 0) { + copycolor(scol, np->scolor); + ns2go = 1; + if (specjitter > 1.5) { /* multiple samples? */ + ns2go = specjitter*r->rweight + .5; + if (sr.rweight <= minweight*ns2go) + ns2go = sr.rweight/minweight; + if (ns2go > 1) { + d = 1./ns2go; + scalecolor(scol, d); + sr.rweight *= d; + } else + ns2go = 1; + } dimlist[ndims++] = (int)np->mp; - for (niter = 0; niter < MAXITER; niter++) { - if (niter) + for (niter = ns2go*MAXITER; (ns2go > 0) & (niter > 0); niter--) { + if (specjitter > 1.5) d = frandom(); else d = urand(ilhash(dimlist,ndims)+samplendx); @@ -370,7 +415,8 @@ register NORMDAT *np; d = 2.0*PI * rv[0]; cosp = tcos(d); sinp = tsin(d); - rv[1] = 1.0 - specjitter*rv[1]; + if ((0. <= specjitter) & (specjitter < 1.)) + rv[1] = 1.0 - specjitter*rv[1]; if (rv[1] <= FTINY) d = 1.0; else @@ -378,23 +424,45 @@ register NORMDAT *np; 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; + if (d <= np->pdot + FTINY) + continue; + VSUM(sr.rdir, r->rdir, h, d); + if (DOT(sr.rdir, r->ron) <= FTINY) + continue; + checknorm(sr.rdir); + if (specjitter > 1.5) { /* adjusted W-G-M-D weight */ + d = 2.*(1. - np->pdot/d); + copycolor(sr.rcoef, scol); + scalecolor(sr.rcoef, d); + rayclear(&sr); } + rayvalue(&sr); + multcolor(sr.rcol, sr.rcoef); + addcolor(r->rcol, sr.rcol); + --ns2go; } ndims--; } /* compute transmission */ + copycolor(sr.rcoef, np->mcolor); /* modified by color */ + scalecolor(sr.rcoef, np->tspec); if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN && - rayorigin(&sr, r, SPECULAR, np->tspec) == 0) { + rayorigin(&sr, SPECULAR, r, sr.rcoef) == 0) { + ns2go = 1; + if (specjitter > 1.5) { /* multiple samples? */ + ns2go = specjitter*r->rweight + .5; + if (sr.rweight <= minweight*ns2go) + ns2go = sr.rweight/minweight; + if (ns2go > 1) { + d = 1./ns2go; + scalecolor(sr.rcoef, d); + sr.rweight *= d; + } else + ns2go = 1; + } dimlist[ndims++] = (int)np->mp; - for (niter = 0; niter < MAXITER; niter++) { - if (niter) + for (niter = ns2go*MAXITER; (ns2go > 0) & (niter > 0); niter--) { + if (specjitter > 1.5) d = frandom(); else d = urand(ilhash(dimlist,ndims)+1823+samplendx); @@ -402,21 +470,23 @@ register NORMDAT *np; d = 2.0*PI * rv[0]; cosp = tcos(d); sinp = tsin(d); - rv[1] = 1.0 - specjitter*rv[1]; + if ((0. <= specjitter) & (specjitter < 1.)) + 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; - } + if (DOT(sr.rdir, r->ron) >= -FTINY) + continue; + normalize(sr.rdir); /* OK, normalize */ + if (specjitter > 1.5) /* multi-sampling */ + rayclear(&sr); + rayvalue(&sr); + multcolor(sr.rcol, sr.rcoef); + addcolor(r->rcol, sr.rcol); + --ns2go; } ndims--; }