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 (8 years, 11 months 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

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: dielectric.c,v 1.5 2015/05/08 13:13:20 taschreg Exp taschreg $";
3 #endif
4 /*
5 * dielectric.c - shading function for transparent materials.
6 */
7
8 #include "copyright.h"
9
10 #include "ray.h"
11 #include "otypes.h"
12 #include "rtotypes.h"
13 #include "pmapmat.h"
14
15 #ifdef DISPERSE
16 #include "source.h"
17 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 #endif
20
21 static double mylog(double x);
22
23
24 /*
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 static
55 double
56 mylog( /* special log for extinction coefficients */
57 double x
58 )
59 {
60 if (x < 1e-40)
61 return(-100.);
62 if (x >= 1.)
63 return(0.);
64 return(log(x));
65 }
66
67
68 int
69 m_dielectric( /* color a ray which hit a dielectric interface */
70 OBJREC *m,
71 RAY *r
72 )
73 {
74 double cos1, cos2, nratio;
75 COLOR ctrans;
76 COLOR talb;
77 int hastexture;
78 double transdist=0, transtest=0;
79 double mirdist=0, mirtest=0;
80 int flatsurface;
81 double refl, trans;
82 FVECT dnorm;
83 double d1, d2;
84 RAY p;
85 int i;
86
87 /* PMAP: skip refracted shadow or ambient ray if accounted for in
88 photon map */
89 if (shadowRayInPmap(r) || ambRayInPmap(r))
90 return(1);
91
92 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 if ( (hastexture = DOT(r->pert,r->pert) > FTINY*FTINY) )
98 cos1 = raynormal(dnorm, r); /* perturb normal */
99 else {
100 VCOPY(dnorm, r->ron);
101 cos1 = r->rod;
102 }
103 flatsurface = r->ro != NULL && isflat(r->ro->otype) &&
104 !hastexture | (r->crtype & AMBIENT);
105
106 /* 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 hastexture = -hastexture;
114 cos1 = -cos1;
115 dnorm[0] = -dnorm[0];
116 dnorm[1] = -dnorm[1];
117 dnorm[2] = -dnorm[2];
118 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 setcolor(r->albedo, 0., 0., 0.);
122 r->gecc = 0.;
123 if (m->otype == MAT_INTERFACE) {
124 setcolor(ctrans,
125 -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 setcolor(talb, 0., 0., 0.);
129 } else {
130 copycolor(ctrans, cextinction);
131 copycolor(talb, salbedo);
132 }
133 } else { /* outside */
134 nratio = 1.0 / nratio;
135
136 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 setcolor(talb, 0., 0., 0.);
140 if (m->otype == MAT_INTERFACE) {
141 setcolor(r->cext,
142 -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 setcolor(r->albedo, 0., 0., 0.);
146 r->gecc = 0.;
147 }
148 }
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 refl *= 0.5;
170 trans = 1.0 - refl;
171
172 trans *= nratio*nratio; /* solid angle ratio */
173
174 setcolor(p.rcoef, trans, trans, trans);
175
176 if (rayorigin(&p, REFRACTED, r, p.rcoef) == 0) {
177
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 /* 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 } else
191 checknorm(p.rdir);
192 #ifdef DISPERSE
193 if (m->otype != MAT_DIELECTRIC
194 || r->rod > 0.0
195 || r->crtype & SHADOW
196 || !directvis
197 || m->oargs.farg[4] == 0.0
198 || !disperse(m, r, p.rdir,
199 trans, ctrans, talb))
200 #endif
201 {
202 copycolor(p.cext, ctrans);
203 copycolor(p.albedo, talb);
204 rayvalue(&p);
205 multcolor(p.rcol, p.rcoef);
206 addcolor(r->rcol, p.rcol);
207 /* virtual distance */
208 if (flatsurface ||
209 (1.-FTINY <= nratio) &
210 (nratio <= 1.+FTINY)) {
211 transtest = 2*bright(p.rcol);
212 transdist = r->rot + p.rt;
213 }
214 }
215 }
216 }
217 setcolor(p.rcoef, refl, refl, refl);
218
219 if (!(r->crtype & SHADOW) &&
220 rayorigin(&p, REFLECTED, r, p.rcoef) == 0) {
221
222 /* compute reflected ray */
223 VSUM(p.rdir, r->rdir, dnorm, 2.*cos1);
224 /* accidental penetration? */
225 if (hastexture && DOT(p.rdir,r->ron)*hastexture <= FTINY)
226 VSUM(p.rdir, r->rdir, r->ron, 2.*r->rod);
227 checknorm(p.rdir);
228 rayvalue(&p); /* reflected ray value */
229
230 multcolor(p.rcol, p.rcoef); /* color contribution */
231 addcolor(r->rcol, p.rcol);
232 /* virtual distance */
233 if (flatsurface) {
234 mirtest = 2*bright(p.rcol);
235 mirdist = r->rot + p.rt;
236 }
237 }
238 /* 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 /* rayvalue() computes absorption */
245 return(1);
246 }
247
248
249 #ifdef DISPERSE
250
251 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 {
261 RAY sray;
262 const RAY *entray;
263 FVECT v1, v2, n1, n2;
264 FVECT dv, v2Xdv;
265 double v2Xdvv2Xdv;
266 int success = 0;
267 SRCINDEX si;
268 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 dtmp1 = 1./DOT(n1, v1);
318 dtmp2 = 1./DOT(n2, v2);
319 for (i = 0; i < 3; i++)
320 dv[i] = v1[i] + v2[i] - n1[i]*dtmp1 - n2[i]*dtmp2;
321
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 initsrcindex(&si);
330 while (srcray(&sray, r, &si)) {
331
332 if (DOT(sray.rdir, v2) < MINCOS)
333 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 copycolor(sray.cext, cet);
347 copycolor(sray.albedo, abt);
348 normalize(sray.rdir);
349 rayvalue(&sray);
350 if (bright(sray.rcol) <= FTINY) /* missed it */
351 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 dtmp1 = sqrt(si.dom / v2Xdvv2Xdv / PI);
361
362 /* compute first ray */
363 VSUM(vtmp2, sray.rdir, vtmp1, dtmp1);
364
365 l1 = lambda(m, v2, dv, vtmp2); /* first lambda */
366 if (l1 < 0)
367 continue;
368 /* compute second ray */
369 VSUM(vtmp2, sray.rdir, vtmp1, -dtmp1);
370
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 lambda( /* compute lambda for material */
390 OBJREC *m,
391 FVECT v2,
392 FVECT dv,
393 FVECT lr
394 )
395 {
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 if ((lrXdv[i] > FTINY) | (lrXdv[i] < -FTINY))
403 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 */