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