1 |
greg |
2.1 |
#ifndef lint |
2 |
greg |
2.10 |
static const char RCSid[] = "$Id: m_bsdf.c,v 2.9 2011/04/19 21:31:22 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 |
|
|
|
17 |
|
|
/* |
18 |
|
|
* Arguments to this material include optional diffuse colors. |
19 |
|
|
* String arguments include the BSDF and function files. |
20 |
greg |
2.5 |
* A non-zero thickness causes the strange but useful behavior |
21 |
|
|
* of translating transmitted rays this distance beneath the surface |
22 |
|
|
* (opposite the surface normal) to bypass any intervening geometry. |
23 |
|
|
* Translation only affects scattered, non-source-directed samples. |
24 |
|
|
* A non-zero thickness has the further side-effect that an unscattered |
25 |
greg |
2.1 |
* (view) ray will pass right through our material if it has any |
26 |
greg |
2.5 |
* non-diffuse transmission, making the BSDF surface invisible. This |
27 |
|
|
* shows the proxied geometry instead. Thickness has the further |
28 |
|
|
* effect of turning off reflection on the hidden side so that rays |
29 |
|
|
* heading in the opposite direction pass unimpeded through the BSDF |
30 |
|
|
* 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.1 |
* The "up" vector for the BSDF is given by three variables, defined |
39 |
|
|
* (along with the thickness) by the named function file, or '.' if none. |
40 |
|
|
* Together with the surface normal, this defines the local coordinate |
41 |
|
|
* system for the BSDF. |
42 |
|
|
* We do not reorient the surface, so if the BSDF has no back-side |
43 |
greg |
2.5 |
* reflectance and none is given in the real arguments, a BSDF surface |
44 |
|
|
* with zero thickness will appear black when viewed from behind |
45 |
|
|
* unless backface visibility is off. |
46 |
|
|
* The diffuse arguments are added to components in the BSDF file, |
47 |
greg |
2.1 |
* not multiplied. However, patterns affect this material as a multiplier |
48 |
|
|
* on everything except non-diffuse reflection. |
49 |
|
|
* |
50 |
|
|
* Arguments for MAT_BSDF are: |
51 |
|
|
* 6+ thick BSDFfile ux uy uz funcfile transform |
52 |
|
|
* 0 |
53 |
greg |
2.8 |
* 0|3|6|9 rdf gdf bdf |
54 |
greg |
2.1 |
* rdb gdb bdb |
55 |
|
|
* rdt gdt bdt |
56 |
|
|
*/ |
57 |
|
|
|
58 |
greg |
2.4 |
/* |
59 |
|
|
* Note that our reverse ray-tracing process means that the positions |
60 |
|
|
* of incoming and outgoing vectors may be reversed in our calls |
61 |
|
|
* to the BSDF library. This is fine, since the bidirectional nature |
62 |
|
|
* of the BSDF (that's what the 'B' stands for) means it all works out. |
63 |
|
|
*/ |
64 |
|
|
|
65 |
greg |
2.1 |
typedef struct { |
66 |
|
|
OBJREC *mp; /* material pointer */ |
67 |
|
|
RAY *pr; /* intersected ray */ |
68 |
|
|
FVECT pnorm; /* perturbed surface normal */ |
69 |
greg |
2.4 |
FVECT vray; /* local outgoing (return) vector */ |
70 |
greg |
2.9 |
double sr_vpsa[2]; /* sqrt of BSDF projected solid angle extrema */ |
71 |
|
|
double thru_psa; /* through direction projected solid angle */ |
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 |
|
|
SDData *sd; /* loaded BSDF data */ |
76 |
|
|
COLOR runsamp; /* BSDF hemispherical reflection */ |
77 |
|
|
COLOR rdiff; /* added diffuse reflection */ |
78 |
|
|
COLOR tunsamp; /* BSDF hemispherical transmission */ |
79 |
|
|
COLOR tdiff; /* added diffuse transmission */ |
80 |
|
|
} BSDFDAT; /* BSDF material data */ |
81 |
|
|
|
82 |
|
|
#define cvt_sdcolor(cv, svp) ccy2rgb(&(svp)->spec, (svp)->cieY, cv) |
83 |
|
|
|
84 |
greg |
2.4 |
/* Jitter ray sample according to projected solid angle and specjitter */ |
85 |
|
|
static void |
86 |
greg |
2.9 |
bsdf_jitter(FVECT vres, BSDFDAT *ndp, int domax) |
87 |
greg |
2.4 |
{ |
88 |
greg |
2.9 |
double sr_psa = ndp->sr_vpsa[domax]; |
89 |
greg |
2.4 |
|
90 |
|
|
VCOPY(vres, ndp->vray); |
91 |
|
|
if (specjitter < 1.) |
92 |
|
|
sr_psa *= specjitter; |
93 |
|
|
if (sr_psa <= FTINY) |
94 |
|
|
return; |
95 |
|
|
vres[0] += sr_psa*(.5 - frandom()); |
96 |
|
|
vres[1] += sr_psa*(.5 - frandom()); |
97 |
|
|
normalize(vres); |
98 |
|
|
} |
99 |
|
|
|
100 |
greg |
2.7 |
/* Evaluate BSDF for direct component, returning true if OK to proceed */ |
101 |
|
|
static int |
102 |
|
|
direct_bsdf_OK(COLOR cval, FVECT ldir, BSDFDAT *ndp) |
103 |
|
|
{ |
104 |
|
|
FVECT vsrc, vjit; |
105 |
|
|
SDValue sv; |
106 |
|
|
SDError ec; |
107 |
|
|
/* transform source direction */ |
108 |
|
|
if (SDmapDir(vsrc, ndp->toloc, ldir) != SDEnone) |
109 |
|
|
return(0); |
110 |
|
|
/* jitter query direction */ |
111 |
greg |
2.9 |
bsdf_jitter(vjit, ndp, 0); |
112 |
greg |
2.7 |
/* avoid indirect over-counting */ |
113 |
greg |
2.9 |
if (ndp->thick != 0 && ndp->pr->crtype & (SPECULAR|AMBIENT) && |
114 |
|
|
vsrc[2] > 0 ^ vjit[2] > 0) { |
115 |
greg |
2.7 |
double dx = vsrc[0] + vjit[0]; |
116 |
|
|
double dy = vsrc[1] + vjit[1]; |
117 |
greg |
2.9 |
if (dx*dx + dy*dy <= ndp->thru_psa) |
118 |
greg |
2.7 |
return(0); |
119 |
|
|
} |
120 |
|
|
ec = SDevalBSDF(&sv, vjit, vsrc, ndp->sd); |
121 |
|
|
if (ec) |
122 |
|
|
objerror(ndp->mp, USER, transSDError(ec)); |
123 |
|
|
|
124 |
|
|
if (sv.cieY <= FTINY) /* not worth using? */ |
125 |
|
|
return(0); |
126 |
|
|
/* else we're good to go */ |
127 |
|
|
cvt_sdcolor(cval, &sv); |
128 |
|
|
return(1); |
129 |
|
|
} |
130 |
|
|
|
131 |
greg |
2.5 |
/* Compute source contribution for BSDF (reflected & transmitted) */ |
132 |
greg |
2.1 |
static void |
133 |
greg |
2.5 |
dir_bsdf( |
134 |
greg |
2.1 |
COLOR cval, /* returned coefficient */ |
135 |
|
|
void *nnp, /* material data */ |
136 |
|
|
FVECT ldir, /* light source direction */ |
137 |
|
|
double omega /* light source size */ |
138 |
|
|
) |
139 |
|
|
{ |
140 |
greg |
2.3 |
BSDFDAT *np = (BSDFDAT *)nnp; |
141 |
greg |
2.1 |
double ldot; |
142 |
|
|
double dtmp; |
143 |
|
|
COLOR ctmp; |
144 |
|
|
|
145 |
|
|
setcolor(cval, .0, .0, .0); |
146 |
|
|
|
147 |
|
|
ldot = DOT(np->pnorm, ldir); |
148 |
|
|
if ((-FTINY <= ldot) & (ldot <= FTINY)) |
149 |
|
|
return; |
150 |
|
|
|
151 |
greg |
2.9 |
if (ldot > 0 && bright(np->rdiff) > FTINY) { |
152 |
greg |
2.1 |
/* |
153 |
|
|
* Compute added diffuse reflected component. |
154 |
|
|
*/ |
155 |
|
|
copycolor(ctmp, np->rdiff); |
156 |
|
|
dtmp = ldot * omega * (1./PI); |
157 |
|
|
scalecolor(ctmp, dtmp); |
158 |
|
|
addcolor(cval, ctmp); |
159 |
|
|
} |
160 |
greg |
2.9 |
if (ldot < 0 && bright(np->tdiff) > FTINY) { |
161 |
greg |
2.1 |
/* |
162 |
|
|
* Compute added diffuse transmission. |
163 |
|
|
*/ |
164 |
|
|
copycolor(ctmp, np->tdiff); |
165 |
|
|
dtmp = -ldot * omega * (1.0/PI); |
166 |
|
|
scalecolor(ctmp, dtmp); |
167 |
|
|
addcolor(cval, ctmp); |
168 |
|
|
} |
169 |
|
|
/* |
170 |
|
|
* Compute scattering coefficient using BSDF. |
171 |
|
|
*/ |
172 |
greg |
2.7 |
if (!direct_bsdf_OK(ctmp, ldir, np)) |
173 |
greg |
2.1 |
return; |
174 |
greg |
2.9 |
if (ldot > 0) { /* pattern only diffuse reflection */ |
175 |
greg |
2.1 |
COLOR ctmp1, ctmp2; |
176 |
greg |
2.9 |
dtmp = (np->pr->rod > 0) ? np->sd->rLambFront.cieY |
177 |
greg |
2.1 |
: np->sd->rLambBack.cieY; |
178 |
greg |
2.7 |
/* diffuse fraction */ |
179 |
|
|
dtmp /= PI * bright(ctmp); |
180 |
greg |
2.1 |
copycolor(ctmp2, np->pr->pcol); |
181 |
|
|
scalecolor(ctmp2, dtmp); |
182 |
|
|
setcolor(ctmp1, 1.-dtmp, 1.-dtmp, 1.-dtmp); |
183 |
|
|
addcolor(ctmp1, ctmp2); |
184 |
greg |
2.3 |
multcolor(ctmp, ctmp1); /* apply derated pattern */ |
185 |
greg |
2.1 |
dtmp = ldot * omega; |
186 |
|
|
} else { /* full pattern on transmission */ |
187 |
|
|
multcolor(ctmp, np->pr->pcol); |
188 |
|
|
dtmp = -ldot * omega; |
189 |
|
|
} |
190 |
|
|
scalecolor(ctmp, dtmp); |
191 |
|
|
addcolor(cval, ctmp); |
192 |
|
|
} |
193 |
|
|
|
194 |
greg |
2.5 |
/* Compute source contribution for BSDF (reflected only) */ |
195 |
|
|
static void |
196 |
|
|
dir_brdf( |
197 |
|
|
COLOR cval, /* returned coefficient */ |
198 |
|
|
void *nnp, /* material data */ |
199 |
|
|
FVECT ldir, /* light source direction */ |
200 |
|
|
double omega /* light source size */ |
201 |
|
|
) |
202 |
|
|
{ |
203 |
|
|
BSDFDAT *np = (BSDFDAT *)nnp; |
204 |
|
|
double ldot; |
205 |
|
|
double dtmp; |
206 |
|
|
COLOR ctmp, ctmp1, ctmp2; |
207 |
|
|
|
208 |
|
|
setcolor(cval, .0, .0, .0); |
209 |
|
|
|
210 |
|
|
ldot = DOT(np->pnorm, ldir); |
211 |
|
|
|
212 |
|
|
if (ldot <= FTINY) |
213 |
|
|
return; |
214 |
|
|
|
215 |
|
|
if (bright(np->rdiff) > FTINY) { |
216 |
|
|
/* |
217 |
|
|
* Compute added diffuse reflected component. |
218 |
|
|
*/ |
219 |
|
|
copycolor(ctmp, np->rdiff); |
220 |
|
|
dtmp = ldot * omega * (1./PI); |
221 |
|
|
scalecolor(ctmp, dtmp); |
222 |
|
|
addcolor(cval, ctmp); |
223 |
|
|
} |
224 |
|
|
/* |
225 |
|
|
* Compute reflection coefficient using BSDF. |
226 |
|
|
*/ |
227 |
greg |
2.7 |
if (!direct_bsdf_OK(ctmp, ldir, np)) |
228 |
greg |
2.5 |
return; |
229 |
|
|
/* pattern only diffuse reflection */ |
230 |
greg |
2.9 |
dtmp = (np->pr->rod > 0) ? np->sd->rLambFront.cieY |
231 |
greg |
2.5 |
: np->sd->rLambBack.cieY; |
232 |
greg |
2.7 |
dtmp /= PI * bright(ctmp); /* diffuse fraction */ |
233 |
greg |
2.5 |
copycolor(ctmp2, np->pr->pcol); |
234 |
|
|
scalecolor(ctmp2, dtmp); |
235 |
|
|
setcolor(ctmp1, 1.-dtmp, 1.-dtmp, 1.-dtmp); |
236 |
|
|
addcolor(ctmp1, ctmp2); |
237 |
|
|
multcolor(ctmp, ctmp1); /* apply derated pattern */ |
238 |
|
|
dtmp = ldot * omega; |
239 |
|
|
scalecolor(ctmp, dtmp); |
240 |
|
|
addcolor(cval, ctmp); |
241 |
|
|
} |
242 |
|
|
|
243 |
|
|
/* Compute source contribution for BSDF (transmitted only) */ |
244 |
|
|
static void |
245 |
|
|
dir_btdf( |
246 |
|
|
COLOR cval, /* returned coefficient */ |
247 |
|
|
void *nnp, /* material data */ |
248 |
|
|
FVECT ldir, /* light source direction */ |
249 |
|
|
double omega /* light source size */ |
250 |
|
|
) |
251 |
|
|
{ |
252 |
|
|
BSDFDAT *np = (BSDFDAT *)nnp; |
253 |
|
|
double ldot; |
254 |
|
|
double dtmp; |
255 |
|
|
COLOR ctmp; |
256 |
|
|
|
257 |
|
|
setcolor(cval, .0, .0, .0); |
258 |
|
|
|
259 |
|
|
ldot = DOT(np->pnorm, ldir); |
260 |
|
|
|
261 |
|
|
if (ldot >= -FTINY) |
262 |
|
|
return; |
263 |
|
|
|
264 |
|
|
if (bright(np->tdiff) > FTINY) { |
265 |
|
|
/* |
266 |
|
|
* Compute added diffuse transmission. |
267 |
|
|
*/ |
268 |
|
|
copycolor(ctmp, np->tdiff); |
269 |
|
|
dtmp = -ldot * omega * (1.0/PI); |
270 |
|
|
scalecolor(ctmp, dtmp); |
271 |
|
|
addcolor(cval, ctmp); |
272 |
|
|
} |
273 |
|
|
/* |
274 |
|
|
* Compute scattering coefficient using BSDF. |
275 |
|
|
*/ |
276 |
greg |
2.7 |
if (!direct_bsdf_OK(ctmp, ldir, np)) |
277 |
greg |
2.5 |
return; |
278 |
|
|
/* full pattern on transmission */ |
279 |
|
|
multcolor(ctmp, np->pr->pcol); |
280 |
|
|
dtmp = -ldot * omega; |
281 |
|
|
scalecolor(ctmp, dtmp); |
282 |
|
|
addcolor(cval, ctmp); |
283 |
|
|
} |
284 |
|
|
|
285 |
greg |
2.1 |
/* Sample separate BSDF component */ |
286 |
|
|
static int |
287 |
|
|
sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usepat) |
288 |
|
|
{ |
289 |
|
|
int nstarget = 1; |
290 |
|
|
int nsent = 0; |
291 |
|
|
SDError ec; |
292 |
|
|
SDValue bsv; |
293 |
|
|
double sthick; |
294 |
greg |
2.10 |
FVECT vsmp; |
295 |
greg |
2.1 |
RAY sr; |
296 |
|
|
int ntrials; |
297 |
|
|
/* multiple samples? */ |
298 |
|
|
if (specjitter > 1.5) { |
299 |
|
|
nstarget = specjitter*ndp->pr->rweight + .5; |
300 |
|
|
if (nstarget < 1) |
301 |
|
|
nstarget = 1; |
302 |
|
|
} |
303 |
|
|
/* run through our trials */ |
304 |
|
|
for (ntrials = 0; nsent < nstarget && ntrials < 9*nstarget; ntrials++) { |
305 |
|
|
SDerrorDetail[0] = '\0'; |
306 |
|
|
/* sample direction & coef. */ |
307 |
greg |
2.10 |
bsdf_jitter(vsmp, ndp, 0); |
308 |
|
|
ec = SDsampComponent(&bsv, vsmp, ntrials ? frandom() |
309 |
greg |
2.4 |
: urand(ilhash(dimlist,ndims)+samplendx), dcp); |
310 |
greg |
2.1 |
if (ec) |
311 |
greg |
2.2 |
objerror(ndp->mp, USER, transSDError(ec)); |
312 |
greg |
2.1 |
/* zero component? */ |
313 |
|
|
if (bsv.cieY <= FTINY) |
314 |
|
|
break; |
315 |
|
|
/* map vector to world */ |
316 |
greg |
2.4 |
if (SDmapDir(sr.rdir, ndp->fromloc, vsmp) != SDEnone) |
317 |
greg |
2.1 |
break; |
318 |
|
|
/* unintentional penetration? */ |
319 |
greg |
2.9 |
if (DOT(sr.rdir, ndp->pr->ron) > 0 ^ vsmp[2] > 0) |
320 |
greg |
2.1 |
continue; |
321 |
|
|
/* spawn a specular ray */ |
322 |
|
|
if (nstarget > 1) |
323 |
|
|
bsv.cieY /= (double)nstarget; |
324 |
|
|
cvt_sdcolor(sr.rcoef, &bsv); /* use color */ |
325 |
|
|
if (usepat) /* pattern on transmission */ |
326 |
|
|
multcolor(sr.rcoef, ndp->pr->pcol); |
327 |
|
|
if (rayorigin(&sr, SPECULAR, ndp->pr, sr.rcoef) < 0) { |
328 |
|
|
if (maxdepth > 0) |
329 |
|
|
break; |
330 |
|
|
++nsent; /* Russian roulette victim */ |
331 |
|
|
continue; |
332 |
|
|
} |
333 |
greg |
2.5 |
/* need to offset origin? */ |
334 |
greg |
2.9 |
if (ndp->thick != 0 && ndp->pr->rod > 0 ^ vsmp[2] > 0) |
335 |
greg |
2.5 |
VSUM(sr.rorg, sr.rorg, ndp->pr->ron, -ndp->thick); |
336 |
greg |
2.1 |
rayvalue(&sr); /* send & evaluate sample */ |
337 |
|
|
multcolor(sr.rcol, sr.rcoef); |
338 |
|
|
addcolor(ndp->pr->rcol, sr.rcol); |
339 |
|
|
++nsent; |
340 |
|
|
} |
341 |
|
|
return(nsent); |
342 |
|
|
} |
343 |
|
|
|
344 |
|
|
/* Sample non-diffuse components of BSDF */ |
345 |
|
|
static int |
346 |
|
|
sample_sdf(BSDFDAT *ndp, int sflags) |
347 |
|
|
{ |
348 |
|
|
int n, ntotal = 0; |
349 |
|
|
SDSpectralDF *dfp; |
350 |
|
|
COLORV *unsc; |
351 |
|
|
|
352 |
|
|
if (sflags == SDsampSpT) { |
353 |
|
|
unsc = ndp->tunsamp; |
354 |
|
|
dfp = ndp->sd->tf; |
355 |
|
|
cvt_sdcolor(unsc, &ndp->sd->tLamb); |
356 |
|
|
} else /* sflags == SDsampSpR */ { |
357 |
|
|
unsc = ndp->runsamp; |
358 |
greg |
2.9 |
if (ndp->pr->rod > 0) { |
359 |
greg |
2.1 |
dfp = ndp->sd->rf; |
360 |
|
|
cvt_sdcolor(unsc, &ndp->sd->rLambFront); |
361 |
|
|
} else { |
362 |
|
|
dfp = ndp->sd->rb; |
363 |
|
|
cvt_sdcolor(unsc, &ndp->sd->rLambBack); |
364 |
|
|
} |
365 |
|
|
} |
366 |
|
|
multcolor(unsc, ndp->pr->pcol); |
367 |
|
|
if (dfp == NULL) /* no specular component? */ |
368 |
|
|
return(0); |
369 |
|
|
/* below sampling threshold? */ |
370 |
|
|
if (dfp->maxHemi <= specthresh+FTINY) { |
371 |
greg |
2.3 |
if (dfp->maxHemi > FTINY) { /* XXX no color from BSDF */ |
372 |
greg |
2.4 |
FVECT vjit; |
373 |
|
|
double d; |
374 |
greg |
2.1 |
COLOR ctmp; |
375 |
greg |
2.9 |
bsdf_jitter(vjit, ndp, 1); |
376 |
greg |
2.4 |
d = SDdirectHemi(vjit, sflags, ndp->sd); |
377 |
greg |
2.1 |
if (sflags == SDsampSpT) { |
378 |
|
|
copycolor(ctmp, ndp->pr->pcol); |
379 |
|
|
scalecolor(ctmp, d); |
380 |
|
|
} else /* no pattern on reflection */ |
381 |
|
|
setcolor(ctmp, d, d, d); |
382 |
|
|
addcolor(unsc, ctmp); |
383 |
|
|
} |
384 |
|
|
return(0); |
385 |
|
|
} |
386 |
|
|
/* else need to sample */ |
387 |
|
|
dimlist[ndims++] = (int)(size_t)ndp->mp; |
388 |
|
|
ndims++; |
389 |
|
|
for (n = dfp->ncomp; n--; ) { /* loop over components */ |
390 |
|
|
dimlist[ndims-1] = n + 9438; |
391 |
|
|
ntotal += sample_sdcomp(ndp, &dfp->comp[n], sflags==SDsampSpT); |
392 |
|
|
} |
393 |
|
|
ndims -= 2; |
394 |
|
|
return(ntotal); |
395 |
|
|
} |
396 |
|
|
|
397 |
|
|
/* Color a ray that hit a BSDF material */ |
398 |
|
|
int |
399 |
|
|
m_bsdf(OBJREC *m, RAY *r) |
400 |
|
|
{ |
401 |
greg |
2.6 |
int hitfront; |
402 |
greg |
2.1 |
COLOR ctmp; |
403 |
|
|
SDError ec; |
404 |
greg |
2.5 |
FVECT upvec, vtmp; |
405 |
greg |
2.1 |
MFUNC *mf; |
406 |
|
|
BSDFDAT nd; |
407 |
|
|
/* check arguments */ |
408 |
|
|
if ((m->oargs.nsargs < 6) | (m->oargs.nfargs > 9) | |
409 |
|
|
(m->oargs.nfargs % 3)) |
410 |
|
|
objerror(m, USER, "bad # arguments"); |
411 |
greg |
2.6 |
/* record surface struck */ |
412 |
greg |
2.9 |
hitfront = (r->rod > 0); |
413 |
greg |
2.1 |
/* load cal file */ |
414 |
|
|
mf = getfunc(m, 5, 0x1d, 1); |
415 |
|
|
/* get thickness */ |
416 |
|
|
nd.thick = evalue(mf->ep[0]); |
417 |
greg |
2.5 |
if ((-FTINY <= nd.thick) & (nd.thick <= FTINY)) |
418 |
greg |
2.1 |
nd.thick = .0; |
419 |
|
|
/* check shadow */ |
420 |
|
|
if (r->crtype & SHADOW) { |
421 |
greg |
2.9 |
if (nd.thick != 0) |
422 |
greg |
2.3 |
raytrans(r); /* pass-through */ |
423 |
greg |
2.5 |
return(1); /* or shadow */ |
424 |
greg |
2.1 |
} |
425 |
greg |
2.5 |
/* check other rays to pass */ |
426 |
greg |
2.9 |
if (nd.thick != 0 && (!(r->crtype & (SPECULAR|AMBIENT)) || |
427 |
|
|
nd.thick > 0 ^ hitfront)) { |
428 |
greg |
2.5 |
raytrans(r); /* hide our proxy */ |
429 |
greg |
2.1 |
return(1); |
430 |
|
|
} |
431 |
greg |
2.5 |
/* get BSDF data */ |
432 |
|
|
nd.sd = loadBSDF(m->oargs.sarg[1]); |
433 |
greg |
2.1 |
/* diffuse reflectance */ |
434 |
greg |
2.6 |
if (hitfront) { |
435 |
greg |
2.1 |
if (m->oargs.nfargs < 3) |
436 |
|
|
setcolor(nd.rdiff, .0, .0, .0); |
437 |
|
|
else |
438 |
|
|
setcolor(nd.rdiff, m->oargs.farg[0], |
439 |
|
|
m->oargs.farg[1], |
440 |
|
|
m->oargs.farg[2]); |
441 |
|
|
} else { |
442 |
|
|
if (m->oargs.nfargs < 6) { /* check invisible backside */ |
443 |
greg |
2.3 |
if (!backvis && (nd.sd->rb == NULL) & |
444 |
|
|
(nd.sd->tf == NULL)) { |
445 |
greg |
2.1 |
SDfreeCache(nd.sd); |
446 |
|
|
raytrans(r); |
447 |
|
|
return(1); |
448 |
|
|
} |
449 |
|
|
setcolor(nd.rdiff, .0, .0, .0); |
450 |
|
|
} else |
451 |
|
|
setcolor(nd.rdiff, m->oargs.farg[3], |
452 |
|
|
m->oargs.farg[4], |
453 |
|
|
m->oargs.farg[5]); |
454 |
|
|
} |
455 |
|
|
/* diffuse transmittance */ |
456 |
|
|
if (m->oargs.nfargs < 9) |
457 |
|
|
setcolor(nd.tdiff, .0, .0, .0); |
458 |
|
|
else |
459 |
|
|
setcolor(nd.tdiff, m->oargs.farg[6], |
460 |
|
|
m->oargs.farg[7], |
461 |
|
|
m->oargs.farg[8]); |
462 |
|
|
nd.mp = m; |
463 |
|
|
nd.pr = r; |
464 |
|
|
/* get modifiers */ |
465 |
|
|
raytexture(r, m->omod); |
466 |
|
|
/* modify diffuse values */ |
467 |
|
|
multcolor(nd.rdiff, r->pcol); |
468 |
|
|
multcolor(nd.tdiff, r->pcol); |
469 |
|
|
/* get up vector */ |
470 |
|
|
upvec[0] = evalue(mf->ep[1]); |
471 |
|
|
upvec[1] = evalue(mf->ep[2]); |
472 |
|
|
upvec[2] = evalue(mf->ep[3]); |
473 |
|
|
/* return to world coords */ |
474 |
|
|
if (mf->f != &unitxf) { |
475 |
|
|
multv3(upvec, upvec, mf->f->xfm); |
476 |
|
|
nd.thick *= mf->f->sca; |
477 |
|
|
} |
478 |
|
|
raynormal(nd.pnorm, r); |
479 |
|
|
/* compute local BSDF xform */ |
480 |
|
|
ec = SDcompXform(nd.toloc, nd.pnorm, upvec); |
481 |
|
|
if (!ec) { |
482 |
greg |
2.4 |
nd.vray[0] = -r->rdir[0]; |
483 |
|
|
nd.vray[1] = -r->rdir[1]; |
484 |
|
|
nd.vray[2] = -r->rdir[2]; |
485 |
|
|
ec = SDmapDir(nd.vray, nd.toloc, nd.vray); |
486 |
greg |
2.1 |
} |
487 |
|
|
if (!ec) |
488 |
|
|
ec = SDinvXform(nd.fromloc, nd.toloc); |
489 |
greg |
2.4 |
/* determine BSDF resolution */ |
490 |
|
|
if (!ec) |
491 |
greg |
2.9 |
ec = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL, |
492 |
|
|
SDqueryMin+SDqueryMax, nd.sd); |
493 |
|
|
nd.thru_psa = .0; |
494 |
|
|
if (!ec && nd.thick != 0 && r->crtype & (SPECULAR|AMBIENT)) { |
495 |
|
|
FVECT vthru; |
496 |
|
|
vthru[0] = -nd.vray[0]; |
497 |
|
|
vthru[1] = -nd.vray[1]; |
498 |
|
|
vthru[2] = -nd.vray[2]; |
499 |
|
|
ec = SDsizeBSDF(&nd.thru_psa, nd.vray, vthru, |
500 |
|
|
SDqueryMin, nd.sd); |
501 |
|
|
} |
502 |
|
|
if (ec) { |
503 |
greg |
2.2 |
objerror(m, WARNING, transSDError(ec)); |
504 |
greg |
2.1 |
SDfreeCache(nd.sd); |
505 |
|
|
return(1); |
506 |
|
|
} |
507 |
greg |
2.9 |
nd.sr_vpsa[0] = sqrt(nd.sr_vpsa[0]); |
508 |
|
|
nd.sr_vpsa[1] = sqrt(nd.sr_vpsa[1]); |
509 |
greg |
2.6 |
if (!hitfront) { /* perturb normal towards hit */ |
510 |
greg |
2.1 |
nd.pnorm[0] = -nd.pnorm[0]; |
511 |
|
|
nd.pnorm[1] = -nd.pnorm[1]; |
512 |
|
|
nd.pnorm[2] = -nd.pnorm[2]; |
513 |
|
|
} |
514 |
|
|
/* sample reflection */ |
515 |
|
|
sample_sdf(&nd, SDsampSpR); |
516 |
|
|
/* sample transmission */ |
517 |
|
|
sample_sdf(&nd, SDsampSpT); |
518 |
|
|
/* compute indirect diffuse */ |
519 |
|
|
copycolor(ctmp, nd.rdiff); |
520 |
|
|
addcolor(ctmp, nd.runsamp); |
521 |
greg |
2.5 |
if (bright(ctmp) > FTINY) { /* ambient from reflection */ |
522 |
greg |
2.6 |
if (!hitfront) |
523 |
greg |
2.1 |
flipsurface(r); |
524 |
|
|
multambient(ctmp, r, nd.pnorm); |
525 |
|
|
addcolor(r->rcol, ctmp); |
526 |
greg |
2.6 |
if (!hitfront) |
527 |
greg |
2.1 |
flipsurface(r); |
528 |
|
|
} |
529 |
|
|
copycolor(ctmp, nd.tdiff); |
530 |
|
|
addcolor(ctmp, nd.tunsamp); |
531 |
|
|
if (bright(ctmp) > FTINY) { /* ambient from other side */ |
532 |
|
|
FVECT bnorm; |
533 |
greg |
2.6 |
if (hitfront) |
534 |
greg |
2.1 |
flipsurface(r); |
535 |
|
|
bnorm[0] = -nd.pnorm[0]; |
536 |
|
|
bnorm[1] = -nd.pnorm[1]; |
537 |
|
|
bnorm[2] = -nd.pnorm[2]; |
538 |
greg |
2.9 |
if (nd.thick != 0) { /* proxy with offset? */ |
539 |
greg |
2.5 |
VCOPY(vtmp, r->rop); |
540 |
|
|
VSUM(r->rop, vtmp, r->ron, -nd.thick); |
541 |
|
|
multambient(ctmp, r, bnorm); |
542 |
|
|
VCOPY(r->rop, vtmp); |
543 |
|
|
} else |
544 |
|
|
multambient(ctmp, r, bnorm); |
545 |
greg |
2.1 |
addcolor(r->rcol, ctmp); |
546 |
greg |
2.6 |
if (hitfront) |
547 |
greg |
2.1 |
flipsurface(r); |
548 |
|
|
} |
549 |
|
|
/* add direct component */ |
550 |
greg |
2.5 |
if ((bright(nd.tdiff) <= FTINY) & (nd.sd->tf == NULL)) { |
551 |
|
|
direct(r, dir_brdf, &nd); /* reflection only */ |
552 |
greg |
2.9 |
} else if (nd.thick == 0) { |
553 |
greg |
2.5 |
direct(r, dir_bsdf, &nd); /* thin surface scattering */ |
554 |
|
|
} else { |
555 |
|
|
direct(r, dir_brdf, &nd); /* reflection first */ |
556 |
|
|
VCOPY(vtmp, r->rop); /* offset for transmitted */ |
557 |
|
|
VSUM(r->rop, vtmp, r->ron, -nd.thick); |
558 |
greg |
2.6 |
direct(r, dir_btdf, &nd); /* separate transmission */ |
559 |
greg |
2.5 |
VCOPY(r->rop, vtmp); |
560 |
|
|
} |
561 |
greg |
2.1 |
/* clean up */ |
562 |
|
|
SDfreeCache(nd.sd); |
563 |
|
|
return(1); |
564 |
|
|
} |