--- ray/src/rt/normal.c 2010/10/13 15:29:02 2.59 +++ ray/src/rt/normal.c 2018/01/10 04:08:50 2.76 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: normal.c,v 2.59 2010/10/13 15:29:02 greg Exp $"; +static const char RCSid[] = "$Id: normal.c,v 2.76 2018/01/10 04:08:50 greg Exp $"; #endif /* * normal.c - shading function for normal materials. @@ -19,6 +19,7 @@ static const char RCSid[] = "$Id: normal.c,v 2.59 2010 #include "otypes.h" #include "rtotypes.h" #include "random.h" +#include "pmapmat.h" #ifndef MAXITER #define MAXITER 10 /* maximum # specular ray attempts */ @@ -65,19 +66,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; @@ -112,6 +112,20 @@ dirnorm( /* compute source contribution */ scalecolor(ctmp, dtmp); addcolor(cval, ctmp); } + + if (ldot < -FTINY && ltdiff > FTINY) { + /* + * Compute diffuse transmission. + */ + copycolor(ctmp, np->mcolor); + dtmp = -ldot * omega * ltdiff * (1.0/PI); + scalecolor(ctmp, dtmp); + addcolor(cval, ctmp); + } + + if (ambRayInPmap(np->rp)) + return; /* specular already in photon map */ + if (ldot > FTINY && (np->specfl&(SP_REFL|SP_PURE)) == SP_REFL) { /* * Compute specular reflection coefficient using @@ -123,9 +137,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); @@ -140,15 +152,8 @@ dirnorm( /* compute source contribution */ addcolor(cval, ctmp); } } - if (ldot < -FTINY && ltdiff > FTINY) { - /* - * Compute diffuse transmission. - */ - copycolor(ctmp, np->mcolor); - dtmp = -ldot * omega * ltdiff * (1.0/PI); - scalecolor(ctmp, dtmp); - addcolor(cval, ctmp); - } + + if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_PURE)) == SP_TRAN) { /* * Compute specular transmission. Specular transmission @@ -169,10 +174,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 +187,13 @@ m_normal( /* color a ray that hit something normal * int hastexture; double d; COLOR ctmp; - register int i; + int i; + + /* PMAP: skip transmitted shadow ray if accounted for in photon map */ + /* No longer needed? + if (shadowRayInPmap(r) || ambRayInPmap(r)) + return(1); */ + /* easy shadow test */ if (r->crtype & SHADOW && m->otype != MAT_TRANS) return(1); @@ -191,7 +202,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 +238,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.; @@ -242,12 +253,12 @@ m_normal( /* color a ray that hit something normal * if (!(nd.specfl & SP_PURE) && specthresh >= nd.tspec-FTINY) nd.specfl |= SP_TBLT; - if (!hastexture || r->crtype & SHADOW) { + if (!hastexture || r->crtype & (SHADOW|AMBIENT)) { VCOPY(nd.prdir, r->rdir); transtest = 2; } else { - for (i = 0; i < 3; i++) /* perturb */ - nd.prdir[i] = r->rdir[i] - r->pert[i]; + /* perturb */ + VSUB(nd.prdir, r->rdir, r->pert); if (DOT(nd.prdir, r->ron) < -FTINY) normalize(nd.prdir); /* OK */ else @@ -257,6 +268,7 @@ m_normal( /* color a ray that hit something normal * } else nd.tdiff = nd.tspec = nd.trans = 0.0; /* transmitted ray */ + if ((nd.specfl&(SP_TRAN|SP_PURE|SP_TBLT)) == (SP_TRAN|SP_PURE)) { RAY lr; copycolor(lr.rcoef, nd.mcolor); /* modified by color */ @@ -283,9 +295,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); @@ -308,7 +321,8 @@ m_normal( /* color a ray that hit something normal * rayvalue(&lr); multcolor(lr.rcol, lr.rcoef); addcolor(r->rcol, lr.rcol); - if (!hastexture && nd.specfl & SP_FLAT) { + if (nd.specfl & SP_FLAT && + !hastexture | (r->crtype & AMBIENT)) { mirtest = 2.*bright(lr.rcol); mirdist = r->rot + lr.rt; } @@ -317,18 +331,21 @@ m_normal( /* color a ray that hit something normal * /* diffuse reflection */ nd.rdiff = 1.0 - nd.trans - nd.rspec; - if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY) + if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY) { + if (mirtest > transtest+FTINY) + r->rt = mirdist; + else if (transtest > FTINY) + r->rt = transdist; 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,36 +382,29 @@ 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; + COLOR scol; int maxiter, ntrials, nstarget, nstaken; - register int i; + 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++) - if (np->pnorm[i] < 0.6 && np->pnorm[i] > -0.6) - break; - v[i] = 1.0; - fcross(u, v, np->pnorm); - normalize(u); + getperpendicular(u, np->pnorm, rand_samp); fcross(v, np->pnorm, u); /* compute reflection */ if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL && - rayorigin(&sr, SPECULAR, r, np->scolor) == 0) { + rayorigin(&sr, SPECULAR, np->rp, np->scolor) == 0) { nstarget = 1; if (specjitter > 1.5) { /* multiple samples? */ - nstarget = specjitter*r->rweight + .5; + nstarget = specjitter*np->rp->rweight + .5; if (sr.rweight <= minweight*nstarget) nstarget = sr.rweight/minweight; if (nstarget > 1) { @@ -405,7 +415,7 @@ gaussamp( /* sample Gaussian specular */ nstarget = 1; } setcolor(scol, 0., 0., 0.); - dimlist[ndims++] = (int)np->mp; + dimlist[ndims++] = (int)(size_t)np->mp; maxiter = MAXITER*nstarget; for (nstaken = ntrials = 0; nstaken < nstarget && ntrials < maxiter; ntrials++) { @@ -425,22 +435,22 @@ 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); - VSUM(sr.rdir, r->rdir, h, d); + 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, r->ron)) <= FTINY) + if ((d = DOT(sr.rdir, np->rp->ron)) <= FTINY) continue; checknorm(sr.rdir); if (nstarget > 1) { /* W-G-M-D adjustment */ if (nstaken) rayclear(&sr); rayvalue(&sr); - d = 2./(1. + r->rod/d); + d = 2./(1. + np->rp->rod/d); scalecolor(sr.rcol, d); addcolor(scol, sr.rcol); } else { rayvalue(&sr); multcolor(sr.rcol, sr.rcoef); - addcolor(r->rcol, sr.rcol); + addcolor(np->rp->rcol, sr.rcol); } ++nstaken; } @@ -448,7 +458,7 @@ gaussamp( /* sample Gaussian specular */ multcolor(scol, sr.rcoef); d = (double)nstarget/ntrials; scalecolor(scol, d); - addcolor(r->rcol, scol); + addcolor(np->rp->rcol, scol); } ndims--; } @@ -456,10 +466,10 @@ gaussamp( /* sample Gaussian specular */ 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) { + rayorigin(&sr, SPECULAR, np->rp, sr.rcoef) == 0) { nstarget = 1; if (specjitter > 1.5) { /* multiple samples? */ - nstarget = specjitter*r->rweight + .5; + nstarget = specjitter*np->rp->rweight + .5; if (sr.rweight <= minweight*nstarget) nstarget = sr.rweight/minweight; if (nstarget > 1) { @@ -469,7 +479,7 @@ gaussamp( /* sample Gaussian specular */ } else nstarget = 1; } - dimlist[ndims++] = (int)np->mp; + dimlist[ndims++] = (int)(size_t)np->mp; maxiter = MAXITER*nstarget; for (nstaken = ntrials = 0; nstaken < nstarget && ntrials < maxiter; ntrials++) { @@ -490,14 +500,14 @@ gaussamp( /* sample Gaussian specular */ for (i = 0; i < 3; i++) sr.rdir[i] = np->prdir[i] + d*(cosp*u[i] + sinp*v[i]); /* sample rejection test */ - if (DOT(sr.rdir, r->ron) >= -FTINY) + if (DOT(sr.rdir, np->rp->ron) >= -FTINY) continue; normalize(sr.rdir); /* OK, normalize */ if (nstaken) /* multi-sampling */ rayclear(&sr); rayvalue(&sr); multcolor(sr.rcol, sr.rcoef); - addcolor(r->rcol, sr.rcol); + addcolor(np->rp->rcol, sr.rcol); ++nstaken; } ndims--;