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 |
static disperse(); |
20 |
#endif |
21 |
|
22 |
/* |
23 |
* Explicit calculations for Fresnel's equation are performed, |
24 |
* but only one square root computation is necessary. |
25 |
* The index of refraction is given as a Hartmann equation |
26 |
* with lambda0 equal to zero. If the slope of Hartmann's |
27 |
* equation is non-zero, the material disperses light upon |
28 |
* refraction. This condition is examined on rays traced to |
29 |
* light sources. If a ray is exiting a dielectric material, we |
30 |
* check the sources to see if any would cause bright color to be |
31 |
* directed to the viewer due to dispersion. This gives colorful |
32 |
* sparkle to crystals, etc. (Only if DISPERSE is defined!) |
33 |
* |
34 |
* Arguments for MAT_DIELECTRIC are: |
35 |
* red grn blu rndx Hartmann |
36 |
* |
37 |
* Arguments for MAT_INTERFACE are: |
38 |
* red1 grn1 blu1 rndx1 red2 grn2 blu2 rndx2 |
39 |
* |
40 |
* The primaries are material transmission per unit length. |
41 |
* MAT_INTERFACE uses dielectric1 for inside and dielectric2 for |
42 |
* outside. |
43 |
*/ |
44 |
|
45 |
|
46 |
#define MLAMBDA 500 /* mean lambda */ |
47 |
#define MAXLAMBDA 779 /* maximum lambda */ |
48 |
#define MINLAMBDA 380 /* minimum lambda */ |
49 |
|
50 |
#define MINCOS 0.997 /* minimum dot product for dispersion */ |
51 |
|
52 |
|
53 |
m_dielectric(m, r) /* color a ray which hit something transparent */ |
54 |
OBJREC *m; |
55 |
register RAY *r; |
56 |
{ |
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 |
mabsorp = bright(mcolor); |
96 |
|
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 |
|| !directvis |
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 |
if (nratio >= 1.0-FTINY && nratio <= 1.0+FTINY) |
140 |
r->rt = r->rot + p.rt; |
141 |
} |
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 |
} |
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 |
RAY sray, *entray; |
172 |
FVECT v1, v2, n1, n2; |
173 |
FVECT dv, v2Xdv; |
174 |
double v2Xdvv2Xdv; |
175 |
int success = 0; |
176 |
SRCINDEX si; |
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 |
initsrcindex(&si); |
239 |
while (srcray(&sray, r, &si)) { |
240 |
|
241 |
if (DOT(sray.rdir, v2) < MINCOS) |
242 |
continue; /* bad source */ |
243 |
/* adjust source ray */ |
244 |
|
245 |
dtmp1 = DOT(v2Xdv, sray.rdir) / v2Xdvv2Xdv; |
246 |
sray.rdir[0] -= dtmp1 * v2Xdv[0]; |
247 |
sray.rdir[1] -= dtmp1 * v2Xdv[1]; |
248 |
sray.rdir[2] -= dtmp1 * v2Xdv[2]; |
249 |
|
250 |
l1 = lambda(m, v2, dv, sray.rdir); /* mean lambda */ |
251 |
|
252 |
if (l1 > MAXLAMBDA || l1 < MINLAMBDA) /* not visible */ |
253 |
continue; |
254 |
/* trace source ray */ |
255 |
normalize(sray.rdir); |
256 |
rayvalue(&sray); |
257 |
if (bright(sray.rcol) <= FTINY) /* missed it */ |
258 |
continue; |
259 |
|
260 |
/* |
261 |
* Compute spectral sum over diameter of source. |
262 |
* First find directions for rays going to opposite |
263 |
* sides of source, then compute wavelengths for each. |
264 |
*/ |
265 |
|
266 |
fcross(vtmp1, v2Xdv, sray.rdir); |
267 |
dtmp1 = sqrt(si.dom / v2Xdvv2Xdv / PI); |
268 |
|
269 |
/* compute first ray */ |
270 |
for (i = 0; i < 3; i++) |
271 |
vtmp2[i] = sray.rdir[i] + dtmp1*vtmp1[i]; |
272 |
|
273 |
l1 = lambda(m, v2, dv, vtmp2); /* first lambda */ |
274 |
if (l1 < 0) |
275 |
continue; |
276 |
/* compute second ray */ |
277 |
for (i = 0; i < 3; i++) |
278 |
vtmp2[i] = sray.rdir[i] - dtmp1*vtmp1[i]; |
279 |
|
280 |
l2 = lambda(m, v2, dv, vtmp2); /* second lambda */ |
281 |
if (l2 < 0) |
282 |
continue; |
283 |
/* compute color from spectrum */ |
284 |
if (l1 < l2) |
285 |
spec_rgb(ctmp, l1, l2); |
286 |
else |
287 |
spec_rgb(ctmp, l2, l1); |
288 |
multcolor(ctmp, sray.rcol); |
289 |
scalecolor(ctmp, tr); |
290 |
addcolor(r->rcol, ctmp); |
291 |
success++; |
292 |
} |
293 |
return(success); |
294 |
} |
295 |
|
296 |
|
297 |
static int |
298 |
lambda(m, v2, dv, lr) /* compute lambda for material */ |
299 |
register OBJREC *m; |
300 |
FVECT v2, dv, lr; |
301 |
{ |
302 |
FVECT lrXdv, v2Xlr; |
303 |
double dtmp, denom; |
304 |
int i; |
305 |
|
306 |
fcross(lrXdv, lr, dv); |
307 |
for (i = 0; i < 3; i++) |
308 |
if (lrXdv[i] > FTINY || lrXdv[i] < -FTINY) |
309 |
break; |
310 |
if (i >= 3) |
311 |
return(-1); |
312 |
|
313 |
fcross(v2Xlr, v2, lr); |
314 |
|
315 |
dtmp = m->oargs.farg[4] / MLAMBDA; |
316 |
denom = dtmp + v2Xlr[i]/lrXdv[i] * (m->oargs.farg[3] + dtmp); |
317 |
|
318 |
if (denom < FTINY) |
319 |
return(-1); |
320 |
|
321 |
return(m->oargs.farg[4] / denom); |
322 |
} |
323 |
|
324 |
#endif /* DISPERSE */ |