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 1.1 by greg, Thu Feb 2 10:41:30 1989 UTC vs.
Revision 2.87 by greg, Fri Dec 20 16:29:50 2024 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1986 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 11 | Line 8 | static char SCCSid[] = "$SunId$ LBL";
8   *     12/19/85 - added stuff for metals.
9   *     6/26/87 - improved specular model.
10   *     9/28/87 - added model for translucent materials.
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
19   #include  "otypes.h"
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.00202943064)
29 + #define  FRESTHRESH     0.017999        /* minimum specularity for approx. */
30 +
31 +
32   /*
33 < *      This routine uses portions of the reflection
34 < *  model described by Cook and Torrance.
25 < *      The computation of specular components has been simplified by
26 < *  numerous approximations and ommisions to improve speed.
33 > *      This routine implements the isotropic Gaussian
34 > *  model described by Ward in Siggraph `92 article.
35   *      We orient the surface towards the incoming ray, so a single
36   *  surface can be used to represent an infinitely thin object.
37   *
# Line 34 | Line 42 | static char SCCSid[] = "$SunId$ LBL";
42   *      red     grn     blu     rspec   rough   trans   tspec
43   */
44  
45 < #define  BSPEC(m)       (6.0)           /* specularity parameter b */
45 >                                /* specularity flags */
46 > #define  SP_REFL        01              /* has reflected specular component */
47 > #define  SP_TRAN        02              /* has transmitted specular */
48 > #define  SP_PURE        04              /* purely specular (zero roughness) */
49 > #define  SP_FLAT        010             /* flat reflecting surface */
50 > #define  SP_RBLT        020             /* reflection below sample threshold */
51 > #define  SP_TBLT        040             /* transmission below threshold */
52  
53 <
54 < m_normal(m, r)                  /* color a ray which hit something normal */
55 < register OBJREC  *m;
56 < register RAY  *r;
57 < {
58 <        double  exp();
59 <        COLOR  mcolor;          /* color of this material */
60 <        COLOR  scolor;          /* color of specular component */
47 <        FVECT  vrefl;           /* vector in direction of reflected ray */
48 <        double  alpha2;         /* roughness squared times 2 */
49 <        RAY  lr;                /* ray to illumination source */
53 > typedef struct {
54 >        OBJREC  *mp;            /* material pointer */
55 >        RAY  *rp;               /* ray pointer */
56 >        short  specfl;          /* specularity flags, defined above */
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 */
62          double  trans;          /* transmissivity */
63          double  tdiff, tspec;   /* transmitted specular, diffuse */
64          FVECT  pnorm;           /* perturbed surface normal */
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(                /* 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  omega;
82 <        double  dtmp;
83 <        COLOR  ctmp;
84 <        register int  i;
81 >        double  lrdiff, ltdiff;
82 >        double  dtmp, d2, d3, d4;
83 >        FVECT  vtmp;
84 >        SCOLOR  sctmp;
85  
86 <        if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5))
87 <                objerror(m, USER, "bad # arguments");
86 >        scolorblack(scval);
87 >
88 >        ldot = DOT(np->pnorm, ldir);
89 >
90 >        if (ldot < 0.0 ? np->trans <= FTINY : np->trans >= 1.0-FTINY)
91 >                return;         /* wrong side */
92 >
93 >                                /* Fresnel estimate */
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) & (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 >                copyscolor(sctmp, np->mcolor);
110 >                dtmp = ldot * omega * lrdiff * (1.0/PI);
111 >                scalescolor(sctmp, dtmp);
112 >                saddscolor(scval, sctmp);
113 >        }
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.
132 >                 */
133 >                                                /* roughness */
134 >                dtmp = np->alpha2;
135 >                                                /* + source if flat */
136 >                if (np->specfl & SP_FLAT)
137 >                        dtmp += (1. - dstrsrc) * omega * (0.25/PI);
138 >                                                /* half vector */
139 >                VSUB(vtmp, ldir, np->rp->rdir);
140 >                d2 = DOT(vtmp, np->pnorm);
141 >                d2 *= d2;
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 >                        copyscolor(sctmp, np->scolor);
149 >                        dtmp *= ldot * omega;
150 >                        scalescolor(sctmp, dtmp);
151 >                        saddscolor(scval, sctmp);
152 >                }
153 >        }
154 >        
155 >
156 >        if ((ldot < -FTINY) & ((np->specfl&(SP_TRAN|SP_PURE)) == SP_TRAN)) {
157 >                /*
158 >                 *  Compute specular transmission.  Specular transmission
159 >                 *  is always modified by material color.
160 >                 */
161 >                                                /* roughness + source */
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 >                        copyscolor(sctmp, np->mcolor);
168 >                        dtmp *= np->tspec * omega * sqrt(-ldot/np->pdot);
169 >                        scalescolor(sctmp, dtmp);
170 >                        saddscolor(scval, sctmp);
171 >                }
172 >        }
173 > }
174 >
175 >
176 > int
177 > m_normal(                       /* color a ray that hit something normal */
178 >        OBJREC  *m,
179 >        RAY  *r
180 > )
181 > {
182 >        NORMDAT  nd;
183 >        double  fest;
184 >        int     hastexture;
185 >        double  d;
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;
196 >                return(1);
197 >
198 >        if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5))
199 >                objerror(m, USER, "bad number of arguments");
200 >                                                /* check for back side */
201 >        if (r->rod < 0.0) {
202 >                if (!backvis) {
203 >                        raytrans(r);
204 >                        return(1);
205 >                }
206 >                raytexture(r, m->omod);
207 >                flipsurface(r);                 /* reorient if backvis */
208 >        } else
209 >                raytexture(r, m->omod);
210 >        nd.mp = m;
211 >        nd.rp = r;
212                                                  /* get material color */
213 <        setcolor(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 */
217 <        alpha2 = m->oargs.farg[4];
218 <        alpha2 *= 2.0 * alpha2;
219 <                                                /* reorient if necessary */
220 <        if (r->rod < 0.0)
75 <                flipsurface(r);
76 <                                                /* get modifiers */
77 <        raytexture(r, m->omod);
78 <        pdot = raynormal(pnorm, r);             /* perturb normal */
79 <        multcolor(mcolor, r->pcol);             /* modify material color */
80 <                                                /* get specular component */
81 <        rspec = m->oargs.farg[3];
217 >        nd.specfl = 0;
218 >        nd.alpha2 = m->oargs.farg[4];
219 >        if ((nd.alpha2 *= nd.alpha2) <= FTINY)
220 >                nd.specfl |= SP_PURE;
221  
222 <        if (rspec > FTINY) {                    /* has specular component */
223 <                                                /* compute specular color */
224 <                if (m->otype == MAT_METAL)
225 <                        copycolor(scolor, mcolor);
226 <                else
88 <                        setcolor(scolor, 1.0, 1.0, 1.0);
89 <                scalecolor(scolor, rspec);
90 <                                                /* improved model */
91 <                dtmp = exp(-BSPEC(m)*pdot);
92 <                for (i = 0; i < 3; i++)
93 <                        colval(scolor,i) += (1.0-colval(scolor,i))*dtmp;
94 <                rspec += (1.0-rspec)*dtmp;
95 <                                                /* compute reflected ray */
96 <                for (i = 0; i < 3; i++)
97 <                        vrefl[i] = r->rdir[i] + 2.0*pdot*pnorm[i];
98 <
99 <                if (alpha2 <= FTINY && !(r->crtype & SHADOW))
100 <                        if (rayorigin(&lr, r, REFLECTED, rspec) == 0) {
101 <                                VCOPY(lr.rdir, vrefl);
102 <                                rayvalue(&lr);
103 <                                multcolor(lr.rcol, scolor);
104 <                                addcolor(r->rcol, lr.rcol);
105 <                        }
222 >        if ( (hastexture = (DOT(r->pert,r->pert) > FTINY*FTINY)) ) {
223 >                nd.pdot = raynormal(nd.pnorm, r);       /* perturb normal */
224 >        } else {
225 >                VCOPY(nd.pnorm, r->ron);
226 >                nd.pdot = r->rod;
227          }
228 <
228 >        if (!hastexture && r->ro != NULL && isflat(r->ro->otype))
229 >                nd.specfl |= SP_FLAT;
230 >        if (nd.pdot < .001)
231 >                nd.pdot = .001;                 /* non-zero for dirnorm() */
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 >= 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 <                trans = m->oargs.farg[5]*(1.0 - rspec);
243 <                tspec = trans * m->oargs.farg[6];
244 <                tdiff = trans - tspec;
242 >                nd.trans = m->oargs.farg[5]*(1.0 - nd.rspec);
243 >                nd.tspec = nd.trans * m->oargs.farg[6];
244 >                nd.tdiff = nd.trans - nd.tspec;
245 >                if (nd.tspec > FTINY) {
246 >                        nd.specfl |= SP_TRAN;
247 >                                                        /* check threshold */
248 >                        if (!(nd.specfl & SP_PURE) &&
249 >                                        specthresh >= nd.tspec-FTINY)
250 >                                nd.specfl |= SP_TBLT;
251 >                        if (!hastexture || r->crtype & (SHADOW|AMBIENT)) {
252 >                                VCOPY(nd.prdir, r->rdir);
253 >                        } else {
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
259 >                                        VCOPY(nd.prdir, r->rdir);
260 >                        }
261 >                }
262          } else
263 <                tdiff = tspec = trans = 0.0;
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 (tspec > FTINY && alpha2 <= FTINY)
268 <                if (rayorigin(&lr, r, TRANS, tspec) == 0) {
269 <                        VCOPY(lr.rdir, r->rdir);
267 >        if ((nd.specfl&(SP_TRAN|SP_PURE|SP_TBLT)) == (SP_TRAN|SP_PURE)) {
268 >                RAY  lr;
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, tspec);
275 <                        addcolor(r->rcol, lr.rcol);
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 +        }
286 +
287          if (r->crtype & SHADOW)                 /* the rest is shadow */
288 <                return;
289 <                                                /* diffuse reflection */
290 <        rdiff = 1.0 - trans - rspec;
288 >                return(1);
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 >                        setscolor(nd.scolor, nd.rspec, nd.rspec, nd.rspec);
295 >                } else if (fest > FTINY) {
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 >                        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;
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, 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 >                        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 && r->crtype & AMBIENT)
323 >                                r->rmt += raydistance(&lr);
324 >                }
325 >        }
326  
327 <        if (rdiff <= FTINY && tdiff <= FTINY && alpha2 <= FTINY)
328 <                return;                         /* purely specular */
327 >        if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY)
328 >                return(1);                      /* 100% pure specular */
329  
330 <        ambient(ctmp, r);               /* compute ambient component */
331 <        scalecolor(ctmp, 1.0-trans);    /* from this side */
132 <        multcolor(ctmp, mcolor);        /* modified by material color */
133 <        addcolor(r->rcol, ctmp);        /* add to returned color */
330 >        if (!(nd.specfl & SP_PURE))
331 >                gaussamp(&nd);                  /* checks *BLT flags */
332  
333 <        if (trans > FTINY) {            /* ambient from other side */
334 <                flipsurface(r);
335 <                scalecolor(ctmp, trans);
336 <                multcolor(ctmp, mcolor);
337 <                addcolor(r->rcol, ctmp);
338 <                flipsurface(r);
333 >        if (nd.rdiff > FTINY) {         /* ambient from this side */
334 >                copyscolor(sctmp, nd.mcolor);   /* modified by material color */
335 >                scalescolor(sctmp, nd.rdiff);
336 >                if (nd.specfl & SP_RBLT)        /* add in specular as well? */
337 >                        saddscolor(sctmp, nd.scolor);
338 >                multambient(sctmp, r, nd.pnorm);
339 >                saddscolor(r->rcol, sctmp);     /* add to returned color */
340          }
341 <        
342 <        for (i = 0; i < nsources; i++) {        /* add specular and diffuse */
341 >        if (nd.tdiff > FTINY) {         /* ambient from other side */
342 >                FVECT  bnorm;
343 >                copyscolor(sctmp, nd.mcolor);   /* modified by color */
344 >                if (nd.specfl & SP_TBLT) {
345 >                        scalescolor(sctmp, nd.trans);
346 >                } else {
347 >                        scalescolor(sctmp, nd.tdiff);
348 >                }
349 >                bnorm[0] = -nd.pnorm[0];
350 >                bnorm[1] = -nd.pnorm[1];
351 >                bnorm[2] = -nd.pnorm[2];
352 >                multambient(sctmp, r, bnorm);
353 >                saddscolor(r->rcol, sctmp);
354 >        }
355 >                                        /* add direct component */
356 >        direct(r, dirnorm, &nd);
357  
358 <                if ((omega = srcray(&lr, r, i)) == 0.0)
359 <                        continue;               /* bad source */
358 >        return(1);
359 > }
360  
148                ldot = DOT(pnorm, lr.rdir);
149        
150                if (ldot < 0.0 ? trans <= FTINY : trans >= 1.0-FTINY)
151                        continue;               /* wrong side */
152        
153                rayvalue(&lr);                  /* compute light ray value */
154        
155                if (intens(lr.rcol) <= FTINY)
156                        continue;               /* didn't hit light source */
361  
362 <                if (ldot > FTINY && rdiff > FTINY) {
363 <                        /*
364 <                         *  Compute and add diffuse component to returned color.
365 <                         *  The diffuse component will always be modified by the
366 <                         *  color of the material.
367 <                         */
368 <                        copycolor(ctmp, lr.rcol);
369 <                        dtmp = ldot * omega * rdiff / PI;
370 <                        scalecolor(ctmp, dtmp);
371 <                        multcolor(ctmp, mcolor);
372 <                        addcolor(r->rcol, ctmp);
362 > static void
363 > gaussamp(                       /* sample Gaussian specular */
364 >        NORMDAT  *np
365 > )
366 > {
367 >        RAY  sr;
368 >        FVECT  u, v, h;
369 >        double  rv[2];
370 >        double  d, sinp, cosp;
371 >        SCOLOR  scol;
372 >        int  maxiter, ntrials, nstarget, nstaken;
373 >        int  i;
374 >                                        /* quick test */
375 >        if ((np->specfl & (SP_REFL|SP_RBLT)) != SP_REFL &&
376 >                        (np->specfl & (SP_TRAN|SP_TBLT)) != SP_TRAN)
377 >                return;
378 >                                        /* set up sample coordinates */
379 >        getperpendicular(u, np->pnorm, rand_samp);
380 >        fcross(v, np->pnorm, u);
381 >                                        /* compute reflection */
382 >        if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL &&
383 >                        rayorigin(&sr, RSPECULAR, np->rp, np->scolor) == 0) {
384 >                nstarget = 1;
385 >                if (specjitter > 1.5) { /* multiple samples? */
386 >                        nstarget = specjitter*np->rp->rweight + .5;
387 >                        if (sr.rweight <= minweight*nstarget)
388 >                                nstarget = sr.rweight/minweight;
389 >                        if (nstarget > 1) {
390 >                                d = 1./nstarget;
391 >                                scalescolor(sr.rcoef, d);
392 >                                sr.rweight *= d;
393 >                        } else
394 >                                nstarget = 1;
395                  }
396 <                if (ldot > FTINY && rspec > FTINY && alpha2 > FTINY) {
397 <                        /*
398 <                         *  Compute specular reflection coefficient using
399 <                         *  gaussian distribution model.
400 <                         */
401 <                                                        /* roughness + source */
402 <                        dtmp = alpha2 + omega/(2.0*PI);
403 <                                                        /* gaussian */
404 <                        dtmp = exp((DOT(vrefl,lr.rdir)-1.)/dtmp)/(2.*PI)/dtmp;
405 <                                                        /* worth using? */
406 <                        if (dtmp > FTINY) {
407 <                                copycolor(ctmp, lr.rcol);
408 <                                dtmp *= omega;
409 <                                scalecolor(ctmp, dtmp);
410 <                                multcolor(ctmp, scolor);
411 <                                addcolor(r->rcol, ctmp);
396 >                scolorblack(scol);
397 >                dimlist[ndims++] = (int)(size_t)np->mp;
398 >                maxiter = MAXITER*nstarget;
399 >                for (nstaken = ntrials = 0; nstaken < nstarget &&
400 >                                                ntrials < maxiter; ntrials++) {
401 >                        if (ntrials)
402 >                                d = frandom();
403 >                        else
404 >                                d = urand(ilhash(dimlist,ndims)+samplendx);
405 >                        multisamp(rv, 2, d);
406 >                        d = 2.0*PI * rv[0];
407 >                        cosp = tcos(d);
408 >                        sinp = tsin(d);
409 >                        if ((0. <= specjitter) & (specjitter < 1.))
410 >                                rv[1] = 1.0 - specjitter*rv[1];
411 >                        if (rv[1] <= FTINY)
412 >                                d = 1.0;
413 >                        else
414 >                                d = sqrt( np->alpha2 * -log(rv[1]) );
415 >                        for (i = 0; i < 3; i++)
416 >                                h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*v[i]);
417 >                        d = -2.0 * DOT(h, np->rp->rdir) / (1.0 + d*d);
418 >                        VSUM(sr.rdir, np->rp->rdir, h, d);
419 >                                                /* sample rejection test */
420 >                        if ((d = DOT(sr.rdir, np->rp->ron)) <= FTINY)
421 >                                continue;
422 >                        checknorm(sr.rdir);
423 >                        if (nstarget > 1) {     /* W-G-M-D adjustment */
424 >                                if (nstaken) rayclear(&sr);
425 >                                rayvalue(&sr);
426 >                                d = 2./(1. + np->rp->rod/d);
427 >                                scalescolor(sr.rcol, d);
428 >                                saddscolor(scol, sr.rcol);
429 >                        } else {
430 >                                rayvalue(&sr);
431 >                                smultscolor(sr.rcol, sr.rcoef);
432 >                                saddscolor(np->rp->rcol, sr.rcol);
433                          }
434 +                        ++nstaken;
435                  }
436 <                if (ldot < -FTINY && tdiff > FTINY) {
437 <                        /*
438 <                         *  Compute diffuse transmission.
439 <                         */
440 <                        copycolor(ctmp, lr.rcol);
193 <                        dtmp = -ldot * omega * tdiff / PI;
194 <                        scalecolor(ctmp, dtmp);
195 <                        multcolor(ctmp, mcolor);
196 <                        addcolor(r->rcol, ctmp);
436 >                if (nstarget > 1) {             /* final W-G-M-D weighting */
437 >                        smultscolor(scol, sr.rcoef);
438 >                        d = (double)nstarget/ntrials;
439 >                        scalescolor(scol, d);
440 >                        saddscolor(np->rp->rcol, scol);
441                  }
442 <                if (ldot < -FTINY && tspec > FTINY && alpha2 > FTINY) {
443 <                        /*
444 <                         *  Compute specular transmission.
445 <                         */
446 <                                                        /* roughness + source */
447 <                        dtmp = alpha2 + omega/(2.0*PI);
448 <                                                        /* gaussian */
449 <                        dtmp = exp((DOT(r->rdir,lr.rdir)-1.)/dtmp)/(2.*PI)/dtmp;
450 <                                                        /* worth using? */
451 <                        if (dtmp > FTINY) {
452 <                                copycolor(ctmp, lr.rcol);
453 <                                dtmp *= tspec * omega;
454 <                                scalecolor(ctmp, dtmp);
455 <                                addcolor(r->rcol, ctmp);
456 <                        }
442 >                ndims--;
443 >        }
444 >                                        /* compute transmission */
445 >        copyscolor(sr.rcoef, np->mcolor);       /* modified by color */
446 >        scalescolor(sr.rcoef, np->tspec);
447 >        if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN &&
448 >                        rayorigin(&sr, TSPECULAR, np->rp, sr.rcoef) == 0) {
449 >                nstarget = 1;
450 >                if (specjitter > 1.5) { /* multiple samples? */
451 >                        nstarget = specjitter*np->rp->rweight + .5;
452 >                        if (sr.rweight <= minweight*nstarget)
453 >                                nstarget = sr.rweight/minweight;
454 >                        if (nstarget > 1) {
455 >                                d = 1./nstarget;
456 >                                scalescolor(sr.rcoef, d);
457 >                                sr.rweight *= d;
458 >                        } else
459 >                                nstarget = 1;
460                  }
461 +                dimlist[ndims++] = (int)(size_t)np->mp;
462 +                maxiter = MAXITER*nstarget;
463 +                for (nstaken = ntrials = 0; nstaken < nstarget &&
464 +                                                ntrials < maxiter; ntrials++) {
465 +                        if (ntrials)
466 +                                d = frandom();
467 +                        else
468 +                                d = urand(ilhash(dimlist,ndims)+samplendx);
469 +                        multisamp(rv, 2, d);
470 +                        d = 2.0*PI * rv[0];
471 +                        cosp = tcos(d);
472 +                        sinp = tsin(d);
473 +                        if ((0. <= specjitter) & (specjitter < 1.))
474 +                                rv[1] = 1.0 - specjitter*rv[1];
475 +                        if (rv[1] <= FTINY)
476 +                                d = 1.0;
477 +                        else
478 +                                d = sqrt( np->alpha2 * -log(rv[1]) );
479 +                        for (i = 0; i < 3; i++)
480 +                                sr.rdir[i] = np->prdir[i] + d*(cosp*u[i] + sinp*v[i]);
481 +                                                /* sample rejection test */
482 +                        if (DOT(sr.rdir, np->rp->ron) >= -FTINY)
483 +                                continue;
484 +                        normalize(sr.rdir);     /* OK, normalize */
485 +                        if (nstaken)            /* multi-sampling */
486 +                                rayclear(&sr);
487 +                        rayvalue(&sr);
488 +                        smultscolor(sr.rcol, sr.rcoef);
489 +                        saddscolor(np->rp->rcol, sr.rcol);
490 +                        ++nstaken;
491 +                }
492 +                ndims--;
493          }
494   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines