--- ray/src/rt/aniso.c 1995/11/06 12:03:20 2.30 +++ ray/src/rt/aniso.c 2014/01/25 18:27:39 2.56 @@ -1,38 +1,35 @@ -/* Copyright (c) 1992 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: aniso.c,v 2.56 2014/01/25 18:27:39 greg Exp $"; #endif - /* * Shading functions for anisotropic materials. */ -#include "ray.h" +#include "copyright.h" +#include "ray.h" +#include "ambient.h" #include "otypes.h" - +#include "rtotypes.h" +#include "source.h" #include "func.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 -extern int backvis; /* back faces visible? */ - -static agaussamp(), getacoords(); - /* * This routine implements the anisotropic Gaussian - * model described by Ward in Siggraph `92 article. + * model described by Ward in Siggraph `92 article, updated with + * normalization and sampling adjustments due to Geisler-Moroder and Duer. * We orient the surface towards the incoming ray, so a single * surface can be used to represent an infinitely thin object. * * Arguments for MAT_PLASTIC2 and MAT_METAL2 are: * 4+ ux uy uz funcfile [transform...] * 0 - * 6 red grn blu specular-frac. u-facet-slope v-facet-slope + * 6 red grn blu specular-frac. u-rough v-rough * * Real arguments for MAT_TRANS2 are: * 8 red grn blu rspec u-rough v-rough trans tspec @@ -64,13 +61,19 @@ typedef struct { double pdot; /* perturbed dot product */ } ANISODAT; /* anisotropic material data */ +static void getacoords(ANISODAT *np); +static void agaussamp(ANISODAT *np); -diraniso(cval, np, ldir, omega) /* compute source contribution */ -COLOR cval; /* returned coefficient */ -register ANISODAT *np; /* material data */ -FVECT ldir; /* light source direction */ -double omega; /* light source size */ + +static void +diraniso( /* compute source contribution */ + COLOR cval, /* returned coefficient */ + void *nnp, /* material data */ + FVECT ldir, /* light source direction */ + double omega /* light source size */ +) { + ANISODAT *np = nnp; double ldot; double dtmp, dtmp1, dtmp2; FVECT h; @@ -84,57 +87,56 @@ 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) { + if ((ldot > FTINY) & (np->rdiff > 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 * np->rdiff * (1.0/PI); scalecolor(ctmp, dtmp); addcolor(cval, ctmp); } if (ldot > FTINY && (np->specfl&(SP_REFL|SP_BADU)) == SP_REFL) { /* * Compute specular reflection coefficient using - * anisotropic gaussian distribution model. + * anisotropic Gaussian distribution model. */ /* add source width if flat */ if (np->specfl & SP_FLAT) - au2 = av2 = omega/(4.0*PI); + au2 = av2 = omega * (0.25/PI); else au2 = av2 = 0.0; au2 += np->u_alpha*np->u_alpha; av2 += np->v_alpha*np->v_alpha; /* half vector */ - h[0] = ldir[0] - np->rp->rdir[0]; - h[1] = ldir[1] - np->rp->rdir[1]; - h[2] = ldir[2] - np->rp->rdir[2]; + VSUB(h, ldir, np->rp->rdir); /* ellipse */ dtmp1 = DOT(np->u, h); dtmp1 *= dtmp1 / au2; dtmp2 = DOT(np->v, h); dtmp2 *= dtmp2 / av2; - /* gaussian */ + /* new W-G-M-D model */ dtmp = DOT(np->pnorm, h); - dtmp = (dtmp1 + dtmp2) / (dtmp*dtmp); - dtmp = exp(-dtmp) * (0.25/PI) - * sqrt(ldot/(np->pdot*au2*av2)); + dtmp *= dtmp; + dtmp1 = (dtmp1 + dtmp2) / dtmp; + dtmp = exp(-dtmp1) * DOT(h,h) / + (PI * dtmp*dtmp * sqrt(au2*av2)); /* worth using? */ if (dtmp > FTINY) { copycolor(ctmp, np->scolor); - dtmp *= omega; + dtmp *= ldot * omega; scalecolor(ctmp, dtmp); addcolor(cval, ctmp); } } - if (ldot < -FTINY && np->tdiff > FTINY) { + if ((ldot < -FTINY) & (np->tdiff > FTINY)) { /* * Compute diffuse transmission. */ copycolor(ctmp, np->mcolor); - dtmp = -ldot * omega * np->tdiff / PI; + dtmp = -ldot * omega * np->tdiff * (1.0/PI); scalecolor(ctmp, dtmp); addcolor(cval, ctmp); } @@ -144,13 +146,11 @@ double omega; /* light source size */ * is always modified by material color. */ /* roughness + source */ - au2 = av2 = omega / PI; + au2 = av2 = omega * (1.0/PI); au2 += np->u_alpha*np->u_alpha; av2 += np->v_alpha*np->v_alpha; /* "half vector" */ - h[0] = ldir[0] - np->prdir[0]; - h[1] = ldir[1] - np->prdir[1]; - h[2] = ldir[2] - np->prdir[2]; + VSUB(h, ldir, np->prdir); dtmp = DOT(h,h); if (dtmp > FTINY*FTINY) { dtmp1 = DOT(h,np->pnorm); @@ -164,9 +164,8 @@ double omega; /* light source size */ } } else dtmp = 0.0; - /* gaussian */ - dtmp = exp(-dtmp) * (1.0/PI) - * sqrt(-ldot/(np->pdot*au2*av2)); + /* Gaussian */ + dtmp = exp(-dtmp) * (1.0/PI) * sqrt(-ldot/(np->pdot*au2*av2)); /* worth using? */ if (dtmp > FTINY) { copycolor(ctmp, np->mcolor); @@ -178,22 +177,34 @@ double omega; /* light source size */ } -m_aniso(m, r) /* shade ray that hit something anisotropic */ -register OBJREC *m; -register RAY *r; +int +m_aniso( /* shade ray that hit something anisotropic */ + OBJREC *m, + RAY *r +) { ANISODAT nd; COLOR ctmp; - register int i; + int i; /* easy shadow test */ if (r->crtype & SHADOW) return(1); if (m->oargs.nfargs != (m->otype == MAT_TRANS2 ? 8 : 6)) objerror(m, USER, "bad number of real arguments"); + /* check for back side */ + if (r->rod < 0.0) { + if (!backvis) { + raytrans(r); + return(1); + } + raytexture(r, m->omod); + flipsurface(r); /* reorient if backvis */ + } else + raytexture(r, m->omod); + /* get material color */ nd.mp = m; nd.rp = r; - /* get material color */ setcolor(nd.mcolor, m->oargs.farg[0], m->oargs.farg[1], m->oargs.farg[2]); @@ -201,18 +212,9 @@ register RAY *r; nd.specfl = 0; nd.u_alpha = m->oargs.farg[4]; nd.v_alpha = m->oargs.farg[5]; - if (nd.u_alpha < FTINY || nd.v_alpha <= FTINY) + if ((nd.u_alpha <= FTINY) | (nd.v_alpha <= FTINY)) objerror(m, USER, "roughness too small"); - /* check for back side */ - if (r->rod < 0.0) { - if (!backvis && m->otype != MAT_TRANS2) { - raytrans(r); - return(1); - } - flipsurface(r); /* reorient if backvis */ - } - /* get modifiers */ - raytexture(r, m->omod); + nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ if (nd.pdot < .001) nd.pdot = .001; /* non-zero for diraniso() */ @@ -230,11 +232,9 @@ register RAY *r; if (specthresh >= nd.rspec-FTINY) nd.specfl |= SP_RBLT; /* compute refl. direction */ - for (i = 0; i < 3; i++) - nd.vrefl[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i]; + VSUM(nd.vrefl, r->rdir, nd.pnorm, 2.0*nd.pdot); if (DOT(nd.vrefl, r->ron) <= FTINY) /* penetration? */ - 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.0*r->rod); } /* compute transmission */ if (m->otype == MAT_TRANS2) { @@ -266,28 +266,32 @@ register RAY *r; 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); + agaussamp(&nd); if (nd.rdiff > FTINY) { /* ambient from this side */ - ambient(ctmp, r, nd.pnorm); - if (nd.specfl & SP_RBLT) - scalecolor(ctmp, 1.0-nd.trans); - else - scalecolor(ctmp, nd.rdiff); - multcolor(ctmp, nd.mcolor); /* modified by material color */ + copycolor(ctmp, nd.mcolor); /* modified by material color */ + scalecolor(ctmp, 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 */ } if (nd.tdiff > FTINY) { /* ambient from other side */ + FVECT bnorm; + flipsurface(r); - ambient(ctmp, r, nd.pnorm); + 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); - multcolor(ctmp, nd.mcolor); /* modified by color */ + multambient(ctmp, r, bnorm); addcolor(r->rcol, ctmp); flipsurface(r); } @@ -298,26 +302,26 @@ register RAY *r; } -static -getacoords(r, np) /* set up coordinate system */ -RAY *r; -register ANISODAT *np; +static void +getacoords( /* set up coordinate system */ + ANISODAT *np +) { - register MFUNC *mf; - register int i; + MFUNC *mf; + 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) { + if ((errno == EDOM) | (errno == ERANGE)) { objerror(np->mp, WARNING, "compute error"); np->specfl |= SP_BADU; return; } - if (mf->f != &unitxf) - multv3(np->u, np->u, mf->f->xfm); + 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"); @@ -328,78 +332,140 @@ register ANISODAT *np; } -static -agaussamp(r, np) /* sample anisotropic gaussian specular */ -RAY *r; -register ANISODAT *np; +static void +agaussamp( /* sample anisotropic Gaussian specular */ + ANISODAT *np +) { RAY sr; FVECT h; double rv[2]; double d, sinp, cosp; - register int i; + COLOR scol; + int maxiter, ntrials, nstarget, nstaken; + int i; /* compute reflection */ 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) * np->u_alpha; - sinp = sin(d) * np->v_alpha; - d = sqrt(cosp*cosp + sinp*sinp); - cosp /= d; - sinp /= d; - rv[1] = 1.0 - specjitter*rv[1]; - if (rv[1] <= FTINY) - d = 1.0; - else - d = sqrt(-log(rv[1]) / - (cosp*cosp/(np->u_alpha*np->u_alpha) + - sinp*sinp/(np->v_alpha*np->v_alpha))); - 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); - for (i = 0; i < 3; i++) - sr.rdir[i] = r->rdir[i] + d*h[i]; - if (DOT(sr.rdir, r->ron) <= FTINY) /* penetration? */ - VCOPY(sr.rdir, np->vrefl); /* jitter no good */ - rayvalue(&sr); - multcolor(sr.rcol, np->scolor); - addcolor(r->rcol, sr.rcol); + rayorigin(&sr, SPECULAR, np->rp, np->scolor) == 0) { + nstarget = 1; + if (specjitter > 1.5) { /* multiple samples? */ + 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 + nstarget = 1; + } + 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); + multisamp(rv, 2, d); + d = 2.0*PI * rv[0]; + cosp = tcos(d) * np->u_alpha; + sinp = tsin(d) * np->v_alpha; + d = 1./sqrt(cosp*cosp + sinp*sinp); + cosp *= d; + sinp *= d; + if ((0. <= specjitter) & (specjitter < 1.)) + rv[1] = 1.0 - specjitter*rv[1]; + if (rv[1] <= FTINY) + d = 1.0; + else + d = sqrt(-log(rv[1]) / + (cosp*cosp/(np->u_alpha*np->u_alpha) + + sinp*sinp/(np->v_alpha*np->v_alpha))); + 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, 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; + checknorm(sr.rdir); + 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); + } + ++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); /* modify by material color */ + scalecolor(sr.rcoef, np->tspec); 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) * np->u_alpha; - sinp = sin(d) * np->v_alpha; - d = sqrt(cosp*cosp + sinp*sinp); - cosp /= d; - sinp /= d; - rv[1] = 1.0 - specjitter*rv[1]; - if (rv[1] <= FTINY) - d = 1.0; - else - d = sqrt(-log(rv[1]) / - (cosp*cosp/(np->u_alpha*np->u_alpha) + - sinp*sinp/(np->v_alpha*np->u_alpha))); - 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) - 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); /* modify by color */ - addcolor(r->rcol, sr.rcol); + rayorigin(&sr, SPECULAR, np->rp, sr.rcoef) == 0) { + nstarget = 1; + if (specjitter > 1.5) { /* multiple samples? */ + 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 + nstarget = 1; + } + 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); + multisamp(rv, 2, d); + d = 2.0*PI * rv[0]; + cosp = tcos(d) * np->u_alpha; + sinp = tsin(d) * np->v_alpha; + d = 1./sqrt(cosp*cosp + sinp*sinp); + cosp *= d; + sinp *= d; + if ((0. <= specjitter) & (specjitter < 1.)) + rv[1] = 1.0 - specjitter*rv[1]; + if (rv[1] <= FTINY) + d = 1.0; + else + d = sqrt(-log(rv[1]) / + (cosp*cosp/(np->u_alpha*np->u_alpha) + + sinp*sinp/(np->v_alpha*np->v_alpha))); + 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, np->rp->ron) >= -FTINY) + continue; + normalize(sr.rdir); /* OK, normalize */ + if (nstaken) /* multi-sampling */ + rayclear(&sr); + rayvalue(&sr); + multcolor(sr.rcol, sr.rcoef); + addcolor(np->rp->rcol, sr.rcol); + ++nstaken; + } ndims--; } }