ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/dielectric.c
Revision: 2.9
Committed: Fri Dec 8 18:22:07 1995 UTC (28 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.8: +39 -17 lines
Log Message:
added M_MIST (mist) type and global participating medium

File Contents

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