--- ray/src/rt/aniso.c 2012/07/29 19:01:39 2.54 +++ ray/src/rt/aniso.c 2023/11/17 20:02:07 2.63 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: aniso.c,v 2.54 2012/07/29 19:01:39 greg Exp $"; +static const char RCSid[] = "$Id: aniso.c,v 2.63 2023/11/17 20:02:07 greg Exp $"; #endif /* * Shading functions for anisotropic materials. @@ -14,6 +14,7 @@ static const char RCSid[] = "$Id: aniso.c,v 2.54 2012/ #include "source.h" #include "func.h" #include "random.h" +#include "pmapmat.h" #ifndef MAXITER #define MAXITER 10 /* maximum # specular ray attempts */ @@ -41,14 +42,13 @@ static const char RCSid[] = "$Id: aniso.c,v 2.54 2012/ #define SP_FLAT 04 /* reflecting surface is flat */ #define SP_RBLT 010 /* reflection below sample threshold */ #define SP_TBLT 020 /* transmission below threshold */ -#define SP_BADU 040 /* bad u direction calculation */ 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 */ + SCOLOR mcolor; /* color of this material */ + SCOLOR scolor; /* color of specular component */ FVECT vrefl; /* vector in reflected direction */ FVECT prdir; /* vector in transmitted direction */ FVECT u, v; /* u and v vectors orienting anisotropy */ @@ -61,13 +61,13 @@ typedef struct { double pdot; /* perturbed dot product */ } ANISODAT; /* anisotropic material data */ -static void getacoords(RAY *r, ANISODAT *np); -static void agaussamp(RAY *r, ANISODAT *np); +static void getacoords(ANISODAT *np); +static void agaussamp(ANISODAT *np); static void diraniso( /* compute source contribution */ - COLOR cval, /* returned coefficient */ + SCOLOR scval, /* returned coefficient */ void *nnp, /* material data */ FVECT ldir, /* light source direction */ double omega /* light source size */ @@ -78,9 +78,9 @@ diraniso( /* compute source contribution */ double dtmp, dtmp1, dtmp2; FVECT h; double au2, av2; - COLOR ctmp; + SCOLOR sctmp; - setcolor(cval, 0.0, 0.0, 0.0); + scolorblack(scval); ldot = DOT(np->pnorm, ldir); @@ -93,13 +93,27 @@ diraniso( /* compute source contribution */ * color. The diffuse reflected component will always be * modified by the color of the material. */ - copycolor(ctmp, np->mcolor); + copyscolor(sctmp, np->mcolor); dtmp = ldot * omega * np->rdiff * (1.0/PI); - scalecolor(ctmp, dtmp); - addcolor(cval, ctmp); + scalescolor(sctmp, dtmp); + saddscolor(scval, sctmp); } - if (ldot > FTINY && (np->specfl&(SP_REFL|SP_BADU)) == SP_REFL) { + + if ((ldot < -FTINY) & (np->tdiff > FTINY)) { /* + * Compute diffuse transmission. + */ + copyscolor(sctmp, np->mcolor); + dtmp = -ldot * omega * np->tdiff * (1.0/PI); + scalescolor(sctmp, dtmp); + saddscolor(scval, sctmp); + } + + if (ambRayInPmap(np->rp)) + return; /* specular accounted for in photon map */ + + if (ldot > FTINY && np->specfl&SP_REFL) { + /* * Compute specular reflection coefficient using * anisotropic Gaussian distribution model. */ @@ -125,23 +139,15 @@ diraniso( /* compute source contribution */ (PI * dtmp*dtmp * sqrt(au2*av2)); /* worth using? */ if (dtmp > FTINY) { - copycolor(ctmp, np->scolor); + copyscolor(sctmp, np->scolor); dtmp *= ldot * omega; - scalecolor(ctmp, dtmp); - addcolor(cval, ctmp); + scalescolor(sctmp, dtmp); + saddscolor(scval, sctmp); } } - if ((ldot < -FTINY) & (np->tdiff > FTINY)) { + + if (ldot < -FTINY && np->specfl&SP_TRAN) { /* - * Compute diffuse transmission. - */ - copycolor(ctmp, np->mcolor); - dtmp = -ldot * omega * np->tdiff * (1.0/PI); - scalecolor(ctmp, dtmp); - addcolor(cval, ctmp); - } - if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_BADU)) == SP_TRAN) { - /* * Compute specular transmission. Specular transmission * is always modified by material color. */ @@ -168,10 +174,10 @@ diraniso( /* compute source contribution */ dtmp = exp(-dtmp) * (1.0/PI) * sqrt(-ldot/(np->pdot*au2*av2)); /* worth using? */ if (dtmp > FTINY) { - copycolor(ctmp, np->mcolor); + copyscolor(sctmp, np->mcolor); dtmp *= np->tspec * omega; - scalecolor(ctmp, dtmp); - addcolor(cval, ctmp); + scalescolor(sctmp, dtmp); + saddscolor(scval, sctmp); } } } @@ -184,7 +190,7 @@ m_aniso( /* shade ray that hit something anisotropic ) { ANISODAT nd; - COLOR ctmp; + SCOLOR sctmp; int i; /* easy shadow test */ if (r->crtype & SHADOW) @@ -194,7 +200,7 @@ m_aniso( /* shade ray that hit something anisotropic objerror(m, USER, "bad number of real arguments"); /* check for back side */ if (r->rod < 0.0) { - if (!backvis && m->otype != MAT_TRANS2) { + if (!backvis) { raytrans(r); return(1); } @@ -205,7 +211,7 @@ m_aniso( /* shade ray that hit something anisotropic /* get material color */ nd.mp = m; nd.rp = r; - setcolor(nd.mcolor, m->oargs.farg[0], + setscolor(nd.mcolor, m->oargs.farg[0], m->oargs.farg[1], m->oargs.farg[2]); /* get roughness */ @@ -218,16 +224,16 @@ m_aniso( /* shade ray that hit something anisotropic nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ if (nd.pdot < .001) nd.pdot = .001; /* non-zero for diraniso() */ - multcolor(nd.mcolor, r->pcol); /* modify material color */ + smultscolor(nd.mcolor, r->pcol); /* modify material color */ /* get specular component */ if ((nd.rspec = m->oargs.farg[3]) > FTINY) { nd.specfl |= SP_REFL; /* compute specular color */ if (m->otype == MAT_METAL2) - copycolor(nd.scolor, nd.mcolor); + copyscolor(nd.scolor, nd.mcolor); else - setcolor(nd.scolor, 1.0, 1.0, 1.0); - scalecolor(nd.scolor, nd.rspec); + setscolor(nd.scolor, 1.0, 1.0, 1.0); + scalescolor(nd.scolor, nd.rspec); /* check threshold */ if (specthresh >= nd.rspec-FTINY) nd.specfl |= SP_RBLT; @@ -266,19 +272,20 @@ m_aniso( /* shade ray that hit something anisotropic if (r->ro != NULL && isflat(r->ro->otype)) nd.specfl |= SP_FLAT; - getacoords(r, &nd); /* set up coordinates */ + getacoords(&nd); /* set up coordinates */ - if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_BADU)) - agaussamp(r, &nd); + if (nd.specfl & (SP_REFL|SP_TRAN)) + agaussamp(&nd); if (nd.rdiff > FTINY) { /* ambient from this side */ - copycolor(ctmp, nd.mcolor); /* modified by material color */ - scalecolor(ctmp, nd.rdiff); + copyscolor(sctmp, nd.mcolor); /* modified by material color */ + scalescolor(sctmp, nd.rdiff); if (nd.specfl & SP_RBLT) /* add in specular as well? */ - addcolor(ctmp, nd.scolor); - multambient(ctmp, r, nd.pnorm); - addcolor(r->rcol, ctmp); /* add to returned color */ + saddscolor(sctmp, nd.scolor); + multambient(sctmp, r, nd.pnorm); + saddscolor(r->rcol, sctmp); /* add to returned color */ } + if (nd.tdiff > FTINY) { /* ambient from other side */ FVECT bnorm; @@ -286,13 +293,14 @@ m_aniso( /* shade ray that hit something anisotropic bnorm[0] = -nd.pnorm[0]; bnorm[1] = -nd.pnorm[1]; bnorm[2] = -nd.pnorm[2]; - copycolor(ctmp, nd.mcolor); /* modified by color */ - if (nd.specfl & SP_TBLT) - scalecolor(ctmp, nd.trans); - else - scalecolor(ctmp, nd.tdiff); - multambient(ctmp, r, bnorm); - addcolor(r->rcol, ctmp); + copyscolor(sctmp, nd.mcolor); /* modified by color */ + if (nd.specfl & SP_TBLT) { + scalescolor(sctmp, nd.trans); + } else { + scalescolor(sctmp, nd.tdiff); + } + multambient(sctmp, r, bnorm); + saddscolor(r->rcol, sctmp); flipsurface(r); } /* add direct component */ @@ -301,10 +309,8 @@ m_aniso( /* shade ray that hit something anisotropic return(1); } - static void getacoords( /* set up coordinate system */ - RAY *r, ANISODAT *np ) { @@ -312,30 +318,29 @@ getacoords( /* set up coordinate system */ int i; mf = getfunc(np->mp, 3, 0x7, 1); - setfunc(np->mp, r); + setfunc(np->mp, np->rp); errno = 0; for (i = 0; i < 3; i++) np->u[i] = evalue(mf->ep[i]); - if ((errno == EDOM) | (errno == ERANGE)) { - objerror(np->mp, WARNING, "compute error"); - np->specfl |= SP_BADU; - return; - } + if ((errno == EDOM) | (errno == ERANGE)) + np->u[0] = np->u[1] = np->u[2] = 0.0; if (mf->fxp != &unitxf) multv3(np->u, np->u, mf->fxp->xfm); fcross(np->v, np->pnorm, np->u); if (normalize(np->v) == 0.0) { - objerror(np->mp, WARNING, "illegal orientation vector"); - np->specfl |= SP_BADU; - return; - } - fcross(np->u, np->v, np->pnorm); + if (fabs(np->u_alpha - np->v_alpha) > 0.001) + objerror(np->mp, WARNING, "illegal orientation vector"); + getperpendicular(np->u, np->pnorm, 1); /* punting */ + fcross(np->v, np->pnorm, np->u); + np->u_alpha = np->v_alpha = sqrt( 0.5 * + (np->u_alpha*np->u_alpha + np->v_alpha*np->v_alpha) ); + } else + fcross(np->u, np->v, np->pnorm); } static void agaussamp( /* sample anisotropic Gaussian specular */ - RAY *r, ANISODAT *np ) { @@ -343,25 +348,25 @@ agaussamp( /* sample anisotropic Gaussian specular */ FVECT h; double rv[2]; double d, sinp, cosp; - COLOR scol; + SCOLOR scol; int maxiter, ntrials, nstarget, nstaken; int i; /* 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) { d = 1./nstarget; - scalecolor(sr.rcoef, d); + scalescolor(sr.rcoef, d); sr.rweight *= d; } else nstarget = 1; } - setcolor(scol, 0., 0., 0.); + scolorblack(scol); dimlist[ndims++] = (int)(size_t)np->mp; maxiter = MAXITER*nstarget; for (nstaken = ntrials = 0; nstaken < nstarget && @@ -388,46 +393,46 @@ agaussamp( /* sample anisotropic Gaussian specular */ for (i = 0; i < 3; i++) h[i] = np->pnorm[i] + d*(cosp*np->u[i] + sinp*np->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); - scalecolor(sr.rcol, d); - addcolor(scol, sr.rcol); + d = 2./(1. + np->rp->rod/d); + scalescolor(sr.rcol, d); + saddscolor(scol, sr.rcol); } else { rayvalue(&sr); - multcolor(sr.rcol, sr.rcoef); - addcolor(r->rcol, sr.rcol); + smultscolor(sr.rcol, sr.rcoef); + saddscolor(np->rp->rcol, sr.rcol); } ++nstaken; } if (nstarget > 1) { /* final W-G-M-D weighting */ - multcolor(scol, sr.rcoef); + smultscolor(scol, sr.rcoef); d = (double)nstarget/ntrials; - scalecolor(scol, d); - addcolor(r->rcol, scol); + scalescolor(scol, d); + saddscolor(np->rp->rcol, scol); } ndims--; } /* compute transmission */ - copycolor(sr.rcoef, np->mcolor); /* modify by material color */ - scalecolor(sr.rcoef, np->tspec); + copyscolor(sr.rcoef, np->mcolor); /* modify by material color */ + scalescolor(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) { d = 1./nstarget; - scalecolor(sr.rcoef, d); + scalescolor(sr.rcoef, d); sr.rweight *= d; } else nstarget = 1; @@ -458,14 +463,14 @@ agaussamp( /* sample anisotropic Gaussian specular */ for (i = 0; i < 3; i++) sr.rdir[i] = np->prdir[i] + d*(cosp*np->u[i] + sinp*np->v[i]); - 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); + smultscolor(sr.rcol, sr.rcoef); + saddscolor(np->rp->rcol, sr.rcol); ++nstaken; } ndims--;