--- ray/src/rt/normal.c 2003/02/22 02:07:29 2.38 +++ ray/src/rt/normal.c 2023/11/15 18:02:53 2.83 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: normal.c,v 2.38 2003/02/22 02:07:29 greg Exp $"; +static const char RCSid[] = "$Id: normal.c,v 2.83 2023/11/15 18:02:53 greg Exp $"; #endif /* * normal.c - shading function for normal materials. @@ -11,76 +11,23 @@ static const char RCSid[] = "$Id: normal.c,v 2.38 2003 * Later changes described in delta comments. */ -/* ==================================================================== - * The Radiance Software License, Version 1.0 - * - * Copyright (c) 1990 - 2002 The Regents of the University of California, - * through Lawrence Berkeley National Laboratory. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * - * 3. The end-user documentation included with the redistribution, - * if any, must include the following acknowledgment: - * "This product includes Radiance software - * (http://radsite.lbl.gov/) - * developed by the Lawrence Berkeley National Laboratory - * (http://www.lbl.gov/)." - * Alternately, this acknowledgment may appear in the software itself, - * if and wherever such third-party acknowledgments normally appear. - * - * 4. The names "Radiance," "Lawrence Berkeley National Laboratory" - * and "The Regents of the University of California" must - * not be used to endorse or promote products derived from this - * software without prior written permission. For written - * permission, please contact radiance@radsite.lbl.gov. - * - * 5. Products derived from this software may not be called "Radiance", - * nor may "Radiance" appear in their name, without prior written - * permission of Lawrence Berkeley National Laboratory. - * - * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR - * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF - * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT - * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * ==================================================================== - * - * This software consists of voluntary contributions made by many - * individuals on behalf of Lawrence Berkeley National Laboratory. For more - * information on Lawrence Berkeley National Laboratory, please see - * . - */ +#include "copyright.h" #include "ray.h" - +#include "ambient.h" +#include "source.h" #include "otypes.h" - +#include "rtotypes.h" #include "random.h" +#include "pmapmat.h" #ifndef MAXITER #define MAXITER 10 /* maximum # specular ray attempts */ #endif /* estimate of Fresnel function */ -#define FRESNE(ci) (exp(-6.0*(ci)) - 0.00247875217) +#define FRESNE(ci) (exp(-5.85*(ci)) - 0.00202943064) +#define FRESTHRESH 0.017999 /* minimum specularity for approx. */ -static void gaussamp(); /* * This routine implements the isotropic Gaussian @@ -107,8 +54,8 @@ 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 direction of reflected ray */ FVECT prdir; /* vector in transmitted direction */ double alpha2; /* roughness squared */ @@ -119,21 +66,25 @@ typedef struct { double pdot; /* perturbed dot product */ } NORMDAT; /* normal material data */ +static void gaussamp(NORMDAT *np); + static void -dirnorm(cval, np, ldir, omega) /* compute source contribution */ -COLOR cval; /* returned coefficient */ -register NORMDAT *np; /* material data */ -FVECT ldir; /* light source direction */ -double omega; /* light source size */ +dirnorm( /* compute source contribution */ + SCOLOR scval, /* returned coefficient */ + void *nnp, /* material data */ + FVECT ldir, /* light source direction */ + double omega /* light source size */ +) { + NORMDAT *np = nnp; double ldot; - double ldiff; - double dtmp, d2; + double lrdiff, ltdiff; + double dtmp, d2, d3, d4; FVECT vtmp; - COLOR ctmp; + SCOLOR sctmp; - setcolor(cval, 0.0, 0.0, 0.0); + scolorblack(scval); ldot = DOT(np->pnorm, ldir); @@ -141,90 +92,106 @@ double omega; /* light source size */ return; /* wrong side */ /* Fresnel estimate */ - ldiff = np->rdiff; - if (np->specfl & SP_PURE && (np->rspec > FTINY & ldiff > FTINY)) - ldiff *= 1. - FRESNE(fabs(ldot)); + lrdiff = np->rdiff; + ltdiff = np->tdiff; + if (np->specfl & SP_PURE && np->rspec >= FRESTHRESH && + (lrdiff > FTINY) | (ltdiff > FTINY)) { + dtmp = 1. - FRESNE(fabs(ldot)); + lrdiff *= dtmp; + ltdiff *= dtmp; + } - if (ldot > FTINY && ldiff > FTINY) { + if ((ldot > FTINY) & (lrdiff > 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 * ldiff / PI; - scalecolor(ctmp, dtmp); - addcolor(cval, ctmp); + copyscolor(sctmp, np->mcolor); + dtmp = ldot * omega * lrdiff * (1.0/PI); + scalescolor(sctmp, dtmp); + saddscolor(scval, sctmp); } - if (ldot > FTINY && (np->specfl&(SP_REFL|SP_PURE)) == SP_REFL) { + + if ((ldot < -FTINY) & (ltdiff > FTINY)) { /* + * Compute diffuse transmission. + */ + copyscolor(sctmp, np->mcolor); + dtmp = -ldot * omega * ltdiff * (1.0/PI); + scalescolor(sctmp, dtmp); + saddscolor(scval, sctmp); + } + + if (ambRayInPmap(np->rp)) + return; /* specular already in photon map */ + + if ((ldot > FTINY) & ((np->specfl&(SP_REFL|SP_PURE)) == SP_REFL)) { + /* * Compute specular reflection coefficient using - * gaussian distribution model. + * Gaussian distribution model. */ /* roughness */ dtmp = np->alpha2; /* + source if flat */ if (np->specfl & SP_FLAT) - dtmp += omega/(4.0*PI); + dtmp += omega * (0.25/PI); /* half vector */ - vtmp[0] = ldir[0] - np->rp->rdir[0]; - vtmp[1] = ldir[1] - np->rp->rdir[1]; - vtmp[2] = ldir[2] - np->rp->rdir[2]; + VSUB(vtmp, ldir, np->rp->rdir); d2 = DOT(vtmp, np->pnorm); d2 *= d2; - d2 = (DOT(vtmp,vtmp) - d2) / d2; - /* gaussian */ - dtmp = exp(-d2/dtmp)/(4.*PI*dtmp); + d3 = DOT(vtmp,vtmp); + d4 = (d3 - d2) / d2; + /* new W-G-M-D model */ + dtmp = exp(-d4/dtmp) * d3 / (PI * d2*d2 * dtmp); /* worth using? */ if (dtmp > FTINY) { - copycolor(ctmp, np->scolor); - dtmp *= omega * sqrt(ldot/np->pdot); - scalecolor(ctmp, dtmp); - addcolor(cval, ctmp); + copyscolor(sctmp, np->scolor); + dtmp *= ldot * omega; + scalescolor(sctmp, dtmp); + saddscolor(scval, sctmp); } } - if (ldot < -FTINY && np->tdiff > FTINY) { + + + if ((ldot < -FTINY) & ((np->specfl&(SP_TRAN|SP_PURE)) == SP_TRAN)) { /* - * Compute diffuse transmission. - */ - copycolor(ctmp, np->mcolor); - dtmp = -ldot * omega * np->tdiff / PI; - scalecolor(ctmp, dtmp); - addcolor(cval, ctmp); - } - if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_PURE)) == SP_TRAN) { - /* * Compute specular transmission. Specular transmission * is always modified by material color. */ /* roughness + source */ - dtmp = np->alpha2 + omega/PI; - /* gaussian */ + dtmp = np->alpha2 + omega*(1.0/PI); + /* Gaussian */ dtmp = exp((2.*DOT(np->prdir,ldir)-2.)/dtmp)/(PI*dtmp); /* worth using? */ if (dtmp > FTINY) { - copycolor(ctmp, np->mcolor); + copyscolor(sctmp, np->mcolor); dtmp *= np->tspec * omega * sqrt(-ldot/np->pdot); - scalecolor(ctmp, dtmp); - addcolor(cval, ctmp); + scalescolor(sctmp, dtmp); + saddscolor(scval, sctmp); } } } int -m_normal(m, r) /* color a ray that hit something normal */ -register OBJREC *m; -register RAY *r; +m_normal( /* color a ray that hit something normal */ + OBJREC *m, + RAY *r +) { NORMDAT nd; double fest; - double transtest, transdist; - double mirtest, mirdist; int hastexture; double d; - COLOR ctmp; - register int i; + SCOLOR sctmp; + int i; + + /* PMAP: skip transmitted shadow ray if accounted for in photon map */ + /* No longer needed? + if (shadowRayInPmap(r) || ambRayInPmap(r)) + return(1); */ + /* easy shadow test */ if (r->crtype & SHADOW && m->otype != MAT_TRANS) return(1); @@ -233,16 +200,18 @@ register RAY *r; objerror(m, USER, "bad number of arguments"); /* check for back side */ if (r->rod < 0.0) { - if (!backvis && m->otype != MAT_TRANS) { + if (!backvis) { raytrans(r); return(1); } + raytexture(r, m->omod); flipsurface(r); /* reorient if backvis */ - } + } else + raytexture(r, m->omod); nd.mp = m; nd.rp = r; /* get material color */ - setcolor(nd.mcolor, m->oargs.farg[0], + setscolor(nd.mcolor, m->oargs.farg[0], m->oargs.farg[1], m->oargs.farg[2]); /* get roughness */ @@ -250,25 +219,22 @@ register RAY *r; nd.alpha2 = m->oargs.farg[4]; if ((nd.alpha2 *= nd.alpha2) <= FTINY) nd.specfl |= SP_PURE; - if (r->ro != NULL && isflat(r->ro->otype)) - nd.specfl |= SP_FLAT; - /* get modifiers */ - raytexture(r, m->omod); - if (hastexture = DOT(r->pert,r->pert) > FTINY*FTINY) + + if ( (hastexture = (DOT(r->pert,r->pert) > FTINY*FTINY)) ) { nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ - else { + } else { VCOPY(nd.pnorm, r->ron); nd.pdot = r->rod; } + if (r->ro != NULL && isflat(r->ro->otype)) + nd.specfl |= SP_FLAT; if (nd.pdot < .001) nd.pdot = .001; /* non-zero for dirnorm() */ - multcolor(nd.mcolor, r->pcol); /* modify material color */ - mirtest = transtest = 0; - mirdist = transdist = r->rot; + smultscolor(nd.mcolor, r->pcol); /* modify material color */ nd.rspec = m->oargs.farg[3]; /* compute Fresnel approx. */ - if (nd.specfl & SP_PURE && nd.rspec > FTINY) { - fest = FRESNE(r->rod); + if (nd.specfl & SP_PURE && nd.rspec >= FRESTHRESH) { + fest = FRESNE(nd.pdot); nd.rspec += fest*(1. - nd.rspec); } else fest = 0.; @@ -283,12 +249,11 @@ register RAY *r; if (!(nd.specfl & SP_PURE) && specthresh >= nd.tspec-FTINY) nd.specfl |= SP_TBLT; - if (!hastexture || r->crtype & SHADOW) { + if (!hastexture || r->crtype & (SHADOW|AMBIENT)) { VCOPY(nd.prdir, r->rdir); - transtest = 2; } else { - for (i = 0; i < 3; i++) /* perturb */ - nd.prdir[i] = r->rdir[i] - r->pert[i]; + /* perturb */ + VSUB(nd.prdir, r->rdir, r->pert); if (DOT(nd.prdir, r->ron) < -FTINY) normalize(nd.prdir); /* OK */ else @@ -297,143 +262,151 @@ register RAY *r; } } else nd.tdiff = nd.tspec = nd.trans = 0.0; + /* diffuse reflection */ + nd.rdiff = 1.0 - nd.trans - nd.rspec; /* transmitted ray */ if ((nd.specfl&(SP_TRAN|SP_PURE|SP_TBLT)) == (SP_TRAN|SP_PURE)) { RAY lr; - if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) { + copyscolor(lr.rcoef, nd.mcolor); /* modified by color */ + scalescolor(lr.rcoef, nd.tspec); + if (rayorigin(&lr, TRANS, r, lr.rcoef) == 0) { VCOPY(lr.rdir, nd.prdir); rayvalue(&lr); - scalecolor(lr.rcol, nd.tspec); - multcolor(lr.rcol, nd.mcolor); /* modified by color */ - addcolor(r->rcol, lr.rcol); - transtest *= bright(lr.rcol); - transdist = r->rot + lr.rt; + smultscolor(lr.rcol, lr.rcoef); + saddscolor(r->rcol, lr.rcol); + if (nd.tspec >= 1.0-FTINY) { + /* completely transparent */ + smultscolor(lr.mcol, lr.rcoef); + copyscolor(r->mcol, lr.mcol); + r->rmt = r->rot + lr.rmt; + r->rxt = r->rot + lr.rxt; + } else if (nd.tspec > nd.tdiff + nd.rdiff) + r->rxt = r->rot + raydistance(&lr); } - } else - transtest = 0; + } - if (r->crtype & SHADOW) { /* the rest is shadow */ - r->rt = transdist; + if (r->crtype & SHADOW) /* the rest is shadow */ return(1); - } /* get specular reflection */ if (nd.rspec > FTINY) { nd.specfl |= SP_REFL; /* compute specular color */ if (m->otype != MAT_METAL) { - setcolor(nd.scolor, nd.rspec, nd.rspec, nd.rspec); + setscolor(nd.scolor, nd.rspec, nd.rspec, nd.rspec); } else if (fest > FTINY) { - d = nd.rspec*(1. - fest); - for (i = 0; i < 3; i++) + d = m->oargs.farg[3]*(1. - fest); + for (i = NCSAMP; i--; ) nd.scolor[i] = fest + nd.mcolor[i]*d; } else { - copycolor(nd.scolor, nd.mcolor); - scalecolor(nd.scolor, nd.rspec); + copyscolor(nd.scolor, nd.mcolor); + scalescolor(nd.scolor, nd.rspec); } /* check threshold */ if (!(nd.specfl & SP_PURE) && specthresh >= nd.rspec-FTINY) nd.specfl |= SP_RBLT; /* compute reflected ray */ - for (i = 0; i < 3; i++) - nd.vrefl[i] = r->rdir[i] + 2.*nd.pdot*nd.pnorm[i]; + VSUM(nd.vrefl, r->rdir, nd.pnorm, 2.*nd.pdot); /* penetration? */ if (hastexture && DOT(nd.vrefl, r->ron) <= FTINY) - 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.*r->rod); + checknorm(nd.vrefl); } /* reflected ray */ if ((nd.specfl&(SP_REFL|SP_PURE|SP_RBLT)) == (SP_REFL|SP_PURE)) { RAY lr; - if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) { + if (rayorigin(&lr, REFLECTED, r, nd.scolor) == 0) { VCOPY(lr.rdir, nd.vrefl); rayvalue(&lr); - multcolor(lr.rcol, nd.scolor); - addcolor(r->rcol, lr.rcol); - if (!hastexture && nd.specfl & SP_FLAT) { - mirtest = 2.*bright(lr.rcol); - mirdist = r->rot + lr.rt; - } + smultscolor(lr.rcol, lr.rcoef); + copyscolor(r->mcol, lr.rcol); + saddscolor(r->rcol, lr.rcol); + r->rmt = r->rot; + if (nd.specfl & SP_FLAT && + !hastexture | (r->crtype & AMBIENT)) + r->rmt += raydistance(&lr); } } - /* diffuse reflection */ - nd.rdiff = 1.0 - nd.trans - nd.rspec; if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY) return(1); /* 100% pure specular */ if (!(nd.specfl & SP_PURE)) - gaussamp(r, &nd); /* checks *BLT flags */ + gaussamp(&nd); /* checks *BLT flags */ if (nd.rdiff > FTINY) { /* ambient from this side */ - ambient(ctmp, r, hastexture?nd.pnorm:r->ron); - 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 */ + copyscolor(sctmp, nd.mcolor); /* modified by material color */ + scalescolor(sctmp, nd.rdiff); + if (nd.specfl & SP_RBLT) /* add in specular as well? */ + saddscolor(sctmp, nd.scolor); + multambient(sctmp, r, hastexture ? nd.pnorm : r->ron); + saddscolor(r->rcol, sctmp); /* add to returned color */ } if (nd.tdiff > FTINY) { /* ambient from other side */ + copyscolor(sctmp, nd.mcolor); /* modified by color */ + if (nd.specfl & SP_TBLT) { + scalescolor(sctmp, nd.trans); + } else { + scalescolor(sctmp, nd.tdiff); + } flipsurface(r); if (hastexture) { FVECT bnorm; bnorm[0] = -nd.pnorm[0]; bnorm[1] = -nd.pnorm[1]; bnorm[2] = -nd.pnorm[2]; - ambient(ctmp, r, bnorm); + multambient(sctmp, r, bnorm); } else - ambient(ctmp, r, r->ron); - if (nd.specfl & SP_TBLT) - scalecolor(ctmp, nd.trans); - else - scalecolor(ctmp, nd.tdiff); - multcolor(ctmp, nd.mcolor); /* modified by color */ - addcolor(r->rcol, ctmp); + multambient(sctmp, r, r->ron); + saddscolor(r->rcol, sctmp); flipsurface(r); } /* add direct component */ direct(r, dirnorm, &nd); - /* check distance */ - d = bright(r->rcol); - if (transtest > d) - r->rt = transdist; - else if (mirtest > d) - r->rt = mirdist; return(1); } static void -gaussamp(r, np) /* sample gaussian specular */ -RAY *r; -register NORMDAT *np; +gaussamp( /* sample Gaussian specular */ + NORMDAT *np +) { RAY sr; FVECT u, v, h; double rv[2]; double d, sinp, cosp; - int niter; - register int i; + SCOLOR scol; + int maxiter, ntrials, nstarget, nstaken; + int i; /* quick test */ if ((np->specfl & (SP_REFL|SP_RBLT)) != SP_REFL && (np->specfl & (SP_TRAN|SP_TBLT)) != SP_TRAN) return; /* 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); + getperpendicular(u, np->pnorm, rand_samp); 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 (niter = 0; niter < MAXITER; niter++) { - if (niter) + 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; + scalescolor(sr.rcoef, d); + sr.rweight *= d; + } else + nstarget = 1; + } + scolorblack(scol); + 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); @@ -441,53 +414,88 @@ register NORMDAT *np; d = 2.0*PI * rv[0]; cosp = tcos(d); sinp = tsin(d); - rv[1] = 1.0 - specjitter*rv[1]; + if ((0. <= specjitter) & (specjitter < 1.)) + 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) { + 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); - multcolor(sr.rcol, np->scolor); - addcolor(r->rcol, sr.rcol); - break; + d = 2./(1. + np->rp->rod/d); + scalescolor(sr.rcol, d); + saddscolor(scol, sr.rcol); + } else { + rayvalue(&sr); + smultscolor(sr.rcol, sr.rcoef); + saddscolor(np->rp->rcol, sr.rcol); } + ++nstaken; } + if (nstarget > 1) { /* final W-G-M-D weighting */ + smultscolor(scol, sr.rcoef); + d = (double)nstarget/ntrials; + scalescolor(scol, d); + saddscolor(np->rp->rcol, scol); + } ndims--; } /* compute transmission */ + copyscolor(sr.rcoef, np->mcolor); /* modified by color */ + scalescolor(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; - for (niter = 0; niter < MAXITER; niter++) { - if (niter) + 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; + scalescolor(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); + d = urand(ilhash(dimlist,ndims)+samplendx); multisamp(rv, 2, d); d = 2.0*PI * rv[0]; cosp = tcos(d); sinp = tsin(d); - rv[1] = 1.0 - specjitter*rv[1]; + if ((0. <= specjitter) & (specjitter < 1.)) + 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++) sr.rdir[i] = np->prdir[i] + d*(cosp*u[i] + sinp*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); /* modified */ - addcolor(r->rcol, sr.rcol); - break; - } + /* sample rejection test */ + if (DOT(sr.rdir, np->rp->ron) >= -FTINY) + continue; + normalize(sr.rdir); /* OK, normalize */ + if (nstaken) /* multi-sampling */ + rayclear(&sr); + rayvalue(&sr); + smultscolor(sr.rcol, sr.rcoef); + saddscolor(np->rp->rcol, sr.rcol); + ++nstaken; } ndims--; }