--- ray/src/rt/normal.c 1991/06/13 13:58:18 1.11 +++ ray/src/rt/normal.c 1992/01/15 11:02:40 2.6 @@ -1,4 +1,4 @@ -/* Copyright (c) 1991 Regents of the University of California */ +/* Copyright (c) 1992 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; @@ -11,12 +11,18 @@ static char SCCSid[] = "$SunId$ LBL"; * 12/19/85 - added stuff for metals. * 6/26/87 - improved specular model. * 9/28/87 - added model for translucent materials. + * Later changes described in delta comments. */ #include "ray.h" #include "otypes.h" +#include "random.h" + +extern double specthresh; /* specular sampling threshold */ +extern double specjitter; /* specular sampling jitter */ + /* * This routine uses portions of the reflection * model described by Cook and Torrance. @@ -34,15 +40,22 @@ static char SCCSid[] = "$SunId$ LBL"; #define BSPEC(m) (6.0) /* specularity parameter b */ -extern double exp(); + /* specularity flags */ +#define SP_REFL 01 /* has reflected specular component */ +#define SP_TRAN 02 /* has transmitted specular */ +#define SP_PURE 010 /* purely specular (zero roughness) */ +#define SP_FLAT 020 /* flat reflecting surface */ +#define SP_RBLT 040 /* reflection below sample threshold */ +#define SP_TBLT 0100 /* transmission below threshold */ typedef struct { OBJREC *mp; /* material pointer */ - RAY *pr; /* intersected ray */ + short specfl; /* specularity flags, defined above */ COLOR mcolor; /* color of this material */ COLOR scolor; /* color of specular component */ FVECT vrefl; /* vector in direction of reflected ray */ - double alpha2; /* roughness squared times 2 */ + FVECT prdir; /* vector in transmitted direction */ + double alpha2; /* roughness squared */ double rdiff, rspec; /* reflected specular, diffuse */ double trans; /* transmissivity */ double tdiff, tspec; /* transmitted specular, diffuse */ @@ -59,6 +72,7 @@ double omega; /* light source size */ { double ldot; double dtmp; + int i; COLOR ctmp; setcolor(cval, 0.0, 0.0, 0.0); @@ -79,19 +93,22 @@ double omega; /* light source size */ scalecolor(ctmp, dtmp); addcolor(cval, ctmp); } - if (ldot > FTINY && np->rspec > FTINY && np->alpha2 > FTINY) { + if (ldot > FTINY && (np->specfl&(SP_REFL|SP_PURE)) == SP_REFL) { /* * Compute specular reflection coefficient using * gaussian distribution model. */ - /* roughness + source */ - dtmp = np->alpha2 + omega/(2.0*PI); + /* roughness */ + dtmp = 2.0*np->alpha2; + /* + source if flat */ + if (np->specfl & SP_FLAT) + dtmp += omega/(2.0*PI); /* gaussian */ dtmp = exp((DOT(np->vrefl,ldir)-1.)/dtmp)/(2.*PI)/dtmp; /* worth using? */ if (dtmp > FTINY) { copycolor(ctmp, np->scolor); - dtmp *= omega; + dtmp *= omega / np->pdot; scalecolor(ctmp, dtmp); addcolor(cval, ctmp); } @@ -105,26 +122,27 @@ double omega; /* light source size */ scalecolor(ctmp, dtmp); addcolor(cval, ctmp); } - if (ldot < -FTINY && np->tspec > FTINY && np->alpha2 > FTINY) { + if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_PURE)) == SP_TRAN) { /* * Compute specular transmission. Specular transmission - * is unaffected by material color. + * is always modified by material color. */ /* roughness + source */ dtmp = np->alpha2 + omega/(2.0*PI); /* gaussian */ - dtmp = exp((DOT(np->pr->rdir,ldir)-1.)/dtmp)/(2.*PI)/dtmp; + dtmp = exp((DOT(np->prdir,ldir)-1.)/dtmp)/(2.*PI)/dtmp; /* worth using? */ if (dtmp > FTINY) { - dtmp *= np->tspec * omega; - setcolor(ctmp, dtmp, dtmp, dtmp); + copycolor(ctmp, np->mcolor); + dtmp *= np->tspec * omega / np->pdot; + scalecolor(ctmp, dtmp); addcolor(cval, ctmp); } } } -m_normal(m, r) /* color a ray which hit something normal */ +m_normal(m, r) /* color a ray that hit something normal */ register OBJREC *m; register RAY *r; { @@ -133,33 +151,35 @@ register RAY *r; double dtmp; COLOR ctmp; register int i; - - if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5)) - objerror(m, USER, "bad # arguments"); /* easy shadow test */ if (r->crtype & SHADOW && m->otype != MAT_TRANS) return; + + if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5)) + objerror(m, USER, "bad number of arguments"); nd.mp = m; - nd.pr = r; /* get material color */ setcolor(nd.mcolor, m->oargs.farg[0], m->oargs.farg[1], m->oargs.farg[2]); /* get roughness */ + nd.specfl = 0; nd.alpha2 = m->oargs.farg[4]; - nd.alpha2 *= 2.0 * nd.alpha2; + if ((nd.alpha2 *= nd.alpha2) <= FTINY) + nd.specfl |= SP_PURE; /* reorient if necessary */ if (r->rod < 0.0) flipsurface(r); /* get modifiers */ raytexture(r, m->omod); nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ + if (nd.pdot < .001) + nd.pdot = .001; /* non-zero for dirnorm() */ multcolor(nd.mcolor, r->pcol); /* modify material color */ transtest = 0; /* get specular component */ - nd.rspec = m->oargs.farg[3]; - - if (nd.rspec > FTINY) { /* has specular component */ + if ((nd.rspec = m->oargs.farg[3]) > FTINY) { + nd.specfl |= SP_REFL; /* compute specular color */ if (m->otype == MAT_METAL) copycolor(nd.scolor, nd.mcolor); @@ -171,11 +191,17 @@ register RAY *r; for (i = 0; i < 3; i++) colval(nd.scolor,i) += (1.0-colval(nd.scolor,i))*dtmp; nd.rspec += (1.0-nd.rspec)*dtmp; + /* check threshold */ + if (specthresh > FTINY && + ((specthresh >= 1.-FTINY || + specthresh + (.1 - .2*urand(8199+samplendx)) + > nd.rspec))) + nd.specfl |= SP_RBLT; /* compute reflected ray */ for (i = 0; i < 3; i++) nd.vrefl[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i]; - if (nd.alpha2 <= FTINY && !(r->crtype & SHADOW)) { + if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) { RAY lr; if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) { VCOPY(lr.rdir, nd.vrefl); @@ -190,21 +216,33 @@ register RAY *r; nd.trans = m->oargs.farg[5]*(1.0 - nd.rspec); nd.tspec = nd.trans * m->oargs.farg[6]; nd.tdiff = nd.trans - nd.tspec; + if (nd.tspec > FTINY) { + nd.specfl |= SP_TRAN; + /* check threshold */ + if (specthresh > FTINY && + ((specthresh >= 1.-FTINY || + specthresh + + (.1 - .2*urand(7241+samplendx)) + > nd.tspec))) + nd.specfl |= SP_TBLT; + if (r->crtype & SHADOW || + DOT(r->pert,r->pert) <= FTINY*FTINY) { + VCOPY(nd.prdir, r->rdir); + transtest = 2; + } else { + for (i = 0; i < 3; i++) /* perturb */ + nd.prdir[i] = r->rdir[i] - + .75*r->pert[i]; + normalize(nd.prdir); + } + } } else nd.tdiff = nd.tspec = nd.trans = 0.0; /* transmitted ray */ - if (nd.tspec > FTINY && nd.alpha2 <= FTINY) { + if ((nd.specfl&(SP_TRAN|SP_PURE)) == (SP_TRAN|SP_PURE)) { RAY lr; if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) { - if (DOT(r->pert,r->pert) > FTINY*FTINY) { - for (i = 0; i < 3; i++) /* perturb direction */ - lr.rdir[i] = r->rdir[i] - - .75*r->pert[i]; - normalize(lr.rdir); - } else { - VCOPY(lr.rdir, r->rdir); - transtest = 2; - } + VCOPY(lr.rdir, nd.prdir); rayvalue(&lr); scalecolor(lr.rcol, nd.tspec); multcolor(lr.rcol, nd.mcolor); /* modified by color */ @@ -213,31 +251,38 @@ register RAY *r; transdist = r->rot + lr.rt; } } + if (r->crtype & SHADOW) /* the rest is shadow */ return; /* diffuse reflection */ nd.rdiff = 1.0 - nd.trans - nd.rspec; - if (nd.rdiff <= FTINY && nd.tdiff <= FTINY && nd.alpha2 <= FTINY) - return; /* purely specular */ + if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY) + return; /* 100% pure specular */ + if (r->ro->otype == OBJ_FACE || r->ro->otype == OBJ_RING) + nd.specfl |= SP_FLAT; + + if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_PURE)) + gaussamp(r, &nd); + if (nd.rdiff > FTINY) { /* ambient from this side */ ambient(ctmp, r); - if (nd.alpha2 <= FTINY) - scalecolor(ctmp, nd.rdiff); - else + if (nd.specfl & SP_RBLT) scalecolor(ctmp, 1.0-nd.trans); + else + scalecolor(ctmp, nd.rdiff); multcolor(ctmp, nd.mcolor); /* modified by material color */ addcolor(r->rcol, ctmp); /* add to returned color */ } if (nd.tdiff > FTINY) { /* ambient from other side */ flipsurface(r); ambient(ctmp, r); - if (nd.alpha2 <= FTINY) - scalecolor(ctmp, nd.tdiff); - else + if (nd.specfl & SP_TBLT) scalecolor(ctmp, nd.trans); - multcolor(ctmp, nd.mcolor); + else + scalecolor(ctmp, nd.tdiff); + multcolor(ctmp, nd.mcolor); /* modified by color */ addcolor(r->rcol, ctmp); flipsurface(r); } @@ -246,4 +291,58 @@ register RAY *r; /* check distance */ if (transtest > bright(r->rcol)) r->rt = transdist; +} + + +static +gaussamp(r, np) /* sample gaussian specular */ +RAY *r; +register NORMDAT *np; +{ + RAY sr; + FVECT u, v, h; + double rv[2]; + double d, sinp, cosp; + int ntries; + register int i; + /* 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); + fcross(v, np->pnorm, u); + /* compute reflection */ + if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL && + rayorigin(&sr, r, SPECULAR, np->rspec) == 0) { + dimlist[ndims++] = (int)np->mp; + for (ntries = 0; ntries < 10; ntries++) { + dimlist[ndims] = ntries * 8912; + d = urand(ilhash(dimlist,ndims+1)+samplendx); + multisamp(rv, 2, d); + d = 2.0*PI * rv[0]; + cosp = cos(d); + sinp = sin(d); + rv[1] = 1.0 - specjitter*rv[1]; + if (rv[1] <= FTINY) + d = 1.0; + else + 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); + 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 */ }