ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/dielectric.c
Revision: 2.8
Committed: Tue Dec 5 11:46:00 1995 UTC (28 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.7: +1 -1 lines
Log Message:
changed position of color modifier

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 greg 2.8 multcolor(mcolor, r->pcol); /* modify */
88 greg 1.1 } 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 greg 2.3 || !directvis
133 greg 1.1 || m->oargs.farg[4] == 0.0
134     || !disperse(m, r, p.rdir, trans))
135     #endif
136     {
137     rayvalue(&p);
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 greg 2.7
161     return(1);
162 greg 1.1 }
163    
164    
165     #ifdef DISPERSE
166    
167     static
168     disperse(m, r, vt, tr) /* check light sources for dispersion */
169     OBJREC *m;
170     RAY *r;
171     FVECT vt;
172     double tr;
173     {
174     RAY sray, *entray;
175     FVECT v1, v2, n1, n2;
176     FVECT dv, v2Xdv;
177     double v2Xdvv2Xdv;
178 greg 1.7 int success = 0;
179     SRCINDEX si;
180 greg 1.1 FVECT vtmp1, vtmp2;
181     double dtmp1, dtmp2;
182     int l1, l2;
183     COLOR ctmp;
184     int i;
185    
186     /*
187     * This routine computes dispersion to the first order using
188     * the following assumptions:
189     *
190     * 1) The dependency of the index of refraction on wavelength
191     * is approximated by Hartmann's equation with lambda0
192     * equal to zero.
193     * 2) The entry and exit locations are constant with respect
194     * to dispersion.
195     *
196     * The second assumption permits us to model dispersion without
197     * having to sample refracted directions. We assume that the
198     * geometry inside the material is constant, and concern ourselves
199     * only with the relationship between the entering and exiting ray.
200     * We compute the first derivatives of the entering and exiting
201     * refraction with respect to the index of refraction. This
202     * is then used in a first order Taylor series to determine the
203     * index of refraction necessary to send the exiting ray to each
204     * light source.
205     * If an exiting ray hits a light source within the refraction
206     * boundaries, we sum all the frequencies over the disc of the
207     * light source to determine the resulting color. A smaller light
208     * source will therefore exhibit a sharper spectrum.
209     */
210    
211     if (!(r->crtype & REFRACTED)) { /* ray started in material */
212     VCOPY(v1, r->rdir);
213     n1[0] = -r->rdir[0]; n1[1] = -r->rdir[1]; n1[2] = -r->rdir[2];
214     } else {
215     /* find entry point */
216     for (entray = r; entray->rtype != REFRACTED;
217     entray = entray->parent)
218     ;
219     entray = entray->parent;
220     if (entray->crtype & REFRACTED) /* too difficult */
221     return(0);
222     VCOPY(v1, entray->rdir);
223     VCOPY(n1, entray->ron);
224     }
225     VCOPY(v2, vt); /* exiting ray */
226     VCOPY(n2, r->ron);
227    
228     /* first order dispersion approx. */
229     dtmp1 = DOT(n1, v1);
230     dtmp2 = DOT(n2, v2);
231     for (i = 0; i < 3; i++)
232     dv[i] = v1[i] + v2[i] - n1[i]/dtmp1 - n2[i]/dtmp2;
233    
234     if (DOT(dv, dv) <= FTINY) /* null effect */
235     return(0);
236     /* compute plane normal */
237     fcross(v2Xdv, v2, dv);
238     v2Xdvv2Xdv = DOT(v2Xdv, v2Xdv);
239    
240     /* check sources */
241 greg 1.7 initsrcindex(&si);
242     while (srcray(&sray, r, &si)) {
243 greg 1.1
244 greg 1.7 if (DOT(sray.rdir, v2) < MINCOS)
245 greg 1.1 continue; /* bad source */
246     /* adjust source ray */
247    
248     dtmp1 = DOT(v2Xdv, sray.rdir) / v2Xdvv2Xdv;
249     sray.rdir[0] -= dtmp1 * v2Xdv[0];
250     sray.rdir[1] -= dtmp1 * v2Xdv[1];
251     sray.rdir[2] -= dtmp1 * v2Xdv[2];
252    
253     l1 = lambda(m, v2, dv, sray.rdir); /* mean lambda */
254    
255     if (l1 > MAXLAMBDA || l1 < MINLAMBDA) /* not visible */
256     continue;
257     /* trace source ray */
258     normalize(sray.rdir);
259     rayvalue(&sray);
260 greg 1.2 if (bright(sray.rcol) <= FTINY) /* missed it */
261 greg 1.1 continue;
262    
263     /*
264     * Compute spectral sum over diameter of source.
265     * First find directions for rays going to opposite
266     * sides of source, then compute wavelengths for each.
267     */
268    
269     fcross(vtmp1, v2Xdv, sray.rdir);
270 greg 1.7 dtmp1 = sqrt(si.dom / v2Xdvv2Xdv / PI);
271 greg 1.1
272     /* compute first ray */
273     for (i = 0; i < 3; i++)
274     vtmp2[i] = sray.rdir[i] + dtmp1*vtmp1[i];
275    
276     l1 = lambda(m, v2, dv, vtmp2); /* first lambda */
277     if (l1 < 0)
278     continue;
279     /* compute second ray */
280     for (i = 0; i < 3; i++)
281     vtmp2[i] = sray.rdir[i] - dtmp1*vtmp1[i];
282    
283     l2 = lambda(m, v2, dv, vtmp2); /* second lambda */
284     if (l2 < 0)
285     continue;
286     /* compute color from spectrum */
287     if (l1 < l2)
288     spec_rgb(ctmp, l1, l2);
289     else
290     spec_rgb(ctmp, l2, l1);
291     multcolor(ctmp, sray.rcol);
292     scalecolor(ctmp, tr);
293     addcolor(r->rcol, ctmp);
294     success++;
295     }
296     return(success);
297     }
298    
299    
300     static int
301     lambda(m, v2, dv, lr) /* compute lambda for material */
302     register OBJREC *m;
303     FVECT v2, dv, lr;
304     {
305     FVECT lrXdv, v2Xlr;
306     double dtmp, denom;
307     int i;
308    
309     fcross(lrXdv, lr, dv);
310     for (i = 0; i < 3; i++)
311     if (lrXdv[i] > FTINY || lrXdv[i] < -FTINY)
312     break;
313     if (i >= 3)
314     return(-1);
315    
316     fcross(v2Xlr, v2, lr);
317    
318     dtmp = m->oargs.farg[4] / MLAMBDA;
319     denom = dtmp + v2Xlr[i]/lrXdv[i] * (m->oargs.farg[3] + dtmp);
320    
321     if (denom < FTINY)
322     return(-1);
323    
324     return(m->oargs.farg[4] / denom);
325     }
326    
327     #endif /* DISPERSE */