ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/dielectric.c
Revision: 2.6
Committed: Thu Nov 18 09:43:00 1993 UTC (30 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.5: +1 -0 lines
Log Message:
minor compiler warning fixes

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    
54     m_dielectric(m, r) /* color a ray which hit something transparent */
55     OBJREC *m;
56     register RAY *r;
57     {
58     double cos1, cos2, nratio;
59     COLOR mcolor;
60     double mabsorp;
61 greg 1.5 double refl, trans;
62 greg 1.1 FVECT dnorm;
63     double d1, d2;
64     RAY p;
65     register int i;
66    
67     if (m->oargs.nfargs != (m->otype==MAT_DIELECTRIC ? 5 : 8))
68     objerror(m, USER, "bad arguments");
69    
70     raytexture(r, m->omod); /* get modifiers */
71    
72     cos1 = raynormal(dnorm, r); /* cosine of theta1 */
73     /* index of refraction */
74     if (m->otype == MAT_DIELECTRIC)
75     nratio = m->oargs.farg[3] + m->oargs.farg[4]/MLAMBDA;
76     else
77     nratio = m->oargs.farg[3] / m->oargs.farg[7];
78    
79     if (cos1 < 0.0) { /* inside */
80     cos1 = -cos1;
81     dnorm[0] = -dnorm[0];
82     dnorm[1] = -dnorm[1];
83     dnorm[2] = -dnorm[2];
84     setcolor(mcolor, pow(m->oargs.farg[0], r->rot),
85     pow(m->oargs.farg[1], r->rot),
86     pow(m->oargs.farg[2], r->rot));
87     } else { /* outside */
88     nratio = 1.0 / nratio;
89     if (m->otype == MAT_INTERFACE)
90     setcolor(mcolor, pow(m->oargs.farg[4], r->rot),
91     pow(m->oargs.farg[5], r->rot),
92     pow(m->oargs.farg[6], r->rot));
93     else
94     setcolor(mcolor, 1.0, 1.0, 1.0);
95     }
96 greg 1.2 mabsorp = bright(mcolor);
97 greg 1.1
98     d2 = 1.0 - nratio*nratio*(1.0 - cos1*cos1); /* compute cos theta2 */
99    
100     if (d2 < FTINY) /* total reflection */
101    
102     refl = 1.0;
103    
104     else { /* refraction occurs */
105     /* compute Fresnel's equations */
106     cos2 = sqrt(d2);
107     d1 = cos1;
108     d2 = nratio*cos2;
109     d1 = (d1 - d2) / (d1 + d2);
110     refl = d1 * d1;
111    
112     d1 = 1.0 / cos1;
113     d2 = nratio / cos2;
114     d1 = (d1 - d2) / (d1 + d2);
115     refl += d1 * d1;
116    
117     refl /= 2.0;
118     trans = 1.0 - refl;
119    
120     if (rayorigin(&p, r, REFRACTED, mabsorp*trans) == 0) {
121    
122     /* compute refracted ray */
123     d1 = nratio*cos1 - cos2;
124     for (i = 0; i < 3; i++)
125     p.rdir[i] = nratio*r->rdir[i] + d1*dnorm[i];
126    
127     #ifdef DISPERSE
128     if (m->otype != MAT_DIELECTRIC
129     || r->rod > 0.0
130     || r->crtype & SHADOW
131 greg 2.3 || !directvis
132 greg 1.1 || 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 greg 2.4 if (nratio >= 1.0-FTINY && nratio <= 1.0+FTINY)
141     r->rt = r->rot + p.rt;
142 greg 1.1 }
143     }
144     }
145    
146     if (!(r->crtype & SHADOW) &&
147     rayorigin(&p, r, REFLECTED, mabsorp*refl) == 0) {
148    
149     /* compute reflected ray */
150     for (i = 0; i < 3; i++)
151     p.rdir[i] = r->rdir[i] + 2.0*cos1*dnorm[i];
152    
153     rayvalue(&p); /* reflected ray value */
154    
155     scalecolor(p.rcol, refl); /* color contribution */
156     addcolor(r->rcol, p.rcol);
157     }
158    
159     multcolor(r->rcol, mcolor); /* multiply by transmittance */
160     }
161    
162    
163     #ifdef DISPERSE
164    
165     static
166     disperse(m, r, vt, tr) /* check light sources for dispersion */
167     OBJREC *m;
168     RAY *r;
169     FVECT vt;
170     double tr;
171     {
172     RAY sray, *entray;
173     FVECT v1, v2, n1, n2;
174     FVECT dv, v2Xdv;
175     double v2Xdvv2Xdv;
176 greg 1.7 int success = 0;
177     SRCINDEX si;
178 greg 1.1 FVECT vtmp1, vtmp2;
179     double dtmp1, dtmp2;
180     int l1, l2;
181     COLOR ctmp;
182     int i;
183    
184     /*
185     * This routine computes dispersion to the first order using
186     * the following assumptions:
187     *
188     * 1) The dependency of the index of refraction on wavelength
189     * is approximated by Hartmann's equation with lambda0
190     * equal to zero.
191     * 2) The entry and exit locations are constant with respect
192     * to dispersion.
193     *
194     * The second assumption permits us to model dispersion without
195     * having to sample refracted directions. We assume that the
196     * geometry inside the material is constant, and concern ourselves
197     * only with the relationship between the entering and exiting ray.
198     * We compute the first derivatives of the entering and exiting
199     * refraction with respect to the index of refraction. This
200     * is then used in a first order Taylor series to determine the
201     * index of refraction necessary to send the exiting ray to each
202     * light source.
203     * If an exiting ray hits a light source within the refraction
204     * boundaries, we sum all the frequencies over the disc of the
205     * light source to determine the resulting color. A smaller light
206     * source will therefore exhibit a sharper spectrum.
207     */
208    
209     if (!(r->crtype & REFRACTED)) { /* ray started in material */
210     VCOPY(v1, r->rdir);
211     n1[0] = -r->rdir[0]; n1[1] = -r->rdir[1]; n1[2] = -r->rdir[2];
212     } else {
213     /* find entry point */
214     for (entray = r; entray->rtype != REFRACTED;
215     entray = entray->parent)
216     ;
217     entray = entray->parent;
218     if (entray->crtype & REFRACTED) /* too difficult */
219     return(0);
220     VCOPY(v1, entray->rdir);
221     VCOPY(n1, entray->ron);
222     }
223     VCOPY(v2, vt); /* exiting ray */
224     VCOPY(n2, r->ron);
225    
226     /* first order dispersion approx. */
227     dtmp1 = DOT(n1, v1);
228     dtmp2 = DOT(n2, v2);
229     for (i = 0; i < 3; i++)
230     dv[i] = v1[i] + v2[i] - n1[i]/dtmp1 - n2[i]/dtmp2;
231    
232     if (DOT(dv, dv) <= FTINY) /* null effect */
233     return(0);
234     /* compute plane normal */
235     fcross(v2Xdv, v2, dv);
236     v2Xdvv2Xdv = DOT(v2Xdv, v2Xdv);
237    
238     /* check sources */
239 greg 1.7 initsrcindex(&si);
240     while (srcray(&sray, r, &si)) {
241 greg 1.1
242 greg 1.7 if (DOT(sray.rdir, v2) < MINCOS)
243 greg 1.1 continue; /* bad source */
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 greg 1.7 dtmp1 = sqrt(si.dom / v2Xdvv2Xdv / PI);
269 greg 1.1
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 */