1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: dielectric.c,v 1.4 2014/12/01 17:30:55 taschreg Exp taschreg $"; |
3 |
#endif |
4 |
/* |
5 |
* dielectric.c - shading function for transparent materials. |
6 |
*/ |
7 |
|
8 |
#include "copyright.h" |
9 |
|
10 |
#include "ray.h" |
11 |
#include "otypes.h" |
12 |
#include "rtotypes.h" |
13 |
#include "pmapmat.h" |
14 |
|
15 |
#ifdef DISPERSE |
16 |
#include "source.h" |
17 |
static int disperse(OBJREC *m,RAY *r,FVECT vt,double tr,COLOR cet,COLOR abt); |
18 |
static int lambda(OBJREC *m, FVECT v2, FVECT dv, FVECT lr); |
19 |
#endif |
20 |
|
21 |
static double mylog(double x); |
22 |
|
23 |
|
24 |
/* |
25 |
* Explicit calculations for Fresnel's equation are performed, |
26 |
* but only one square root computation is necessary. |
27 |
* The index of refraction is given as a Hartmann equation |
28 |
* with lambda0 equal to zero. If the slope of Hartmann's |
29 |
* equation is non-zero, the material disperses light upon |
30 |
* refraction. This condition is examined on rays traced to |
31 |
* light sources. If a ray is exiting a dielectric material, we |
32 |
* check the sources to see if any would cause bright color to be |
33 |
* directed to the viewer due to dispersion. This gives colorful |
34 |
* sparkle to crystals, etc. (Only if DISPERSE is defined!) |
35 |
* |
36 |
* Arguments for MAT_DIELECTRIC are: |
37 |
* red grn blu rndx Hartmann |
38 |
* |
39 |
* Arguments for MAT_INTERFACE are: |
40 |
* red1 grn1 blu1 rndx1 red2 grn2 blu2 rndx2 |
41 |
* |
42 |
* The primaries are material transmission per unit length. |
43 |
* MAT_INTERFACE uses dielectric1 for inside and dielectric2 for |
44 |
* outside. |
45 |
*/ |
46 |
|
47 |
|
48 |
#define MLAMBDA 500 /* mean lambda */ |
49 |
#define MAXLAMBDA 779 /* maximum lambda */ |
50 |
#define MINLAMBDA 380 /* minimum lambda */ |
51 |
|
52 |
#define MINCOS 0.997 /* minimum dot product for dispersion */ |
53 |
|
54 |
static |
55 |
double |
56 |
mylog( /* special log for extinction coefficients */ |
57 |
double x |
58 |
) |
59 |
{ |
60 |
if (x < 1e-40) |
61 |
return(-100.); |
62 |
if (x >= 1.) |
63 |
return(0.); |
64 |
return(log(x)); |
65 |
} |
66 |
|
67 |
|
68 |
int |
69 |
m_dielectric( /* color a ray which hit a dielectric interface */ |
70 |
OBJREC *m, |
71 |
RAY *r |
72 |
) |
73 |
{ |
74 |
double cos1, cos2, nratio; |
75 |
COLOR ctrans; |
76 |
COLOR talb; |
77 |
int hastexture; |
78 |
double transdist=0, transtest=0; |
79 |
double mirdist=0, mirtest=0; |
80 |
int flatsurface; |
81 |
double refl, trans; |
82 |
FVECT dnorm; |
83 |
double d1, d2; |
84 |
RAY p; |
85 |
int i; |
86 |
|
87 |
/* PMAP: skip refracted shadow ray if accounted for by photon map */ |
88 |
if (shadowRayInPmap(r)) |
89 |
return(1); |
90 |
|
91 |
if (m->oargs.nfargs != (m->otype==MAT_DIELECTRIC ? 5 : 8)) |
92 |
objerror(m, USER, "bad arguments"); |
93 |
|
94 |
raytexture(r, m->omod); /* get modifiers */ |
95 |
|
96 |
if ( (hastexture = DOT(r->pert,r->pert) > FTINY*FTINY) ) |
97 |
cos1 = raynormal(dnorm, r); /* perturb normal */ |
98 |
else { |
99 |
VCOPY(dnorm, r->ron); |
100 |
cos1 = r->rod; |
101 |
} |
102 |
flatsurface = r->ro != NULL && isflat(r->ro->otype) && |
103 |
!hastexture | (r->crtype & AMBIENT); |
104 |
|
105 |
/* index of refraction */ |
106 |
if (m->otype == MAT_DIELECTRIC) |
107 |
nratio = m->oargs.farg[3] + m->oargs.farg[4]/MLAMBDA; |
108 |
else |
109 |
nratio = m->oargs.farg[3] / m->oargs.farg[7]; |
110 |
|
111 |
if (cos1 < 0.0) { /* inside */ |
112 |
hastexture = -hastexture; |
113 |
cos1 = -cos1; |
114 |
dnorm[0] = -dnorm[0]; |
115 |
dnorm[1] = -dnorm[1]; |
116 |
dnorm[2] = -dnorm[2]; |
117 |
setcolor(r->cext, -mylog(m->oargs.farg[0]*colval(r->pcol,RED)), |
118 |
-mylog(m->oargs.farg[1]*colval(r->pcol,GRN)), |
119 |
-mylog(m->oargs.farg[2]*colval(r->pcol,BLU))); |
120 |
setcolor(r->albedo, 0., 0., 0.); |
121 |
r->gecc = 0.; |
122 |
if (m->otype == MAT_INTERFACE) { |
123 |
setcolor(ctrans, |
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 |
setcolor(talb, 0., 0., 0.); |
128 |
} else { |
129 |
copycolor(ctrans, cextinction); |
130 |
copycolor(talb, salbedo); |
131 |
} |
132 |
} else { /* outside */ |
133 |
nratio = 1.0 / nratio; |
134 |
|
135 |
setcolor(ctrans, -mylog(m->oargs.farg[0]*colval(r->pcol,RED)), |
136 |
-mylog(m->oargs.farg[1]*colval(r->pcol,GRN)), |
137 |
-mylog(m->oargs.farg[2]*colval(r->pcol,BLU))); |
138 |
setcolor(talb, 0., 0., 0.); |
139 |
if (m->otype == MAT_INTERFACE) { |
140 |
setcolor(r->cext, |
141 |
-mylog(m->oargs.farg[4]*colval(r->pcol,RED)), |
142 |
-mylog(m->oargs.farg[5]*colval(r->pcol,GRN)), |
143 |
-mylog(m->oargs.farg[6]*colval(r->pcol,BLU))); |
144 |
setcolor(r->albedo, 0., 0., 0.); |
145 |
r->gecc = 0.; |
146 |
} |
147 |
} |
148 |
|
149 |
d2 = 1.0 - nratio*nratio*(1.0 - cos1*cos1); /* compute cos theta2 */ |
150 |
|
151 |
if (d2 < FTINY) /* total reflection */ |
152 |
|
153 |
refl = 1.0; |
154 |
|
155 |
else { /* refraction occurs */ |
156 |
/* compute Fresnel's equations */ |
157 |
cos2 = sqrt(d2); |
158 |
d1 = cos1; |
159 |
d2 = nratio*cos2; |
160 |
d1 = (d1 - d2) / (d1 + d2); |
161 |
refl = d1 * d1; |
162 |
|
163 |
d1 = 1.0 / cos1; |
164 |
d2 = nratio / cos2; |
165 |
d1 = (d1 - d2) / (d1 + d2); |
166 |
refl += d1 * d1; |
167 |
|
168 |
refl *= 0.5; |
169 |
trans = 1.0 - refl; |
170 |
|
171 |
trans *= nratio*nratio; /* solid angle ratio */ |
172 |
|
173 |
setcolor(p.rcoef, trans, trans, trans); |
174 |
|
175 |
if (rayorigin(&p, REFRACTED, r, p.rcoef) == 0) { |
176 |
|
177 |
/* compute refracted ray */ |
178 |
d1 = nratio*cos1 - cos2; |
179 |
for (i = 0; i < 3; i++) |
180 |
p.rdir[i] = nratio*r->rdir[i] + d1*dnorm[i]; |
181 |
/* accidental reflection? */ |
182 |
if (hastexture && |
183 |
DOT(p.rdir,r->ron)*hastexture >= -FTINY) { |
184 |
d1 *= (double)hastexture; |
185 |
for (i = 0; i < 3; i++) /* ignore texture */ |
186 |
p.rdir[i] = nratio*r->rdir[i] + |
187 |
d1*r->ron[i]; |
188 |
normalize(p.rdir); /* not exact */ |
189 |
} else |
190 |
checknorm(p.rdir); |
191 |
#ifdef DISPERSE |
192 |
if (m->otype != MAT_DIELECTRIC |
193 |
|| r->rod > 0.0 |
194 |
|| r->crtype & SHADOW |
195 |
|| !directvis |
196 |
|| m->oargs.farg[4] == 0.0 |
197 |
|| !disperse(m, r, p.rdir, |
198 |
trans, ctrans, talb)) |
199 |
#endif |
200 |
{ |
201 |
copycolor(p.cext, ctrans); |
202 |
copycolor(p.albedo, talb); |
203 |
rayvalue(&p); |
204 |
multcolor(p.rcol, p.rcoef); |
205 |
addcolor(r->rcol, p.rcol); |
206 |
/* virtual distance */ |
207 |
if (flatsurface || |
208 |
(1.-FTINY <= nratio) & |
209 |
(nratio <= 1.+FTINY)) { |
210 |
transtest = 2*bright(p.rcol); |
211 |
transdist = r->rot + p.rt; |
212 |
} |
213 |
} |
214 |
} |
215 |
} |
216 |
setcolor(p.rcoef, refl, refl, refl); |
217 |
|
218 |
if (!(r->crtype & SHADOW) && |
219 |
rayorigin(&p, REFLECTED, r, p.rcoef) == 0) { |
220 |
|
221 |
/* compute reflected ray */ |
222 |
VSUM(p.rdir, r->rdir, dnorm, 2.*cos1); |
223 |
/* accidental penetration? */ |
224 |
if (hastexture && DOT(p.rdir,r->ron)*hastexture <= FTINY) |
225 |
VSUM(p.rdir, r->rdir, r->ron, 2.*r->rod); |
226 |
checknorm(p.rdir); |
227 |
rayvalue(&p); /* reflected ray value */ |
228 |
|
229 |
multcolor(p.rcol, p.rcoef); /* color contribution */ |
230 |
addcolor(r->rcol, p.rcol); |
231 |
/* virtual distance */ |
232 |
if (flatsurface) { |
233 |
mirtest = 2*bright(p.rcol); |
234 |
mirdist = r->rot + p.rt; |
235 |
} |
236 |
} |
237 |
/* check distance to return */ |
238 |
d1 = bright(r->rcol); |
239 |
if (transtest > d1) |
240 |
r->rt = transdist; |
241 |
else if (mirtest > d1) |
242 |
r->rt = mirdist; |
243 |
/* rayvalue() computes absorption */ |
244 |
return(1); |
245 |
} |
246 |
|
247 |
|
248 |
#ifdef DISPERSE |
249 |
|
250 |
static int |
251 |
disperse( /* check light sources for dispersion */ |
252 |
OBJREC *m, |
253 |
RAY *r, |
254 |
FVECT vt, |
255 |
double tr, |
256 |
COLOR cet, |
257 |
COLOR abt |
258 |
) |
259 |
{ |
260 |
RAY sray; |
261 |
const RAY *entray; |
262 |
FVECT v1, v2, n1, n2; |
263 |
FVECT dv, v2Xdv; |
264 |
double v2Xdvv2Xdv; |
265 |
int success = 0; |
266 |
SRCINDEX si; |
267 |
FVECT vtmp1, vtmp2; |
268 |
double dtmp1, dtmp2; |
269 |
int l1, l2; |
270 |
COLOR ctmp; |
271 |
int i; |
272 |
|
273 |
/* |
274 |
* This routine computes dispersion to the first order using |
275 |
* the following assumptions: |
276 |
* |
277 |
* 1) The dependency of the index of refraction on wavelength |
278 |
* is approximated by Hartmann's equation with lambda0 |
279 |
* equal to zero. |
280 |
* 2) The entry and exit locations are constant with respect |
281 |
* to dispersion. |
282 |
* |
283 |
* The second assumption permits us to model dispersion without |
284 |
* having to sample refracted directions. We assume that the |
285 |
* geometry inside the material is constant, and concern ourselves |
286 |
* only with the relationship between the entering and exiting ray. |
287 |
* We compute the first derivatives of the entering and exiting |
288 |
* refraction with respect to the index of refraction. This |
289 |
* is then used in a first order Taylor series to determine the |
290 |
* index of refraction necessary to send the exiting ray to each |
291 |
* light source. |
292 |
* If an exiting ray hits a light source within the refraction |
293 |
* boundaries, we sum all the frequencies over the disc of the |
294 |
* light source to determine the resulting color. A smaller light |
295 |
* source will therefore exhibit a sharper spectrum. |
296 |
*/ |
297 |
|
298 |
if (!(r->crtype & REFRACTED)) { /* ray started in material */ |
299 |
VCOPY(v1, r->rdir); |
300 |
n1[0] = -r->rdir[0]; n1[1] = -r->rdir[1]; n1[2] = -r->rdir[2]; |
301 |
} else { |
302 |
/* find entry point */ |
303 |
for (entray = r; entray->rtype != REFRACTED; |
304 |
entray = entray->parent) |
305 |
; |
306 |
entray = entray->parent; |
307 |
if (entray->crtype & REFRACTED) /* too difficult */ |
308 |
return(0); |
309 |
VCOPY(v1, entray->rdir); |
310 |
VCOPY(n1, entray->ron); |
311 |
} |
312 |
VCOPY(v2, vt); /* exiting ray */ |
313 |
VCOPY(n2, r->ron); |
314 |
|
315 |
/* first order dispersion approx. */ |
316 |
dtmp1 = 1./DOT(n1, v1); |
317 |
dtmp2 = 1./DOT(n2, v2); |
318 |
for (i = 0; i < 3; i++) |
319 |
dv[i] = v1[i] + v2[i] - n1[i]*dtmp1 - n2[i]*dtmp2; |
320 |
|
321 |
if (DOT(dv, dv) <= FTINY) /* null effect */ |
322 |
return(0); |
323 |
/* compute plane normal */ |
324 |
fcross(v2Xdv, v2, dv); |
325 |
v2Xdvv2Xdv = DOT(v2Xdv, v2Xdv); |
326 |
|
327 |
/* check sources */ |
328 |
initsrcindex(&si); |
329 |
while (srcray(&sray, r, &si)) { |
330 |
|
331 |
if (DOT(sray.rdir, v2) < MINCOS) |
332 |
continue; /* bad source */ |
333 |
/* adjust source ray */ |
334 |
|
335 |
dtmp1 = DOT(v2Xdv, sray.rdir) / v2Xdvv2Xdv; |
336 |
sray.rdir[0] -= dtmp1 * v2Xdv[0]; |
337 |
sray.rdir[1] -= dtmp1 * v2Xdv[1]; |
338 |
sray.rdir[2] -= dtmp1 * v2Xdv[2]; |
339 |
|
340 |
l1 = lambda(m, v2, dv, sray.rdir); /* mean lambda */ |
341 |
|
342 |
if (l1 > MAXLAMBDA || l1 < MINLAMBDA) /* not visible */ |
343 |
continue; |
344 |
/* trace source ray */ |
345 |
copycolor(sray.cext, cet); |
346 |
copycolor(sray.albedo, abt); |
347 |
normalize(sray.rdir); |
348 |
rayvalue(&sray); |
349 |
if (bright(sray.rcol) <= FTINY) /* missed it */ |
350 |
continue; |
351 |
|
352 |
/* |
353 |
* Compute spectral sum over diameter of source. |
354 |
* First find directions for rays going to opposite |
355 |
* sides of source, then compute wavelengths for each. |
356 |
*/ |
357 |
|
358 |
fcross(vtmp1, v2Xdv, sray.rdir); |
359 |
dtmp1 = sqrt(si.dom / v2Xdvv2Xdv / PI); |
360 |
|
361 |
/* compute first ray */ |
362 |
VSUM(vtmp2, sray.rdir, vtmp1, dtmp1); |
363 |
|
364 |
l1 = lambda(m, v2, dv, vtmp2); /* first lambda */ |
365 |
if (l1 < 0) |
366 |
continue; |
367 |
/* compute second ray */ |
368 |
VSUM(vtmp2, sray.rdir, vtmp1, -dtmp1); |
369 |
|
370 |
l2 = lambda(m, v2, dv, vtmp2); /* second lambda */ |
371 |
if (l2 < 0) |
372 |
continue; |
373 |
/* compute color from spectrum */ |
374 |
if (l1 < l2) |
375 |
spec_rgb(ctmp, l1, l2); |
376 |
else |
377 |
spec_rgb(ctmp, l2, l1); |
378 |
multcolor(ctmp, sray.rcol); |
379 |
scalecolor(ctmp, tr); |
380 |
addcolor(r->rcol, ctmp); |
381 |
success++; |
382 |
} |
383 |
return(success); |
384 |
} |
385 |
|
386 |
|
387 |
static int |
388 |
lambda( /* compute lambda for material */ |
389 |
OBJREC *m, |
390 |
FVECT v2, |
391 |
FVECT dv, |
392 |
FVECT lr |
393 |
) |
394 |
{ |
395 |
FVECT lrXdv, v2Xlr; |
396 |
double dtmp, denom; |
397 |
int i; |
398 |
|
399 |
fcross(lrXdv, lr, dv); |
400 |
for (i = 0; i < 3; i++) |
401 |
if ((lrXdv[i] > FTINY) | (lrXdv[i] < -FTINY)) |
402 |
break; |
403 |
if (i >= 3) |
404 |
return(-1); |
405 |
|
406 |
fcross(v2Xlr, v2, lr); |
407 |
|
408 |
dtmp = m->oargs.farg[4] / MLAMBDA; |
409 |
denom = dtmp + v2Xlr[i]/lrXdv[i] * (m->oargs.farg[3] + dtmp); |
410 |
|
411 |
if (denom < FTINY) |
412 |
return(-1); |
413 |
|
414 |
return(m->oargs.farg[4] / denom); |
415 |
} |
416 |
|
417 |
#endif /* DISPERSE */ |