ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/dielectric.c
Revision: 1.5
Committed: Mon Oct 15 20:39:30 1990 UTC (33 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.4: +3 -6 lines
Log Message:
improved setting of rt RAY parameter

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     #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 greg 1.5 double refl, trans;
61 greg 1.1 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 greg 1.5 r->rt = r->rot; /* just use ray length */
70    
71 greg 1.1 raytexture(r, m->omod); /* get modifiers */
72    
73     cos1 = raynormal(dnorm, r); /* cosine of theta1 */
74     /* index of refraction */
75     if (m->otype == MAT_DIELECTRIC)
76     nratio = m->oargs.farg[3] + m->oargs.farg[4]/MLAMBDA;
77     else
78     nratio = m->oargs.farg[3] / m->oargs.farg[7];
79    
80     if (cos1 < 0.0) { /* inside */
81     cos1 = -cos1;
82     dnorm[0] = -dnorm[0];
83     dnorm[1] = -dnorm[1];
84     dnorm[2] = -dnorm[2];
85     setcolor(mcolor, pow(m->oargs.farg[0], r->rot),
86     pow(m->oargs.farg[1], r->rot),
87     pow(m->oargs.farg[2], r->rot));
88     } else { /* outside */
89     nratio = 1.0 / nratio;
90     if (m->otype == MAT_INTERFACE)
91     setcolor(mcolor, pow(m->oargs.farg[4], r->rot),
92     pow(m->oargs.farg[5], r->rot),
93     pow(m->oargs.farg[6], r->rot));
94     else
95     setcolor(mcolor, 1.0, 1.0, 1.0);
96     }
97 greg 1.2 mabsorp = bright(mcolor);
98 greg 1.1
99     d2 = 1.0 - nratio*nratio*(1.0 - cos1*cos1); /* compute cos theta2 */
100    
101     if (d2 < FTINY) /* total reflection */
102    
103     refl = 1.0;
104    
105     else { /* refraction occurs */
106     /* compute Fresnel's equations */
107     cos2 = sqrt(d2);
108     d1 = cos1;
109     d2 = nratio*cos2;
110     d1 = (d1 - d2) / (d1 + d2);
111     refl = d1 * d1;
112    
113     d1 = 1.0 / cos1;
114     d2 = nratio / cos2;
115     d1 = (d1 - d2) / (d1 + d2);
116     refl += d1 * d1;
117    
118     refl /= 2.0;
119     trans = 1.0 - refl;
120    
121     if (rayorigin(&p, r, REFRACTED, mabsorp*trans) == 0) {
122    
123     /* compute refracted ray */
124     d1 = nratio*cos1 - cos2;
125     for (i = 0; i < 3; i++)
126     p.rdir[i] = nratio*r->rdir[i] + d1*dnorm[i];
127    
128     #ifdef DISPERSE
129     if (m->otype != MAT_DIELECTRIC
130     || r->rod > 0.0
131     || r->crtype & SHADOW
132     || m->oargs.farg[4] == 0.0
133     || !disperse(m, r, p.rdir, trans))
134     #endif
135     {
136     rayvalue(&p);
137     multcolor(mcolor, r->pcol); /* modify */
138     scalecolor(p.rcol, trans);
139     addcolor(r->rcol, p.rcol);
140     }
141     }
142     }
143    
144     if (!(r->crtype & SHADOW) &&
145     rayorigin(&p, r, REFLECTED, mabsorp*refl) == 0) {
146    
147     /* compute reflected ray */
148     for (i = 0; i < 3; i++)
149     p.rdir[i] = r->rdir[i] + 2.0*cos1*dnorm[i];
150    
151     rayvalue(&p); /* reflected ray value */
152    
153     scalecolor(p.rcol, refl); /* color contribution */
154     addcolor(r->rcol, p.rcol);
155     }
156    
157     multcolor(r->rcol, mcolor); /* multiply by transmittance */
158     }
159    
160    
161     #ifdef DISPERSE
162    
163     static
164     disperse(m, r, vt, tr) /* check light sources for dispersion */
165     OBJREC *m;
166     RAY *r;
167     FVECT vt;
168     double tr;
169     {
170     double sqrt();
171     RAY sray, *entray;
172     FVECT v1, v2, n1, n2;
173     FVECT dv, v2Xdv;
174     double v2Xdvv2Xdv;
175     int sn, success = 0;
176     double omega;
177     FVECT vtmp1, vtmp2;
178     double dtmp1, dtmp2;
179     int l1, l2;
180     COLOR ctmp;
181     int i;
182    
183     /*
184     * This routine computes dispersion to the first order using
185     * the following assumptions:
186     *
187     * 1) The dependency of the index of refraction on wavelength
188     * is approximated by Hartmann's equation with lambda0
189     * equal to zero.
190     * 2) The entry and exit locations are constant with respect
191     * to dispersion.
192     *
193     * The second assumption permits us to model dispersion without
194     * having to sample refracted directions. We assume that the
195     * geometry inside the material is constant, and concern ourselves
196     * only with the relationship between the entering and exiting ray.
197     * We compute the first derivatives of the entering and exiting
198     * refraction with respect to the index of refraction. This
199     * is then used in a first order Taylor series to determine the
200     * index of refraction necessary to send the exiting ray to each
201     * light source.
202     * If an exiting ray hits a light source within the refraction
203     * boundaries, we sum all the frequencies over the disc of the
204     * light source to determine the resulting color. A smaller light
205     * source will therefore exhibit a sharper spectrum.
206     */
207    
208     if (!(r->crtype & REFRACTED)) { /* ray started in material */
209     VCOPY(v1, r->rdir);
210     n1[0] = -r->rdir[0]; n1[1] = -r->rdir[1]; n1[2] = -r->rdir[2];
211     } else {
212     /* find entry point */
213     for (entray = r; entray->rtype != REFRACTED;
214     entray = entray->parent)
215     ;
216     entray = entray->parent;
217     if (entray->crtype & REFRACTED) /* too difficult */
218     return(0);
219     VCOPY(v1, entray->rdir);
220     VCOPY(n1, entray->ron);
221     }
222     VCOPY(v2, vt); /* exiting ray */
223     VCOPY(n2, r->ron);
224    
225     /* first order dispersion approx. */
226     dtmp1 = DOT(n1, v1);
227     dtmp2 = DOT(n2, v2);
228     for (i = 0; i < 3; i++)
229     dv[i] = v1[i] + v2[i] - n1[i]/dtmp1 - n2[i]/dtmp2;
230    
231     if (DOT(dv, dv) <= FTINY) /* null effect */
232     return(0);
233     /* compute plane normal */
234     fcross(v2Xdv, v2, dv);
235     v2Xdvv2Xdv = DOT(v2Xdv, v2Xdv);
236    
237     /* check sources */
238     for (sn = 0; sn < nsources; sn++) {
239    
240     if ((omega = srcray(&sray, r, sn)) == 0.0 ||
241     DOT(sray.rdir, v2) < MINCOS)
242     continue; /* bad source */
243    
244     /* adjust source ray */
245    
246     dtmp1 = DOT(v2Xdv, sray.rdir) / v2Xdvv2Xdv;
247     sray.rdir[0] -= dtmp1 * v2Xdv[0];
248     sray.rdir[1] -= dtmp1 * v2Xdv[1];
249     sray.rdir[2] -= dtmp1 * v2Xdv[2];
250    
251     l1 = lambda(m, v2, dv, sray.rdir); /* mean lambda */
252    
253     if (l1 > MAXLAMBDA || l1 < MINLAMBDA) /* not visible */
254     continue;
255     /* trace source ray */
256     normalize(sray.rdir);
257     rayvalue(&sray);
258 greg 1.2 if (bright(sray.rcol) <= FTINY) /* missed it */
259 greg 1.1 continue;
260    
261     /*
262     * Compute spectral sum over diameter of source.
263     * First find directions for rays going to opposite
264     * sides of source, then compute wavelengths for each.
265     */
266    
267     fcross(vtmp1, v2Xdv, sray.rdir);
268     dtmp1 = sqrt(omega / v2Xdvv2Xdv / PI);
269    
270     /* compute first ray */
271     for (i = 0; i < 3; i++)
272     vtmp2[i] = sray.rdir[i] + dtmp1*vtmp1[i];
273    
274     l1 = lambda(m, v2, dv, vtmp2); /* first lambda */
275     if (l1 < 0)
276     continue;
277     /* compute second ray */
278     for (i = 0; i < 3; i++)
279     vtmp2[i] = sray.rdir[i] - dtmp1*vtmp1[i];
280    
281     l2 = lambda(m, v2, dv, vtmp2); /* second lambda */
282     if (l2 < 0)
283     continue;
284     /* compute color from spectrum */
285     if (l1 < l2)
286     spec_rgb(ctmp, l1, l2);
287     else
288     spec_rgb(ctmp, l2, l1);
289     multcolor(ctmp, sray.rcol);
290     scalecolor(ctmp, tr);
291     addcolor(r->rcol, ctmp);
292     success++;
293     }
294     return(success);
295     }
296    
297    
298     static int
299     lambda(m, v2, dv, lr) /* compute lambda for material */
300     register OBJREC *m;
301     FVECT v2, dv, lr;
302     {
303     FVECT lrXdv, v2Xlr;
304     double dtmp, denom;
305     int i;
306    
307     fcross(lrXdv, lr, dv);
308     for (i = 0; i < 3; i++)
309     if (lrXdv[i] > FTINY || lrXdv[i] < -FTINY)
310     break;
311     if (i >= 3)
312     return(-1);
313    
314     fcross(v2Xlr, v2, lr);
315    
316     dtmp = m->oargs.farg[4] / MLAMBDA;
317     denom = dtmp + v2Xlr[i]/lrXdv[i] * (m->oargs.farg[3] + dtmp);
318    
319     if (denom < FTINY)
320     return(-1);
321    
322     return(m->oargs.farg[4] / denom);
323     }
324    
325     #endif /* DISPERSE */