ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/normal.c
(Generate patch)

Comparing ray/src/rt/normal.c (file contents):
Revision 2.46 by greg, Thu Aug 28 03:22:16 2003 UTC vs.
Revision 2.85 by greg, Thu Dec 5 19:23:43 2024 UTC

# Line 14 | Line 14 | static const char RCSid[] = "$Id$";
14   #include "copyright.h"
15  
16   #include  "ray.h"
17
17   #include  "ambient.h"
18 <
18 > #include  "source.h"
19   #include  "otypes.h"
20 <
20 > #include  "rtotypes.h"
21   #include  "random.h"
22 + #include  "pmapmat.h"
23  
24   #ifndef  MAXITER
25   #define  MAXITER        10              /* maximum # specular ray attempts */
26   #endif
27                                          /* estimate of Fresnel function */
28 < #define  FRESNE(ci)     (exp(-5.85*(ci)) - 0.00287989916)
28 > #define  FRESNE(ci)     (exp(-5.85*(ci)) - 0.00202943064)
29 > #define  FRESTHRESH     0.017999        /* minimum specularity for approx. */
30  
30 static void  gaussamp();
31  
32   /*
33   *      This routine implements the isotropic Gaussian
# Line 54 | Line 54 | typedef struct {
54          OBJREC  *mp;            /* material pointer */
55          RAY  *rp;               /* ray pointer */
56          short  specfl;          /* specularity flags, defined above */
57 <        COLOR  mcolor;          /* color of this material */
58 <        COLOR  scolor;          /* color of specular component */
59 <        FVECT  vrefl;           /* vector in direction of reflected ray */
57 >        SCOLOR  mcolor;         /* color of this material */
58 >        SCOLOR  scolor;         /* color of specular component */
59          FVECT  prdir;           /* vector in transmitted direction */
60          double  alpha2;         /* roughness squared */
61          double  rdiff, rspec;   /* reflected specular, diffuse */
# Line 66 | Line 65 | typedef struct {
65          double  pdot;           /* perturbed dot product */
66   }  NORMDAT;             /* normal material data */
67  
68 + static void gaussamp(NORMDAT  *np);
69  
70 +
71   static void
72 < dirnorm(cval, np, ldir, omega)          /* compute source contribution */
73 < COLOR  cval;                    /* returned coefficient */
74 < register NORMDAT  *np;          /* material data */
75 < FVECT  ldir;                    /* light source direction */
76 < double  omega;                  /* light source size */
72 > dirnorm(                /* compute source contribution */
73 >        SCOLOR  scval,                  /* returned coefficient */
74 >        void  *nnp,                     /* material data */
75 >        FVECT  ldir,                    /* light source direction */
76 >        double  omega                   /* light source size */
77 > )
78   {
79 +        NORMDAT *np = nnp;
80          double  ldot;
81 <        double  ldiff;
82 <        double  dtmp, d2;
81 >        double  lrdiff, ltdiff;
82 >        double  dtmp, d2, d3, d4;
83          FVECT  vtmp;
84 <        COLOR  ctmp;
84 >        SCOLOR  sctmp;
85  
86 <        setcolor(cval, 0.0, 0.0, 0.0);
86 >        scolorblack(scval);
87  
88          ldot = DOT(np->pnorm, ldir);
89  
# Line 88 | Line 91 | double  omega;                 /* light source size */
91                  return;         /* wrong side */
92  
93                                  /* Fresnel estimate */
94 <        ldiff = np->rdiff;
95 <        if (np->specfl & SP_PURE && (np->rspec > FTINY) & (ldiff > FTINY))
96 <                ldiff *= 1. - FRESNE(fabs(ldot));
94 >        lrdiff = np->rdiff;
95 >        ltdiff = np->tdiff;
96 >        if (np->specfl & SP_PURE && np->rspec >= FRESTHRESH &&
97 >                        (lrdiff > FTINY) | (ltdiff > FTINY)) {
98 >                dtmp = 1. - FRESNE(fabs(ldot));
99 >                lrdiff *= dtmp;
100 >                ltdiff *= dtmp;
101 >        }
102  
103 <        if (ldot > FTINY && ldiff > FTINY) {
103 >        if ((ldot > FTINY) & (lrdiff > FTINY)) {
104                  /*
105                   *  Compute and add diffuse reflected component to returned
106                   *  color.  The diffuse reflected component will always be
107                   *  modified by the color of the material.
108                   */
109 <                copycolor(ctmp, np->mcolor);
110 <                dtmp = ldot * omega * ldiff / PI;
111 <                scalecolor(ctmp, dtmp);
112 <                addcolor(cval, ctmp);
109 >                copyscolor(sctmp, np->mcolor);
110 >                dtmp = ldot * omega * lrdiff * (1.0/PI);
111 >                scalescolor(sctmp, dtmp);
112 >                saddscolor(scval, sctmp);
113          }
114 <        if (ldot > FTINY && (np->specfl&(SP_REFL|SP_PURE)) == SP_REFL) {
114 >
115 >        if ((ldot < -FTINY) & (ltdiff > FTINY)) {
116                  /*
117 +                 *  Compute diffuse transmission.
118 +                 */
119 +                copyscolor(sctmp, np->mcolor);
120 +                dtmp = -ldot * omega * ltdiff * (1.0/PI);
121 +                scalescolor(sctmp, dtmp);
122 +                saddscolor(scval, sctmp);
123 +        }
124 +
125 +        if (ambRayInPmap(np->rp))
126 +                return;         /* specular already in photon map */
127 +
128 +        if ((ldot > FTINY) & ((np->specfl&(SP_REFL|SP_PURE)) == SP_REFL)) {
129 +                /*
130                   *  Compute specular reflection coefficient using
131 <                 *  gaussian distribution model.
131 >                 *  Gaussian distribution model.
132                   */
133                                                  /* roughness */
134                  dtmp = np->alpha2;
135                                                  /* + source if flat */
136                  if (np->specfl & SP_FLAT)
137 <                        dtmp += omega/(4.0*PI);
137 >                        dtmp += omega * (0.25/PI);
138                                                  /* half vector */
139 <                vtmp[0] = ldir[0] - np->rp->rdir[0];
118 <                vtmp[1] = ldir[1] - np->rp->rdir[1];
119 <                vtmp[2] = ldir[2] - np->rp->rdir[2];
139 >                VSUB(vtmp, ldir, np->rp->rdir);
140                  d2 = DOT(vtmp, np->pnorm);
141                  d2 *= d2;
142 <                d2 = (DOT(vtmp,vtmp) - d2) / d2;
143 <                                                /* gaussian */
144 <                dtmp = exp(-d2/dtmp)/(4.*PI*dtmp);
142 >                d3 = DOT(vtmp,vtmp);
143 >                d4 = (d3 - d2) / d2;
144 >                                                /* new W-G-M-D model */
145 >                dtmp = exp(-d4/dtmp) * d3 / (PI * d2*d2 * dtmp);
146                                                  /* worth using? */
147                  if (dtmp > FTINY) {
148 <                        copycolor(ctmp, np->scolor);
149 <                        dtmp *= omega * sqrt(ldot/np->pdot);
150 <                        scalecolor(ctmp, dtmp);
151 <                        addcolor(cval, ctmp);
148 >                        copyscolor(sctmp, np->scolor);
149 >                        dtmp *= ldot * omega;
150 >                        scalescolor(sctmp, dtmp);
151 >                        saddscolor(scval, sctmp);
152                  }
153          }
154 <        if (ldot < -FTINY && np->tdiff > FTINY) {
154 >        
155 >
156 >        if ((ldot < -FTINY) & ((np->specfl&(SP_TRAN|SP_PURE)) == SP_TRAN)) {
157                  /*
135                 *  Compute diffuse transmission.
136                 */
137                copycolor(ctmp, np->mcolor);
138                dtmp = -ldot * omega * np->tdiff / PI;
139                scalecolor(ctmp, dtmp);
140                addcolor(cval, ctmp);
141        }
142        if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_PURE)) == SP_TRAN) {
143                /*
158                   *  Compute specular transmission.  Specular transmission
159                   *  is always modified by material color.
160                   */
161                                                  /* roughness + source */
162 <                dtmp = np->alpha2 + omega/PI;
163 <                                                /* gaussian */
162 >                dtmp = np->alpha2 + omega*(1.0/PI);
163 >                                                /* Gaussian */
164                  dtmp = exp((2.*DOT(np->prdir,ldir)-2.)/dtmp)/(PI*dtmp);
165                                                  /* worth using? */
166                  if (dtmp > FTINY) {
167 <                        copycolor(ctmp, np->mcolor);
167 >                        copyscolor(sctmp, np->mcolor);
168                          dtmp *= np->tspec * omega * sqrt(-ldot/np->pdot);
169 <                        scalecolor(ctmp, dtmp);
170 <                        addcolor(cval, ctmp);
169 >                        scalescolor(sctmp, dtmp);
170 >                        saddscolor(scval, sctmp);
171                  }
172          }
173   }
174  
175  
176   int
177 < m_normal(m, r)                  /* color a ray that hit something normal */
178 < register OBJREC  *m;
179 < register RAY  *r;
177 > m_normal(                       /* color a ray that hit something normal */
178 >        OBJREC  *m,
179 >        RAY  *r
180 > )
181   {
182          NORMDAT  nd;
183          double  fest;
169        double  transtest, transdist;
170        double  mirtest, mirdist;
184          int     hastexture;
185          double  d;
186 <        COLOR  ctmp;
187 <        register int  i;
186 >        SCOLOR  sctmp;
187 >        int  i;
188 >
189 >        /* PMAP: skip transmitted shadow ray if accounted for in photon map */
190 >        /* No longer needed?
191 >        if (shadowRayInPmap(r) || ambRayInPmap(r))
192 >                return(1); */          
193 >                
194                                                  /* easy shadow test */
195          if (r->crtype & SHADOW && m->otype != MAT_TRANS)
196                  return(1);
# Line 180 | Line 199 | register RAY  *r;
199                  objerror(m, USER, "bad number of arguments");
200                                                  /* check for back side */
201          if (r->rod < 0.0) {
202 <                if (!backvis && m->otype != MAT_TRANS) {
202 >                if (!backvis) {
203                          raytrans(r);
204                          return(1);
205                  }
# Line 191 | Line 210 | register RAY  *r;
210          nd.mp = m;
211          nd.rp = r;
212                                                  /* get material color */
213 <        setcolor(nd.mcolor, m->oargs.farg[0],
213 >        setscolor(nd.mcolor, m->oargs.farg[0],
214                             m->oargs.farg[1],
215                             m->oargs.farg[2]);
216                                                  /* get roughness */
# Line 210 | Line 229 | register RAY  *r;
229                  nd.specfl |= SP_FLAT;
230          if (nd.pdot < .001)
231                  nd.pdot = .001;                 /* non-zero for dirnorm() */
232 <        multcolor(nd.mcolor, r->pcol);          /* modify material color */
214 <        mirtest = transtest = 0;
215 <        mirdist = transdist = r->rot;
232 >        smultscolor(nd.mcolor, r->pcol);        /* modify material color */
233          nd.rspec = m->oargs.farg[3];
234                                                  /* compute Fresnel approx. */
235 <        if (nd.specfl & SP_PURE && nd.rspec > FTINY) {
236 <                fest = FRESNE(r->rod);
235 >        if (nd.specfl & SP_PURE && nd.rspec >= FRESTHRESH) {
236 >                fest = FRESNE(nd.pdot);
237                  nd.rspec += fest*(1. - nd.rspec);
238          } else
239                  fest = 0.;
# Line 231 | Line 248 | register RAY  *r;
248                          if (!(nd.specfl & SP_PURE) &&
249                                          specthresh >= nd.tspec-FTINY)
250                                  nd.specfl |= SP_TBLT;
251 <                        if (!hastexture || r->crtype & SHADOW) {
251 >                        if (!hastexture || r->crtype & (SHADOW|AMBIENT)) {
252                                  VCOPY(nd.prdir, r->rdir);
236                                transtest = 2;
253                          } else {
254 <                                for (i = 0; i < 3; i++)         /* perturb */
255 <                                        nd.prdir[i] = r->rdir[i] - r->pert[i];
254 >                                                        /* perturb */
255 >                                VSUB(nd.prdir, r->rdir, r->pert);
256                                  if (DOT(nd.prdir, r->ron) < -FTINY)
257                                          normalize(nd.prdir);    /* OK */
258                                  else
# Line 245 | Line 261 | register RAY  *r;
261                  }
262          } else
263                  nd.tdiff = nd.tspec = nd.trans = 0.0;
264 +                                                /* diffuse reflection */
265 +        nd.rdiff = 1.0 - nd.trans - nd.rspec;
266                                                  /* transmitted ray */
267          if ((nd.specfl&(SP_TRAN|SP_PURE|SP_TBLT)) == (SP_TRAN|SP_PURE)) {
268                  RAY  lr;
269 <                if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) {
269 >                copyscolor(lr.rcoef, nd.mcolor);        /* modified by color */
270 >                scalescolor(lr.rcoef, nd.tspec);
271 >                if (rayorigin(&lr, TRANS, r, lr.rcoef) == 0) {
272                          VCOPY(lr.rdir, nd.prdir);
273                          rayvalue(&lr);
274 <                        scalecolor(lr.rcol, nd.tspec);
275 <                        multcolor(lr.rcol, nd.mcolor);  /* modified by color */
276 <                        addcolor(r->rcol, lr.rcol);
277 <                        transtest *= bright(lr.rcol);
278 <                        transdist = r->rot + lr.rt;
274 >                        smultscolor(lr.rcol, lr.rcoef);
275 >                        saddscolor(r->rcol, lr.rcol);
276 >                        if (nd.tspec >= 1.0-FTINY) {
277 >                                                /* completely transparent */
278 >                                smultscolor(lr.mcol, lr.rcoef);
279 >                                copyscolor(r->mcol, lr.mcol);
280 >                                r->rmt = r->rot + lr.rmt;
281 >                                r->rxt = r->rot + lr.rxt;
282 >                        } else if (nd.tspec > nd.tdiff + nd.rdiff)
283 >                                r->rxt = r->rot + raydistance(&lr);
284                  }
285 <        } else
261 <                transtest = 0;
285 >        }
286  
287 <        if (r->crtype & SHADOW) {               /* the rest is shadow */
264 <                r->rt = transdist;
287 >        if (r->crtype & SHADOW)                 /* the rest is shadow */
288                  return(1);
266        }
289                                                  /* get specular reflection */
290          if (nd.rspec > FTINY) {
291                  nd.specfl |= SP_REFL;
292                                                  /* compute specular color */
293                  if (m->otype != MAT_METAL) {
294 <                        setcolor(nd.scolor, nd.rspec, nd.rspec, nd.rspec);
294 >                        setscolor(nd.scolor, nd.rspec, nd.rspec, nd.rspec);
295                  } else if (fest > FTINY) {
296 <                        d = nd.rspec*(1. - fest);
297 <                        for (i = 0; i < 3; i++)
296 >                        d = m->oargs.farg[3]*(1. - fest);
297 >                        for (i = NCSAMP; i--; )
298                                  nd.scolor[i] = fest + nd.mcolor[i]*d;
299                  } else {
300 <                        copycolor(nd.scolor, nd.mcolor);
301 <                        scalecolor(nd.scolor, nd.rspec);
300 >                        copyscolor(nd.scolor, nd.mcolor);
301 >                        scalescolor(nd.scolor, nd.rspec);
302                  }
303                                                  /* check threshold */
304                  if (!(nd.specfl & SP_PURE) && specthresh >= nd.rspec-FTINY)
305                          nd.specfl |= SP_RBLT;
284                                                /* compute reflected ray */
285                for (i = 0; i < 3; i++)
286                        nd.vrefl[i] = r->rdir[i] + 2.*nd.pdot*nd.pnorm[i];
287                                                /* penetration? */
288                if (hastexture && DOT(nd.vrefl, r->ron) <= FTINY)
289                        for (i = 0; i < 3; i++)         /* safety measure */
290                                nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i];
306          }
307                                                  /* reflected ray */
308          if ((nd.specfl&(SP_REFL|SP_PURE|SP_RBLT)) == (SP_REFL|SP_PURE)) {
309                  RAY  lr;
310 <                if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) {
311 <                        VCOPY(lr.rdir, nd.vrefl);
310 >                if (rayorigin(&lr, REFLECTED, r, nd.scolor) == 0) {
311 >                                                /* compute reflected ray */
312 >                        VSUM(lr.rdir, r->rdir, nd.pnorm, 2.*nd.pdot);
313 >                                                /* penetration? */
314 >                        if (hastexture && DOT(lr.rdir, r->ron) <= FTINY)
315 >                                VSUM(lr.rdir, r->rdir, r->ron, 2.*r->rod);
316 >                        checknorm(lr.rdir);
317                          rayvalue(&lr);
318 <                        multcolor(lr.rcol, nd.scolor);
319 <                        addcolor(r->rcol, lr.rcol);
320 <                        if (!hastexture && nd.specfl & SP_FLAT) {
321 <                                mirtest = 2.*bright(lr.rcol);
322 <                                mirdist = r->rot + lr.rt;
323 <                        }
318 >                        smultscolor(lr.rcol, lr.rcoef);
319 >                        copyscolor(r->mcol, lr.rcol);
320 >                        saddscolor(r->rcol, lr.rcol);
321 >                        r->rmt = r->rot;
322 >                        if (nd.specfl & SP_FLAT &&
323 >                                        !hastexture | (r->crtype & AMBIENT))
324 >                                r->rmt += raydistance(&lr);
325                  }
326          }
306                                                /* diffuse reflection */
307        nd.rdiff = 1.0 - nd.trans - nd.rspec;
327  
328          if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY)
329                  return(1);                      /* 100% pure specular */
330  
331          if (!(nd.specfl & SP_PURE))
332 <                gaussamp(r, &nd);               /* checks *BLT flags */
332 >                gaussamp(&nd);                  /* checks *BLT flags */
333  
334          if (nd.rdiff > FTINY) {         /* ambient from this side */
335 <                ambient(ctmp, r, hastexture?nd.pnorm:r->ron);
336 <                if (nd.specfl & SP_RBLT)
337 <                        scalecolor(ctmp, 1.0-nd.trans);
338 <                else
339 <                        scalecolor(ctmp, nd.rdiff);
340 <                multcolor(ctmp, nd.mcolor);     /* modified by material color */
322 <                addcolor(r->rcol, ctmp);        /* add to returned color */
335 >                copyscolor(sctmp, nd.mcolor);   /* modified by material color */
336 >                scalescolor(sctmp, nd.rdiff);
337 >                if (nd.specfl & SP_RBLT)        /* add in specular as well? */
338 >                        saddscolor(sctmp, nd.scolor);
339 >                multambient(sctmp, r, nd.pnorm);
340 >                saddscolor(r->rcol, sctmp);     /* add to returned color */
341          }
342          if (nd.tdiff > FTINY) {         /* ambient from other side */
343 <                flipsurface(r);
344 <                if (hastexture) {
345 <                        FVECT  bnorm;
346 <                        bnorm[0] = -nd.pnorm[0];
347 <                        bnorm[1] = -nd.pnorm[1];
348 <                        bnorm[2] = -nd.pnorm[2];
349 <                        ambient(ctmp, r, bnorm);
350 <                } else
351 <                        ambient(ctmp, r, r->ron);
352 <                if (nd.specfl & SP_TBLT)
353 <                        scalecolor(ctmp, nd.trans);
354 <                else
337 <                        scalecolor(ctmp, nd.tdiff);
338 <                multcolor(ctmp, nd.mcolor);     /* modified by color */
339 <                addcolor(r->rcol, ctmp);
340 <                flipsurface(r);
343 >                FVECT  bnorm;
344 >                copyscolor(sctmp, nd.mcolor);   /* modified by color */
345 >                if (nd.specfl & SP_TBLT) {
346 >                        scalescolor(sctmp, nd.trans);
347 >                } else {
348 >                        scalescolor(sctmp, nd.tdiff);
349 >                }
350 >                bnorm[0] = -nd.pnorm[0];
351 >                bnorm[1] = -nd.pnorm[1];
352 >                bnorm[2] = -nd.pnorm[2];
353 >                multambient(sctmp, r, bnorm);
354 >                saddscolor(r->rcol, sctmp);
355          }
356                                          /* add direct component */
357          direct(r, dirnorm, &nd);
344                                        /* check distance */
345        d = bright(r->rcol);
346        if (transtest > d)
347                r->rt = transdist;
348        else if (mirtest > d)
349                r->rt = mirdist;
358  
359          return(1);
360   }
361  
362  
363   static void
364 < gaussamp(r, np)                 /* sample gaussian specular */
365 < RAY  *r;
366 < register NORMDAT  *np;
364 > gaussamp(                       /* sample Gaussian specular */
365 >        NORMDAT  *np
366 > )
367   {
368          RAY  sr;
369          FVECT  u, v, h;
370          double  rv[2];
371          double  d, sinp, cosp;
372 <        int  niter;
373 <        register int  i;
372 >        SCOLOR  scol;
373 >        int  maxiter, ntrials, nstarget, nstaken;
374 >        int  i;
375                                          /* quick test */
376          if ((np->specfl & (SP_REFL|SP_RBLT)) != SP_REFL &&
377                          (np->specfl & (SP_TRAN|SP_TBLT)) != SP_TRAN)
378                  return;
379                                          /* set up sample coordinates */
380 <        v[0] = v[1] = v[2] = 0.0;
372 <        for (i = 0; i < 3; i++)
373 <                if (np->pnorm[i] < 0.6 && np->pnorm[i] > -0.6)
374 <                        break;
375 <        v[i] = 1.0;
376 <        fcross(u, v, np->pnorm);
377 <        normalize(u);
380 >        getperpendicular(u, np->pnorm, rand_samp);
381          fcross(v, np->pnorm, u);
382                                          /* compute reflection */
383          if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL &&
384 <                        rayorigin(&sr, r, SPECULAR, np->rspec) == 0) {
385 <                dimlist[ndims++] = (int)np->mp;
386 <                for (niter = 0; niter < MAXITER; niter++) {
387 <                        if (niter)
384 >                        rayorigin(&sr, RSPECULAR, np->rp, np->scolor) == 0) {
385 >                nstarget = 1;
386 >                if (specjitter > 1.5) { /* multiple samples? */
387 >                        nstarget = specjitter*np->rp->rweight + .5;
388 >                        if (sr.rweight <= minweight*nstarget)
389 >                                nstarget = sr.rweight/minweight;
390 >                        if (nstarget > 1) {
391 >                                d = 1./nstarget;
392 >                                scalescolor(sr.rcoef, d);
393 >                                sr.rweight *= d;
394 >                        } else
395 >                                nstarget = 1;
396 >                }
397 >                scolorblack(scol);
398 >                dimlist[ndims++] = (int)(size_t)np->mp;
399 >                maxiter = MAXITER*nstarget;
400 >                for (nstaken = ntrials = 0; nstaken < nstarget &&
401 >                                                ntrials < maxiter; ntrials++) {
402 >                        if (ntrials)
403                                  d = frandom();
404                          else
405                                  d = urand(ilhash(dimlist,ndims)+samplendx);
# Line 389 | Line 407 | register NORMDAT  *np;
407                          d = 2.0*PI * rv[0];
408                          cosp = tcos(d);
409                          sinp = tsin(d);
410 <                        rv[1] = 1.0 - specjitter*rv[1];
410 >                        if ((0. <= specjitter) & (specjitter < 1.))
411 >                                rv[1] = 1.0 - specjitter*rv[1];
412                          if (rv[1] <= FTINY)
413                                  d = 1.0;
414                          else
415                                  d = sqrt( np->alpha2 * -log(rv[1]) );
416                          for (i = 0; i < 3; i++)
417                                  h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*v[i]);
418 <                        d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d);
419 <                        for (i = 0; i < 3; i++)
420 <                                sr.rdir[i] = r->rdir[i] + d*h[i];
421 <                        if (DOT(sr.rdir, r->ron) > FTINY) {
418 >                        d = -2.0 * DOT(h, np->rp->rdir) / (1.0 + d*d);
419 >                        VSUM(sr.rdir, np->rp->rdir, h, d);
420 >                                                /* sample rejection test */
421 >                        if ((d = DOT(sr.rdir, np->rp->ron)) <= FTINY)
422 >                                continue;
423 >                        checknorm(sr.rdir);
424 >                        if (nstarget > 1) {     /* W-G-M-D adjustment */
425 >                                if (nstaken) rayclear(&sr);
426                                  rayvalue(&sr);
427 <                                multcolor(sr.rcol, np->scolor);
428 <                                addcolor(r->rcol, sr.rcol);
429 <                                break;
427 >                                d = 2./(1. + np->rp->rod/d);
428 >                                scalescolor(sr.rcol, d);
429 >                                saddscolor(scol, sr.rcol);
430 >                        } else {
431 >                                rayvalue(&sr);
432 >                                smultscolor(sr.rcol, sr.rcoef);
433 >                                saddscolor(np->rp->rcol, sr.rcol);
434                          }
435 +                        ++nstaken;
436                  }
437 +                if (nstarget > 1) {             /* final W-G-M-D weighting */
438 +                        smultscolor(scol, sr.rcoef);
439 +                        d = (double)nstarget/ntrials;
440 +                        scalescolor(scol, d);
441 +                        saddscolor(np->rp->rcol, scol);
442 +                }
443                  ndims--;
444          }
445                                          /* compute transmission */
446 +        copyscolor(sr.rcoef, np->mcolor);       /* modified by color */
447 +        scalescolor(sr.rcoef, np->tspec);
448          if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN &&
449 <                        rayorigin(&sr, r, SPECULAR, np->tspec) == 0) {
450 <                dimlist[ndims++] = (int)np->mp;
451 <                for (niter = 0; niter < MAXITER; niter++) {
452 <                        if (niter)
449 >                        rayorigin(&sr, TSPECULAR, np->rp, sr.rcoef) == 0) {
450 >                nstarget = 1;
451 >                if (specjitter > 1.5) { /* multiple samples? */
452 >                        nstarget = specjitter*np->rp->rweight + .5;
453 >                        if (sr.rweight <= minweight*nstarget)
454 >                                nstarget = sr.rweight/minweight;
455 >                        if (nstarget > 1) {
456 >                                d = 1./nstarget;
457 >                                scalescolor(sr.rcoef, d);
458 >                                sr.rweight *= d;
459 >                        } else
460 >                                nstarget = 1;
461 >                }
462 >                dimlist[ndims++] = (int)(size_t)np->mp;
463 >                maxiter = MAXITER*nstarget;
464 >                for (nstaken = ntrials = 0; nstaken < nstarget &&
465 >                                                ntrials < maxiter; ntrials++) {
466 >                        if (ntrials)
467                                  d = frandom();
468                          else
469 <                                d = urand(ilhash(dimlist,ndims)+1823+samplendx);
469 >                                d = urand(ilhash(dimlist,ndims)+samplendx);
470                          multisamp(rv, 2, d);
471                          d = 2.0*PI * rv[0];
472                          cosp = tcos(d);
473                          sinp = tsin(d);
474 <                        rv[1] = 1.0 - specjitter*rv[1];
474 >                        if ((0. <= specjitter) & (specjitter < 1.))
475 >                                rv[1] = 1.0 - specjitter*rv[1];
476                          if (rv[1] <= FTINY)
477                                  d = 1.0;
478                          else
479                                  d = sqrt( np->alpha2 * -log(rv[1]) );
480                          for (i = 0; i < 3; i++)
481                                  sr.rdir[i] = np->prdir[i] + d*(cosp*u[i] + sinp*v[i]);
482 <                        if (DOT(sr.rdir, r->ron) < -FTINY) {
483 <                                normalize(sr.rdir);     /* OK, normalize */
484 <                                rayvalue(&sr);
485 <                                scalecolor(sr.rcol, np->tspec);
486 <                                multcolor(sr.rcol, np->mcolor); /* modified */
487 <                                addcolor(r->rcol, sr.rcol);
488 <                                break;
489 <                        }
482 >                                                /* sample rejection test */
483 >                        if (DOT(sr.rdir, np->rp->ron) >= -FTINY)
484 >                                continue;
485 >                        normalize(sr.rdir);     /* OK, normalize */
486 >                        if (nstaken)            /* multi-sampling */
487 >                                rayclear(&sr);
488 >                        rayvalue(&sr);
489 >                        smultscolor(sr.rcol, sr.rcoef);
490 >                        saddscolor(np->rp->rcol, sr.rcol);
491 >                        ++nstaken;
492                  }
493                  ndims--;
494          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines