ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/dielectric.c
Revision: 2.26
Committed: Wed May 20 13:12:06 2015 UTC (9 years ago) by rschregle
Content type: text/plain
Branch: MAIN
Changes since 2.25: +4 -3 lines
Log Message:
Fixed photon map double counting bug with ambient rays

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 rschregle 2.26 static const char RCSid[] = "$Id: dielectric.c,v 1.5 2015/05/08 13:13:20 taschreg Exp taschreg $";
3 greg 1.1 #endif
4     /*
5     * dielectric.c - shading function for transparent materials.
6 greg 2.15 */
7    
8 greg 2.16 #include "copyright.h"
9 greg 1.1
10     #include "ray.h"
11     #include "otypes.h"
12 schorsch 2.18 #include "rtotypes.h"
13 greg 2.25 #include "pmapmat.h"
14 greg 1.1
15     #ifdef DISPERSE
16     #include "source.h"
17 schorsch 2.18 static int disperse(OBJREC *m,RAY *r,FVECT vt,double tr,COLOR cet,COLOR abt);
18     static int lambda(OBJREC *m, FVECT v2, FVECT dv, FVECT lr);
19 greg 1.1 #endif
20    
21 schorsch 2.18 static double mylog(double x);
22    
23    
24 greg 1.1 /*
25     * Explicit calculations for Fresnel's equation are performed,
26     * but only one square root computation is necessary.
27     * The index of refraction is given as a Hartmann equation
28     * with lambda0 equal to zero. If the slope of Hartmann's
29     * equation is non-zero, the material disperses light upon
30     * refraction. This condition is examined on rays traced to
31     * light sources. If a ray is exiting a dielectric material, we
32     * check the sources to see if any would cause bright color to be
33     * directed to the viewer due to dispersion. This gives colorful
34     * sparkle to crystals, etc. (Only if DISPERSE is defined!)
35     *
36     * Arguments for MAT_DIELECTRIC are:
37     * red grn blu rndx Hartmann
38     *
39     * Arguments for MAT_INTERFACE are:
40     * red1 grn1 blu1 rndx1 red2 grn2 blu2 rndx2
41     *
42     * The primaries are material transmission per unit length.
43     * MAT_INTERFACE uses dielectric1 for inside and dielectric2 for
44     * outside.
45     */
46    
47    
48     #define MLAMBDA 500 /* mean lambda */
49     #define MAXLAMBDA 779 /* maximum lambda */
50     #define MINLAMBDA 380 /* minimum lambda */
51    
52     #define MINCOS 0.997 /* minimum dot product for dispersion */
53    
54 greg 2.25 static
55     double
56 schorsch 2.18 mylog( /* special log for extinction coefficients */
57     double x
58     )
59 greg 2.10 {
60     if (x < 1e-40)
61     return(-100.);
62     if (x >= 1.)
63     return(0.);
64     return(log(x));
65     }
66    
67    
68 greg 2.24 int
69 schorsch 2.18 m_dielectric( /* color a ray which hit a dielectric interface */
70     OBJREC *m,
71 greg 2.24 RAY *r
72 schorsch 2.18 )
73 greg 1.1 {
74     double cos1, cos2, nratio;
75 greg 2.9 COLOR ctrans;
76 greg 2.11 COLOR talb;
77 gwlarson 2.14 int hastexture;
78 greg 2.23 double transdist=0, transtest=0;
79     double mirdist=0, mirtest=0;
80 greg 2.19 int flatsurface;
81 greg 1.5 double refl, trans;
82 greg 1.1 FVECT dnorm;
83     double d1, d2;
84     RAY p;
85 greg 2.24 int i;
86 greg 1.1
87 rschregle 2.26 /* PMAP: skip refracted shadow or ambient ray if accounted for in
88     photon map */
89     if (shadowRayInPmap(r) || ambRayInPmap(r))
90 greg 2.25 return(1);
91    
92 greg 1.1 if (m->oargs.nfargs != (m->otype==MAT_DIELECTRIC ? 5 : 8))
93     objerror(m, USER, "bad arguments");
94    
95     raytexture(r, m->omod); /* get modifiers */
96    
97 schorsch 2.17 if ( (hastexture = DOT(r->pert,r->pert) > FTINY*FTINY) )
98 gwlarson 2.14 cos1 = raynormal(dnorm, r); /* perturb normal */
99     else {
100     VCOPY(dnorm, r->ron);
101     cos1 = r->rod;
102     }
103 greg 2.24 flatsurface = r->ro != NULL && isflat(r->ro->otype) &&
104     !hastexture | (r->crtype & AMBIENT);
105 greg 2.19
106 greg 1.1 /* index of refraction */
107     if (m->otype == MAT_DIELECTRIC)
108     nratio = m->oargs.farg[3] + m->oargs.farg[4]/MLAMBDA;
109     else
110     nratio = m->oargs.farg[3] / m->oargs.farg[7];
111    
112     if (cos1 < 0.0) { /* inside */
113 gwlarson 2.14 hastexture = -hastexture;
114 greg 1.1 cos1 = -cos1;
115     dnorm[0] = -dnorm[0];
116     dnorm[1] = -dnorm[1];
117     dnorm[2] = -dnorm[2];
118 greg 2.10 setcolor(r->cext, -mylog(m->oargs.farg[0]*colval(r->pcol,RED)),
119     -mylog(m->oargs.farg[1]*colval(r->pcol,GRN)),
120     -mylog(m->oargs.farg[2]*colval(r->pcol,BLU)));
121 greg 2.11 setcolor(r->albedo, 0., 0., 0.);
122 greg 2.9 r->gecc = 0.;
123     if (m->otype == MAT_INTERFACE) {
124     setcolor(ctrans,
125 greg 2.10 -mylog(m->oargs.farg[4]*colval(r->pcol,RED)),
126     -mylog(m->oargs.farg[5]*colval(r->pcol,GRN)),
127     -mylog(m->oargs.farg[6]*colval(r->pcol,BLU)));
128 greg 2.11 setcolor(talb, 0., 0., 0.);
129 greg 2.9 } else {
130     copycolor(ctrans, cextinction);
131 greg 2.11 copycolor(talb, salbedo);
132 greg 2.9 }
133 greg 1.1 } else { /* outside */
134     nratio = 1.0 / nratio;
135 greg 2.9
136 greg 2.10 setcolor(ctrans, -mylog(m->oargs.farg[0]*colval(r->pcol,RED)),
137     -mylog(m->oargs.farg[1]*colval(r->pcol,GRN)),
138     -mylog(m->oargs.farg[2]*colval(r->pcol,BLU)));
139 greg 2.11 setcolor(talb, 0., 0., 0.);
140 greg 2.9 if (m->otype == MAT_INTERFACE) {
141     setcolor(r->cext,
142 greg 2.10 -mylog(m->oargs.farg[4]*colval(r->pcol,RED)),
143     -mylog(m->oargs.farg[5]*colval(r->pcol,GRN)),
144     -mylog(m->oargs.farg[6]*colval(r->pcol,BLU)));
145 greg 2.11 setcolor(r->albedo, 0., 0., 0.);
146 greg 2.9 r->gecc = 0.;
147     }
148 greg 1.1 }
149    
150     d2 = 1.0 - nratio*nratio*(1.0 - cos1*cos1); /* compute cos theta2 */
151    
152     if (d2 < FTINY) /* total reflection */
153    
154     refl = 1.0;
155    
156     else { /* refraction occurs */
157     /* compute Fresnel's equations */
158     cos2 = sqrt(d2);
159     d1 = cos1;
160     d2 = nratio*cos2;
161     d1 = (d1 - d2) / (d1 + d2);
162     refl = d1 * d1;
163    
164     d1 = 1.0 / cos1;
165     d2 = nratio / cos2;
166     d1 = (d1 - d2) / (d1 + d2);
167     refl += d1 * d1;
168    
169 greg 2.9 refl *= 0.5;
170 greg 1.1 trans = 1.0 - refl;
171 greg 2.15
172     trans *= nratio*nratio; /* solid angle ratio */
173 greg 1.1
174 greg 2.20 setcolor(p.rcoef, trans, trans, trans);
175    
176     if (rayorigin(&p, REFRACTED, r, p.rcoef) == 0) {
177 greg 1.1
178     /* compute refracted ray */
179     d1 = nratio*cos1 - cos2;
180     for (i = 0; i < 3; i++)
181     p.rdir[i] = nratio*r->rdir[i] + d1*dnorm[i];
182 gwlarson 2.14 /* accidental reflection? */
183     if (hastexture &&
184     DOT(p.rdir,r->ron)*hastexture >= -FTINY) {
185     d1 *= (double)hastexture;
186     for (i = 0; i < 3; i++) /* ignore texture */
187     p.rdir[i] = nratio*r->rdir[i] +
188     d1*r->ron[i];
189     normalize(p.rdir); /* not exact */
190 greg 2.21 } else
191     checknorm(p.rdir);
192 greg 1.1 #ifdef DISPERSE
193     if (m->otype != MAT_DIELECTRIC
194     || r->rod > 0.0
195     || r->crtype & SHADOW
196 greg 2.3 || !directvis
197 greg 1.1 || m->oargs.farg[4] == 0.0
198 greg 2.12 || !disperse(m, r, p.rdir,
199     trans, ctrans, talb))
200 greg 1.1 #endif
201     {
202 greg 2.9 copycolor(p.cext, ctrans);
203 greg 2.11 copycolor(p.albedo, talb);
204 greg 1.1 rayvalue(&p);
205 greg 2.20 multcolor(p.rcol, p.rcoef);
206 greg 1.1 addcolor(r->rcol, p.rcol);
207 greg 2.19 /* virtual distance */
208     if (flatsurface ||
209 greg 2.24 (1.-FTINY <= nratio) &
210     (nratio <= 1.+FTINY)) {
211 greg 2.19 transtest = 2*bright(p.rcol);
212     transdist = r->rot + p.rt;
213     }
214 greg 1.1 }
215     }
216     }
217 greg 2.20 setcolor(p.rcoef, refl, refl, refl);
218    
219 greg 1.1 if (!(r->crtype & SHADOW) &&
220 greg 2.20 rayorigin(&p, REFLECTED, r, p.rcoef) == 0) {
221 greg 1.1
222     /* compute reflected ray */
223 greg 2.22 VSUM(p.rdir, r->rdir, dnorm, 2.*cos1);
224 gwlarson 2.14 /* accidental penetration? */
225     if (hastexture && DOT(p.rdir,r->ron)*hastexture <= FTINY)
226 greg 2.22 VSUM(p.rdir, r->rdir, r->ron, 2.*r->rod);
227 greg 2.21 checknorm(p.rdir);
228 greg 1.1 rayvalue(&p); /* reflected ray value */
229    
230 greg 2.20 multcolor(p.rcol, p.rcoef); /* color contribution */
231 greg 1.1 addcolor(r->rcol, p.rcol);
232 greg 2.19 /* virtual distance */
233     if (flatsurface) {
234     mirtest = 2*bright(p.rcol);
235     mirdist = r->rot + p.rt;
236     }
237 greg 1.1 }
238 greg 2.19 /* check distance to return */
239     d1 = bright(r->rcol);
240     if (transtest > d1)
241     r->rt = transdist;
242     else if (mirtest > d1)
243     r->rt = mirdist;
244 greg 2.9 /* rayvalue() computes absorption */
245 greg 2.7 return(1);
246 greg 1.1 }
247    
248    
249     #ifdef DISPERSE
250    
251 schorsch 2.18 static int
252     disperse( /* check light sources for dispersion */
253     OBJREC *m,
254     RAY *r,
255     FVECT vt,
256     double tr,
257     COLOR cet,
258     COLOR abt
259     )
260 greg 1.1 {
261 greg 2.20 RAY sray;
262     const RAY *entray;
263 greg 1.1 FVECT v1, v2, n1, n2;
264     FVECT dv, v2Xdv;
265     double v2Xdvv2Xdv;
266 greg 1.7 int success = 0;
267     SRCINDEX si;
268 greg 1.1 FVECT vtmp1, vtmp2;
269     double dtmp1, dtmp2;
270     int l1, l2;
271     COLOR ctmp;
272     int i;
273    
274     /*
275     * This routine computes dispersion to the first order using
276     * the following assumptions:
277     *
278     * 1) The dependency of the index of refraction on wavelength
279     * is approximated by Hartmann's equation with lambda0
280     * equal to zero.
281     * 2) The entry and exit locations are constant with respect
282     * to dispersion.
283     *
284     * The second assumption permits us to model dispersion without
285     * having to sample refracted directions. We assume that the
286     * geometry inside the material is constant, and concern ourselves
287     * only with the relationship between the entering and exiting ray.
288     * We compute the first derivatives of the entering and exiting
289     * refraction with respect to the index of refraction. This
290     * is then used in a first order Taylor series to determine the
291     * index of refraction necessary to send the exiting ray to each
292     * light source.
293     * If an exiting ray hits a light source within the refraction
294     * boundaries, we sum all the frequencies over the disc of the
295     * light source to determine the resulting color. A smaller light
296     * source will therefore exhibit a sharper spectrum.
297     */
298    
299     if (!(r->crtype & REFRACTED)) { /* ray started in material */
300     VCOPY(v1, r->rdir);
301     n1[0] = -r->rdir[0]; n1[1] = -r->rdir[1]; n1[2] = -r->rdir[2];
302     } else {
303     /* find entry point */
304     for (entray = r; entray->rtype != REFRACTED;
305     entray = entray->parent)
306     ;
307     entray = entray->parent;
308     if (entray->crtype & REFRACTED) /* too difficult */
309     return(0);
310     VCOPY(v1, entray->rdir);
311     VCOPY(n1, entray->ron);
312     }
313     VCOPY(v2, vt); /* exiting ray */
314     VCOPY(n2, r->ron);
315    
316     /* first order dispersion approx. */
317 greg 2.22 dtmp1 = 1./DOT(n1, v1);
318     dtmp2 = 1./DOT(n2, v2);
319 greg 1.1 for (i = 0; i < 3; i++)
320 greg 2.22 dv[i] = v1[i] + v2[i] - n1[i]*dtmp1 - n2[i]*dtmp2;
321 greg 1.1
322     if (DOT(dv, dv) <= FTINY) /* null effect */
323     return(0);
324     /* compute plane normal */
325     fcross(v2Xdv, v2, dv);
326     v2Xdvv2Xdv = DOT(v2Xdv, v2Xdv);
327    
328     /* check sources */
329 greg 1.7 initsrcindex(&si);
330     while (srcray(&sray, r, &si)) {
331 greg 1.1
332 greg 1.7 if (DOT(sray.rdir, v2) < MINCOS)
333 greg 1.1 continue; /* bad source */
334     /* adjust source ray */
335    
336     dtmp1 = DOT(v2Xdv, sray.rdir) / v2Xdvv2Xdv;
337     sray.rdir[0] -= dtmp1 * v2Xdv[0];
338     sray.rdir[1] -= dtmp1 * v2Xdv[1];
339     sray.rdir[2] -= dtmp1 * v2Xdv[2];
340    
341     l1 = lambda(m, v2, dv, sray.rdir); /* mean lambda */
342    
343     if (l1 > MAXLAMBDA || l1 < MINLAMBDA) /* not visible */
344     continue;
345     /* trace source ray */
346 greg 2.12 copycolor(sray.cext, cet);
347     copycolor(sray.albedo, abt);
348 greg 1.1 normalize(sray.rdir);
349     rayvalue(&sray);
350 greg 1.2 if (bright(sray.rcol) <= FTINY) /* missed it */
351 greg 1.1 continue;
352    
353     /*
354     * Compute spectral sum over diameter of source.
355     * First find directions for rays going to opposite
356     * sides of source, then compute wavelengths for each.
357     */
358    
359     fcross(vtmp1, v2Xdv, sray.rdir);
360 greg 1.7 dtmp1 = sqrt(si.dom / v2Xdvv2Xdv / PI);
361 greg 1.1
362     /* compute first ray */
363 greg 2.22 VSUM(vtmp2, sray.rdir, vtmp1, dtmp1);
364 greg 1.1
365     l1 = lambda(m, v2, dv, vtmp2); /* first lambda */
366     if (l1 < 0)
367     continue;
368     /* compute second ray */
369 greg 2.22 VSUM(vtmp2, sray.rdir, vtmp1, -dtmp1);
370 greg 1.1
371     l2 = lambda(m, v2, dv, vtmp2); /* second lambda */
372     if (l2 < 0)
373     continue;
374     /* compute color from spectrum */
375     if (l1 < l2)
376     spec_rgb(ctmp, l1, l2);
377     else
378     spec_rgb(ctmp, l2, l1);
379     multcolor(ctmp, sray.rcol);
380     scalecolor(ctmp, tr);
381     addcolor(r->rcol, ctmp);
382     success++;
383     }
384     return(success);
385     }
386    
387    
388     static int
389 schorsch 2.18 lambda( /* compute lambda for material */
390 greg 2.24 OBJREC *m,
391 schorsch 2.18 FVECT v2,
392     FVECT dv,
393     FVECT lr
394     )
395 greg 1.1 {
396     FVECT lrXdv, v2Xlr;
397     double dtmp, denom;
398     int i;
399    
400     fcross(lrXdv, lr, dv);
401     for (i = 0; i < 3; i++)
402 greg 2.22 if ((lrXdv[i] > FTINY) | (lrXdv[i] < -FTINY))
403 greg 1.1 break;
404     if (i >= 3)
405     return(-1);
406    
407     fcross(v2Xlr, v2, lr);
408    
409     dtmp = m->oargs.farg[4] / MLAMBDA;
410     denom = dtmp + v2Xlr[i]/lrXdv[i] * (m->oargs.farg[3] + dtmp);
411    
412     if (denom < FTINY)
413     return(-1);
414    
415     return(m->oargs.farg[4] / denom);
416     }
417    
418     #endif /* DISPERSE */