16 |
|
|
17 |
|
#ifdef DISPERSE |
18 |
|
#include "source.h" |
19 |
+ |
static disperse(); |
20 |
+ |
static int lambda(); |
21 |
|
#endif |
22 |
|
|
23 |
|
/* |
50 |
|
|
51 |
|
#define MINCOS 0.997 /* minimum dot product for dispersion */ |
52 |
|
|
53 |
+ |
extern COLOR cextinction; /* global coefficient of extinction */ |
54 |
+ |
extern COLOR salbedo; /* global scattering albedo */ |
55 |
|
|
56 |
< |
m_dielectric(m, r) /* color a ray which hit something transparent */ |
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 |
|
{ |
56 |
– |
double sqrt(), pow(); |
73 |
|
double cos1, cos2, nratio; |
74 |
< |
COLOR mcolor; |
74 |
> |
COLOR ctrans; |
75 |
> |
COLOR talb; |
76 |
|
double mabsorp; |
77 |
< |
double refl, trans, transbright; |
77 |
> |
double refl, trans; |
78 |
|
FVECT dnorm; |
79 |
|
double d1, d2; |
80 |
|
RAY p; |
97 |
|
dnorm[0] = -dnorm[0]; |
98 |
|
dnorm[1] = -dnorm[1]; |
99 |
|
dnorm[2] = -dnorm[2]; |
100 |
< |
setcolor(mcolor, pow(m->oargs.farg[0], r->rot), |
101 |
< |
pow(m->oargs.farg[1], r->rot), |
102 |
< |
pow(m->oargs.farg[2], r->rot)); |
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 |
> |
setcolor(r->albedo, 0., 0., 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 |
> |
setcolor(talb, 0., 0., 0.); |
111 |
> |
} else { |
112 |
> |
copycolor(ctrans, cextinction); |
113 |
> |
copycolor(talb, salbedo); |
114 |
> |
} |
115 |
|
} else { /* outside */ |
116 |
|
nratio = 1.0 / nratio; |
117 |
< |
if (m->otype == MAT_INTERFACE) |
118 |
< |
setcolor(mcolor, pow(m->oargs.farg[4], r->rot), |
119 |
< |
pow(m->oargs.farg[5], r->rot), |
120 |
< |
pow(m->oargs.farg[6], r->rot)); |
121 |
< |
else |
122 |
< |
setcolor(mcolor, 1.0, 1.0, 1.0); |
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 |
> |
setcolor(talb, 0., 0., 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 |
> |
setcolor(r->albedo, 0., 0., 0.); |
128 |
> |
r->gecc = 0.; |
129 |
> |
} |
130 |
|
} |
131 |
< |
mabsorp = bright(mcolor); |
131 |
> |
/* estimate absorption */ |
132 |
> |
mabsorp = colval(r->cext,RED) < colval(r->cext,GRN) ? |
133 |
> |
colval(r->cext,RED) : colval(r->cext,GRN); |
134 |
> |
if (colval(r->cext,BLU) < mabsorp) mabsorp = colval(r->cext,BLU); |
135 |
> |
if (mabsorp > 0.) |
136 |
> |
mabsorp = exp(-mabsorp*r->rot); /* conservative */ |
137 |
> |
else |
138 |
> |
mabsorp = 1.0; |
139 |
|
|
140 |
|
d2 = 1.0 - nratio*nratio*(1.0 - cos1*cos1); /* compute cos theta2 */ |
141 |
|
|
156 |
|
d1 = (d1 - d2) / (d1 + d2); |
157 |
|
refl += d1 * d1; |
158 |
|
|
159 |
< |
refl /= 2.0; |
159 |
> |
refl *= 0.5; |
160 |
|
trans = 1.0 - refl; |
118 |
– |
transbright = -FTINY; |
161 |
|
|
162 |
|
if (rayorigin(&p, r, REFRACTED, mabsorp*trans) == 0) { |
163 |
|
|
170 |
|
if (m->otype != MAT_DIELECTRIC |
171 |
|
|| r->rod > 0.0 |
172 |
|
|| r->crtype & SHADOW |
173 |
+ |
|| !directvis |
174 |
|
|| m->oargs.farg[4] == 0.0 |
175 |
< |
|| !disperse(m, r, p.rdir, trans)) |
175 |
> |
|| !disperse(m, r, p.rdir, |
176 |
> |
trans, ctrans, talb)) |
177 |
|
#endif |
178 |
|
{ |
179 |
+ |
copycolor(p.cext, ctrans); |
180 |
+ |
copycolor(p.albedo, talb); |
181 |
|
rayvalue(&p); |
136 |
– |
multcolor(mcolor, r->pcol); /* modify */ |
182 |
|
scalecolor(p.rcol, trans); |
183 |
|
addcolor(r->rcol, p.rcol); |
184 |
< |
transbright = bright(p.rcol); |
185 |
< |
r->rt = r->rot + p.rt; |
184 |
> |
if (nratio >= 1.0-FTINY && nratio <= 1.0+FTINY) |
185 |
> |
r->rt = r->rot + p.rt; |
186 |
|
} |
187 |
|
} |
188 |
|
} |
198 |
|
|
199 |
|
scalecolor(p.rcol, refl); /* color contribution */ |
200 |
|
addcolor(r->rcol, p.rcol); |
156 |
– |
if (bright(p.rcol) > transbright) |
157 |
– |
r->rt = r->rot + p.rt; |
201 |
|
} |
202 |
< |
|
203 |
< |
multcolor(r->rcol, mcolor); /* multiply by transmittance */ |
202 |
> |
/* rayvalue() computes absorption */ |
203 |
> |
return(1); |
204 |
|
} |
205 |
|
|
206 |
|
|
207 |
|
#ifdef DISPERSE |
208 |
|
|
209 |
|
static |
210 |
< |
disperse(m, r, vt, tr) /* check light sources for dispersion */ |
210 |
> |
disperse(m, r, vt, tr, cet, abt) /* check light sources for dispersion */ |
211 |
|
OBJREC *m; |
212 |
|
RAY *r; |
213 |
|
FVECT vt; |
214 |
|
double tr; |
215 |
+ |
COLOR cet, abt; |
216 |
|
{ |
173 |
– |
double sqrt(); |
217 |
|
RAY sray, *entray; |
218 |
|
FVECT v1, v2, n1, n2; |
219 |
|
FVECT dv, v2Xdv; |
220 |
|
double v2Xdvv2Xdv; |
221 |
< |
int sn, success = 0; |
222 |
< |
double omega; |
221 |
> |
int success = 0; |
222 |
> |
SRCINDEX si; |
223 |
|
FVECT vtmp1, vtmp2; |
224 |
|
double dtmp1, dtmp2; |
225 |
|
int l1, l2; |
281 |
|
v2Xdvv2Xdv = DOT(v2Xdv, v2Xdv); |
282 |
|
|
283 |
|
/* check sources */ |
284 |
< |
for (sn = 0; sn < nsources; sn++) { |
284 |
> |
initsrcindex(&si); |
285 |
> |
while (srcray(&sray, r, &si)) { |
286 |
|
|
287 |
< |
if ((omega = srcray(&sray, r, sn)) == 0.0 || |
244 |
< |
DOT(sray.rdir, v2) < MINCOS) |
287 |
> |
if (DOT(sray.rdir, v2) < MINCOS) |
288 |
|
continue; /* bad source */ |
246 |
– |
|
289 |
|
/* adjust source ray */ |
290 |
|
|
291 |
|
dtmp1 = DOT(v2Xdv, sray.rdir) / v2Xdvv2Xdv; |
298 |
|
if (l1 > MAXLAMBDA || l1 < MINLAMBDA) /* not visible */ |
299 |
|
continue; |
300 |
|
/* trace source ray */ |
301 |
+ |
copycolor(sray.cext, cet); |
302 |
+ |
copycolor(sray.albedo, abt); |
303 |
|
normalize(sray.rdir); |
304 |
|
rayvalue(&sray); |
305 |
|
if (bright(sray.rcol) <= FTINY) /* missed it */ |
312 |
|
*/ |
313 |
|
|
314 |
|
fcross(vtmp1, v2Xdv, sray.rdir); |
315 |
< |
dtmp1 = sqrt(omega / v2Xdvv2Xdv / PI); |
315 |
> |
dtmp1 = sqrt(si.dom / v2Xdvv2Xdv / PI); |
316 |
|
|
317 |
|
/* compute first ray */ |
318 |
|
for (i = 0; i < 3; i++) |