1 |
– |
/* Copyright (c) 1986 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* dielectric.c - shading function for transparent materials. |
6 |
+ |
*/ |
7 |
+ |
|
8 |
+ |
/* ==================================================================== |
9 |
+ |
* The Radiance Software License, Version 1.0 |
10 |
|
* |
11 |
< |
* 9/6/85 |
11 |
> |
* Copyright (c) 1990 - 2002 The Regents of the University of California, |
12 |
> |
* through Lawrence Berkeley National Laboratory. All rights reserved. |
13 |
> |
* |
14 |
> |
* Redistribution and use in source and binary forms, with or without |
15 |
> |
* modification, are permitted provided that the following conditions |
16 |
> |
* are met: |
17 |
> |
* |
18 |
> |
* 1. Redistributions of source code must retain the above copyright |
19 |
> |
* notice, this list of conditions and the following disclaimer. |
20 |
> |
* |
21 |
> |
* 2. Redistributions in binary form must reproduce the above copyright |
22 |
> |
* notice, this list of conditions and the following disclaimer in |
23 |
> |
* the documentation and/or other materials provided with the |
24 |
> |
* distribution. |
25 |
> |
* |
26 |
> |
* 3. The end-user documentation included with the redistribution, |
27 |
> |
* if any, must include the following acknowledgment: |
28 |
> |
* "This product includes Radiance software |
29 |
> |
* (http://radsite.lbl.gov/) |
30 |
> |
* developed by the Lawrence Berkeley National Laboratory |
31 |
> |
* (http://www.lbl.gov/)." |
32 |
> |
* Alternately, this acknowledgment may appear in the software itself, |
33 |
> |
* if and wherever such third-party acknowledgments normally appear. |
34 |
> |
* |
35 |
> |
* 4. The names "Radiance," "Lawrence Berkeley National Laboratory" |
36 |
> |
* and "The Regents of the University of California" must |
37 |
> |
* not be used to endorse or promote products derived from this |
38 |
> |
* software without prior written permission. For written |
39 |
> |
* permission, please contact [email protected]. |
40 |
> |
* |
41 |
> |
* 5. Products derived from this software may not be called "Radiance", |
42 |
> |
* nor may "Radiance" appear in their name, without prior written |
43 |
> |
* permission of Lawrence Berkeley National Laboratory. |
44 |
> |
* |
45 |
> |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
46 |
> |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
47 |
> |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
48 |
> |
* DISCLAIMED. IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR |
49 |
> |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
50 |
> |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
51 |
> |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
52 |
> |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
53 |
> |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
54 |
> |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
55 |
> |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
56 |
> |
* SUCH DAMAGE. |
57 |
> |
* ==================================================================== |
58 |
> |
* |
59 |
> |
* This software consists of voluntary contributions made by many |
60 |
> |
* individuals on behalf of Lawrence Berkeley National Laboratory. For more |
61 |
> |
* information on Lawrence Berkeley National Laboratory, please see |
62 |
> |
* <http://www.lbl.gov/>. |
63 |
|
*/ |
64 |
|
|
65 |
|
#include "ray.h" |
68 |
|
|
69 |
|
#ifdef DISPERSE |
70 |
|
#include "source.h" |
71 |
+ |
static disperse(); |
72 |
+ |
static int lambda(); |
73 |
|
#endif |
74 |
|
|
75 |
|
/* |
103 |
|
#define MINCOS 0.997 /* minimum dot product for dispersion */ |
104 |
|
|
105 |
|
|
106 |
< |
m_dielectric(m, r) /* color a ray which hit something transparent */ |
106 |
> |
static double |
107 |
> |
mylog(x) /* special log for extinction coefficients */ |
108 |
> |
double x; |
109 |
> |
{ |
110 |
> |
if (x < 1e-40) |
111 |
> |
return(-100.); |
112 |
> |
if (x >= 1.) |
113 |
> |
return(0.); |
114 |
> |
return(log(x)); |
115 |
> |
} |
116 |
> |
|
117 |
> |
|
118 |
> |
m_dielectric(m, r) /* color a ray which hit a dielectric interface */ |
119 |
|
OBJREC *m; |
120 |
|
register RAY *r; |
121 |
|
{ |
56 |
– |
double sqrt(), pow(); |
122 |
|
double cos1, cos2, nratio; |
123 |
< |
COLOR mcolor; |
124 |
< |
double mabsorp; |
123 |
> |
COLOR ctrans; |
124 |
> |
COLOR talb; |
125 |
> |
int hastexture; |
126 |
|
double refl, trans; |
127 |
|
FVECT dnorm; |
128 |
|
double d1, d2; |
134 |
|
|
135 |
|
raytexture(r, m->omod); /* get modifiers */ |
136 |
|
|
137 |
< |
cos1 = raynormal(dnorm, r); /* cosine of theta1 */ |
137 |
> |
if (hastexture = DOT(r->pert,r->pert) > FTINY*FTINY) |
138 |
> |
cos1 = raynormal(dnorm, r); /* perturb normal */ |
139 |
> |
else { |
140 |
> |
VCOPY(dnorm, r->ron); |
141 |
> |
cos1 = r->rod; |
142 |
> |
} |
143 |
|
/* index of refraction */ |
144 |
|
if (m->otype == MAT_DIELECTRIC) |
145 |
|
nratio = m->oargs.farg[3] + m->oargs.farg[4]/MLAMBDA; |
147 |
|
nratio = m->oargs.farg[3] / m->oargs.farg[7]; |
148 |
|
|
149 |
|
if (cos1 < 0.0) { /* inside */ |
150 |
+ |
hastexture = -hastexture; |
151 |
|
cos1 = -cos1; |
152 |
|
dnorm[0] = -dnorm[0]; |
153 |
|
dnorm[1] = -dnorm[1]; |
154 |
|
dnorm[2] = -dnorm[2]; |
155 |
< |
setcolor(mcolor, pow(m->oargs.farg[0], r->rot), |
156 |
< |
pow(m->oargs.farg[1], r->rot), |
157 |
< |
pow(m->oargs.farg[2], r->rot)); |
155 |
> |
setcolor(r->cext, -mylog(m->oargs.farg[0]*colval(r->pcol,RED)), |
156 |
> |
-mylog(m->oargs.farg[1]*colval(r->pcol,GRN)), |
157 |
> |
-mylog(m->oargs.farg[2]*colval(r->pcol,BLU))); |
158 |
> |
setcolor(r->albedo, 0., 0., 0.); |
159 |
> |
r->gecc = 0.; |
160 |
> |
if (m->otype == MAT_INTERFACE) { |
161 |
> |
setcolor(ctrans, |
162 |
> |
-mylog(m->oargs.farg[4]*colval(r->pcol,RED)), |
163 |
> |
-mylog(m->oargs.farg[5]*colval(r->pcol,GRN)), |
164 |
> |
-mylog(m->oargs.farg[6]*colval(r->pcol,BLU))); |
165 |
> |
setcolor(talb, 0., 0., 0.); |
166 |
> |
} else { |
167 |
> |
copycolor(ctrans, cextinction); |
168 |
> |
copycolor(talb, salbedo); |
169 |
> |
} |
170 |
|
} else { /* outside */ |
171 |
|
nratio = 1.0 / nratio; |
172 |
< |
if (m->otype == MAT_INTERFACE) |
173 |
< |
setcolor(mcolor, pow(m->oargs.farg[4], r->rot), |
174 |
< |
pow(m->oargs.farg[5], r->rot), |
175 |
< |
pow(m->oargs.farg[6], r->rot)); |
176 |
< |
else |
177 |
< |
setcolor(mcolor, 1.0, 1.0, 1.0); |
172 |
> |
|
173 |
> |
setcolor(ctrans, -mylog(m->oargs.farg[0]*colval(r->pcol,RED)), |
174 |
> |
-mylog(m->oargs.farg[1]*colval(r->pcol,GRN)), |
175 |
> |
-mylog(m->oargs.farg[2]*colval(r->pcol,BLU))); |
176 |
> |
setcolor(talb, 0., 0., 0.); |
177 |
> |
if (m->otype == MAT_INTERFACE) { |
178 |
> |
setcolor(r->cext, |
179 |
> |
-mylog(m->oargs.farg[4]*colval(r->pcol,RED)), |
180 |
> |
-mylog(m->oargs.farg[5]*colval(r->pcol,GRN)), |
181 |
> |
-mylog(m->oargs.farg[6]*colval(r->pcol,BLU))); |
182 |
> |
setcolor(r->albedo, 0., 0., 0.); |
183 |
> |
r->gecc = 0.; |
184 |
> |
} |
185 |
|
} |
95 |
– |
mabsorp = intens(mcolor); |
186 |
|
|
187 |
|
d2 = 1.0 - nratio*nratio*(1.0 - cos1*cos1); /* compute cos theta2 */ |
188 |
|
|
203 |
|
d1 = (d1 - d2) / (d1 + d2); |
204 |
|
refl += d1 * d1; |
205 |
|
|
206 |
< |
refl /= 2.0; |
206 |
> |
refl *= 0.5; |
207 |
|
trans = 1.0 - refl; |
208 |
|
|
209 |
< |
if (rayorigin(&p, r, REFRACTED, mabsorp*trans) == 0) { |
209 |
> |
trans *= nratio*nratio; /* solid angle ratio */ |
210 |
|
|
211 |
+ |
if (rayorigin(&p, r, REFRACTED, trans) == 0) { |
212 |
+ |
|
213 |
|
/* compute refracted ray */ |
214 |
|
d1 = nratio*cos1 - cos2; |
215 |
|
for (i = 0; i < 3; i++) |
216 |
|
p.rdir[i] = nratio*r->rdir[i] + d1*dnorm[i]; |
217 |
< |
|
217 |
> |
/* accidental reflection? */ |
218 |
> |
if (hastexture && |
219 |
> |
DOT(p.rdir,r->ron)*hastexture >= -FTINY) { |
220 |
> |
d1 *= (double)hastexture; |
221 |
> |
for (i = 0; i < 3; i++) /* ignore texture */ |
222 |
> |
p.rdir[i] = nratio*r->rdir[i] + |
223 |
> |
d1*r->ron[i]; |
224 |
> |
normalize(p.rdir); /* not exact */ |
225 |
> |
} |
226 |
|
#ifdef DISPERSE |
227 |
|
if (m->otype != MAT_DIELECTRIC |
228 |
|
|| r->rod > 0.0 |
229 |
|
|| r->crtype & SHADOW |
230 |
+ |
|| !directvis |
231 |
|
|| m->oargs.farg[4] == 0.0 |
232 |
< |
|| !disperse(m, r, p.rdir, trans)) |
232 |
> |
|| !disperse(m, r, p.rdir, |
233 |
> |
trans, ctrans, talb)) |
234 |
|
#endif |
235 |
|
{ |
236 |
+ |
copycolor(p.cext, ctrans); |
237 |
+ |
copycolor(p.albedo, talb); |
238 |
|
rayvalue(&p); |
135 |
– |
multcolor(mcolor, r->pcol); /* modify */ |
239 |
|
scalecolor(p.rcol, trans); |
240 |
|
addcolor(r->rcol, p.rcol); |
241 |
+ |
if (nratio >= 1.0-FTINY && nratio <= 1.0+FTINY) |
242 |
+ |
r->rt = r->rot + p.rt; |
243 |
|
} |
244 |
|
} |
245 |
|
} |
246 |
|
|
247 |
|
if (!(r->crtype & SHADOW) && |
248 |
< |
rayorigin(&p, r, REFLECTED, mabsorp*refl) == 0) { |
248 |
> |
rayorigin(&p, r, REFLECTED, refl) == 0) { |
249 |
|
|
250 |
|
/* compute reflected ray */ |
251 |
|
for (i = 0; i < 3; i++) |
252 |
|
p.rdir[i] = r->rdir[i] + 2.0*cos1*dnorm[i]; |
253 |
+ |
/* accidental penetration? */ |
254 |
+ |
if (hastexture && DOT(p.rdir,r->ron)*hastexture <= FTINY) |
255 |
+ |
for (i = 0; i < 3; i++) /* ignore texture */ |
256 |
+ |
p.rdir[i] = r->rdir[i] + 2.0*r->rod*r->ron[i]; |
257 |
|
|
258 |
|
rayvalue(&p); /* reflected ray value */ |
259 |
|
|
260 |
|
scalecolor(p.rcol, refl); /* color contribution */ |
261 |
|
addcolor(r->rcol, p.rcol); |
262 |
|
} |
263 |
< |
|
264 |
< |
multcolor(r->rcol, mcolor); /* multiply by transmittance */ |
263 |
> |
/* rayvalue() computes absorption */ |
264 |
> |
return(1); |
265 |
|
} |
266 |
|
|
267 |
|
|
268 |
|
#ifdef DISPERSE |
269 |
|
|
270 |
|
static |
271 |
< |
disperse(m, r, vt, tr) /* check light sources for dispersion */ |
271 |
> |
disperse(m, r, vt, tr, cet, abt) /* check light sources for dispersion */ |
272 |
|
OBJREC *m; |
273 |
|
RAY *r; |
274 |
|
FVECT vt; |
275 |
|
double tr; |
276 |
+ |
COLOR cet, abt; |
277 |
|
{ |
168 |
– |
double sqrt(); |
278 |
|
RAY sray, *entray; |
279 |
|
FVECT v1, v2, n1, n2; |
280 |
|
FVECT dv, v2Xdv; |
281 |
|
double v2Xdvv2Xdv; |
282 |
< |
int sn, success = 0; |
283 |
< |
double omega; |
282 |
> |
int success = 0; |
283 |
> |
SRCINDEX si; |
284 |
|
FVECT vtmp1, vtmp2; |
285 |
|
double dtmp1, dtmp2; |
286 |
|
int l1, l2; |
342 |
|
v2Xdvv2Xdv = DOT(v2Xdv, v2Xdv); |
343 |
|
|
344 |
|
/* check sources */ |
345 |
< |
for (sn = 0; sn < nsources; sn++) { |
345 |
> |
initsrcindex(&si); |
346 |
> |
while (srcray(&sray, r, &si)) { |
347 |
|
|
348 |
< |
if ((omega = srcray(&sray, r, sn)) == 0.0 || |
239 |
< |
DOT(sray.rdir, v2) < MINCOS) |
348 |
> |
if (DOT(sray.rdir, v2) < MINCOS) |
349 |
|
continue; /* bad source */ |
241 |
– |
|
350 |
|
/* adjust source ray */ |
351 |
|
|
352 |
|
dtmp1 = DOT(v2Xdv, sray.rdir) / v2Xdvv2Xdv; |
359 |
|
if (l1 > MAXLAMBDA || l1 < MINLAMBDA) /* not visible */ |
360 |
|
continue; |
361 |
|
/* trace source ray */ |
362 |
+ |
copycolor(sray.cext, cet); |
363 |
+ |
copycolor(sray.albedo, abt); |
364 |
|
normalize(sray.rdir); |
365 |
|
rayvalue(&sray); |
366 |
< |
if (intens(sray.rcol) <= FTINY) /* missed it */ |
366 |
> |
if (bright(sray.rcol) <= FTINY) /* missed it */ |
367 |
|
continue; |
368 |
|
|
369 |
|
/* |
373 |
|
*/ |
374 |
|
|
375 |
|
fcross(vtmp1, v2Xdv, sray.rdir); |
376 |
< |
dtmp1 = sqrt(omega / v2Xdvv2Xdv / PI); |
376 |
> |
dtmp1 = sqrt(si.dom / v2Xdvv2Xdv / PI); |
377 |
|
|
378 |
|
/* compute first ray */ |
379 |
|
for (i = 0; i < 3; i++) |