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