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 |
> |
double thru_r2; /* through rejection angle squared */ |
72 |
|
RREAL toloc[3][3]; /* world to local BSDF coords */ |
73 |
|
RREAL fromloc[3][3]; /* local BSDF coords to world */ |
74 |
|
double thick; /* surface thickness */ |
83 |
|
|
84 |
|
/* Jitter ray sample according to projected solid angle and specjitter */ |
85 |
|
static void |
86 |
< |
bsdf_jitter(FVECT vres, BSDFDAT *ndp) |
86 |
> |
bsdf_jitter(FVECT vres, BSDFDAT *ndp, int domax) |
87 |
|
{ |
88 |
< |
double sr_psa = ndp->sr_vpsa; |
88 |
> |
double sr_psa = ndp->sr_vpsa[domax]; |
89 |
|
|
90 |
|
VCOPY(vres, ndp->vray); |
91 |
|
if (specjitter < 1.) |
108 |
|
if (SDmapDir(vsrc, ndp->toloc, ldir) != SDEnone) |
109 |
|
return(0); |
110 |
|
/* jitter query direction */ |
111 |
< |
bsdf_jitter(vjit, ndp); |
111 |
> |
bsdf_jitter(vjit, ndp, 0); |
112 |
|
/* avoid indirect over-counting */ |
113 |
< |
if (ndp->thick != .0 && ndp->pr->crtype & (SPECULAR|AMBIENT) && |
113 |
< |
vsrc[2] > .0 ^ vjit[2] > .0) { |
113 |
> |
if (ndp->thru_r2 > FTINY && 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) |
116 |
> |
if (dx*dx + dy*dy <= ndp->thru_r2) |
117 |
|
return(0); |
118 |
|
} |
119 |
|
ec = SDevalBSDF(&sv, vjit, vsrc, ndp->sd); |
147 |
|
if ((-FTINY <= ldot) & (ldot <= FTINY)) |
148 |
|
return; |
149 |
|
|
150 |
< |
if (ldot > .0 && bright(np->rdiff) > FTINY) { |
150 |
> |
if (ldot > 0 && bright(np->rdiff) > FTINY) { |
151 |
|
/* |
152 |
|
* Compute added diffuse reflected component. |
153 |
|
*/ |
156 |
|
scalecolor(ctmp, dtmp); |
157 |
|
addcolor(cval, ctmp); |
158 |
|
} |
159 |
< |
if (ldot < .0 && bright(np->tdiff) > FTINY) { |
159 |
> |
if (ldot < 0 && bright(np->tdiff) > FTINY) { |
160 |
|
/* |
161 |
|
* Compute added diffuse transmission. |
162 |
|
*/ |
170 |
|
*/ |
171 |
|
if (!direct_bsdf_OK(ctmp, ldir, np)) |
172 |
|
return; |
173 |
< |
if (ldot > .0) { /* pattern only diffuse reflection */ |
173 |
> |
if (ldot > 0) { /* pattern only diffuse reflection */ |
174 |
|
COLOR ctmp1, ctmp2; |
175 |
< |
dtmp = (np->pr->rod > .0) ? np->sd->rLambFront.cieY |
175 |
> |
dtmp = (np->pr->rod > 0) ? np->sd->rLambFront.cieY |
176 |
|
: np->sd->rLambBack.cieY; |
177 |
|
/* diffuse fraction */ |
178 |
|
dtmp /= PI * bright(ctmp); |
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 |
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); |
286 |
|
sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usepat) |
287 |
|
{ |
288 |
|
int nstarget = 1; |
289 |
< |
int nsent = 0; |
289 |
> |
int nsent; |
290 |
|
SDError ec; |
291 |
|
SDValue bsv; |
292 |
< |
double sthick; |
293 |
< |
FVECT vjit, vsmp; |
292 |
> |
double xrand; |
293 |
> |
FVECT vsmp; |
294 |
|
RAY sr; |
295 |
– |
int ntrials; |
295 |
|
/* multiple samples? */ |
296 |
|
if (specjitter > 1.5) { |
297 |
|
nstarget = specjitter*ndp->pr->rweight + .5; |
298 |
|
if (nstarget < 1) |
299 |
|
nstarget = 1; |
300 |
|
} |
301 |
< |
/* run through our trials */ |
302 |
< |
for (ntrials = 0; nsent < nstarget && ntrials < 9*nstarget; ntrials++) { |
303 |
< |
SDerrorDetail[0] = '\0'; |
304 |
< |
/* sample direction & coef. */ |
305 |
< |
bsdf_jitter(vjit, ndp); |
306 |
< |
ec = SDsampComponent(&bsv, vsmp, vjit, ntrials ? frandom() |
307 |
< |
: urand(ilhash(dimlist,ndims)+samplendx), dcp); |
301 |
> |
/* run through our samples */ |
302 |
> |
for (nsent = 0; nsent < nstarget; nsent++) { |
303 |
> |
if (nstarget == 1) /* stratify random variable */ |
304 |
> |
xrand = urand(ilhash(dimlist,ndims)+samplendx); |
305 |
> |
else |
306 |
> |
xrand = (nsent + frandom())/(double)nstarget; |
307 |
> |
SDerrorDetail[0] = '\0'; /* sample direction & coef. */ |
308 |
> |
bsdf_jitter(vsmp, ndp, 0); |
309 |
> |
ec = SDsampComponent(&bsv, vsmp, xrand, dcp); |
310 |
|
if (ec) |
311 |
|
objerror(ndp->mp, USER, transSDError(ec)); |
312 |
< |
/* zero component? */ |
312 |
< |
if (bsv.cieY <= FTINY) |
312 |
> |
if (bsv.cieY <= FTINY) /* zero component? */ |
313 |
|
break; |
314 |
|
/* map vector to world */ |
315 |
|
if (SDmapDir(sr.rdir, ndp->fromloc, vsmp) != SDEnone) |
316 |
|
break; |
317 |
– |
/* unintentional penetration? */ |
318 |
– |
if (DOT(sr.rdir, ndp->pr->ron) > .0 ^ vsmp[2] > .0) |
319 |
– |
continue; |
317 |
|
/* spawn a specular ray */ |
318 |
|
if (nstarget > 1) |
319 |
|
bsv.cieY /= (double)nstarget; |
320 |
< |
cvt_sdcolor(sr.rcoef, &bsv); /* use color */ |
321 |
< |
if (usepat) /* pattern on transmission */ |
320 |
> |
cvt_sdcolor(sr.rcoef, &bsv); /* use sample color */ |
321 |
> |
if (usepat) /* apply pattern? */ |
322 |
|
multcolor(sr.rcoef, ndp->pr->pcol); |
323 |
|
if (rayorigin(&sr, SPECULAR, ndp->pr, sr.rcoef) < 0) { |
324 |
< |
if (maxdepth > 0) |
324 |
> |
if (maxdepth > 0) |
325 |
|
break; |
326 |
< |
++nsent; /* Russian roulette victim */ |
330 |
< |
continue; |
326 |
> |
continue; /* Russian roulette victim */ |
327 |
|
} |
328 |
|
/* need to offset origin? */ |
329 |
< |
if (ndp->thick != .0 && ndp->pr->rod > .0 ^ vsmp[2] > .0) |
329 |
> |
if (ndp->thick != 0 && ndp->pr->rod > 0 ^ vsmp[2] > 0) |
330 |
|
VSUM(sr.rorg, sr.rorg, ndp->pr->ron, -ndp->thick); |
331 |
|
rayvalue(&sr); /* send & evaluate sample */ |
332 |
|
multcolor(sr.rcol, sr.rcoef); |
333 |
|
addcolor(ndp->pr->rcol, sr.rcol); |
338 |
– |
++nsent; |
334 |
|
} |
335 |
|
return(nsent); |
336 |
|
} |
349 |
|
cvt_sdcolor(unsc, &ndp->sd->tLamb); |
350 |
|
} else /* sflags == SDsampSpR */ { |
351 |
|
unsc = ndp->runsamp; |
352 |
< |
if (ndp->pr->rod > .0) { |
352 |
> |
if (ndp->pr->rod > 0) { |
353 |
|
dfp = ndp->sd->rf; |
354 |
|
cvt_sdcolor(unsc, &ndp->sd->rLambFront); |
355 |
|
} else { |
366 |
|
FVECT vjit; |
367 |
|
double d; |
368 |
|
COLOR ctmp; |
369 |
< |
bsdf_jitter(vjit, ndp); |
369 |
> |
bsdf_jitter(vjit, ndp, 1); |
370 |
|
d = SDdirectHemi(vjit, sflags, ndp->sd); |
371 |
|
if (sflags == SDsampSpT) { |
372 |
|
copycolor(ctmp, ndp->pr->pcol); |
403 |
|
(m->oargs.nfargs % 3)) |
404 |
|
objerror(m, USER, "bad # arguments"); |
405 |
|
/* record surface struck */ |
406 |
< |
hitfront = (r->rod > .0); |
406 |
> |
hitfront = (r->rod > 0); |
407 |
|
/* load cal file */ |
408 |
|
mf = getfunc(m, 5, 0x1d, 1); |
409 |
|
/* get thickness */ |
412 |
|
nd.thick = .0; |
413 |
|
/* check shadow */ |
414 |
|
if (r->crtype & SHADOW) { |
415 |
< |
if (nd.thick != .0) |
415 |
> |
if (nd.thick != 0) |
416 |
|
raytrans(r); /* pass-through */ |
417 |
|
return(1); /* or shadow */ |
418 |
|
} |
419 |
|
/* check other rays to pass */ |
420 |
< |
if (nd.thick != .0 && (!(r->crtype & (SPECULAR|AMBIENT)) || |
421 |
< |
nd.thick > .0 ^ hitfront)) { |
420 |
> |
if (nd.thick != 0 && (!(r->crtype & (SPECULAR|AMBIENT)) || |
421 |
> |
nd.thick > 0 ^ hitfront)) { |
422 |
|
raytrans(r); /* hide our proxy */ |
423 |
|
return(1); |
424 |
|
} |
482 |
|
ec = SDinvXform(nd.fromloc, nd.toloc); |
483 |
|
/* determine BSDF resolution */ |
484 |
|
if (!ec) |
485 |
< |
ec = SDsizeBSDF(&nd.sr_vpsa, nd.vray, SDqueryMin, nd.sd); |
486 |
< |
if (!ec) |
487 |
< |
nd.sr_vpsa = sqrt(nd.sr_vpsa); |
488 |
< |
else { |
485 |
> |
ec = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL, |
486 |
> |
SDqueryMin+SDqueryMax, nd.sd); |
487 |
> |
nd.thru_r2 = .0; |
488 |
> |
if (!ec && nd.thick != 0 && r->crtype & (SPECULAR|AMBIENT)) { |
489 |
> |
FVECT vthru; |
490 |
> |
vthru[0] = -nd.vray[0]; |
491 |
> |
vthru[1] = -nd.vray[1]; |
492 |
> |
vthru[2] = -nd.vray[2]; |
493 |
> |
ec = SDsizeBSDF(&nd.thru_r2, nd.vray, vthru, |
494 |
> |
SDqueryMin, nd.sd); |
495 |
> |
nd.thru_r2 *= 1./PI; |
496 |
> |
} |
497 |
> |
if (ec) { |
498 |
|
objerror(m, WARNING, transSDError(ec)); |
499 |
|
SDfreeCache(nd.sd); |
500 |
|
return(1); |
501 |
|
} |
502 |
+ |
nd.sr_vpsa[0] = sqrt(nd.sr_vpsa[0]); |
503 |
+ |
nd.sr_vpsa[1] = sqrt(nd.sr_vpsa[1]); |
504 |
|
if (!hitfront) { /* perturb normal towards hit */ |
505 |
|
nd.pnorm[0] = -nd.pnorm[0]; |
506 |
|
nd.pnorm[1] = -nd.pnorm[1]; |
530 |
|
bnorm[0] = -nd.pnorm[0]; |
531 |
|
bnorm[1] = -nd.pnorm[1]; |
532 |
|
bnorm[2] = -nd.pnorm[2]; |
533 |
< |
if (nd.thick != .0) { /* proxy with offset? */ |
533 |
> |
if (nd.thick != 0) { /* proxy with offset? */ |
534 |
|
VCOPY(vtmp, r->rop); |
535 |
|
VSUM(r->rop, vtmp, r->ron, -nd.thick); |
536 |
|
multambient(ctmp, r, bnorm); |
544 |
|
/* add direct component */ |
545 |
|
if ((bright(nd.tdiff) <= FTINY) & (nd.sd->tf == NULL)) { |
546 |
|
direct(r, dir_brdf, &nd); /* reflection only */ |
547 |
< |
} else if (nd.thick == .0) { |
547 |
> |
} else if (nd.thick == 0) { |
548 |
|
direct(r, dir_bsdf, &nd); /* thin surface scattering */ |
549 |
|
} else { |
550 |
|
direct(r, dir_brdf, &nd); /* reflection first */ |