--- ray/src/rt/aniso.c 1996/04/24 15:47:25 2.32 +++ ray/src/rt/aniso.c 2004/09/20 17:32:04 2.42 @@ -1,32 +1,24 @@ -/* Copyright (c) 1996 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: aniso.c,v 2.42 2004/09/20 17:32:04 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 */ - -extern int backvis; /* back faces visible? */ - #ifndef MAXITER #define MAXITER 10 /* maximum # specular ray attempts */ #endif -static agaussamp(), getacoords(); - /* * This routine implements the anisotropic Gaussian * model described by Ward in Siggraph `92 article. @@ -68,13 +60,20 @@ typedef struct { double pdot; /* perturbed dot product */ } ANISODAT; /* anisotropic material data */ +static srcdirf_t diraniso; +static void getacoords(RAY *r, ANISODAT *np); +static void agaussamp(RAY *r, 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 */ +) { + register ANISODAT *np = nnp; double ldot; double dtmp, dtmp1, dtmp2; FVECT h; @@ -95,7 +94,7 @@ double omega; /* light source size */ * 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); } @@ -106,7 +105,7 @@ double omega; /* light source size */ */ /* 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; @@ -123,8 +122,7 @@ double omega; /* light source size */ /* gaussian */ dtmp = DOT(np->pnorm, h); dtmp = (dtmp1 + dtmp2) / (dtmp*dtmp); - dtmp = exp(-dtmp) * (0.25/PI) - * sqrt(ldot/(np->pdot*au2*av2)); + dtmp = exp(-dtmp) / (4.0*PI * np->pdot * sqrt(au2*av2)); /* worth using? */ if (dtmp > FTINY) { copycolor(ctmp, np->scolor); @@ -138,7 +136,7 @@ double omega; /* light source size */ * 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); } @@ -148,7 +146,7 @@ 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" */ @@ -169,8 +167,7 @@ double omega; /* light source size */ } else dtmp = 0.0; /* gaussian */ - dtmp = exp(-dtmp) * (1.0/PI) - * sqrt(-ldot/(np->pdot*au2*av2)); + dtmp = exp(-dtmp) / (PI * np->pdot * sqrt(au2*av2)); /* worth using? */ if (dtmp > FTINY) { copycolor(ctmp, np->mcolor); @@ -182,9 +179,11 @@ double omega; /* light source size */ } -m_aniso(m, r) /* shade ray that hit something anisotropic */ -register OBJREC *m; -register RAY *r; +extern int +m_aniso( /* shade ray that hit something anisotropic */ + register OBJREC *m, + register RAY *r +) { ANISODAT nd; COLOR ctmp; @@ -195,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]); @@ -207,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() */ @@ -307,10 +307,11 @@ register RAY *r; } -static -getacoords(r, np) /* set up coordinate system */ -RAY *r; -register ANISODAT *np; +static void +getacoords( /* set up coordinate system */ + RAY *r, + register ANISODAT *np +) { register MFUNC *mf; register int i; @@ -320,7 +321,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; @@ -337,10 +338,11 @@ register ANISODAT *np; } -static -agaussamp(r, np) /* sample anisotropic gaussian specular */ -RAY *r; -register ANISODAT *np; +static void +agaussamp( /* sample anisotropic gaussian specular */ + RAY *r, + register ANISODAT *np +) { RAY sr; FVECT h; @@ -359,8 +361,8 @@ register ANISODAT *np; 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; + cosp = tcos(d) * np->u_alpha; + sinp = tsin(d) * np->v_alpha; d = sqrt(cosp*cosp + sinp*sinp); cosp /= d; sinp /= d; @@ -397,8 +399,8 @@ register ANISODAT *np; 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; + cosp = tcos(d) * np->u_alpha; + sinp = tsin(d) * np->v_alpha; d = sqrt(cosp*cosp + sinp*sinp); cosp /= d; sinp /= d; @@ -408,7 +410,7 @@ register ANISODAT *np; else d = sqrt(-log(rv[1]) / (cosp*cosp/(np->u_alpha*np->u_alpha) + - sinp*sinp/(np->v_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]);