ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/dielectric.c
Revision: 1.2
Committed: Wed Jun 7 08:38:31 1989 UTC (34 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.1: +2 -2 lines
Log Message:
Changed intens() to bright(), which is better measure of visibility

File Contents

# Content
1 /* Copyright (c) 1986 Regents of the University of California */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ LBL";
5 #endif
6
7 /*
8 * dielectric.c - shading function for transparent materials.
9 *
10 * 9/6/85
11 */
12
13 #include "ray.h"
14
15 #include "otypes.h"
16
17 #ifdef DISPERSE
18 #include "source.h"
19 #endif
20
21 /*
22 * Explicit calculations for Fresnel's equation are performed,
23 * but only one square root computation is necessary.
24 * The index of refraction is given as a Hartmann equation
25 * with lambda0 equal to zero. If the slope of Hartmann's
26 * equation is non-zero, the material disperses light upon
27 * refraction. This condition is examined on rays traced to
28 * light sources. If a ray is exiting a dielectric material, we
29 * check the sources to see if any would cause bright color to be
30 * directed to the viewer due to dispersion. This gives colorful
31 * sparkle to crystals, etc. (Only if DISPERSE is defined!)
32 *
33 * Arguments for MAT_DIELECTRIC are:
34 * red grn blu rndx Hartmann
35 *
36 * Arguments for MAT_INTERFACE are:
37 * red1 grn1 blu1 rndx1 red2 grn2 blu2 rndx2
38 *
39 * The primaries are material transmission per unit length.
40 * MAT_INTERFACE uses dielectric1 for inside and dielectric2 for
41 * outside.
42 */
43
44
45 #define MLAMBDA 500 /* mean lambda */
46 #define MAXLAMBDA 779 /* maximum lambda */
47 #define MINLAMBDA 380 /* minimum lambda */
48
49 #define MINCOS 0.997 /* minimum dot product for dispersion */
50
51
52 m_dielectric(m, r) /* color a ray which hit something transparent */
53 OBJREC *m;
54 register RAY *r;
55 {
56 double sqrt(), pow();
57 double cos1, cos2, nratio;
58 COLOR mcolor;
59 double mabsorp;
60 double refl, trans;
61 FVECT dnorm;
62 double d1, d2;
63 RAY p;
64 register int i;
65
66 if (m->oargs.nfargs != (m->otype==MAT_DIELECTRIC ? 5 : 8))
67 objerror(m, USER, "bad arguments");
68
69 raytexture(r, m->omod); /* get modifiers */
70
71 cos1 = raynormal(dnorm, r); /* cosine of theta1 */
72 /* index of refraction */
73 if (m->otype == MAT_DIELECTRIC)
74 nratio = m->oargs.farg[3] + m->oargs.farg[4]/MLAMBDA;
75 else
76 nratio = m->oargs.farg[3] / m->oargs.farg[7];
77
78 if (cos1 < 0.0) { /* inside */
79 cos1 = -cos1;
80 dnorm[0] = -dnorm[0];
81 dnorm[1] = -dnorm[1];
82 dnorm[2] = -dnorm[2];
83 setcolor(mcolor, pow(m->oargs.farg[0], r->rot),
84 pow(m->oargs.farg[1], r->rot),
85 pow(m->oargs.farg[2], r->rot));
86 } else { /* outside */
87 nratio = 1.0 / nratio;
88 if (m->otype == MAT_INTERFACE)
89 setcolor(mcolor, pow(m->oargs.farg[4], r->rot),
90 pow(m->oargs.farg[5], r->rot),
91 pow(m->oargs.farg[6], r->rot));
92 else
93 setcolor(mcolor, 1.0, 1.0, 1.0);
94 }
95 mabsorp = bright(mcolor);
96
97 d2 = 1.0 - nratio*nratio*(1.0 - cos1*cos1); /* compute cos theta2 */
98
99 if (d2 < FTINY) /* total reflection */
100
101 refl = 1.0;
102
103 else { /* refraction occurs */
104 /* compute Fresnel's equations */
105 cos2 = sqrt(d2);
106 d1 = cos1;
107 d2 = nratio*cos2;
108 d1 = (d1 - d2) / (d1 + d2);
109 refl = d1 * d1;
110
111 d1 = 1.0 / cos1;
112 d2 = nratio / cos2;
113 d1 = (d1 - d2) / (d1 + d2);
114 refl += d1 * d1;
115
116 refl /= 2.0;
117 trans = 1.0 - refl;
118
119 if (rayorigin(&p, r, REFRACTED, mabsorp*trans) == 0) {
120
121 /* compute refracted ray */
122 d1 = nratio*cos1 - cos2;
123 for (i = 0; i < 3; i++)
124 p.rdir[i] = nratio*r->rdir[i] + d1*dnorm[i];
125
126 #ifdef DISPERSE
127 if (m->otype != MAT_DIELECTRIC
128 || r->rod > 0.0
129 || r->crtype & SHADOW
130 || m->oargs.farg[4] == 0.0
131 || !disperse(m, r, p.rdir, trans))
132 #endif
133 {
134 rayvalue(&p);
135 multcolor(mcolor, r->pcol); /* modify */
136 scalecolor(p.rcol, trans);
137 addcolor(r->rcol, p.rcol);
138 }
139 }
140 }
141
142 if (!(r->crtype & SHADOW) &&
143 rayorigin(&p, r, REFLECTED, mabsorp*refl) == 0) {
144
145 /* compute reflected ray */
146 for (i = 0; i < 3; i++)
147 p.rdir[i] = r->rdir[i] + 2.0*cos1*dnorm[i];
148
149 rayvalue(&p); /* reflected ray value */
150
151 scalecolor(p.rcol, refl); /* color contribution */
152 addcolor(r->rcol, p.rcol);
153 }
154
155 multcolor(r->rcol, mcolor); /* multiply by transmittance */
156 }
157
158
159 #ifdef DISPERSE
160
161 static
162 disperse(m, r, vt, tr) /* check light sources for dispersion */
163 OBJREC *m;
164 RAY *r;
165 FVECT vt;
166 double tr;
167 {
168 double sqrt();
169 RAY sray, *entray;
170 FVECT v1, v2, n1, n2;
171 FVECT dv, v2Xdv;
172 double v2Xdvv2Xdv;
173 int sn, success = 0;
174 double omega;
175 FVECT vtmp1, vtmp2;
176 double dtmp1, dtmp2;
177 int l1, l2;
178 COLOR ctmp;
179 int i;
180
181 /*
182 * This routine computes dispersion to the first order using
183 * the following assumptions:
184 *
185 * 1) The dependency of the index of refraction on wavelength
186 * is approximated by Hartmann's equation with lambda0
187 * equal to zero.
188 * 2) The entry and exit locations are constant with respect
189 * to dispersion.
190 *
191 * The second assumption permits us to model dispersion without
192 * having to sample refracted directions. We assume that the
193 * geometry inside the material is constant, and concern ourselves
194 * only with the relationship between the entering and exiting ray.
195 * We compute the first derivatives of the entering and exiting
196 * refraction with respect to the index of refraction. This
197 * is then used in a first order Taylor series to determine the
198 * index of refraction necessary to send the exiting ray to each
199 * light source.
200 * If an exiting ray hits a light source within the refraction
201 * boundaries, we sum all the frequencies over the disc of the
202 * light source to determine the resulting color. A smaller light
203 * source will therefore exhibit a sharper spectrum.
204 */
205
206 if (!(r->crtype & REFRACTED)) { /* ray started in material */
207 VCOPY(v1, r->rdir);
208 n1[0] = -r->rdir[0]; n1[1] = -r->rdir[1]; n1[2] = -r->rdir[2];
209 } else {
210 /* find entry point */
211 for (entray = r; entray->rtype != REFRACTED;
212 entray = entray->parent)
213 ;
214 entray = entray->parent;
215 if (entray->crtype & REFRACTED) /* too difficult */
216 return(0);
217 VCOPY(v1, entray->rdir);
218 VCOPY(n1, entray->ron);
219 }
220 VCOPY(v2, vt); /* exiting ray */
221 VCOPY(n2, r->ron);
222
223 /* first order dispersion approx. */
224 dtmp1 = DOT(n1, v1);
225 dtmp2 = DOT(n2, v2);
226 for (i = 0; i < 3; i++)
227 dv[i] = v1[i] + v2[i] - n1[i]/dtmp1 - n2[i]/dtmp2;
228
229 if (DOT(dv, dv) <= FTINY) /* null effect */
230 return(0);
231 /* compute plane normal */
232 fcross(v2Xdv, v2, dv);
233 v2Xdvv2Xdv = DOT(v2Xdv, v2Xdv);
234
235 /* check sources */
236 for (sn = 0; sn < nsources; sn++) {
237
238 if ((omega = srcray(&sray, r, sn)) == 0.0 ||
239 DOT(sray.rdir, v2) < MINCOS)
240 continue; /* bad source */
241
242 /* adjust source ray */
243
244 dtmp1 = DOT(v2Xdv, sray.rdir) / v2Xdvv2Xdv;
245 sray.rdir[0] -= dtmp1 * v2Xdv[0];
246 sray.rdir[1] -= dtmp1 * v2Xdv[1];
247 sray.rdir[2] -= dtmp1 * v2Xdv[2];
248
249 l1 = lambda(m, v2, dv, sray.rdir); /* mean lambda */
250
251 if (l1 > MAXLAMBDA || l1 < MINLAMBDA) /* not visible */
252 continue;
253 /* trace source ray */
254 normalize(sray.rdir);
255 rayvalue(&sray);
256 if (bright(sray.rcol) <= FTINY) /* missed it */
257 continue;
258
259 /*
260 * Compute spectral sum over diameter of source.
261 * First find directions for rays going to opposite
262 * sides of source, then compute wavelengths for each.
263 */
264
265 fcross(vtmp1, v2Xdv, sray.rdir);
266 dtmp1 = sqrt(omega / v2Xdvv2Xdv / PI);
267
268 /* compute first ray */
269 for (i = 0; i < 3; i++)
270 vtmp2[i] = sray.rdir[i] + dtmp1*vtmp1[i];
271
272 l1 = lambda(m, v2, dv, vtmp2); /* first lambda */
273 if (l1 < 0)
274 continue;
275 /* compute second ray */
276 for (i = 0; i < 3; i++)
277 vtmp2[i] = sray.rdir[i] - dtmp1*vtmp1[i];
278
279 l2 = lambda(m, v2, dv, vtmp2); /* second lambda */
280 if (l2 < 0)
281 continue;
282 /* compute color from spectrum */
283 if (l1 < l2)
284 spec_rgb(ctmp, l1, l2);
285 else
286 spec_rgb(ctmp, l2, l1);
287 multcolor(ctmp, sray.rcol);
288 scalecolor(ctmp, tr);
289 addcolor(r->rcol, ctmp);
290 success++;
291 }
292 return(success);
293 }
294
295
296 static int
297 lambda(m, v2, dv, lr) /* compute lambda for material */
298 register OBJREC *m;
299 FVECT v2, dv, lr;
300 {
301 FVECT lrXdv, v2Xlr;
302 double dtmp, denom;
303 int i;
304
305 fcross(lrXdv, lr, dv);
306 for (i = 0; i < 3; i++)
307 if (lrXdv[i] > FTINY || lrXdv[i] < -FTINY)
308 break;
309 if (i >= 3)
310 return(-1);
311
312 fcross(v2Xlr, v2, lr);
313
314 dtmp = m->oargs.farg[4] / MLAMBDA;
315 denom = dtmp + v2Xlr[i]/lrXdv[i] * (m->oargs.farg[3] + dtmp);
316
317 if (denom < FTINY)
318 return(-1);
319
320 return(m->oargs.farg[4] / denom);
321 }
322
323 #endif /* DISPERSE */