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.2 by greg, Sat Apr 15 12:23:22 1989 UTC vs.
Revision 2.39 by greg, Tue Feb 25 02:47:22 2003 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 "copyright.h"
15 +
16   #include  "ray.h"
17  
18 #include  "source.h"
19
18   #include  "otypes.h"
19  
20 + #include  "random.h"
21 +
22 + #ifndef  MAXITER
23 + #define  MAXITER        10              /* maximum # specular ray attempts */
24 + #endif
25 +                                        /* estimate of Fresnel function */
26 + #define  FRESNE(ci)     (exp(-6.0*(ci)) - 0.00247875217)
27 +
28 + static void  gaussamp();
29 +
30   /*
31 < *      This routine uses portions of the reflection
32 < *  model described by Cook and Torrance.
25 < *      The computation of specular components has been simplified by
26 < *  numerous approximations and ommisions to improve speed.
31 > *      This routine implements the isotropic Gaussian
32 > *  model described by Ward in Siggraph `92 article.
33   *      We orient the surface towards the incoming ray, so a single
34   *  surface can be used to represent an infinitely thin object.
35   *
# Line 34 | Line 40 | static char SCCSid[] = "$SunId$ LBL";
40   *      red     grn     blu     rspec   rough   trans   tspec
41   */
42  
43 < #define  BSPEC(m)       (6.0)           /* specularity parameter b */
43 >                                /* specularity flags */
44 > #define  SP_REFL        01              /* has reflected specular component */
45 > #define  SP_TRAN        02              /* has transmitted specular */
46 > #define  SP_PURE        04              /* purely specular (zero roughness) */
47 > #define  SP_FLAT        010             /* flat reflecting surface */
48 > #define  SP_RBLT        020             /* reflection below sample threshold */
49 > #define  SP_TBLT        040             /* transmission below threshold */
50  
51 <
52 < m_normal(m, r)                  /* color a ray which hit something normal */
53 < register OBJREC  *m;
54 < register RAY  *r;
43 < {
44 <        double  exp();
51 > typedef struct {
52 >        OBJREC  *mp;            /* material pointer */
53 >        RAY  *rp;               /* ray pointer */
54 >        short  specfl;          /* specularity flags, defined above */
55          COLOR  mcolor;          /* color of this material */
56          COLOR  scolor;          /* color of specular component */
57          FVECT  vrefl;           /* vector in direction of reflected ray */
58 <        double  alpha2;         /* roughness squared times 2 */
59 <        RAY  lr;                /* ray to illumination source */
58 >        FVECT  prdir;           /* vector in transmitted direction */
59 >        double  alpha2;         /* roughness squared */
60          double  rdiff, rspec;   /* reflected specular, diffuse */
61          double  trans;          /* transmissivity */
62          double  tdiff, tspec;   /* transmitted specular, diffuse */
63          FVECT  pnorm;           /* perturbed surface normal */
64          double  pdot;           /* perturbed dot product */
65 + }  NORMDAT;             /* normal material data */
66 +
67 +
68 + static void
69 + dirnorm(cval, np, ldir, omega)          /* compute source contribution */
70 + COLOR  cval;                    /* returned coefficient */
71 + register NORMDAT  *np;          /* material data */
72 + FVECT  ldir;                    /* light source direction */
73 + double  omega;                  /* light source size */
74 + {
75          double  ldot;
76 <        double  omega;
77 <        double  dtmp;
76 >        double  ldiff;
77 >        double  dtmp, d2;
78 >        FVECT  vtmp;
79          COLOR  ctmp;
59        register int  i;
80  
81 <        if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5))
82 <                objerror(m, USER, "bad # arguments");
81 >        setcolor(cval, 0.0, 0.0, 0.0);
82 >
83 >        ldot = DOT(np->pnorm, ldir);
84 >
85 >        if (ldot < 0.0 ? np->trans <= FTINY : np->trans >= 1.0-FTINY)
86 >                return;         /* wrong side */
87 >
88 >                                /* Fresnel estimate */
89 >        ldiff = np->rdiff;
90 >        if (np->specfl & SP_PURE && (np->rspec > FTINY & ldiff > FTINY))
91 >                ldiff *= 1. - FRESNE(fabs(ldot));
92 >
93 >        if (ldot > FTINY && ldiff > FTINY) {
94 >                /*
95 >                 *  Compute and add diffuse reflected component to returned
96 >                 *  color.  The diffuse reflected component will always be
97 >                 *  modified by the color of the material.
98 >                 */
99 >                copycolor(ctmp, np->mcolor);
100 >                dtmp = ldot * omega * ldiff / PI;
101 >                scalecolor(ctmp, dtmp);
102 >                addcolor(cval, ctmp);
103 >        }
104 >        if (ldot > FTINY && (np->specfl&(SP_REFL|SP_PURE)) == SP_REFL) {
105 >                /*
106 >                 *  Compute specular reflection coefficient using
107 >                 *  gaussian distribution model.
108 >                 */
109 >                                                /* roughness */
110 >                dtmp = np->alpha2;
111 >                                                /* + source if flat */
112 >                if (np->specfl & SP_FLAT)
113 >                        dtmp += omega/(4.0*PI);
114 >                                                /* half vector */
115 >                vtmp[0] = ldir[0] - np->rp->rdir[0];
116 >                vtmp[1] = ldir[1] - np->rp->rdir[1];
117 >                vtmp[2] = ldir[2] - np->rp->rdir[2];
118 >                d2 = DOT(vtmp, np->pnorm);
119 >                d2 *= d2;
120 >                d2 = (DOT(vtmp,vtmp) - d2) / d2;
121 >                                                /* gaussian */
122 >                dtmp = exp(-d2/dtmp)/(4.*PI*dtmp);
123 >                                                /* worth using? */
124 >                if (dtmp > FTINY) {
125 >                        copycolor(ctmp, np->scolor);
126 >                        dtmp *= omega * sqrt(ldot/np->pdot);
127 >                        scalecolor(ctmp, dtmp);
128 >                        addcolor(cval, ctmp);
129 >                }
130 >        }
131 >        if (ldot < -FTINY && np->tdiff > FTINY) {
132 >                /*
133 >                 *  Compute diffuse transmission.
134 >                 */
135 >                copycolor(ctmp, np->mcolor);
136 >                dtmp = -ldot * omega * np->tdiff / PI;
137 >                scalecolor(ctmp, dtmp);
138 >                addcolor(cval, ctmp);
139 >        }
140 >        if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_PURE)) == SP_TRAN) {
141 >                /*
142 >                 *  Compute specular transmission.  Specular transmission
143 >                 *  is always modified by material color.
144 >                 */
145 >                                                /* roughness + source */
146 >                dtmp = np->alpha2 + omega/PI;
147 >                                                /* gaussian */
148 >                dtmp = exp((2.*DOT(np->prdir,ldir)-2.)/dtmp)/(PI*dtmp);
149 >                                                /* worth using? */
150 >                if (dtmp > FTINY) {
151 >                        copycolor(ctmp, np->mcolor);
152 >                        dtmp *= np->tspec * omega * sqrt(-ldot/np->pdot);
153 >                        scalecolor(ctmp, dtmp);
154 >                        addcolor(cval, ctmp);
155 >                }
156 >        }
157 > }
158 >
159 >
160 > int
161 > m_normal(m, r)                  /* color a ray that hit something normal */
162 > register OBJREC  *m;
163 > register RAY  *r;
164 > {
165 >        NORMDAT  nd;
166 >        double  fest;
167 >        double  transtest, transdist;
168 >        double  mirtest, mirdist;
169 >        int     hastexture;
170 >        double  d;
171 >        COLOR  ctmp;
172 >        register int  i;
173                                                  /* easy shadow test */
174          if (r->crtype & SHADOW && m->otype != MAT_TRANS)
175 <                return;
175 >                return(1);
176 >
177 >        if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5))
178 >                objerror(m, USER, "bad number of arguments");
179 >                                                /* check for back side */
180 >        if (r->rod < 0.0) {
181 >                if (!backvis && m->otype != MAT_TRANS) {
182 >                        raytrans(r);
183 >                        return(1);
184 >                }
185 >                flipsurface(r);                 /* reorient if backvis */
186 >        }
187 >        nd.mp = m;
188 >        nd.rp = r;
189                                                  /* get material color */
190 <        setcolor(mcolor, m->oargs.farg[0],
190 >        setcolor(nd.mcolor, m->oargs.farg[0],
191                             m->oargs.farg[1],
192                             m->oargs.farg[2]);
193                                                  /* get roughness */
194 <        alpha2 = m->oargs.farg[4];
195 <        alpha2 *= 2.0 * alpha2;
196 <                                                /* reorient if necessary */
197 <        if (r->rod < 0.0)
198 <                flipsurface(r);
194 >        nd.specfl = 0;
195 >        nd.alpha2 = m->oargs.farg[4];
196 >        if ((nd.alpha2 *= nd.alpha2) <= FTINY)
197 >                nd.specfl |= SP_PURE;
198 >        if (r->ro != NULL && isflat(r->ro->otype))
199 >                nd.specfl |= SP_FLAT;
200                                                  /* get modifiers */
201          raytexture(r, m->omod);
202 <        pdot = raynormal(pnorm, r);             /* perturb normal */
203 <        multcolor(mcolor, r->pcol);             /* modify material color */
204 <                                                /* get specular component */
205 <        rspec = m->oargs.farg[3];
202 >        if (hastexture = DOT(r->pert,r->pert) > FTINY*FTINY)
203 >                nd.pdot = raynormal(nd.pnorm, r);       /* perturb normal */
204 >        else {
205 >                VCOPY(nd.pnorm, r->ron);
206 >                nd.pdot = r->rod;
207 >        }
208 >        if (nd.pdot < .001)
209 >                nd.pdot = .001;                 /* non-zero for dirnorm() */
210 >        multcolor(nd.mcolor, r->pcol);          /* modify material color */
211 >        mirtest = transtest = 0;
212 >        mirdist = transdist = r->rot;
213 >        nd.rspec = m->oargs.farg[3];
214 >                                                /* compute Fresnel approx. */
215 >        if (nd.specfl & SP_PURE && nd.rspec > FTINY) {
216 >                fest = FRESNE(r->rod);
217 >                nd.rspec += fest*(1. - nd.rspec);
218 >        } else
219 >                fest = 0.;
220 >                                                /* compute transmission */
221 >        if (m->otype == MAT_TRANS) {
222 >                nd.trans = m->oargs.farg[5]*(1.0 - nd.rspec);
223 >                nd.tspec = nd.trans * m->oargs.farg[6];
224 >                nd.tdiff = nd.trans - nd.tspec;
225 >                if (nd.tspec > FTINY) {
226 >                        nd.specfl |= SP_TRAN;
227 >                                                        /* check threshold */
228 >                        if (!(nd.specfl & SP_PURE) &&
229 >                                        specthresh >= nd.tspec-FTINY)
230 >                                nd.specfl |= SP_TBLT;
231 >                        if (!hastexture || r->crtype & SHADOW) {
232 >                                VCOPY(nd.prdir, r->rdir);
233 >                                transtest = 2;
234 >                        } else {
235 >                                for (i = 0; i < 3; i++)         /* perturb */
236 >                                        nd.prdir[i] = r->rdir[i] - r->pert[i];
237 >                                if (DOT(nd.prdir, r->ron) < -FTINY)
238 >                                        normalize(nd.prdir);    /* OK */
239 >                                else
240 >                                        VCOPY(nd.prdir, r->rdir);
241 >                        }
242 >                }
243 >        } else
244 >                nd.tdiff = nd.tspec = nd.trans = 0.0;
245 >                                                /* transmitted ray */
246 >        if ((nd.specfl&(SP_TRAN|SP_PURE|SP_TBLT)) == (SP_TRAN|SP_PURE)) {
247 >                RAY  lr;
248 >                if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) {
249 >                        VCOPY(lr.rdir, nd.prdir);
250 >                        rayvalue(&lr);
251 >                        scalecolor(lr.rcol, nd.tspec);
252 >                        multcolor(lr.rcol, nd.mcolor);  /* modified by color */
253 >                        addcolor(r->rcol, lr.rcol);
254 >                        transtest *= bright(lr.rcol);
255 >                        transdist = r->rot + lr.rt;
256 >                }
257 >        } else
258 >                transtest = 0;
259  
260 <        if (rspec > FTINY) {                    /* has specular component */
260 >        if (r->crtype & SHADOW) {               /* the rest is shadow */
261 >                r->rt = transdist;
262 >                return(1);
263 >        }
264 >                                                /* get specular reflection */
265 >        if (nd.rspec > FTINY) {
266 >                nd.specfl |= SP_REFL;
267                                                  /* compute specular color */
268 <                if (m->otype == MAT_METAL)
269 <                        copycolor(scolor, mcolor);
270 <                else
271 <                        setcolor(scolor, 1.0, 1.0, 1.0);
272 <                scalecolor(scolor, rspec);
273 <                                                /* improved model */
274 <                dtmp = exp(-BSPEC(m)*pdot);
275 <                for (i = 0; i < 3; i++)
276 <                        colval(scolor,i) += (1.0-colval(scolor,i))*dtmp;
277 <                rspec += (1.0-rspec)*dtmp;
268 >                if (m->otype != MAT_METAL) {
269 >                        setcolor(nd.scolor, nd.rspec, nd.rspec, nd.rspec);
270 >                } else if (fest > FTINY) {
271 >                        d = nd.rspec*(1. - fest);
272 >                        for (i = 0; i < 3; i++)
273 >                                nd.scolor[i] = fest + nd.mcolor[i]*d;
274 >                } else {
275 >                        copycolor(nd.scolor, nd.mcolor);
276 >                        scalecolor(nd.scolor, nd.rspec);
277 >                }
278 >                                                /* check threshold */
279 >                if (!(nd.specfl & SP_PURE) && specthresh >= nd.rspec-FTINY)
280 >                        nd.specfl |= SP_RBLT;
281                                                  /* compute reflected ray */
282                  for (i = 0; i < 3; i++)
283 <                        vrefl[i] = r->rdir[i] + 2.0*pdot*pnorm[i];
284 <
285 <                if (alpha2 <= FTINY && !(r->crtype & SHADOW))
286 <                        if (rayorigin(&lr, r, REFLECTED, rspec) == 0) {
287 <                                VCOPY(lr.rdir, vrefl);
102 <                                rayvalue(&lr);
103 <                                multcolor(lr.rcol, scolor);
104 <                                addcolor(r->rcol, lr.rcol);
105 <                        }
283 >                        nd.vrefl[i] = r->rdir[i] + 2.*nd.pdot*nd.pnorm[i];
284 >                                                /* penetration? */
285 >                if (hastexture && DOT(nd.vrefl, r->ron) <= FTINY)
286 >                        for (i = 0; i < 3; i++)         /* safety measure */
287 >                                nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i];
288          }
289 <
290 <        if (m->otype == MAT_TRANS) {
291 <                trans = m->oargs.farg[5]*(1.0 - rspec);
292 <                tspec = trans * m->oargs.farg[6];
293 <                tdiff = trans - tspec;
112 <        } else
113 <                tdiff = tspec = trans = 0.0;
114 <                                                /* transmitted ray */
115 <        if (tspec > FTINY && alpha2 <= FTINY)
116 <                if (rayorigin(&lr, r, TRANS, tspec) == 0) {
117 <                        VCOPY(lr.rdir, r->rdir);
289 >                                                /* reflected ray */
290 >        if ((nd.specfl&(SP_REFL|SP_PURE|SP_RBLT)) == (SP_REFL|SP_PURE)) {
291 >                RAY  lr;
292 >                if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) {
293 >                        VCOPY(lr.rdir, nd.vrefl);
294                          rayvalue(&lr);
295 <                        scalecolor(lr.rcol, tspec);
295 >                        multcolor(lr.rcol, nd.scolor);
296                          addcolor(r->rcol, lr.rcol);
297 +                        if (!hastexture && nd.specfl & SP_FLAT) {
298 +                                mirtest = 2.*bright(lr.rcol);
299 +                                mirdist = r->rot + lr.rt;
300 +                        }
301                  }
302 <        if (r->crtype & SHADOW)                 /* the rest is shadow */
123 <                return;
302 >        }
303                                                  /* diffuse reflection */
304 <        rdiff = 1.0 - trans - rspec;
304 >        nd.rdiff = 1.0 - nd.trans - nd.rspec;
305  
306 <        if (rdiff <= FTINY && tdiff <= FTINY && alpha2 <= FTINY)
307 <                return;                         /* purely specular */
306 >        if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY)
307 >                return(1);                      /* 100% pure specular */
308  
309 <        if (rdiff > FTINY) {            /* ambient from this side */
310 <                ambient(ctmp, r);
311 <                if (alpha2 <= FTINY)
312 <                        scalecolor(ctmp, rdiff);
309 >        if (!(nd.specfl & SP_PURE))
310 >                gaussamp(r, &nd);               /* checks *BLT flags */
311 >
312 >        if (nd.rdiff > FTINY) {         /* ambient from this side */
313 >                ambient(ctmp, r, hastexture?nd.pnorm:r->ron);
314 >                if (nd.specfl & SP_RBLT)
315 >                        scalecolor(ctmp, 1.0-nd.trans);
316                  else
317 <                        scalecolor(ctmp, 1.0-trans);
318 <                multcolor(ctmp, mcolor);        /* modified by material color */
317 >                        scalecolor(ctmp, nd.rdiff);
318 >                multcolor(ctmp, nd.mcolor);     /* modified by material color */
319                  addcolor(r->rcol, ctmp);        /* add to returned color */
320          }
321 <        if (tdiff > FTINY) {            /* ambient from other side */
321 >        if (nd.tdiff > FTINY) {         /* ambient from other side */
322                  flipsurface(r);
323 <                ambient(ctmp, r);
324 <                if (alpha2 <= FTINY)
325 <                        scalecolor(ctmp, tdiff);
323 >                if (hastexture) {
324 >                        FVECT  bnorm;
325 >                        bnorm[0] = -nd.pnorm[0];
326 >                        bnorm[1] = -nd.pnorm[1];
327 >                        bnorm[2] = -nd.pnorm[2];
328 >                        ambient(ctmp, r, bnorm);
329 >                } else
330 >                        ambient(ctmp, r, r->ron);
331 >                if (nd.specfl & SP_TBLT)
332 >                        scalecolor(ctmp, nd.trans);
333                  else
334 <                        scalecolor(ctmp, trans);
335 <                multcolor(ctmp, mcolor);
334 >                        scalecolor(ctmp, nd.tdiff);
335 >                multcolor(ctmp, nd.mcolor);     /* modified by color */
336                  addcolor(r->rcol, ctmp);
337                  flipsurface(r);
338          }
339 <        
340 <        for (i = 0; i < nsources; i++) {        /* add specular and diffuse */
339 >                                        /* add direct component */
340 >        direct(r, dirnorm, &nd);
341 >                                        /* check distance */
342 >        d = bright(r->rcol);
343 >        if (transtest > d)
344 >                r->rt = transdist;
345 >        else if (mirtest > d)
346 >                r->rt = mirdist;
347  
348 <                if ((omega = srcray(&lr, r, i)) == 0.0)
349 <                        continue;               /* bad source */
348 >        return(1);
349 > }
350  
156                ldot = DOT(pnorm, lr.rdir);
157        
158                if (ldot < 0.0 ? trans <= FTINY : trans >= 1.0-FTINY)
159                        continue;               /* wrong side */
160        
161                rayvalue(&lr);                  /* compute light ray value */
162        
163                if (intens(lr.rcol) <= FTINY)
164                        continue;               /* didn't hit light source */
351  
352 <                if (ldot > FTINY && rdiff > FTINY) {
353 <                        /*
354 <                         *  Compute and add diffuse component to returned color.
355 <                         *  The diffuse component will always be modified by the
356 <                         *  color of the material.
357 <                         */
358 <                        copycolor(ctmp, lr.rcol);
359 <                        dtmp = ldot * omega * rdiff / PI;
360 <                        scalecolor(ctmp, dtmp);
361 <                        multcolor(ctmp, mcolor);
362 <                        addcolor(r->rcol, ctmp);
363 <                }
364 <                if (ldot > FTINY && rspec > FTINY && alpha2 > FTINY) {
365 <                        /*
366 <                         *  Compute specular reflection coefficient using
367 <                         *  gaussian distribution model.
368 <                         */
369 <                                                        /* roughness + source */
370 <                        dtmp = alpha2 + omega/(2.0*PI);
371 <                                                        /* gaussian */
372 <                        dtmp = exp((DOT(vrefl,lr.rdir)-1.)/dtmp)/(2.*PI)/dtmp;
373 <                                                        /* worth using? */
374 <                        if (dtmp > FTINY) {
375 <                                copycolor(ctmp, lr.rcol);
376 <                                dtmp *= omega;
377 <                                scalecolor(ctmp, dtmp);
378 <                                multcolor(ctmp, scolor);
379 <                                addcolor(r->rcol, ctmp);
352 > static void
353 > gaussamp(r, np)                 /* sample gaussian specular */
354 > RAY  *r;
355 > register NORMDAT  *np;
356 > {
357 >        RAY  sr;
358 >        FVECT  u, v, h;
359 >        double  rv[2];
360 >        double  d, sinp, cosp;
361 >        int  niter;
362 >        register int  i;
363 >                                        /* quick test */
364 >        if ((np->specfl & (SP_REFL|SP_RBLT)) != SP_REFL &&
365 >                        (np->specfl & (SP_TRAN|SP_TBLT)) != SP_TRAN)
366 >                return;
367 >                                        /* set up sample coordinates */
368 >        v[0] = v[1] = v[2] = 0.0;
369 >        for (i = 0; i < 3; i++)
370 >                if (np->pnorm[i] < 0.6 && np->pnorm[i] > -0.6)
371 >                        break;
372 >        v[i] = 1.0;
373 >        fcross(u, v, np->pnorm);
374 >        normalize(u);
375 >        fcross(v, np->pnorm, u);
376 >                                        /* compute reflection */
377 >        if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL &&
378 >                        rayorigin(&sr, r, SPECULAR, np->rspec) == 0) {
379 >                dimlist[ndims++] = (int)np->mp;
380 >                for (niter = 0; niter < MAXITER; niter++) {
381 >                        if (niter)
382 >                                d = frandom();
383 >                        else
384 >                                d = urand(ilhash(dimlist,ndims)+samplendx);
385 >                        multisamp(rv, 2, d);
386 >                        d = 2.0*PI * rv[0];
387 >                        cosp = tcos(d);
388 >                        sinp = tsin(d);
389 >                        rv[1] = 1.0 - specjitter*rv[1];
390 >                        if (rv[1] <= FTINY)
391 >                                d = 1.0;
392 >                        else
393 >                                d = sqrt( np->alpha2 * -log(rv[1]) );
394 >                        for (i = 0; i < 3; i++)
395 >                                h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*v[i]);
396 >                        d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d);
397 >                        for (i = 0; i < 3; i++)
398 >                                sr.rdir[i] = r->rdir[i] + d*h[i];
399 >                        if (DOT(sr.rdir, r->ron) > FTINY) {
400 >                                rayvalue(&sr);
401 >                                multcolor(sr.rcol, np->scolor);
402 >                                addcolor(r->rcol, sr.rcol);
403 >                                break;
404                          }
405                  }
406 <                if (ldot < -FTINY && tdiff > FTINY) {
407 <                        /*
408 <                         *  Compute diffuse transmission.
409 <                         */
410 <                        copycolor(ctmp, lr.rcol);
411 <                        dtmp = -ldot * omega * tdiff / PI;
412 <                        scalecolor(ctmp, dtmp);
413 <                        multcolor(ctmp, mcolor);
414 <                        addcolor(r->rcol, ctmp);
415 <                }
416 <                if (ldot < -FTINY && tspec > FTINY && alpha2 > FTINY) {
417 <                        /*
418 <                         *  Compute specular transmission.
419 <                         */
420 <                                                        /* roughness + source */
421 <                        dtmp = alpha2 + omega/(2.0*PI);
422 <                                                        /* gaussian */
423 <                        dtmp = exp((DOT(r->rdir,lr.rdir)-1.)/dtmp)/(2.*PI)/dtmp;
424 <                                                        /* worth using? */
425 <                        if (dtmp > FTINY) {
426 <                                copycolor(ctmp, lr.rcol);
427 <                                dtmp *= tspec * omega;
428 <                                scalecolor(ctmp, dtmp);
429 <                                addcolor(r->rcol, ctmp);
406 >                ndims--;
407 >        }
408 >                                        /* compute transmission */
409 >        if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN &&
410 >                        rayorigin(&sr, r, SPECULAR, np->tspec) == 0) {
411 >                dimlist[ndims++] = (int)np->mp;
412 >                for (niter = 0; niter < MAXITER; niter++) {
413 >                        if (niter)
414 >                                d = frandom();
415 >                        else
416 >                                d = urand(ilhash(dimlist,ndims)+1823+samplendx);
417 >                        multisamp(rv, 2, d);
418 >                        d = 2.0*PI * rv[0];
419 >                        cosp = tcos(d);
420 >                        sinp = tsin(d);
421 >                        rv[1] = 1.0 - specjitter*rv[1];
422 >                        if (rv[1] <= FTINY)
423 >                                d = 1.0;
424 >                        else
425 >                                d = sqrt( np->alpha2 * -log(rv[1]) );
426 >                        for (i = 0; i < 3; i++)
427 >                                sr.rdir[i] = np->prdir[i] + d*(cosp*u[i] + sinp*v[i]);
428 >                        if (DOT(sr.rdir, r->ron) < -FTINY) {
429 >                                normalize(sr.rdir);     /* OK, normalize */
430 >                                rayvalue(&sr);
431 >                                scalecolor(sr.rcol, np->tspec);
432 >                                multcolor(sr.rcol, np->mcolor); /* modified */
433 >                                addcolor(r->rcol, sr.rcol);
434 >                                break;
435                          }
436                  }
437 +                ndims--;
438          }
439   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines