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