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.26 by greg, Tue Aug 24 12:59:28 1993 UTC vs.
Revision 2.71 by greg, Tue May 26 13:21:07 2015 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines