--- ray/src/rt/normal.c 2010/10/10 22:31:46 2.57 +++ ray/src/rt/normal.c 2014/01/25 18:27:39 2.66 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: normal.c,v 2.57 2010/10/10 22:31:46 greg Exp $"; +static const char RCSid[] = "$Id: normal.c,v 2.66 2014/01/25 18:27:39 greg Exp $"; #endif /* * normal.c - shading function for normal materials. @@ -65,19 +65,18 @@ typedef struct { double pdot; /* perturbed dot product */ } NORMDAT; /* normal material data */ -static srcdirf_t dirnorm; -static void gaussamp(RAY *r, NORMDAT *np); +static void gaussamp(NORMDAT *np); static void dirnorm( /* compute source contribution */ COLOR cval, /* returned coefficient */ - void *nnp, /* material data */ + void *nnp, /* material data */ FVECT ldir, /* light source direction */ double omega /* light source size */ ) { - register NORMDAT *np = nnp; + NORMDAT *np = nnp; double ldot; double lrdiff, ltdiff; double dtmp, d2, d3, d4; @@ -123,9 +122,7 @@ dirnorm( /* compute source contribution */ if (np->specfl & SP_FLAT) 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]; + VSUB(vtmp, ldir, np->rp->rdir); d2 = DOT(vtmp, np->pnorm); d2 *= d2; d3 = DOT(vtmp,vtmp); @@ -169,10 +166,10 @@ dirnorm( /* compute source contribution */ } -extern int +int m_normal( /* color a ray that hit something normal */ - register OBJREC *m, - register RAY *r + OBJREC *m, + RAY *r ) { NORMDAT nd; @@ -182,7 +179,7 @@ m_normal( /* color a ray that hit something normal * int hastexture; double d; COLOR ctmp; - register int i; + int i; /* easy shadow test */ if (r->crtype & SHADOW && m->otype != MAT_TRANS) return(1); @@ -191,7 +188,7 @@ m_normal( /* color a ray that hit something normal * objerror(m, USER, "bad number of arguments"); /* check for back side */ if (r->rod < 0.0) { - if (!backvis && m->otype != MAT_TRANS) { + if (!backvis) { raytrans(r); return(1); } @@ -227,7 +224,7 @@ m_normal( /* color a ray that hit something normal * nd.rspec = m->oargs.farg[3]; /* compute Fresnel approx. */ if (nd.specfl & SP_PURE && nd.rspec >= FRESTHRESH) { - fest = FRESNE(r->rod); + fest = FRESNE(nd.pdot); nd.rspec += fest*(1. - nd.rspec); } else fest = 0.; @@ -283,9 +280,10 @@ m_normal( /* color a ray that hit something normal * if (m->otype != MAT_METAL) { setcolor(nd.scolor, nd.rspec, nd.rspec, nd.rspec); } else if (fest > FTINY) { - d = nd.rspec*(1. - fest); + d = m->oargs.farg[3]*(1. - fest); for (i = 0; i < 3; i++) - nd.scolor[i] = fest + nd.mcolor[i]*d; + colval(nd.scolor,i) = fest + + colval(nd.mcolor,i)*d; } else { copycolor(nd.scolor, nd.mcolor); scalecolor(nd.scolor, nd.rspec); @@ -321,14 +319,13 @@ m_normal( /* color a ray that hit something normal * return(1); /* 100% pure specular */ if (!(nd.specfl & SP_PURE)) - gaussamp(r, &nd); /* checks *BLT flags */ + gaussamp(&nd); /* checks *BLT flags */ if (nd.rdiff > FTINY) { /* ambient from this side */ copycolor(ctmp, nd.mcolor); /* modified by material color */ - if (nd.specfl & SP_RBLT) - scalecolor(ctmp, 1.0-nd.trans); - else - scalecolor(ctmp, nd.rdiff); + scalecolor(ctmp, nd.rdiff); + if (nd.specfl & SP_RBLT) /* add in specular as well? */ + addcolor(ctmp, nd.scolor); multambient(ctmp, r, hastexture ? nd.pnorm : r->ron); addcolor(r->rcol, ctmp); /* add to returned color */ } @@ -365,17 +362,16 @@ m_normal( /* color a ray that hit something normal * static void gaussamp( /* sample Gaussian specular */ - RAY *r, - register NORMDAT *np + NORMDAT *np ) { RAY sr; FVECT u, v, h; double rv[2]; double d, sinp, cosp; - COLOR scol; - int niter, ns2go; - register int i; + COLOR scol; + int maxiter, ntrials, nstarget, nstaken; + int i; /* quick test */ if ((np->specfl & (SP_REFL|SP_RBLT)) != SP_REFL && (np->specfl & (SP_TRAN|SP_TBLT)) != SP_TRAN) @@ -391,23 +387,25 @@ gaussamp( /* sample Gaussian specular */ fcross(v, np->pnorm, u); /* compute reflection */ if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL && - rayorigin(&sr, SPECULAR, r, np->scolor) == 0) { - copycolor(scol, np->scolor); - ns2go = 1; + rayorigin(&sr, SPECULAR, np->rp, np->scolor) == 0) { + nstarget = 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); + nstarget = specjitter*np->rp->rweight + .5; + if (sr.rweight <= minweight*nstarget) + nstarget = sr.rweight/minweight; + if (nstarget > 1) { + d = 1./nstarget; + scalecolor(sr.rcoef, d); sr.rweight *= d; } else - ns2go = 1; + nstarget = 1; } - dimlist[ndims++] = (int)np->mp; - for (niter = ns2go*MAXITER; (ns2go > 0) & (niter > 0); niter--) { - if (specjitter > 1.5) + setcolor(scol, 0., 0., 0.); + dimlist[ndims++] = (int)(size_t)np->mp; + maxiter = MAXITER*nstarget; + for (nstaken = ntrials = 0; nstaken < nstarget && + ntrials < maxiter; ntrials++) { + if (ntrials) d = frandom(); else d = urand(ilhash(dimlist,ndims)+samplendx); @@ -423,49 +421,58 @@ gaussamp( /* sample Gaussian specular */ 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); - if (d <= np->pdot + FTINY) + d = -2.0 * DOT(h, np->rp->rdir) / (1.0 + d*d); + VSUM(sr.rdir, np->rp->rdir, h, d); + /* sample rejection test */ + if ((d = DOT(sr.rdir, np->rp->ron)) <= 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); + if (nstarget > 1) { /* W-G-M-D adjustment */ + if (nstaken) rayclear(&sr); + rayvalue(&sr); + d = 2./(1. + np->rp->rod/d); + scalecolor(sr.rcol, d); + addcolor(scol, sr.rcol); + } else { + rayvalue(&sr); + multcolor(sr.rcol, sr.rcoef); + addcolor(np->rp->rcol, sr.rcol); } - rayvalue(&sr); - multcolor(sr.rcol, sr.rcoef); - addcolor(r->rcol, sr.rcol); - --ns2go; + ++nstaken; } + if (nstarget > 1) { /* final W-G-M-D weighting */ + multcolor(scol, sr.rcoef); + d = (double)nstarget/ntrials; + scalecolor(scol, d); + addcolor(np->rp->rcol, scol); + } 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, SPECULAR, r, sr.rcoef) == 0) { - ns2go = 1; + rayorigin(&sr, SPECULAR, np->rp, sr.rcoef) == 0) { + nstarget = 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; + nstarget = specjitter*np->rp->rweight + .5; + if (sr.rweight <= minweight*nstarget) + nstarget = sr.rweight/minweight; + if (nstarget > 1) { + d = 1./nstarget; scalecolor(sr.rcoef, d); sr.rweight *= d; } else - ns2go = 1; + nstarget = 1; } - dimlist[ndims++] = (int)np->mp; - for (niter = ns2go*MAXITER; (ns2go > 0) & (niter > 0); niter--) { - if (specjitter > 1.5) + dimlist[ndims++] = (int)(size_t)np->mp; + maxiter = MAXITER*nstarget; + for (nstaken = ntrials = 0; nstaken < nstarget && + ntrials < maxiter; ntrials++) { + if (ntrials) d = frandom(); else - d = urand(ilhash(dimlist,ndims)+1823+samplendx); + d = urand(ilhash(dimlist,ndims)+samplendx); multisamp(rv, 2, d); d = 2.0*PI * rv[0]; cosp = tcos(d); @@ -478,15 +485,16 @@ gaussamp( /* sample Gaussian specular */ 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) + /* sample rejection test */ + if (DOT(sr.rdir, np->rp->ron) >= -FTINY) continue; normalize(sr.rdir); /* OK, normalize */ - if (specjitter > 1.5) /* multi-sampling */ + if (nstaken) /* multi-sampling */ rayclear(&sr); rayvalue(&sr); multcolor(sr.rcol, sr.rcoef); - addcolor(r->rcol, sr.rcol); - --ns2go; + addcolor(np->rp->rcol, sr.rcol); + ++nstaken; } ndims--; }