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 |
static int lambda(); |
21 |
#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 |
extern COLOR cextinction; /* global coefficient of extinction */ |
54 |
extern double salbedo; /* global scattering albedo */ |
55 |
|
56 |
|
57 |
static double |
58 |
mylog(x) /* special log for extinction coefficients */ |
59 |
double x; |
60 |
{ |
61 |
if (x < 1e-40) |
62 |
return(-100.); |
63 |
if (x >= 1.) |
64 |
return(0.); |
65 |
return(log(x)); |
66 |
} |
67 |
|
68 |
|
69 |
m_dielectric(m, r) /* color a ray which hit a dielectric interface */ |
70 |
OBJREC *m; |
71 |
register RAY *r; |
72 |
{ |
73 |
double cos1, cos2, nratio; |
74 |
COLOR ctrans; |
75 |
double talb; |
76 |
double mabsorp; |
77 |
double refl, trans; |
78 |
FVECT dnorm; |
79 |
double d1, d2; |
80 |
RAY p; |
81 |
register int i; |
82 |
|
83 |
if (m->oargs.nfargs != (m->otype==MAT_DIELECTRIC ? 5 : 8)) |
84 |
objerror(m, USER, "bad arguments"); |
85 |
|
86 |
raytexture(r, m->omod); /* get modifiers */ |
87 |
|
88 |
cos1 = raynormal(dnorm, r); /* cosine of theta1 */ |
89 |
/* index of refraction */ |
90 |
if (m->otype == MAT_DIELECTRIC) |
91 |
nratio = m->oargs.farg[3] + m->oargs.farg[4]/MLAMBDA; |
92 |
else |
93 |
nratio = m->oargs.farg[3] / m->oargs.farg[7]; |
94 |
|
95 |
if (cos1 < 0.0) { /* inside */ |
96 |
cos1 = -cos1; |
97 |
dnorm[0] = -dnorm[0]; |
98 |
dnorm[1] = -dnorm[1]; |
99 |
dnorm[2] = -dnorm[2]; |
100 |
setcolor(r->cext, -mylog(m->oargs.farg[0]*colval(r->pcol,RED)), |
101 |
-mylog(m->oargs.farg[1]*colval(r->pcol,GRN)), |
102 |
-mylog(m->oargs.farg[2]*colval(r->pcol,BLU))); |
103 |
r->albedo = 0.; |
104 |
r->gecc = 0.; |
105 |
if (m->otype == MAT_INTERFACE) { |
106 |
setcolor(ctrans, |
107 |
-mylog(m->oargs.farg[4]*colval(r->pcol,RED)), |
108 |
-mylog(m->oargs.farg[5]*colval(r->pcol,GRN)), |
109 |
-mylog(m->oargs.farg[6]*colval(r->pcol,BLU))); |
110 |
talb = 0.; |
111 |
} else { |
112 |
copycolor(ctrans, cextinction); |
113 |
talb = salbedo; |
114 |
} |
115 |
} else { /* outside */ |
116 |
nratio = 1.0 / nratio; |
117 |
|
118 |
setcolor(ctrans, -mylog(m->oargs.farg[0]*colval(r->pcol,RED)), |
119 |
-mylog(m->oargs.farg[1]*colval(r->pcol,GRN)), |
120 |
-mylog(m->oargs.farg[2]*colval(r->pcol,BLU))); |
121 |
talb = 0.; |
122 |
if (m->otype == MAT_INTERFACE) { |
123 |
setcolor(r->cext, |
124 |
-mylog(m->oargs.farg[4]*colval(r->pcol,RED)), |
125 |
-mylog(m->oargs.farg[5]*colval(r->pcol,GRN)), |
126 |
-mylog(m->oargs.farg[6]*colval(r->pcol,BLU))); |
127 |
r->albedo = 0.; |
128 |
r->gecc = 0.; |
129 |
} |
130 |
} |
131 |
mabsorp = exp(-bright(r->cext)*r->rot); /* approximate */ |
132 |
|
133 |
d2 = 1.0 - nratio*nratio*(1.0 - cos1*cos1); /* compute cos theta2 */ |
134 |
|
135 |
if (d2 < FTINY) /* total reflection */ |
136 |
|
137 |
refl = 1.0; |
138 |
|
139 |
else { /* refraction occurs */ |
140 |
/* compute Fresnel's equations */ |
141 |
cos2 = sqrt(d2); |
142 |
d1 = cos1; |
143 |
d2 = nratio*cos2; |
144 |
d1 = (d1 - d2) / (d1 + d2); |
145 |
refl = d1 * d1; |
146 |
|
147 |
d1 = 1.0 / cos1; |
148 |
d2 = nratio / cos2; |
149 |
d1 = (d1 - d2) / (d1 + d2); |
150 |
refl += d1 * d1; |
151 |
|
152 |
refl *= 0.5; |
153 |
trans = 1.0 - refl; |
154 |
|
155 |
if (rayorigin(&p, r, REFRACTED, mabsorp*trans) == 0) { |
156 |
|
157 |
/* compute refracted ray */ |
158 |
d1 = nratio*cos1 - cos2; |
159 |
for (i = 0; i < 3; i++) |
160 |
p.rdir[i] = nratio*r->rdir[i] + d1*dnorm[i]; |
161 |
|
162 |
#ifdef DISPERSE |
163 |
if (m->otype != MAT_DIELECTRIC |
164 |
|| r->rod > 0.0 |
165 |
|| r->crtype & SHADOW |
166 |
|| !directvis |
167 |
|| m->oargs.farg[4] == 0.0 |
168 |
|| !disperse(m, r, p.rdir, trans)) |
169 |
#endif |
170 |
{ |
171 |
copycolor(p.cext, ctrans); |
172 |
p.albedo = talb; |
173 |
rayvalue(&p); |
174 |
scalecolor(p.rcol, trans); |
175 |
addcolor(r->rcol, p.rcol); |
176 |
if (nratio >= 1.0-FTINY && nratio <= 1.0+FTINY) |
177 |
r->rt = r->rot + p.rt; |
178 |
} |
179 |
} |
180 |
} |
181 |
|
182 |
if (!(r->crtype & SHADOW) && |
183 |
rayorigin(&p, r, REFLECTED, mabsorp*refl) == 0) { |
184 |
|
185 |
/* compute reflected ray */ |
186 |
for (i = 0; i < 3; i++) |
187 |
p.rdir[i] = r->rdir[i] + 2.0*cos1*dnorm[i]; |
188 |
|
189 |
rayvalue(&p); /* reflected ray value */ |
190 |
|
191 |
scalecolor(p.rcol, refl); /* color contribution */ |
192 |
addcolor(r->rcol, p.rcol); |
193 |
} |
194 |
/* rayvalue() computes absorption */ |
195 |
return(1); |
196 |
} |
197 |
|
198 |
|
199 |
#ifdef DISPERSE |
200 |
|
201 |
static |
202 |
disperse(m, r, vt, tr) /* check light sources for dispersion */ |
203 |
OBJREC *m; |
204 |
RAY *r; |
205 |
FVECT vt; |
206 |
double tr; |
207 |
{ |
208 |
RAY sray, *entray; |
209 |
FVECT v1, v2, n1, n2; |
210 |
FVECT dv, v2Xdv; |
211 |
double v2Xdvv2Xdv; |
212 |
int success = 0; |
213 |
SRCINDEX si; |
214 |
FVECT vtmp1, vtmp2; |
215 |
double dtmp1, dtmp2; |
216 |
int l1, l2; |
217 |
COLOR ctmp; |
218 |
int i; |
219 |
|
220 |
/* |
221 |
* This routine computes dispersion to the first order using |
222 |
* the following assumptions: |
223 |
* |
224 |
* 1) The dependency of the index of refraction on wavelength |
225 |
* is approximated by Hartmann's equation with lambda0 |
226 |
* equal to zero. |
227 |
* 2) The entry and exit locations are constant with respect |
228 |
* to dispersion. |
229 |
* |
230 |
* The second assumption permits us to model dispersion without |
231 |
* having to sample refracted directions. We assume that the |
232 |
* geometry inside the material is constant, and concern ourselves |
233 |
* only with the relationship between the entering and exiting ray. |
234 |
* We compute the first derivatives of the entering and exiting |
235 |
* refraction with respect to the index of refraction. This |
236 |
* is then used in a first order Taylor series to determine the |
237 |
* index of refraction necessary to send the exiting ray to each |
238 |
* light source. |
239 |
* If an exiting ray hits a light source within the refraction |
240 |
* boundaries, we sum all the frequencies over the disc of the |
241 |
* light source to determine the resulting color. A smaller light |
242 |
* source will therefore exhibit a sharper spectrum. |
243 |
*/ |
244 |
|
245 |
if (!(r->crtype & REFRACTED)) { /* ray started in material */ |
246 |
VCOPY(v1, r->rdir); |
247 |
n1[0] = -r->rdir[0]; n1[1] = -r->rdir[1]; n1[2] = -r->rdir[2]; |
248 |
} else { |
249 |
/* find entry point */ |
250 |
for (entray = r; entray->rtype != REFRACTED; |
251 |
entray = entray->parent) |
252 |
; |
253 |
entray = entray->parent; |
254 |
if (entray->crtype & REFRACTED) /* too difficult */ |
255 |
return(0); |
256 |
VCOPY(v1, entray->rdir); |
257 |
VCOPY(n1, entray->ron); |
258 |
} |
259 |
VCOPY(v2, vt); /* exiting ray */ |
260 |
VCOPY(n2, r->ron); |
261 |
|
262 |
/* first order dispersion approx. */ |
263 |
dtmp1 = DOT(n1, v1); |
264 |
dtmp2 = DOT(n2, v2); |
265 |
for (i = 0; i < 3; i++) |
266 |
dv[i] = v1[i] + v2[i] - n1[i]/dtmp1 - n2[i]/dtmp2; |
267 |
|
268 |
if (DOT(dv, dv) <= FTINY) /* null effect */ |
269 |
return(0); |
270 |
/* compute plane normal */ |
271 |
fcross(v2Xdv, v2, dv); |
272 |
v2Xdvv2Xdv = DOT(v2Xdv, v2Xdv); |
273 |
|
274 |
/* check sources */ |
275 |
initsrcindex(&si); |
276 |
while (srcray(&sray, r, &si)) { |
277 |
|
278 |
if (DOT(sray.rdir, v2) < MINCOS) |
279 |
continue; /* bad source */ |
280 |
/* adjust source ray */ |
281 |
|
282 |
dtmp1 = DOT(v2Xdv, sray.rdir) / v2Xdvv2Xdv; |
283 |
sray.rdir[0] -= dtmp1 * v2Xdv[0]; |
284 |
sray.rdir[1] -= dtmp1 * v2Xdv[1]; |
285 |
sray.rdir[2] -= dtmp1 * v2Xdv[2]; |
286 |
|
287 |
l1 = lambda(m, v2, dv, sray.rdir); /* mean lambda */ |
288 |
|
289 |
if (l1 > MAXLAMBDA || l1 < MINLAMBDA) /* not visible */ |
290 |
continue; |
291 |
/* trace source ray */ |
292 |
normalize(sray.rdir); |
293 |
rayvalue(&sray); |
294 |
if (bright(sray.rcol) <= FTINY) /* missed it */ |
295 |
continue; |
296 |
|
297 |
/* |
298 |
* Compute spectral sum over diameter of source. |
299 |
* First find directions for rays going to opposite |
300 |
* sides of source, then compute wavelengths for each. |
301 |
*/ |
302 |
|
303 |
fcross(vtmp1, v2Xdv, sray.rdir); |
304 |
dtmp1 = sqrt(si.dom / v2Xdvv2Xdv / PI); |
305 |
|
306 |
/* compute first ray */ |
307 |
for (i = 0; i < 3; i++) |
308 |
vtmp2[i] = sray.rdir[i] + dtmp1*vtmp1[i]; |
309 |
|
310 |
l1 = lambda(m, v2, dv, vtmp2); /* first lambda */ |
311 |
if (l1 < 0) |
312 |
continue; |
313 |
/* compute second ray */ |
314 |
for (i = 0; i < 3; i++) |
315 |
vtmp2[i] = sray.rdir[i] - dtmp1*vtmp1[i]; |
316 |
|
317 |
l2 = lambda(m, v2, dv, vtmp2); /* second lambda */ |
318 |
if (l2 < 0) |
319 |
continue; |
320 |
/* compute color from spectrum */ |
321 |
if (l1 < l2) |
322 |
spec_rgb(ctmp, l1, l2); |
323 |
else |
324 |
spec_rgb(ctmp, l2, l1); |
325 |
multcolor(ctmp, sray.rcol); |
326 |
scalecolor(ctmp, tr); |
327 |
addcolor(r->rcol, ctmp); |
328 |
success++; |
329 |
} |
330 |
return(success); |
331 |
} |
332 |
|
333 |
|
334 |
static int |
335 |
lambda(m, v2, dv, lr) /* compute lambda for material */ |
336 |
register OBJREC *m; |
337 |
FVECT v2, dv, lr; |
338 |
{ |
339 |
FVECT lrXdv, v2Xlr; |
340 |
double dtmp, denom; |
341 |
int i; |
342 |
|
343 |
fcross(lrXdv, lr, dv); |
344 |
for (i = 0; i < 3; i++) |
345 |
if (lrXdv[i] > FTINY || lrXdv[i] < -FTINY) |
346 |
break; |
347 |
if (i >= 3) |
348 |
return(-1); |
349 |
|
350 |
fcross(v2Xlr, v2, lr); |
351 |
|
352 |
dtmp = m->oargs.farg[4] / MLAMBDA; |
353 |
denom = dtmp + v2Xlr[i]/lrXdv[i] * (m->oargs.farg[3] + dtmp); |
354 |
|
355 |
if (denom < FTINY) |
356 |
return(-1); |
357 |
|
358 |
return(m->oargs.farg[4] / denom); |
359 |
} |
360 |
|
361 |
#endif /* DISPERSE */ |