50 |
|
* Arguments for MAT_BSDF are: |
51 |
|
* 6+ thick BSDFfile ux uy uz funcfile transform |
52 |
|
* 0 |
53 |
< |
* 0|3|9 rdf gdf bdf |
53 |
> |
* 0|3|6|9 rdf gdf bdf |
54 |
|
* rdb gdb bdb |
55 |
|
* rdt gdt bdt |
56 |
|
*/ |
67 |
|
RAY *pr; /* intersected ray */ |
68 |
|
FVECT pnorm; /* perturbed surface normal */ |
69 |
|
FVECT vray; /* local outgoing (return) vector */ |
70 |
< |
double sr_vpsa; /* sqrt of BSDF projected solid angle */ |
70 |
> |
double sr_vpsa[2]; /* sqrt of BSDF projected solid angle extrema */ |
71 |
|
RREAL toloc[3][3]; /* world to local BSDF coords */ |
72 |
|
RREAL fromloc[3][3]; /* local BSDF coords to world */ |
73 |
|
double thick; /* surface thickness */ |
82 |
|
|
83 |
|
/* Jitter ray sample according to projected solid angle and specjitter */ |
84 |
|
static void |
85 |
< |
bsdf_jitter(FVECT vres, BSDFDAT *ndp) |
85 |
> |
bsdf_jitter(FVECT vres, BSDFDAT *ndp, double sr_psa) |
86 |
|
{ |
87 |
– |
double sr_psa = ndp->sr_vpsa; |
88 |
– |
|
87 |
|
VCOPY(vres, ndp->vray); |
88 |
|
if (specjitter < 1.) |
89 |
|
sr_psa *= specjitter; |
94 |
|
normalize(vres); |
95 |
|
} |
96 |
|
|
97 |
+ |
/* Evaluate BSDF for direct component, returning true if OK to proceed */ |
98 |
+ |
static int |
99 |
+ |
direct_bsdf_OK(COLOR cval, FVECT ldir, double omega, BSDFDAT *ndp) |
100 |
+ |
{ |
101 |
+ |
int nsamp, ok = 0; |
102 |
+ |
FVECT vsrc, vsmp, vjit; |
103 |
+ |
double tomega; |
104 |
+ |
double sf, tsr, sd[2]; |
105 |
+ |
COLOR csmp; |
106 |
+ |
SDValue sv; |
107 |
+ |
SDError ec; |
108 |
+ |
int i; |
109 |
+ |
/* transform source direction */ |
110 |
+ |
if (SDmapDir(vsrc, ndp->toloc, ldir) != SDEnone) |
111 |
+ |
return(0); |
112 |
+ |
/* assign number of samples */ |
113 |
+ |
ec = SDsizeBSDF(&tomega, ndp->vray, vsrc, SDqueryMin, ndp->sd); |
114 |
+ |
if (ec) |
115 |
+ |
goto baderror; |
116 |
+ |
/* check indirect over-counting */ |
117 |
+ |
if (ndp->thick != 0 && ndp->pr->crtype & (SPECULAR|AMBIENT) |
118 |
+ |
&& vsrc[2] > 0 ^ ndp->vray[2] > 0) { |
119 |
+ |
double dx = vsrc[0] + ndp->vray[0]; |
120 |
+ |
double dy = vsrc[1] + ndp->vray[1]; |
121 |
+ |
if (dx*dx + dy*dy <= omega+tomega) |
122 |
+ |
return(0); |
123 |
+ |
} |
124 |
+ |
sf = specjitter * ndp->pr->rweight; |
125 |
+ |
if (25.*tomega <= omega) |
126 |
+ |
nsamp = 100.*sf + .5; |
127 |
+ |
else |
128 |
+ |
nsamp = 4.*sf*omega/tomega + .5; |
129 |
+ |
nsamp += !nsamp; |
130 |
+ |
setcolor(cval, .0, .0, .0); /* sample our source area */ |
131 |
+ |
sf = sqrt(omega); |
132 |
+ |
tsr = sqrt(tomega); |
133 |
+ |
for (i = nsamp; i--; ) { |
134 |
+ |
VCOPY(vsmp, vsrc); /* jitter query directions */ |
135 |
+ |
if (nsamp > 1) { |
136 |
+ |
multisamp(sd, 2, (i + frandom())/(double)nsamp); |
137 |
+ |
vsmp[0] += (sd[0] - .5)*sf; |
138 |
+ |
vsmp[1] += (sd[1] - .5)*sf; |
139 |
+ |
if (normalize(vsmp) == 0) { |
140 |
+ |
--nsamp; |
141 |
+ |
continue; |
142 |
+ |
} |
143 |
+ |
} |
144 |
+ |
bsdf_jitter(vjit, ndp, tsr); |
145 |
+ |
/* compute BSDF */ |
146 |
+ |
ec = SDevalBSDF(&sv, vjit, vsmp, ndp->sd); |
147 |
+ |
if (ec) |
148 |
+ |
goto baderror; |
149 |
+ |
if (sv.cieY <= FTINY) /* worth using? */ |
150 |
+ |
continue; |
151 |
+ |
cvt_sdcolor(csmp, &sv); |
152 |
+ |
addcolor(cval, csmp); /* average it in */ |
153 |
+ |
++ok; |
154 |
+ |
} |
155 |
+ |
sf = 1./(double)nsamp; |
156 |
+ |
scalecolor(cval, sf); |
157 |
+ |
return(ok); |
158 |
+ |
baderror: |
159 |
+ |
objerror(ndp->mp, USER, transSDError(ec)); |
160 |
+ |
return(0); /* gratis return */ |
161 |
+ |
} |
162 |
+ |
|
163 |
|
/* Compute source contribution for BSDF (reflected & transmitted) */ |
164 |
|
static void |
165 |
|
dir_bsdf( |
170 |
|
) |
171 |
|
{ |
172 |
|
BSDFDAT *np = (BSDFDAT *)nnp; |
109 |
– |
SDError ec; |
110 |
– |
SDValue sv; |
111 |
– |
FVECT vsrc; |
112 |
– |
FVECT vjit; |
173 |
|
double ldot; |
174 |
|
double dtmp; |
175 |
|
COLOR ctmp; |
180 |
|
if ((-FTINY <= ldot) & (ldot <= FTINY)) |
181 |
|
return; |
182 |
|
|
183 |
< |
if (ldot > .0 && bright(np->rdiff) > FTINY) { |
183 |
> |
if (ldot > 0 && bright(np->rdiff) > FTINY) { |
184 |
|
/* |
185 |
|
* Compute added diffuse reflected component. |
186 |
|
*/ |
189 |
|
scalecolor(ctmp, dtmp); |
190 |
|
addcolor(cval, ctmp); |
191 |
|
} |
192 |
< |
if (ldot < .0 && bright(np->tdiff) > FTINY) { |
192 |
> |
if (ldot < 0 && bright(np->tdiff) > FTINY) { |
193 |
|
/* |
194 |
|
* Compute added diffuse transmission. |
195 |
|
*/ |
201 |
|
/* |
202 |
|
* Compute scattering coefficient using BSDF. |
203 |
|
*/ |
204 |
< |
if (SDmapDir(vsrc, np->toloc, ldir) != SDEnone) |
204 |
> |
if (!direct_bsdf_OK(ctmp, ldir, omega, np)) |
205 |
|
return; |
206 |
< |
bsdf_jitter(vjit, np); |
147 |
< |
ec = SDevalBSDF(&sv, vjit, vsrc, np->sd); |
148 |
< |
if (ec) |
149 |
< |
objerror(np->mp, USER, transSDError(ec)); |
150 |
< |
|
151 |
< |
if (sv.cieY <= FTINY) /* not worth using? */ |
152 |
< |
return; |
153 |
< |
cvt_sdcolor(ctmp, &sv); |
154 |
< |
if (ldot > .0) { /* pattern only diffuse reflection */ |
206 |
> |
if (ldot > 0) { /* pattern only diffuse reflection */ |
207 |
|
COLOR ctmp1, ctmp2; |
208 |
< |
dtmp = (np->pr->rod > .0) ? np->sd->rLambFront.cieY |
208 |
> |
dtmp = (np->pr->rod > 0) ? np->sd->rLambFront.cieY |
209 |
|
: np->sd->rLambBack.cieY; |
210 |
< |
dtmp /= PI * sv.cieY; /* diffuse fraction */ |
210 |
> |
/* diffuse fraction */ |
211 |
> |
dtmp /= PI * bright(ctmp); |
212 |
|
copycolor(ctmp2, np->pr->pcol); |
213 |
|
scalecolor(ctmp2, dtmp); |
214 |
|
setcolor(ctmp1, 1.-dtmp, 1.-dtmp, 1.-dtmp); |
233 |
|
) |
234 |
|
{ |
235 |
|
BSDFDAT *np = (BSDFDAT *)nnp; |
183 |
– |
SDError ec; |
184 |
– |
SDValue sv; |
185 |
– |
FVECT vsrc; |
186 |
– |
FVECT vjit; |
236 |
|
double ldot; |
237 |
|
double dtmp; |
238 |
|
COLOR ctmp, ctmp1, ctmp2; |
256 |
|
/* |
257 |
|
* Compute reflection coefficient using BSDF. |
258 |
|
*/ |
259 |
< |
if (SDmapDir(vsrc, np->toloc, ldir) != SDEnone) |
259 |
> |
if (!direct_bsdf_OK(ctmp, ldir, omega, np)) |
260 |
|
return; |
212 |
– |
bsdf_jitter(vjit, np); |
213 |
– |
ec = SDevalBSDF(&sv, vjit, vsrc, np->sd); |
214 |
– |
if (ec) |
215 |
– |
objerror(np->mp, USER, transSDError(ec)); |
216 |
– |
|
217 |
– |
if (sv.cieY <= FTINY) /* not worth using? */ |
218 |
– |
return; |
219 |
– |
cvt_sdcolor(ctmp, &sv); |
261 |
|
/* pattern only diffuse reflection */ |
262 |
< |
dtmp = (np->pr->rod > .0) ? np->sd->rLambFront.cieY |
262 |
> |
dtmp = (np->pr->rod > 0) ? np->sd->rLambFront.cieY |
263 |
|
: np->sd->rLambBack.cieY; |
264 |
< |
dtmp /= PI * sv.cieY; /* diffuse fraction */ |
264 |
> |
dtmp /= PI * bright(ctmp); /* diffuse fraction */ |
265 |
|
copycolor(ctmp2, np->pr->pcol); |
266 |
|
scalecolor(ctmp2, dtmp); |
267 |
|
setcolor(ctmp1, 1.-dtmp, 1.-dtmp, 1.-dtmp); |
282 |
|
) |
283 |
|
{ |
284 |
|
BSDFDAT *np = (BSDFDAT *)nnp; |
244 |
– |
SDError ec; |
245 |
– |
SDValue sv; |
246 |
– |
FVECT vsrc; |
247 |
– |
FVECT vjit; |
285 |
|
double ldot; |
286 |
|
double dtmp; |
287 |
|
COLOR ctmp; |
305 |
|
/* |
306 |
|
* Compute scattering coefficient using BSDF. |
307 |
|
*/ |
308 |
< |
if (SDmapDir(vsrc, np->toloc, ldir) != SDEnone) |
308 |
> |
if (!direct_bsdf_OK(ctmp, ldir, omega, np)) |
309 |
|
return; |
273 |
– |
bsdf_jitter(vjit, np); |
274 |
– |
ec = SDevalBSDF(&sv, vjit, vsrc, np->sd); |
275 |
– |
if (ec) |
276 |
– |
objerror(np->mp, USER, transSDError(ec)); |
277 |
– |
|
278 |
– |
if (sv.cieY <= FTINY) /* not worth using? */ |
279 |
– |
return; |
280 |
– |
cvt_sdcolor(ctmp, &sv); |
310 |
|
/* full pattern on transmission */ |
311 |
|
multcolor(ctmp, np->pr->pcol); |
312 |
|
dtmp = -ldot * omega; |
319 |
|
sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usepat) |
320 |
|
{ |
321 |
|
int nstarget = 1; |
322 |
< |
int nsent = 0; |
322 |
> |
int nsent; |
323 |
|
SDError ec; |
324 |
|
SDValue bsv; |
325 |
< |
double sthick; |
326 |
< |
FVECT vjit, vsmp; |
325 |
> |
double xrand; |
326 |
> |
FVECT vsmp; |
327 |
|
RAY sr; |
299 |
– |
int ntrials; |
328 |
|
/* multiple samples? */ |
329 |
|
if (specjitter > 1.5) { |
330 |
|
nstarget = specjitter*ndp->pr->rweight + .5; |
331 |
< |
if (nstarget < 1) |
304 |
< |
nstarget = 1; |
331 |
> |
nstarget += !nstarget; |
332 |
|
} |
333 |
< |
/* run through our trials */ |
334 |
< |
for (ntrials = 0; nsent < nstarget && ntrials < 9*nstarget; ntrials++) { |
335 |
< |
SDerrorDetail[0] = '\0'; |
336 |
< |
/* sample direction & coef. */ |
337 |
< |
bsdf_jitter(vjit, ndp); |
338 |
< |
ec = SDsampComponent(&bsv, vsmp, vjit, ntrials ? frandom() |
339 |
< |
: urand(ilhash(dimlist,ndims)+samplendx), dcp); |
333 |
> |
/* run through our samples */ |
334 |
> |
for (nsent = 0; nsent < nstarget; nsent++) { |
335 |
> |
if (nstarget == 1) { /* stratify random variable */ |
336 |
> |
xrand = urand(ilhash(dimlist,ndims)+samplendx); |
337 |
> |
if (specjitter < 1.) |
338 |
> |
xrand = .5 + specjitter*(xrand-.5); |
339 |
> |
} else { |
340 |
> |
xrand = (nsent + frandom())/(double)nstarget; |
341 |
> |
} |
342 |
> |
SDerrorDetail[0] = '\0'; /* sample direction & coef. */ |
343 |
> |
bsdf_jitter(vsmp, ndp, ndp->sr_vpsa[0]); |
344 |
> |
ec = SDsampComponent(&bsv, vsmp, xrand, dcp); |
345 |
|
if (ec) |
346 |
|
objerror(ndp->mp, USER, transSDError(ec)); |
347 |
< |
/* zero component? */ |
316 |
< |
if (bsv.cieY <= FTINY) |
347 |
> |
if (bsv.cieY <= FTINY) /* zero component? */ |
348 |
|
break; |
349 |
|
/* map vector to world */ |
350 |
|
if (SDmapDir(sr.rdir, ndp->fromloc, vsmp) != SDEnone) |
351 |
|
break; |
321 |
– |
/* unintentional penetration? */ |
322 |
– |
if (DOT(sr.rdir, ndp->pr->ron) > .0 ^ vsmp[2] > .0) |
323 |
– |
continue; |
352 |
|
/* spawn a specular ray */ |
353 |
|
if (nstarget > 1) |
354 |
|
bsv.cieY /= (double)nstarget; |
355 |
< |
cvt_sdcolor(sr.rcoef, &bsv); /* use color */ |
356 |
< |
if (usepat) /* pattern on transmission */ |
355 |
> |
cvt_sdcolor(sr.rcoef, &bsv); /* use sample color */ |
356 |
> |
if (usepat) /* apply pattern? */ |
357 |
|
multcolor(sr.rcoef, ndp->pr->pcol); |
358 |
|
if (rayorigin(&sr, SPECULAR, ndp->pr, sr.rcoef) < 0) { |
359 |
< |
if (maxdepth > 0) |
359 |
> |
if (maxdepth > 0) |
360 |
|
break; |
361 |
< |
++nsent; /* Russian roulette victim */ |
334 |
< |
continue; |
361 |
> |
continue; /* Russian roulette victim */ |
362 |
|
} |
363 |
|
/* need to offset origin? */ |
364 |
< |
if (ndp->thick != .0 && ndp->pr->rod > .0 ^ vsmp[2] > .0) |
364 |
> |
if (ndp->thick != 0 && ndp->pr->rod > 0 ^ vsmp[2] > 0) |
365 |
|
VSUM(sr.rorg, sr.rorg, ndp->pr->ron, -ndp->thick); |
366 |
|
rayvalue(&sr); /* send & evaluate sample */ |
367 |
|
multcolor(sr.rcol, sr.rcoef); |
368 |
|
addcolor(ndp->pr->rcol, sr.rcol); |
342 |
– |
++nsent; |
369 |
|
} |
370 |
|
return(nsent); |
371 |
|
} |
384 |
|
cvt_sdcolor(unsc, &ndp->sd->tLamb); |
385 |
|
} else /* sflags == SDsampSpR */ { |
386 |
|
unsc = ndp->runsamp; |
387 |
< |
if (ndp->pr->rod > .0) { |
387 |
> |
if (ndp->pr->rod > 0) { |
388 |
|
dfp = ndp->sd->rf; |
389 |
|
cvt_sdcolor(unsc, &ndp->sd->rLambFront); |
390 |
|
} else { |
401 |
|
FVECT vjit; |
402 |
|
double d; |
403 |
|
COLOR ctmp; |
404 |
< |
bsdf_jitter(vjit, ndp); |
404 |
> |
bsdf_jitter(vjit, ndp, ndp->sr_vpsa[1]); |
405 |
|
d = SDdirectHemi(vjit, sflags, ndp->sd); |
406 |
|
if (sflags == SDsampSpT) { |
407 |
|
copycolor(ctmp, ndp->pr->pcol); |
427 |
|
int |
428 |
|
m_bsdf(OBJREC *m, RAY *r) |
429 |
|
{ |
430 |
+ |
int hitfront; |
431 |
|
COLOR ctmp; |
432 |
|
SDError ec; |
433 |
|
FVECT upvec, vtmp; |
437 |
|
if ((m->oargs.nsargs < 6) | (m->oargs.nfargs > 9) | |
438 |
|
(m->oargs.nfargs % 3)) |
439 |
|
objerror(m, USER, "bad # arguments"); |
440 |
< |
|
440 |
> |
/* record surface struck */ |
441 |
> |
hitfront = (r->rod > 0); |
442 |
|
/* load cal file */ |
443 |
|
mf = getfunc(m, 5, 0x1d, 1); |
444 |
|
/* get thickness */ |
447 |
|
nd.thick = .0; |
448 |
|
/* check shadow */ |
449 |
|
if (r->crtype & SHADOW) { |
450 |
< |
if (nd.thick != .0) |
450 |
> |
if (nd.thick != 0) |
451 |
|
raytrans(r); /* pass-through */ |
452 |
|
return(1); /* or shadow */ |
453 |
|
} |
454 |
|
/* check other rays to pass */ |
455 |
|
if (nd.thick != 0 && (!(r->crtype & (SPECULAR|AMBIENT)) || |
456 |
< |
nd.thick > .0 ^ r->rod > .0)) { |
456 |
> |
nd.thick > 0 ^ hitfront)) { |
457 |
|
raytrans(r); /* hide our proxy */ |
458 |
|
return(1); |
459 |
|
} |
460 |
|
/* get BSDF data */ |
461 |
|
nd.sd = loadBSDF(m->oargs.sarg[1]); |
462 |
|
/* diffuse reflectance */ |
463 |
< |
if (r->rod > .0) { |
463 |
> |
if (hitfront) { |
464 |
|
if (m->oargs.nfargs < 3) |
465 |
|
setcolor(nd.rdiff, .0, .0, .0); |
466 |
|
else |
517 |
|
ec = SDinvXform(nd.fromloc, nd.toloc); |
518 |
|
/* determine BSDF resolution */ |
519 |
|
if (!ec) |
520 |
< |
ec = SDsizeBSDF(&nd.sr_vpsa, nd.vray, SDqueryMin, nd.sd); |
521 |
< |
if (!ec) |
522 |
< |
nd.sr_vpsa = sqrt(nd.sr_vpsa); |
495 |
< |
else { |
520 |
> |
ec = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL, |
521 |
> |
SDqueryMin+SDqueryMax, nd.sd); |
522 |
> |
if (ec) { |
523 |
|
objerror(m, WARNING, transSDError(ec)); |
524 |
|
SDfreeCache(nd.sd); |
525 |
|
return(1); |
526 |
|
} |
527 |
< |
if (r->rod < .0) { /* perturb normal towards hit */ |
527 |
> |
nd.sr_vpsa[0] = sqrt(nd.sr_vpsa[0]); |
528 |
> |
nd.sr_vpsa[1] = sqrt(nd.sr_vpsa[1]); |
529 |
> |
if (!hitfront) { /* perturb normal towards hit */ |
530 |
|
nd.pnorm[0] = -nd.pnorm[0]; |
531 |
|
nd.pnorm[1] = -nd.pnorm[1]; |
532 |
|
nd.pnorm[2] = -nd.pnorm[2]; |
539 |
|
copycolor(ctmp, nd.rdiff); |
540 |
|
addcolor(ctmp, nd.runsamp); |
541 |
|
if (bright(ctmp) > FTINY) { /* ambient from reflection */ |
542 |
< |
if (r->rod < .0) |
542 |
> |
if (!hitfront) |
543 |
|
flipsurface(r); |
544 |
|
multambient(ctmp, r, nd.pnorm); |
545 |
|
addcolor(r->rcol, ctmp); |
546 |
< |
if (r->rod < .0) |
546 |
> |
if (!hitfront) |
547 |
|
flipsurface(r); |
548 |
|
} |
549 |
|
copycolor(ctmp, nd.tdiff); |
550 |
|
addcolor(ctmp, nd.tunsamp); |
551 |
|
if (bright(ctmp) > FTINY) { /* ambient from other side */ |
552 |
|
FVECT bnorm; |
553 |
< |
if (r->rod > .0) |
553 |
> |
if (hitfront) |
554 |
|
flipsurface(r); |
555 |
|
bnorm[0] = -nd.pnorm[0]; |
556 |
|
bnorm[1] = -nd.pnorm[1]; |
557 |
|
bnorm[2] = -nd.pnorm[2]; |
558 |
< |
if (nd.thick != .0) { /* proxy with offset? */ |
558 |
> |
if (nd.thick != 0) { /* proxy with offset? */ |
559 |
|
VCOPY(vtmp, r->rop); |
560 |
|
VSUM(r->rop, vtmp, r->ron, -nd.thick); |
561 |
|
multambient(ctmp, r, bnorm); |
563 |
|
} else |
564 |
|
multambient(ctmp, r, bnorm); |
565 |
|
addcolor(r->rcol, ctmp); |
566 |
< |
if (r->rod > .0) |
566 |
> |
if (hitfront) |
567 |
|
flipsurface(r); |
568 |
|
} |
569 |
|
/* add direct component */ |
570 |
|
if ((bright(nd.tdiff) <= FTINY) & (nd.sd->tf == NULL)) { |
571 |
|
direct(r, dir_brdf, &nd); /* reflection only */ |
572 |
< |
} else if (nd.thick == .0) { |
572 |
> |
} else if (nd.thick == 0) { |
573 |
|
direct(r, dir_bsdf, &nd); /* thin surface scattering */ |
574 |
|
} else { |
575 |
|
direct(r, dir_brdf, &nd); /* reflection first */ |
576 |
|
VCOPY(vtmp, r->rop); /* offset for transmitted */ |
577 |
|
VSUM(r->rop, vtmp, r->ron, -nd.thick); |
578 |
< |
direct(r, dir_btdf, &nd); |
578 |
> |
direct(r, dir_btdf, &nd); /* separate transmission */ |
579 |
|
VCOPY(r->rop, vtmp); |
580 |
|
} |
581 |
|
/* clean up */ |