--- ray/src/rt/aniso.c 1995/11/22 09:27:51 2.31 +++ ray/src/rt/aniso.c 2003/08/28 03:22:16 2.40 @@ -1,28 +1,26 @@ -/* 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.40 2003/08/28 03:22:16 greg Exp $"; #endif - /* * Shading functions for anisotropic materials. */ +#include "copyright.h" + #include "ray.h" +#include "ambient.h" + #include "otypes.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. @@ -64,7 +62,11 @@ typedef struct { double pdot; /* perturbed dot product */ } ANISODAT; /* anisotropic material data */ +static void getacoords(); +static void agaussamp(); + +static void diraniso(cval, np, ldir, omega) /* compute source contribution */ COLOR cval; /* returned coefficient */ register ANISODAT *np; /* material data */ @@ -178,6 +180,7 @@ double omega; /* light source size */ } +int m_aniso(m, r) /* shade ray that hit something anisotropic */ register OBJREC *m; register RAY *r; @@ -191,9 +194,19 @@ register RAY *r; 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 && m->otype != MAT_TRANS2) { + 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]); @@ -203,16 +216,7 @@ register RAY *r; nd.v_alpha = m->oargs.farg[5]; 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() */ @@ -303,7 +307,7 @@ register RAY *r; } -static +static void getacoords(r, np) /* set up coordinate system */ RAY *r; register ANISODAT *np; @@ -316,7 +320,7 @@ register ANISODAT *np; 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; @@ -333,7 +337,7 @@ register ANISODAT *np; } -static +static void agaussamp(r, np) /* sample anisotropic gaussian specular */ RAY *r; register ANISODAT *np; @@ -342,69 +346,81 @@ register ANISODAT *np; FVECT h; double rv[2]; double d, sinp, cosp; + int niter; register 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); + for (niter = 0; niter < MAXITER; niter++) { + if (niter) + 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 = 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) { + rayvalue(&sr); + multcolor(sr.rcol, np->scolor); + addcolor(r->rcol, sr.rcol); + break; + } + } ndims--; } /* compute transmission */ 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); + for (niter = 0; niter < MAXITER; niter++) { + if (niter) + 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 = 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++) + 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 */ + rayvalue(&sr); + scalecolor(sr.rcol, np->tspec); + multcolor(sr.rcol, np->mcolor); /* modify */ + addcolor(r->rcol, sr.rcol); + break; + } + } ndims--; } }