ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/dielectric.c
Revision: 1.3
Committed: Tue Mar 27 11:39:58 1990 UTC (34 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.2: +3 -0 lines
Log Message:
Added rt field to RAY structure for more accurate z-buffering

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     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 greg 1.2 mabsorp = bright(mcolor);
96 greg 1.1
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 greg 1.3 r->rt = r->rot + p.rt;
139 greg 1.1 }
140     }
141     }
142    
143     if (!(r->crtype & SHADOW) &&
144     rayorigin(&p, r, REFLECTED, mabsorp*refl) == 0) {
145    
146     /* compute reflected ray */
147     for (i = 0; i < 3; i++)
148     p.rdir[i] = r->rdir[i] + 2.0*cos1*dnorm[i];
149    
150     rayvalue(&p); /* reflected ray value */
151    
152     scalecolor(p.rcol, refl); /* color contribution */
153     addcolor(r->rcol, p.rcol);
154 greg 1.3 if (refl > trans)
155     r->rt = r->rot + p.rt;
156 greg 1.1 }
157    
158     multcolor(r->rcol, mcolor); /* multiply by transmittance */
159     }
160    
161    
162     #ifdef DISPERSE
163    
164     static
165     disperse(m, r, vt, tr) /* check light sources for dispersion */
166     OBJREC *m;
167     RAY *r;
168     FVECT vt;
169     double tr;
170     {
171     double sqrt();
172     RAY sray, *entray;
173     FVECT v1, v2, n1, n2;
174     FVECT dv, v2Xdv;
175     double v2Xdvv2Xdv;
176     int sn, success = 0;
177     double omega;
178     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     for (sn = 0; sn < nsources; sn++) {
240    
241     if ((omega = srcray(&sray, r, sn)) == 0.0 ||
242     DOT(sray.rdir, v2) < MINCOS)
243     continue; /* bad source */
244    
245     /* adjust source ray */
246    
247     dtmp1 = DOT(v2Xdv, sray.rdir) / v2Xdvv2Xdv;
248     sray.rdir[0] -= dtmp1 * v2Xdv[0];
249     sray.rdir[1] -= dtmp1 * v2Xdv[1];
250     sray.rdir[2] -= dtmp1 * v2Xdv[2];
251    
252     l1 = lambda(m, v2, dv, sray.rdir); /* mean lambda */
253    
254     if (l1 > MAXLAMBDA || l1 < MINLAMBDA) /* not visible */
255     continue;
256     /* trace source ray */
257     normalize(sray.rdir);
258     rayvalue(&sray);
259 greg 1.2 if (bright(sray.rcol) <= FTINY) /* missed it */
260 greg 1.1 continue;
261    
262     /*
263     * Compute spectral sum over diameter of source.
264     * First find directions for rays going to opposite
265     * sides of source, then compute wavelengths for each.
266     */
267    
268     fcross(vtmp1, v2Xdv, sray.rdir);
269     dtmp1 = sqrt(omega / v2Xdvv2Xdv / PI);
270    
271     /* compute first ray */
272     for (i = 0; i < 3; i++)
273     vtmp2[i] = sray.rdir[i] + dtmp1*vtmp1[i];
274    
275     l1 = lambda(m, v2, dv, vtmp2); /* first lambda */
276     if (l1 < 0)
277     continue;
278     /* compute second ray */
279     for (i = 0; i < 3; i++)
280     vtmp2[i] = sray.rdir[i] - dtmp1*vtmp1[i];
281    
282     l2 = lambda(m, v2, dv, vtmp2); /* second lambda */
283     if (l2 < 0)
284     continue;
285     /* compute color from spectrum */
286     if (l1 < l2)
287     spec_rgb(ctmp, l1, l2);
288     else
289     spec_rgb(ctmp, l2, l1);
290     multcolor(ctmp, sray.rcol);
291     scalecolor(ctmp, tr);
292     addcolor(r->rcol, ctmp);
293     success++;
294     }
295     return(success);
296     }
297    
298    
299     static int
300     lambda(m, v2, dv, lr) /* compute lambda for material */
301     register OBJREC *m;
302     FVECT v2, dv, lr;
303     {
304     FVECT lrXdv, v2Xlr;
305     double dtmp, denom;
306     int i;
307    
308     fcross(lrXdv, lr, dv);
309     for (i = 0; i < 3; i++)
310     if (lrXdv[i] > FTINY || lrXdv[i] < -FTINY)
311     break;
312     if (i >= 3)
313     return(-1);
314    
315     fcross(v2Xlr, v2, lr);
316    
317     dtmp = m->oargs.farg[4] / MLAMBDA;
318     denom = dtmp + v2Xlr[i]/lrXdv[i] * (m->oargs.farg[3] + dtmp);
319    
320     if (denom < FTINY)
321     return(-1);
322    
323     return(m->oargs.farg[4] / denom);
324     }
325    
326     #endif /* DISPERSE */