1 |
greg |
2.1 |
#ifndef lint |
2 |
greg |
2.34 |
static const char RCSid[] = "$Id: m_bsdf.c,v 2.33 2017/02/19 17:22:18 greg Exp $"; |
3 |
greg |
2.1 |
#endif |
4 |
|
|
/* |
5 |
|
|
* Shading for materials with BSDFs taken from XML data files |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
#include "copyright.h" |
9 |
|
|
|
10 |
|
|
#include "ray.h" |
11 |
|
|
#include "ambient.h" |
12 |
|
|
#include "source.h" |
13 |
|
|
#include "func.h" |
14 |
|
|
#include "bsdf.h" |
15 |
|
|
#include "random.h" |
16 |
greg |
2.30 |
#include "pmapmat.h" |
17 |
greg |
2.1 |
|
18 |
|
|
/* |
19 |
|
|
* Arguments to this material include optional diffuse colors. |
20 |
|
|
* String arguments include the BSDF and function files. |
21 |
greg |
2.5 |
* A non-zero thickness causes the strange but useful behavior |
22 |
|
|
* of translating transmitted rays this distance beneath the surface |
23 |
|
|
* (opposite the surface normal) to bypass any intervening geometry. |
24 |
|
|
* Translation only affects scattered, non-source-directed samples. |
25 |
|
|
* A non-zero thickness has the further side-effect that an unscattered |
26 |
greg |
2.1 |
* (view) ray will pass right through our material if it has any |
27 |
greg |
2.5 |
* non-diffuse transmission, making the BSDF surface invisible. This |
28 |
|
|
* shows the proxied geometry instead. Thickness has the further |
29 |
|
|
* effect of turning off reflection on the hidden side so that rays |
30 |
|
|
* heading in the opposite direction pass unimpeded through the BSDF |
31 |
|
|
* surface. A paired surface may be placed on the opposide side of |
32 |
|
|
* the detail geometry, less than this thickness away, if a two-way |
33 |
|
|
* proxy is desired. Note that the sign of the thickness is important. |
34 |
|
|
* A positive thickness hides geometry behind the BSDF surface and uses |
35 |
|
|
* front reflectance and transmission properties. A negative thickness |
36 |
|
|
* hides geometry in front of the surface when rays hit from behind, |
37 |
|
|
* and applies only the transmission and backside reflectance properties. |
38 |
|
|
* Reflection is ignored on the hidden side, as those rays pass through. |
39 |
greg |
2.1 |
* The "up" vector for the BSDF is given by three variables, defined |
40 |
|
|
* (along with the thickness) by the named function file, or '.' if none. |
41 |
|
|
* Together with the surface normal, this defines the local coordinate |
42 |
|
|
* system for the BSDF. |
43 |
|
|
* We do not reorient the surface, so if the BSDF has no back-side |
44 |
greg |
2.5 |
* reflectance and none is given in the real arguments, a BSDF surface |
45 |
|
|
* with zero thickness will appear black when viewed from behind |
46 |
|
|
* unless backface visibility is off. |
47 |
|
|
* The diffuse arguments are added to components in the BSDF file, |
48 |
greg |
2.1 |
* not multiplied. However, patterns affect this material as a multiplier |
49 |
|
|
* on everything except non-diffuse reflection. |
50 |
|
|
* |
51 |
|
|
* Arguments for MAT_BSDF are: |
52 |
|
|
* 6+ thick BSDFfile ux uy uz funcfile transform |
53 |
|
|
* 0 |
54 |
greg |
2.8 |
* 0|3|6|9 rdf gdf bdf |
55 |
greg |
2.1 |
* rdb gdb bdb |
56 |
|
|
* rdt gdt bdt |
57 |
|
|
*/ |
58 |
|
|
|
59 |
greg |
2.4 |
/* |
60 |
|
|
* Note that our reverse ray-tracing process means that the positions |
61 |
|
|
* of incoming and outgoing vectors may be reversed in our calls |
62 |
|
|
* to the BSDF library. This is fine, since the bidirectional nature |
63 |
|
|
* of the BSDF (that's what the 'B' stands for) means it all works out. |
64 |
|
|
*/ |
65 |
|
|
|
66 |
greg |
2.1 |
typedef struct { |
67 |
|
|
OBJREC *mp; /* material pointer */ |
68 |
|
|
RAY *pr; /* intersected ray */ |
69 |
|
|
FVECT pnorm; /* perturbed surface normal */ |
70 |
greg |
2.4 |
FVECT vray; /* local outgoing (return) vector */ |
71 |
greg |
2.9 |
double sr_vpsa[2]; /* sqrt of BSDF projected solid angle extrema */ |
72 |
greg |
2.1 |
RREAL toloc[3][3]; /* world to local BSDF coords */ |
73 |
|
|
RREAL fromloc[3][3]; /* local BSDF coords to world */ |
74 |
|
|
double thick; /* surface thickness */ |
75 |
greg |
2.34 |
COLOR cthru; /* through component multiplier */ |
76 |
greg |
2.1 |
SDData *sd; /* loaded BSDF data */ |
77 |
greg |
2.31 |
COLOR rdiff; /* diffuse reflection */ |
78 |
|
|
COLOR tdiff; /* diffuse transmission */ |
79 |
greg |
2.1 |
} BSDFDAT; /* BSDF material data */ |
80 |
|
|
|
81 |
|
|
#define cvt_sdcolor(cv, svp) ccy2rgb(&(svp)->spec, (svp)->cieY, cv) |
82 |
|
|
|
83 |
greg |
2.34 |
/* Compute through component color */ |
84 |
|
|
static void |
85 |
|
|
compute_through(BSDFDAT *ndp) |
86 |
|
|
{ |
87 |
|
|
#define NDIR2CHECK 13 |
88 |
|
|
static const float dir2check[NDIR2CHECK][2] = { |
89 |
|
|
{0, 0}, |
90 |
|
|
{-0.8, 0}, |
91 |
|
|
{0, 0.8}, |
92 |
|
|
{0, -0.8}, |
93 |
|
|
{0.8, 0}, |
94 |
|
|
{-0.8, 0.8}, |
95 |
|
|
{-0.8, -0.8}, |
96 |
|
|
{0.8, 0.8}, |
97 |
|
|
{0.8, -0.8}, |
98 |
|
|
{-1.6, 0}, |
99 |
|
|
{0, 1.6}, |
100 |
|
|
{0, -1.6}, |
101 |
|
|
{1.6, 0}, |
102 |
|
|
}; |
103 |
|
|
const double peak_over = 2.0; |
104 |
|
|
SDSpectralDF *dfp; |
105 |
|
|
FVECT pdir; |
106 |
|
|
double tomega, srchrad; |
107 |
|
|
COLOR vpeak, vsum; |
108 |
|
|
int nsum, i; |
109 |
|
|
SDError ec; |
110 |
|
|
|
111 |
|
|
setcolor(ndp->cthru, .0, .0, .0); /* starting assumption */ |
112 |
|
|
|
113 |
|
|
if (ndp->pr->rod > 0) |
114 |
|
|
dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb; |
115 |
|
|
else |
116 |
|
|
dfp = (ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf; |
117 |
|
|
|
118 |
|
|
if (dfp == NULL) |
119 |
|
|
return; /* no specular transmission */ |
120 |
|
|
if (bright(ndp->pr->pcol) <= FTINY) |
121 |
|
|
return; /* pattern is black, here */ |
122 |
|
|
srchrad = sqrt(dfp->minProjSA); /* else search for peak */ |
123 |
|
|
setcolor(vpeak, .0, .0, .0); |
124 |
|
|
setcolor(vsum, .0, .0, .0); |
125 |
|
|
nsum = 0; |
126 |
|
|
for (i = 0; i < NDIR2CHECK; i++) { |
127 |
|
|
FVECT tdir; |
128 |
|
|
SDValue sv; |
129 |
|
|
COLOR vcol; |
130 |
|
|
tdir[0] = -ndp->vray[0] + dir2check[i][0]*srchrad; |
131 |
|
|
tdir[1] = -ndp->vray[1] + dir2check[i][1]*srchrad; |
132 |
|
|
tdir[2] = -ndp->vray[2]; |
133 |
|
|
if (normalize(tdir) == 0) |
134 |
|
|
continue; |
135 |
|
|
ec = SDevalBSDF(&sv, tdir, ndp->vray, ndp->sd); |
136 |
|
|
if (ec) |
137 |
|
|
goto baderror; |
138 |
|
|
cvt_sdcolor(vcol, &sv); |
139 |
|
|
addcolor(vsum, vcol); |
140 |
|
|
++nsum; |
141 |
|
|
if (bright(vcol) > bright(vpeak)) { |
142 |
|
|
copycolor(vpeak, vcol); |
143 |
|
|
VCOPY(pdir, tdir); |
144 |
|
|
} |
145 |
|
|
} |
146 |
|
|
ec = SDsizeBSDF(&tomega, pdir, ndp->vray, SDqueryMin, ndp->sd); |
147 |
|
|
if (ec) |
148 |
|
|
goto baderror; |
149 |
|
|
if (tomega > 1.5*dfp->minProjSA) |
150 |
|
|
return; /* not really a peak? */ |
151 |
|
|
if ((bright(vpeak) - ndp->sd->tLamb.cieY*(1./PI))*tomega <= .001) |
152 |
|
|
return; /* < 0.1% transmission */ |
153 |
|
|
for (i = 3; i--; ) /* remove peak from average */ |
154 |
|
|
colval(vsum,i) -= colval(vpeak,i); |
155 |
|
|
--nsum; |
156 |
|
|
if (peak_over*bright(vsum) >= nsum*bright(vpeak)) |
157 |
|
|
return; /* not peaky enough */ |
158 |
|
|
copycolor(ndp->cthru, vpeak); /* else use it */ |
159 |
|
|
scalecolor(ndp->cthru, tomega); |
160 |
|
|
multcolor(ndp->cthru, ndp->pr->pcol); /* modify by pattern */ |
161 |
|
|
return; |
162 |
|
|
baderror: |
163 |
|
|
objerror(ndp->mp, USER, transSDError(ec)); |
164 |
|
|
#undef NDIR2CHECK |
165 |
|
|
} |
166 |
|
|
|
167 |
greg |
2.4 |
/* Jitter ray sample according to projected solid angle and specjitter */ |
168 |
|
|
static void |
169 |
greg |
2.15 |
bsdf_jitter(FVECT vres, BSDFDAT *ndp, double sr_psa) |
170 |
greg |
2.4 |
{ |
171 |
|
|
VCOPY(vres, ndp->vray); |
172 |
|
|
if (specjitter < 1.) |
173 |
|
|
sr_psa *= specjitter; |
174 |
|
|
if (sr_psa <= FTINY) |
175 |
|
|
return; |
176 |
|
|
vres[0] += sr_psa*(.5 - frandom()); |
177 |
|
|
vres[1] += sr_psa*(.5 - frandom()); |
178 |
|
|
normalize(vres); |
179 |
|
|
} |
180 |
|
|
|
181 |
greg |
2.33 |
/* Get BSDF specular for direct component, returning true if OK to proceed */ |
182 |
greg |
2.7 |
static int |
183 |
greg |
2.33 |
direct_specular_OK(COLOR cval, FVECT ldir, double omega, BSDFDAT *ndp) |
184 |
greg |
2.7 |
{ |
185 |
greg |
2.15 |
int nsamp, ok = 0; |
186 |
greg |
2.13 |
FVECT vsrc, vsmp, vjit; |
187 |
|
|
double tomega; |
188 |
greg |
2.15 |
double sf, tsr, sd[2]; |
189 |
greg |
2.32 |
COLOR csmp, cdiff; |
190 |
|
|
double diffY; |
191 |
greg |
2.7 |
SDValue sv; |
192 |
|
|
SDError ec; |
193 |
greg |
2.13 |
int i; |
194 |
greg |
2.7 |
/* transform source direction */ |
195 |
|
|
if (SDmapDir(vsrc, ndp->toloc, ldir) != SDEnone) |
196 |
|
|
return(0); |
197 |
greg |
2.32 |
/* will discount diffuse portion */ |
198 |
|
|
switch ((vsrc[2] > 0)<<1 | (ndp->vray[2] > 0)) { |
199 |
|
|
case 3: |
200 |
|
|
if (ndp->sd->rf == NULL) |
201 |
|
|
return(0); /* all diffuse */ |
202 |
|
|
sv = ndp->sd->rLambFront; |
203 |
|
|
break; |
204 |
|
|
case 0: |
205 |
|
|
if (ndp->sd->rb == NULL) |
206 |
|
|
return(0); /* all diffuse */ |
207 |
|
|
sv = ndp->sd->rLambBack; |
208 |
|
|
break; |
209 |
|
|
default: |
210 |
|
|
if ((ndp->sd->tf == NULL) & (ndp->sd->tb == NULL)) |
211 |
|
|
return(0); /* all diffuse */ |
212 |
|
|
sv = ndp->sd->tLamb; |
213 |
|
|
break; |
214 |
|
|
} |
215 |
greg |
2.33 |
if (sv.cieY > FTINY) { |
216 |
|
|
diffY = sv.cieY *= 1./PI; |
217 |
greg |
2.32 |
cvt_sdcolor(cdiff, &sv); |
218 |
|
|
} else { |
219 |
|
|
diffY = .0; |
220 |
|
|
setcolor(cdiff, .0, .0, .0); |
221 |
|
|
} |
222 |
greg |
2.16 |
/* assign number of samples */ |
223 |
|
|
ec = SDsizeBSDF(&tomega, ndp->vray, vsrc, SDqueryMin, ndp->sd); |
224 |
|
|
if (ec) |
225 |
|
|
goto baderror; |
226 |
greg |
2.13 |
/* check indirect over-counting */ |
227 |
greg |
2.34 |
if ((ndp->thick != 0 || bright(ndp->cthru) > FTINY) |
228 |
|
|
&& ndp->pr->crtype & (SPECULAR|AMBIENT) |
229 |
greg |
2.32 |
&& (vsrc[2] > 0) ^ (ndp->vray[2] > 0)) { |
230 |
greg |
2.13 |
double dx = vsrc[0] + ndp->vray[0]; |
231 |
|
|
double dy = vsrc[1] + ndp->vray[1]; |
232 |
greg |
2.16 |
if (dx*dx + dy*dy <= omega+tomega) |
233 |
greg |
2.7 |
return(0); |
234 |
|
|
} |
235 |
greg |
2.15 |
sf = specjitter * ndp->pr->rweight; |
236 |
greg |
2.24 |
if (tomega <= .0) |
237 |
|
|
nsamp = 1; |
238 |
|
|
else if (25.*tomega <= omega) |
239 |
greg |
2.15 |
nsamp = 100.*sf + .5; |
240 |
|
|
else |
241 |
|
|
nsamp = 4.*sf*omega/tomega + .5; |
242 |
|
|
nsamp += !nsamp; |
243 |
|
|
setcolor(cval, .0, .0, .0); /* sample our source area */ |
244 |
greg |
2.13 |
sf = sqrt(omega); |
245 |
greg |
2.15 |
tsr = sqrt(tomega); |
246 |
greg |
2.13 |
for (i = nsamp; i--; ) { |
247 |
|
|
VCOPY(vsmp, vsrc); /* jitter query directions */ |
248 |
|
|
if (nsamp > 1) { |
249 |
|
|
multisamp(sd, 2, (i + frandom())/(double)nsamp); |
250 |
|
|
vsmp[0] += (sd[0] - .5)*sf; |
251 |
|
|
vsmp[1] += (sd[1] - .5)*sf; |
252 |
|
|
if (normalize(vsmp) == 0) { |
253 |
|
|
--nsamp; |
254 |
|
|
continue; |
255 |
|
|
} |
256 |
|
|
} |
257 |
greg |
2.15 |
bsdf_jitter(vjit, ndp, tsr); |
258 |
greg |
2.13 |
/* compute BSDF */ |
259 |
|
|
ec = SDevalBSDF(&sv, vjit, vsmp, ndp->sd); |
260 |
|
|
if (ec) |
261 |
|
|
goto baderror; |
262 |
greg |
2.32 |
if (sv.cieY - diffY <= FTINY) { |
263 |
|
|
addcolor(cval, cdiff); |
264 |
|
|
continue; /* no specular part */ |
265 |
|
|
} |
266 |
greg |
2.13 |
cvt_sdcolor(csmp, &sv); |
267 |
greg |
2.32 |
addcolor(cval, csmp); /* else average it in */ |
268 |
greg |
2.13 |
++ok; |
269 |
|
|
} |
270 |
greg |
2.32 |
if (!ok) { |
271 |
|
|
setcolor(cval, .0, .0, .0); |
272 |
|
|
return(0); /* no valid specular samples */ |
273 |
|
|
} |
274 |
greg |
2.13 |
sf = 1./(double)nsamp; |
275 |
|
|
scalecolor(cval, sf); |
276 |
greg |
2.32 |
/* subtract diffuse contribution */ |
277 |
|
|
for (i = 3*(diffY > FTINY); i--; ) |
278 |
|
|
if ((colval(cval,i) -= colval(cdiff,i)) < .0) |
279 |
|
|
colval(cval,i) = .0; |
280 |
|
|
return(1); |
281 |
greg |
2.13 |
baderror: |
282 |
|
|
objerror(ndp->mp, USER, transSDError(ec)); |
283 |
greg |
2.17 |
return(0); /* gratis return */ |
284 |
greg |
2.7 |
} |
285 |
|
|
|
286 |
greg |
2.5 |
/* Compute source contribution for BSDF (reflected & transmitted) */ |
287 |
greg |
2.1 |
static void |
288 |
greg |
2.5 |
dir_bsdf( |
289 |
greg |
2.1 |
COLOR cval, /* returned coefficient */ |
290 |
|
|
void *nnp, /* material data */ |
291 |
|
|
FVECT ldir, /* light source direction */ |
292 |
|
|
double omega /* light source size */ |
293 |
|
|
) |
294 |
|
|
{ |
295 |
greg |
2.3 |
BSDFDAT *np = (BSDFDAT *)nnp; |
296 |
greg |
2.1 |
double ldot; |
297 |
|
|
double dtmp; |
298 |
|
|
COLOR ctmp; |
299 |
|
|
|
300 |
|
|
setcolor(cval, .0, .0, .0); |
301 |
|
|
|
302 |
|
|
ldot = DOT(np->pnorm, ldir); |
303 |
|
|
if ((-FTINY <= ldot) & (ldot <= FTINY)) |
304 |
|
|
return; |
305 |
|
|
|
306 |
greg |
2.9 |
if (ldot > 0 && bright(np->rdiff) > FTINY) { |
307 |
greg |
2.1 |
/* |
308 |
|
|
* Compute added diffuse reflected component. |
309 |
|
|
*/ |
310 |
|
|
copycolor(ctmp, np->rdiff); |
311 |
|
|
dtmp = ldot * omega * (1./PI); |
312 |
|
|
scalecolor(ctmp, dtmp); |
313 |
|
|
addcolor(cval, ctmp); |
314 |
|
|
} |
315 |
greg |
2.9 |
if (ldot < 0 && bright(np->tdiff) > FTINY) { |
316 |
greg |
2.1 |
/* |
317 |
|
|
* Compute added diffuse transmission. |
318 |
|
|
*/ |
319 |
|
|
copycolor(ctmp, np->tdiff); |
320 |
|
|
dtmp = -ldot * omega * (1.0/PI); |
321 |
|
|
scalecolor(ctmp, dtmp); |
322 |
|
|
addcolor(cval, ctmp); |
323 |
|
|
} |
324 |
greg |
2.30 |
if (ambRayInPmap(np->pr)) |
325 |
|
|
return; /* specular already in photon map */ |
326 |
greg |
2.1 |
/* |
327 |
greg |
2.33 |
* Compute specular scattering coefficient using BSDF. |
328 |
greg |
2.1 |
*/ |
329 |
greg |
2.33 |
if (!direct_specular_OK(ctmp, ldir, omega, np)) |
330 |
greg |
2.1 |
return; |
331 |
greg |
2.31 |
if (ldot < 0) { /* pattern for specular transmission */ |
332 |
greg |
2.1 |
multcolor(ctmp, np->pr->pcol); |
333 |
|
|
dtmp = -ldot * omega; |
334 |
greg |
2.31 |
} else |
335 |
|
|
dtmp = ldot * omega; |
336 |
greg |
2.1 |
scalecolor(ctmp, dtmp); |
337 |
|
|
addcolor(cval, ctmp); |
338 |
|
|
} |
339 |
|
|
|
340 |
greg |
2.5 |
/* Compute source contribution for BSDF (reflected only) */ |
341 |
|
|
static void |
342 |
|
|
dir_brdf( |
343 |
|
|
COLOR cval, /* returned coefficient */ |
344 |
|
|
void *nnp, /* material data */ |
345 |
|
|
FVECT ldir, /* light source direction */ |
346 |
|
|
double omega /* light source size */ |
347 |
|
|
) |
348 |
|
|
{ |
349 |
|
|
BSDFDAT *np = (BSDFDAT *)nnp; |
350 |
|
|
double ldot; |
351 |
|
|
double dtmp; |
352 |
|
|
COLOR ctmp, ctmp1, ctmp2; |
353 |
|
|
|
354 |
|
|
setcolor(cval, .0, .0, .0); |
355 |
|
|
|
356 |
|
|
ldot = DOT(np->pnorm, ldir); |
357 |
|
|
|
358 |
|
|
if (ldot <= FTINY) |
359 |
|
|
return; |
360 |
|
|
|
361 |
|
|
if (bright(np->rdiff) > FTINY) { |
362 |
|
|
/* |
363 |
|
|
* Compute added diffuse reflected component. |
364 |
|
|
*/ |
365 |
|
|
copycolor(ctmp, np->rdiff); |
366 |
|
|
dtmp = ldot * omega * (1./PI); |
367 |
|
|
scalecolor(ctmp, dtmp); |
368 |
|
|
addcolor(cval, ctmp); |
369 |
|
|
} |
370 |
greg |
2.30 |
if (ambRayInPmap(np->pr)) |
371 |
|
|
return; /* specular already in photon map */ |
372 |
greg |
2.5 |
/* |
373 |
greg |
2.33 |
* Compute specular reflection coefficient using BSDF. |
374 |
greg |
2.5 |
*/ |
375 |
greg |
2.33 |
if (!direct_specular_OK(ctmp, ldir, omega, np)) |
376 |
greg |
2.5 |
return; |
377 |
|
|
dtmp = ldot * omega; |
378 |
|
|
scalecolor(ctmp, dtmp); |
379 |
|
|
addcolor(cval, ctmp); |
380 |
|
|
} |
381 |
|
|
|
382 |
|
|
/* Compute source contribution for BSDF (transmitted only) */ |
383 |
|
|
static void |
384 |
|
|
dir_btdf( |
385 |
|
|
COLOR cval, /* returned coefficient */ |
386 |
|
|
void *nnp, /* material data */ |
387 |
|
|
FVECT ldir, /* light source direction */ |
388 |
|
|
double omega /* light source size */ |
389 |
|
|
) |
390 |
|
|
{ |
391 |
|
|
BSDFDAT *np = (BSDFDAT *)nnp; |
392 |
|
|
double ldot; |
393 |
|
|
double dtmp; |
394 |
|
|
COLOR ctmp; |
395 |
|
|
|
396 |
|
|
setcolor(cval, .0, .0, .0); |
397 |
|
|
|
398 |
|
|
ldot = DOT(np->pnorm, ldir); |
399 |
|
|
|
400 |
|
|
if (ldot >= -FTINY) |
401 |
|
|
return; |
402 |
|
|
|
403 |
|
|
if (bright(np->tdiff) > FTINY) { |
404 |
|
|
/* |
405 |
|
|
* Compute added diffuse transmission. |
406 |
|
|
*/ |
407 |
|
|
copycolor(ctmp, np->tdiff); |
408 |
|
|
dtmp = -ldot * omega * (1.0/PI); |
409 |
|
|
scalecolor(ctmp, dtmp); |
410 |
|
|
addcolor(cval, ctmp); |
411 |
|
|
} |
412 |
greg |
2.30 |
if (ambRayInPmap(np->pr)) |
413 |
|
|
return; /* specular already in photon map */ |
414 |
greg |
2.5 |
/* |
415 |
greg |
2.33 |
* Compute specular scattering coefficient using BSDF. |
416 |
greg |
2.5 |
*/ |
417 |
greg |
2.33 |
if (!direct_specular_OK(ctmp, ldir, omega, np)) |
418 |
greg |
2.5 |
return; |
419 |
|
|
/* full pattern on transmission */ |
420 |
|
|
multcolor(ctmp, np->pr->pcol); |
421 |
|
|
dtmp = -ldot * omega; |
422 |
|
|
scalecolor(ctmp, dtmp); |
423 |
|
|
addcolor(cval, ctmp); |
424 |
|
|
} |
425 |
|
|
|
426 |
greg |
2.1 |
/* Sample separate BSDF component */ |
427 |
|
|
static int |
428 |
|
|
sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usepat) |
429 |
|
|
{ |
430 |
|
|
int nstarget = 1; |
431 |
greg |
2.11 |
int nsent; |
432 |
greg |
2.1 |
SDError ec; |
433 |
|
|
SDValue bsv; |
434 |
greg |
2.11 |
double xrand; |
435 |
greg |
2.10 |
FVECT vsmp; |
436 |
greg |
2.1 |
RAY sr; |
437 |
|
|
/* multiple samples? */ |
438 |
|
|
if (specjitter > 1.5) { |
439 |
|
|
nstarget = specjitter*ndp->pr->rweight + .5; |
440 |
greg |
2.14 |
nstarget += !nstarget; |
441 |
greg |
2.1 |
} |
442 |
greg |
2.11 |
/* run through our samples */ |
443 |
|
|
for (nsent = 0; nsent < nstarget; nsent++) { |
444 |
greg |
2.15 |
if (nstarget == 1) { /* stratify random variable */ |
445 |
greg |
2.11 |
xrand = urand(ilhash(dimlist,ndims)+samplendx); |
446 |
greg |
2.15 |
if (specjitter < 1.) |
447 |
|
|
xrand = .5 + specjitter*(xrand-.5); |
448 |
|
|
} else { |
449 |
greg |
2.11 |
xrand = (nsent + frandom())/(double)nstarget; |
450 |
greg |
2.15 |
} |
451 |
greg |
2.11 |
SDerrorDetail[0] = '\0'; /* sample direction & coef. */ |
452 |
greg |
2.15 |
bsdf_jitter(vsmp, ndp, ndp->sr_vpsa[0]); |
453 |
greg |
2.11 |
ec = SDsampComponent(&bsv, vsmp, xrand, dcp); |
454 |
greg |
2.1 |
if (ec) |
455 |
greg |
2.2 |
objerror(ndp->mp, USER, transSDError(ec)); |
456 |
greg |
2.11 |
if (bsv.cieY <= FTINY) /* zero component? */ |
457 |
greg |
2.1 |
break; |
458 |
|
|
/* map vector to world */ |
459 |
greg |
2.4 |
if (SDmapDir(sr.rdir, ndp->fromloc, vsmp) != SDEnone) |
460 |
greg |
2.1 |
break; |
461 |
|
|
/* spawn a specular ray */ |
462 |
|
|
if (nstarget > 1) |
463 |
|
|
bsv.cieY /= (double)nstarget; |
464 |
greg |
2.11 |
cvt_sdcolor(sr.rcoef, &bsv); /* use sample color */ |
465 |
|
|
if (usepat) /* apply pattern? */ |
466 |
greg |
2.1 |
multcolor(sr.rcoef, ndp->pr->pcol); |
467 |
|
|
if (rayorigin(&sr, SPECULAR, ndp->pr, sr.rcoef) < 0) { |
468 |
greg |
2.11 |
if (maxdepth > 0) |
469 |
greg |
2.1 |
break; |
470 |
greg |
2.11 |
continue; /* Russian roulette victim */ |
471 |
greg |
2.1 |
} |
472 |
greg |
2.5 |
/* need to offset origin? */ |
473 |
greg |
2.32 |
if (ndp->thick != 0 && (ndp->pr->rod > 0) ^ (vsmp[2] > 0)) |
474 |
greg |
2.5 |
VSUM(sr.rorg, sr.rorg, ndp->pr->ron, -ndp->thick); |
475 |
greg |
2.1 |
rayvalue(&sr); /* send & evaluate sample */ |
476 |
|
|
multcolor(sr.rcol, sr.rcoef); |
477 |
|
|
addcolor(ndp->pr->rcol, sr.rcol); |
478 |
|
|
} |
479 |
|
|
return(nsent); |
480 |
|
|
} |
481 |
|
|
|
482 |
|
|
/* Sample non-diffuse components of BSDF */ |
483 |
|
|
static int |
484 |
|
|
sample_sdf(BSDFDAT *ndp, int sflags) |
485 |
|
|
{ |
486 |
|
|
int n, ntotal = 0; |
487 |
|
|
SDSpectralDF *dfp; |
488 |
|
|
COLORV *unsc; |
489 |
|
|
|
490 |
|
|
if (sflags == SDsampSpT) { |
491 |
greg |
2.31 |
unsc = ndp->tdiff; |
492 |
greg |
2.22 |
if (ndp->pr->rod > 0) |
493 |
|
|
dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb; |
494 |
|
|
else |
495 |
|
|
dfp = (ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf; |
496 |
greg |
2.1 |
} else /* sflags == SDsampSpR */ { |
497 |
greg |
2.31 |
unsc = ndp->rdiff; |
498 |
|
|
if (ndp->pr->rod > 0) |
499 |
greg |
2.1 |
dfp = ndp->sd->rf; |
500 |
greg |
2.31 |
else |
501 |
greg |
2.1 |
dfp = ndp->sd->rb; |
502 |
|
|
} |
503 |
|
|
if (dfp == NULL) /* no specular component? */ |
504 |
|
|
return(0); |
505 |
|
|
/* below sampling threshold? */ |
506 |
|
|
if (dfp->maxHemi <= specthresh+FTINY) { |
507 |
greg |
2.3 |
if (dfp->maxHemi > FTINY) { /* XXX no color from BSDF */ |
508 |
greg |
2.4 |
FVECT vjit; |
509 |
|
|
double d; |
510 |
greg |
2.1 |
COLOR ctmp; |
511 |
greg |
2.15 |
bsdf_jitter(vjit, ndp, ndp->sr_vpsa[1]); |
512 |
greg |
2.4 |
d = SDdirectHemi(vjit, sflags, ndp->sd); |
513 |
greg |
2.1 |
if (sflags == SDsampSpT) { |
514 |
|
|
copycolor(ctmp, ndp->pr->pcol); |
515 |
|
|
scalecolor(ctmp, d); |
516 |
|
|
} else /* no pattern on reflection */ |
517 |
|
|
setcolor(ctmp, d, d, d); |
518 |
|
|
addcolor(unsc, ctmp); |
519 |
|
|
} |
520 |
|
|
return(0); |
521 |
|
|
} |
522 |
|
|
/* else need to sample */ |
523 |
|
|
dimlist[ndims++] = (int)(size_t)ndp->mp; |
524 |
|
|
ndims++; |
525 |
|
|
for (n = dfp->ncomp; n--; ) { /* loop over components */ |
526 |
|
|
dimlist[ndims-1] = n + 9438; |
527 |
|
|
ntotal += sample_sdcomp(ndp, &dfp->comp[n], sflags==SDsampSpT); |
528 |
|
|
} |
529 |
|
|
ndims -= 2; |
530 |
|
|
return(ntotal); |
531 |
|
|
} |
532 |
|
|
|
533 |
|
|
/* Color a ray that hit a BSDF material */ |
534 |
|
|
int |
535 |
|
|
m_bsdf(OBJREC *m, RAY *r) |
536 |
|
|
{ |
537 |
greg |
2.6 |
int hitfront; |
538 |
greg |
2.1 |
COLOR ctmp; |
539 |
|
|
SDError ec; |
540 |
greg |
2.5 |
FVECT upvec, vtmp; |
541 |
greg |
2.1 |
MFUNC *mf; |
542 |
|
|
BSDFDAT nd; |
543 |
|
|
/* check arguments */ |
544 |
|
|
if ((m->oargs.nsargs < 6) | (m->oargs.nfargs > 9) | |
545 |
|
|
(m->oargs.nfargs % 3)) |
546 |
|
|
objerror(m, USER, "bad # arguments"); |
547 |
greg |
2.6 |
/* record surface struck */ |
548 |
greg |
2.9 |
hitfront = (r->rod > 0); |
549 |
greg |
2.1 |
/* load cal file */ |
550 |
|
|
mf = getfunc(m, 5, 0x1d, 1); |
551 |
greg |
2.25 |
setfunc(m, r); |
552 |
greg |
2.1 |
/* get thickness */ |
553 |
|
|
nd.thick = evalue(mf->ep[0]); |
554 |
greg |
2.5 |
if ((-FTINY <= nd.thick) & (nd.thick <= FTINY)) |
555 |
greg |
2.1 |
nd.thick = .0; |
556 |
greg |
2.26 |
/* check backface visibility */ |
557 |
|
|
if (!hitfront & !backvis) { |
558 |
|
|
raytrans(r); |
559 |
|
|
return(1); |
560 |
|
|
} |
561 |
greg |
2.5 |
/* check other rays to pass */ |
562 |
greg |
2.34 |
if (nd.thick != 0 && (r->crtype & SHADOW || |
563 |
|
|
!(r->crtype & (SPECULAR|AMBIENT)) || |
564 |
greg |
2.29 |
(nd.thick > 0) ^ hitfront)) { |
565 |
greg |
2.5 |
raytrans(r); /* hide our proxy */ |
566 |
greg |
2.1 |
return(1); |
567 |
|
|
} |
568 |
greg |
2.31 |
nd.mp = m; |
569 |
|
|
nd.pr = r; |
570 |
greg |
2.5 |
/* get BSDF data */ |
571 |
|
|
nd.sd = loadBSDF(m->oargs.sarg[1]); |
572 |
greg |
2.34 |
/* early shadow check */ |
573 |
|
|
if (r->crtype & SHADOW && (nd.sd->tf == NULL) & (nd.sd->tb == NULL)) |
574 |
|
|
return(1); |
575 |
greg |
2.1 |
/* diffuse reflectance */ |
576 |
greg |
2.6 |
if (hitfront) { |
577 |
greg |
2.31 |
cvt_sdcolor(nd.rdiff, &nd.sd->rLambFront); |
578 |
|
|
if (m->oargs.nfargs >= 3) { |
579 |
|
|
setcolor(ctmp, m->oargs.farg[0], |
580 |
greg |
2.1 |
m->oargs.farg[1], |
581 |
|
|
m->oargs.farg[2]); |
582 |
greg |
2.31 |
addcolor(nd.rdiff, ctmp); |
583 |
|
|
} |
584 |
greg |
2.1 |
} else { |
585 |
greg |
2.31 |
cvt_sdcolor(nd.rdiff, &nd.sd->rLambBack); |
586 |
|
|
if (m->oargs.nfargs >= 6) { |
587 |
|
|
setcolor(ctmp, m->oargs.farg[3], |
588 |
greg |
2.1 |
m->oargs.farg[4], |
589 |
|
|
m->oargs.farg[5]); |
590 |
greg |
2.31 |
addcolor(nd.rdiff, ctmp); |
591 |
|
|
} |
592 |
greg |
2.1 |
} |
593 |
|
|
/* diffuse transmittance */ |
594 |
greg |
2.31 |
cvt_sdcolor(nd.tdiff, &nd.sd->tLamb); |
595 |
|
|
if (m->oargs.nfargs >= 9) { |
596 |
|
|
setcolor(ctmp, m->oargs.farg[6], |
597 |
greg |
2.1 |
m->oargs.farg[7], |
598 |
|
|
m->oargs.farg[8]); |
599 |
greg |
2.31 |
addcolor(nd.tdiff, ctmp); |
600 |
|
|
} |
601 |
greg |
2.1 |
/* get modifiers */ |
602 |
|
|
raytexture(r, m->omod); |
603 |
|
|
/* modify diffuse values */ |
604 |
|
|
multcolor(nd.rdiff, r->pcol); |
605 |
|
|
multcolor(nd.tdiff, r->pcol); |
606 |
|
|
/* get up vector */ |
607 |
|
|
upvec[0] = evalue(mf->ep[1]); |
608 |
|
|
upvec[1] = evalue(mf->ep[2]); |
609 |
|
|
upvec[2] = evalue(mf->ep[3]); |
610 |
|
|
/* return to world coords */ |
611 |
greg |
2.21 |
if (mf->fxp != &unitxf) { |
612 |
|
|
multv3(upvec, upvec, mf->fxp->xfm); |
613 |
|
|
nd.thick *= mf->fxp->sca; |
614 |
greg |
2.1 |
} |
615 |
greg |
2.23 |
if (r->rox != NULL) { |
616 |
|
|
multv3(upvec, upvec, r->rox->f.xfm); |
617 |
|
|
nd.thick *= r->rox->f.sca; |
618 |
|
|
} |
619 |
greg |
2.1 |
raynormal(nd.pnorm, r); |
620 |
|
|
/* compute local BSDF xform */ |
621 |
|
|
ec = SDcompXform(nd.toloc, nd.pnorm, upvec); |
622 |
|
|
if (!ec) { |
623 |
greg |
2.4 |
nd.vray[0] = -r->rdir[0]; |
624 |
|
|
nd.vray[1] = -r->rdir[1]; |
625 |
|
|
nd.vray[2] = -r->rdir[2]; |
626 |
|
|
ec = SDmapDir(nd.vray, nd.toloc, nd.vray); |
627 |
greg |
2.20 |
} |
628 |
greg |
2.19 |
if (ec) { |
629 |
|
|
objerror(m, WARNING, "Illegal orientation vector"); |
630 |
|
|
return(1); |
631 |
greg |
2.1 |
} |
632 |
greg |
2.34 |
compute_through(&nd); /* compute through component */ |
633 |
|
|
if (r->crtype & SHADOW) { |
634 |
|
|
RAY tr; /* attempt to pass shadow ray */ |
635 |
|
|
if (rayorigin(&tr, TRANS, r, nd.cthru) < 0) |
636 |
|
|
return(1); /* blocked */ |
637 |
|
|
VCOPY(tr.rdir, r->rdir); |
638 |
|
|
rayvalue(&tr); /* transmit with scaling */ |
639 |
|
|
multcolor(tr.rcol, tr.rcoef); |
640 |
|
|
copycolor(r->rcol, tr.rcol); |
641 |
|
|
return(1); /* we're done */ |
642 |
|
|
} |
643 |
|
|
ec = SDinvXform(nd.fromloc, nd.toloc); |
644 |
|
|
if (!ec) /* determine BSDF resolution */ |
645 |
|
|
ec = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL, |
646 |
|
|
SDqueryMin+SDqueryMax, nd.sd); |
647 |
greg |
2.20 |
if (ec) |
648 |
|
|
objerror(m, USER, transSDError(ec)); |
649 |
|
|
|
650 |
greg |
2.9 |
nd.sr_vpsa[0] = sqrt(nd.sr_vpsa[0]); |
651 |
|
|
nd.sr_vpsa[1] = sqrt(nd.sr_vpsa[1]); |
652 |
greg |
2.6 |
if (!hitfront) { /* perturb normal towards hit */ |
653 |
greg |
2.1 |
nd.pnorm[0] = -nd.pnorm[0]; |
654 |
|
|
nd.pnorm[1] = -nd.pnorm[1]; |
655 |
|
|
nd.pnorm[2] = -nd.pnorm[2]; |
656 |
|
|
} |
657 |
|
|
/* sample reflection */ |
658 |
|
|
sample_sdf(&nd, SDsampSpR); |
659 |
|
|
/* sample transmission */ |
660 |
|
|
sample_sdf(&nd, SDsampSpT); |
661 |
|
|
/* compute indirect diffuse */ |
662 |
greg |
2.31 |
if (bright(nd.rdiff) > FTINY) { /* ambient from reflection */ |
663 |
greg |
2.6 |
if (!hitfront) |
664 |
greg |
2.1 |
flipsurface(r); |
665 |
greg |
2.31 |
copycolor(ctmp, nd.rdiff); |
666 |
greg |
2.1 |
multambient(ctmp, r, nd.pnorm); |
667 |
|
|
addcolor(r->rcol, ctmp); |
668 |
greg |
2.6 |
if (!hitfront) |
669 |
greg |
2.1 |
flipsurface(r); |
670 |
|
|
} |
671 |
greg |
2.31 |
if (bright(nd.tdiff) > FTINY) { /* ambient from other side */ |
672 |
greg |
2.1 |
FVECT bnorm; |
673 |
greg |
2.6 |
if (hitfront) |
674 |
greg |
2.1 |
flipsurface(r); |
675 |
|
|
bnorm[0] = -nd.pnorm[0]; |
676 |
|
|
bnorm[1] = -nd.pnorm[1]; |
677 |
|
|
bnorm[2] = -nd.pnorm[2]; |
678 |
greg |
2.31 |
copycolor(ctmp, nd.tdiff); |
679 |
greg |
2.9 |
if (nd.thick != 0) { /* proxy with offset? */ |
680 |
greg |
2.5 |
VCOPY(vtmp, r->rop); |
681 |
greg |
2.18 |
VSUM(r->rop, vtmp, r->ron, nd.thick); |
682 |
greg |
2.5 |
multambient(ctmp, r, bnorm); |
683 |
|
|
VCOPY(r->rop, vtmp); |
684 |
|
|
} else |
685 |
|
|
multambient(ctmp, r, bnorm); |
686 |
greg |
2.1 |
addcolor(r->rcol, ctmp); |
687 |
greg |
2.6 |
if (hitfront) |
688 |
greg |
2.1 |
flipsurface(r); |
689 |
|
|
} |
690 |
|
|
/* add direct component */ |
691 |
greg |
2.22 |
if ((bright(nd.tdiff) <= FTINY) & (nd.sd->tf == NULL) & |
692 |
|
|
(nd.sd->tb == NULL)) { |
693 |
greg |
2.5 |
direct(r, dir_brdf, &nd); /* reflection only */ |
694 |
greg |
2.9 |
} else if (nd.thick == 0) { |
695 |
greg |
2.5 |
direct(r, dir_bsdf, &nd); /* thin surface scattering */ |
696 |
|
|
} else { |
697 |
|
|
direct(r, dir_brdf, &nd); /* reflection first */ |
698 |
|
|
VCOPY(vtmp, r->rop); /* offset for transmitted */ |
699 |
|
|
VSUM(r->rop, vtmp, r->ron, -nd.thick); |
700 |
greg |
2.6 |
direct(r, dir_btdf, &nd); /* separate transmission */ |
701 |
greg |
2.5 |
VCOPY(r->rop, vtmp); |
702 |
|
|
} |
703 |
greg |
2.1 |
/* clean up */ |
704 |
|
|
SDfreeCache(nd.sd); |
705 |
|
|
return(1); |
706 |
|
|
} |