17 |
|
/* |
18 |
|
* Arguments to this material include optional diffuse colors. |
19 |
|
* String arguments include the BSDF and function files. |
20 |
< |
* A thickness variable causes the strange but useful behavior |
21 |
< |
* of translating transmitted rays this distance past the surface |
22 |
< |
* intersection in the normal direction to bypass intervening geometry. |
23 |
< |
* This only affects scattered, non-source directed samples. Thus, |
24 |
< |
* thickness is relevant only if there is a transmitted component. |
25 |
< |
* A positive thickness has the further side-effect that an unscattered |
20 |
> |
* 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 |
|
* (view) ray will pass right through our material if it has any |
26 |
< |
* non-diffuse transmission, making our BSDF invisible. This allows the |
27 |
< |
* underlying geometry to become visible. A matching surface should be |
28 |
< |
* placed on the other side, less than the thickness away, if the backside |
29 |
< |
* reflectance is non-zero. |
26 |
> |
* 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 |
|
* 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 |
< |
* reflectance and none is given in the real arguments, the surface will |
44 |
< |
* appear as black when viewed from behind (unless backvis is false). |
45 |
< |
* The diffuse compnent arguments are added to components in the BSDF file, |
43 |
> |
* 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 |
|
* not multiplied. However, patterns affect this material as a multiplier |
48 |
|
* on everything except non-diffuse reflection. |
49 |
|
* |
96 |
|
normalize(vres); |
97 |
|
} |
98 |
|
|
99 |
< |
/* Compute source contribution for BSDF */ |
99 |
> |
/* Evaluate BSDF for direct component, returning true if OK to proceed */ |
100 |
> |
static int |
101 |
> |
direct_bsdf_OK(COLOR cval, FVECT ldir, BSDFDAT *ndp) |
102 |
> |
{ |
103 |
> |
FVECT vsrc, vjit; |
104 |
> |
SDValue sv; |
105 |
> |
SDError ec; |
106 |
> |
/* transform source direction */ |
107 |
> |
if (SDmapDir(vsrc, ndp->toloc, ldir) != SDEnone) |
108 |
> |
return(0); |
109 |
> |
/* jitter query direction */ |
110 |
> |
bsdf_jitter(vjit, ndp); |
111 |
> |
/* avoid indirect over-counting */ |
112 |
> |
if (ndp->thick != .0 && ndp->pr->crtype & (SPECULAR|AMBIENT) && |
113 |
> |
vsrc[2] > .0 ^ vjit[2] > .0) { |
114 |
> |
double dx = vsrc[0] + vjit[0]; |
115 |
> |
double dy = vsrc[1] + vjit[1]; |
116 |
> |
if (dx*dx + dy*dy <= ndp->sr_vpsa*ndp->sr_vpsa) |
117 |
> |
return(0); |
118 |
> |
} |
119 |
> |
ec = SDevalBSDF(&sv, vjit, vsrc, ndp->sd); |
120 |
> |
if (ec) |
121 |
> |
objerror(ndp->mp, USER, transSDError(ec)); |
122 |
> |
|
123 |
> |
if (sv.cieY <= FTINY) /* not worth using? */ |
124 |
> |
return(0); |
125 |
> |
/* else we're good to go */ |
126 |
> |
cvt_sdcolor(cval, &sv); |
127 |
> |
return(1); |
128 |
> |
} |
129 |
> |
|
130 |
> |
/* Compute source contribution for BSDF (reflected & transmitted) */ |
131 |
|
static void |
132 |
< |
dirbsdf( |
132 |
> |
dir_bsdf( |
133 |
|
COLOR cval, /* returned coefficient */ |
134 |
|
void *nnp, /* material data */ |
135 |
|
FVECT ldir, /* light source direction */ |
137 |
|
) |
138 |
|
{ |
139 |
|
BSDFDAT *np = (BSDFDAT *)nnp; |
101 |
– |
SDError ec; |
102 |
– |
SDValue sv; |
103 |
– |
FVECT vsrc; |
104 |
– |
FVECT vjit; |
140 |
|
double ldot; |
141 |
|
double dtmp; |
142 |
|
COLOR ctmp; |
168 |
|
/* |
169 |
|
* Compute scattering coefficient using BSDF. |
170 |
|
*/ |
171 |
< |
if (SDmapDir(vsrc, np->toloc, ldir) != SDEnone) |
171 |
> |
if (!direct_bsdf_OK(ctmp, ldir, np)) |
172 |
|
return; |
138 |
– |
bsdf_jitter(vjit, np); |
139 |
– |
ec = SDevalBSDF(&sv, vjit, vsrc, np->sd); |
140 |
– |
if (ec) |
141 |
– |
objerror(np->mp, USER, transSDError(ec)); |
142 |
– |
|
143 |
– |
if (sv.cieY <= FTINY) /* not worth using? */ |
144 |
– |
return; |
145 |
– |
cvt_sdcolor(ctmp, &sv); |
173 |
|
if (ldot > .0) { /* pattern only diffuse reflection */ |
174 |
|
COLOR ctmp1, ctmp2; |
175 |
|
dtmp = (np->pr->rod > .0) ? np->sd->rLambFront.cieY |
176 |
|
: np->sd->rLambBack.cieY; |
177 |
< |
dtmp /= PI * sv.cieY; /* diffuse fraction */ |
177 |
> |
/* diffuse fraction */ |
178 |
> |
dtmp /= PI * bright(ctmp); |
179 |
|
copycolor(ctmp2, np->pr->pcol); |
180 |
|
scalecolor(ctmp2, dtmp); |
181 |
|
setcolor(ctmp1, 1.-dtmp, 1.-dtmp, 1.-dtmp); |
190 |
|
addcolor(cval, ctmp); |
191 |
|
} |
192 |
|
|
193 |
+ |
/* Compute source contribution for BSDF (reflected only) */ |
194 |
+ |
static void |
195 |
+ |
dir_brdf( |
196 |
+ |
COLOR cval, /* returned coefficient */ |
197 |
+ |
void *nnp, /* material data */ |
198 |
+ |
FVECT ldir, /* light source direction */ |
199 |
+ |
double omega /* light source size */ |
200 |
+ |
) |
201 |
+ |
{ |
202 |
+ |
BSDFDAT *np = (BSDFDAT *)nnp; |
203 |
+ |
double ldot; |
204 |
+ |
double dtmp; |
205 |
+ |
COLOR ctmp, ctmp1, ctmp2; |
206 |
+ |
|
207 |
+ |
setcolor(cval, .0, .0, .0); |
208 |
+ |
|
209 |
+ |
ldot = DOT(np->pnorm, ldir); |
210 |
+ |
|
211 |
+ |
if (ldot <= FTINY) |
212 |
+ |
return; |
213 |
+ |
|
214 |
+ |
if (bright(np->rdiff) > FTINY) { |
215 |
+ |
/* |
216 |
+ |
* Compute added diffuse reflected component. |
217 |
+ |
*/ |
218 |
+ |
copycolor(ctmp, np->rdiff); |
219 |
+ |
dtmp = ldot * omega * (1./PI); |
220 |
+ |
scalecolor(ctmp, dtmp); |
221 |
+ |
addcolor(cval, ctmp); |
222 |
+ |
} |
223 |
+ |
/* |
224 |
+ |
* Compute reflection coefficient using BSDF. |
225 |
+ |
*/ |
226 |
+ |
if (!direct_bsdf_OK(ctmp, ldir, np)) |
227 |
+ |
return; |
228 |
+ |
/* pattern only diffuse reflection */ |
229 |
+ |
dtmp = (np->pr->rod > .0) ? np->sd->rLambFront.cieY |
230 |
+ |
: np->sd->rLambBack.cieY; |
231 |
+ |
dtmp /= PI * bright(ctmp); /* diffuse fraction */ |
232 |
+ |
copycolor(ctmp2, np->pr->pcol); |
233 |
+ |
scalecolor(ctmp2, dtmp); |
234 |
+ |
setcolor(ctmp1, 1.-dtmp, 1.-dtmp, 1.-dtmp); |
235 |
+ |
addcolor(ctmp1, ctmp2); |
236 |
+ |
multcolor(ctmp, ctmp1); /* apply derated pattern */ |
237 |
+ |
dtmp = ldot * omega; |
238 |
+ |
scalecolor(ctmp, dtmp); |
239 |
+ |
addcolor(cval, ctmp); |
240 |
+ |
} |
241 |
+ |
|
242 |
+ |
/* Compute source contribution for BSDF (transmitted only) */ |
243 |
+ |
static void |
244 |
+ |
dir_btdf( |
245 |
+ |
COLOR cval, /* returned coefficient */ |
246 |
+ |
void *nnp, /* material data */ |
247 |
+ |
FVECT ldir, /* light source direction */ |
248 |
+ |
double omega /* light source size */ |
249 |
+ |
) |
250 |
+ |
{ |
251 |
+ |
BSDFDAT *np = (BSDFDAT *)nnp; |
252 |
+ |
double ldot; |
253 |
+ |
double dtmp; |
254 |
+ |
COLOR ctmp; |
255 |
+ |
|
256 |
+ |
setcolor(cval, .0, .0, .0); |
257 |
+ |
|
258 |
+ |
ldot = DOT(np->pnorm, ldir); |
259 |
+ |
|
260 |
+ |
if (ldot >= -FTINY) |
261 |
+ |
return; |
262 |
+ |
|
263 |
+ |
if (bright(np->tdiff) > FTINY) { |
264 |
+ |
/* |
265 |
+ |
* Compute added diffuse transmission. |
266 |
+ |
*/ |
267 |
+ |
copycolor(ctmp, np->tdiff); |
268 |
+ |
dtmp = -ldot * omega * (1.0/PI); |
269 |
+ |
scalecolor(ctmp, dtmp); |
270 |
+ |
addcolor(cval, ctmp); |
271 |
+ |
} |
272 |
+ |
/* |
273 |
+ |
* Compute scattering coefficient using BSDF. |
274 |
+ |
*/ |
275 |
+ |
if (!direct_bsdf_OK(ctmp, ldir, np)) |
276 |
+ |
return; |
277 |
+ |
/* full pattern on transmission */ |
278 |
+ |
multcolor(ctmp, np->pr->pcol); |
279 |
+ |
dtmp = -ldot * omega; |
280 |
+ |
scalecolor(ctmp, dtmp); |
281 |
+ |
addcolor(cval, ctmp); |
282 |
+ |
} |
283 |
+ |
|
284 |
|
/* Sample separate BSDF component */ |
285 |
|
static int |
286 |
|
sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usepat) |
329 |
|
++nsent; /* Russian roulette victim */ |
330 |
|
continue; |
331 |
|
} |
332 |
< |
if (ndp->thick > FTINY) { /* need to move origin? */ |
333 |
< |
sthick = (ndp->pr->rod > .0) ? -ndp->thick : ndp->thick; |
334 |
< |
if (sthick < .0 ^ vsmp[2] > .0) |
216 |
< |
VSUM(sr.rorg, sr.rorg, ndp->pr->ron, sthick); |
217 |
< |
} |
332 |
> |
/* need to offset origin? */ |
333 |
> |
if (ndp->thick != .0 && ndp->pr->rod > .0 ^ vsmp[2] > .0) |
334 |
> |
VSUM(sr.rorg, sr.rorg, ndp->pr->ron, -ndp->thick); |
335 |
|
rayvalue(&sr); /* send & evaluate sample */ |
336 |
|
multcolor(sr.rcol, sr.rcoef); |
337 |
|
addcolor(ndp->pr->rcol, sr.rcol); |
397 |
|
int |
398 |
|
m_bsdf(OBJREC *m, RAY *r) |
399 |
|
{ |
400 |
+ |
int hitfront; |
401 |
|
COLOR ctmp; |
402 |
|
SDError ec; |
403 |
< |
FVECT upvec, outVec; |
403 |
> |
FVECT upvec, vtmp; |
404 |
|
MFUNC *mf; |
405 |
|
BSDFDAT nd; |
406 |
|
/* check arguments */ |
407 |
|
if ((m->oargs.nsargs < 6) | (m->oargs.nfargs > 9) | |
408 |
|
(m->oargs.nfargs % 3)) |
409 |
|
objerror(m, USER, "bad # arguments"); |
410 |
< |
|
411 |
< |
/* get BSDF data */ |
294 |
< |
nd.sd = loadBSDF(m->oargs.sarg[1]); |
410 |
> |
/* record surface struck */ |
411 |
> |
hitfront = (r->rod > .0); |
412 |
|
/* load cal file */ |
413 |
|
mf = getfunc(m, 5, 0x1d, 1); |
414 |
|
/* get thickness */ |
415 |
|
nd.thick = evalue(mf->ep[0]); |
416 |
< |
if (nd.thick < .0) |
416 |
> |
if ((-FTINY <= nd.thick) & (nd.thick <= FTINY)) |
417 |
|
nd.thick = .0; |
418 |
|
/* check shadow */ |
419 |
|
if (r->crtype & SHADOW) { |
420 |
< |
if ((nd.thick > FTINY) & (nd.sd->tf != NULL)) |
420 |
> |
if (nd.thick != .0) |
421 |
|
raytrans(r); /* pass-through */ |
422 |
< |
SDfreeCache(nd.sd); |
306 |
< |
return(1); /* else shadow */ |
422 |
> |
return(1); /* or shadow */ |
423 |
|
} |
424 |
< |
/* check unscattered ray */ |
425 |
< |
if (!(r->crtype & (SPECULAR|AMBIENT)) && |
426 |
< |
(nd.thick > FTINY) & (nd.sd->tf != NULL)) { |
427 |
< |
SDfreeCache(nd.sd); |
312 |
< |
raytrans(r); /* pass-through */ |
424 |
> |
/* check other rays to pass */ |
425 |
> |
if (nd.thick != .0 && (!(r->crtype & (SPECULAR|AMBIENT)) || |
426 |
> |
nd.thick > .0 ^ hitfront)) { |
427 |
> |
raytrans(r); /* hide our proxy */ |
428 |
|
return(1); |
429 |
|
} |
430 |
+ |
/* get BSDF data */ |
431 |
+ |
nd.sd = loadBSDF(m->oargs.sarg[1]); |
432 |
|
/* diffuse reflectance */ |
433 |
< |
if (r->rod > .0) { |
433 |
> |
if (hitfront) { |
434 |
|
if (m->oargs.nfargs < 3) |
435 |
|
setcolor(nd.rdiff, .0, .0, .0); |
436 |
|
else |
462 |
|
nd.pr = r; |
463 |
|
/* get modifiers */ |
464 |
|
raytexture(r, m->omod); |
348 |
– |
if (bright(r->pcol) <= FTINY) { /* black pattern?! */ |
349 |
– |
SDfreeCache(nd.sd); |
350 |
– |
return(1); |
351 |
– |
} |
465 |
|
/* modify diffuse values */ |
466 |
|
multcolor(nd.rdiff, r->pcol); |
467 |
|
multcolor(nd.tdiff, r->pcol); |
495 |
|
SDfreeCache(nd.sd); |
496 |
|
return(1); |
497 |
|
} |
498 |
< |
|
386 |
< |
if (r->rod < .0) { /* perturb normal towards hit */ |
498 |
> |
if (!hitfront) { /* perturb normal towards hit */ |
499 |
|
nd.pnorm[0] = -nd.pnorm[0]; |
500 |
|
nd.pnorm[1] = -nd.pnorm[1]; |
501 |
|
nd.pnorm[2] = -nd.pnorm[2]; |
507 |
|
/* compute indirect diffuse */ |
508 |
|
copycolor(ctmp, nd.rdiff); |
509 |
|
addcolor(ctmp, nd.runsamp); |
510 |
< |
if (bright(ctmp) > FTINY) { /* ambient from this side */ |
511 |
< |
if (r->rod < .0) |
510 |
> |
if (bright(ctmp) > FTINY) { /* ambient from reflection */ |
511 |
> |
if (!hitfront) |
512 |
|
flipsurface(r); |
513 |
|
multambient(ctmp, r, nd.pnorm); |
514 |
|
addcolor(r->rcol, ctmp); |
515 |
< |
if (r->rod < .0) |
515 |
> |
if (!hitfront) |
516 |
|
flipsurface(r); |
517 |
|
} |
518 |
|
copycolor(ctmp, nd.tdiff); |
519 |
|
addcolor(ctmp, nd.tunsamp); |
520 |
|
if (bright(ctmp) > FTINY) { /* ambient from other side */ |
521 |
|
FVECT bnorm; |
522 |
< |
if (r->rod > .0) |
522 |
> |
if (hitfront) |
523 |
|
flipsurface(r); |
524 |
|
bnorm[0] = -nd.pnorm[0]; |
525 |
|
bnorm[1] = -nd.pnorm[1]; |
526 |
|
bnorm[2] = -nd.pnorm[2]; |
527 |
< |
multambient(ctmp, r, bnorm); |
527 |
> |
if (nd.thick != .0) { /* proxy with offset? */ |
528 |
> |
VCOPY(vtmp, r->rop); |
529 |
> |
VSUM(r->rop, vtmp, r->ron, -nd.thick); |
530 |
> |
multambient(ctmp, r, bnorm); |
531 |
> |
VCOPY(r->rop, vtmp); |
532 |
> |
} else |
533 |
> |
multambient(ctmp, r, bnorm); |
534 |
|
addcolor(r->rcol, ctmp); |
535 |
< |
if (r->rod > .0) |
535 |
> |
if (hitfront) |
536 |
|
flipsurface(r); |
537 |
|
} |
538 |
|
/* add direct component */ |
539 |
< |
direct(r, dirbsdf, &nd); |
539 |
> |
if ((bright(nd.tdiff) <= FTINY) & (nd.sd->tf == NULL)) { |
540 |
> |
direct(r, dir_brdf, &nd); /* reflection only */ |
541 |
> |
} else if (nd.thick == .0) { |
542 |
> |
direct(r, dir_bsdf, &nd); /* thin surface scattering */ |
543 |
> |
} else { |
544 |
> |
direct(r, dir_brdf, &nd); /* reflection first */ |
545 |
> |
VCOPY(vtmp, r->rop); /* offset for transmitted */ |
546 |
> |
VSUM(r->rop, vtmp, r->ron, -nd.thick); |
547 |
> |
direct(r, dir_btdf, &nd); /* separate transmission */ |
548 |
> |
VCOPY(r->rop, vtmp); |
549 |
> |
} |
550 |
|
/* clean up */ |
551 |
|
SDfreeCache(nd.sd); |
552 |
|
return(1); |