| 68 |
|
FVECT pnorm; /* perturbed surface normal */ |
| 69 |
|
FVECT vray; /* local outgoing (return) vector */ |
| 70 |
|
double sr_vpsa[2]; /* sqrt of BSDF projected solid angle extrema */ |
| 71 |
– |
double thru_psa; /* through direction projected solid angle */ |
| 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, int domax) |
| 85 |
> |
bsdf_jitter(FVECT vres, BSDFDAT *ndp, double sr_psa) |
| 86 |
|
{ |
| 88 |
– |
double sr_psa = ndp->sr_vpsa[domax]; |
| 89 |
– |
|
| 87 |
|
VCOPY(vres, ndp->vray); |
| 88 |
|
if (specjitter < 1.) |
| 89 |
|
sr_psa *= specjitter; |
| 96 |
|
|
| 97 |
|
/* Evaluate BSDF for direct component, returning true if OK to proceed */ |
| 98 |
|
static int |
| 99 |
< |
direct_bsdf_OK(COLOR cval, FVECT ldir, BSDFDAT *ndp) |
| 99 |
> |
direct_bsdf_OK(COLOR cval, FVECT ldir, double omega, BSDFDAT *ndp) |
| 100 |
|
{ |
| 101 |
< |
FVECT vsrc, vjit; |
| 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 |
< |
/* jitter query direction */ |
| 113 |
< |
bsdf_jitter(vjit, ndp, 0); |
| 114 |
< |
/* avoid indirect over-counting */ |
| 115 |
< |
if (ndp->thick != 0 && ndp->pr->crtype & (SPECULAR|AMBIENT) && |
| 116 |
< |
vsrc[2] > 0 ^ vjit[2] > 0) { |
| 117 |
< |
double dx = vsrc[0] + vjit[0]; |
| 118 |
< |
double dy = vsrc[1] + vjit[1]; |
| 119 |
< |
if (dx*dx + dy*dy <= ndp->thru_psa) |
| 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 |
< |
ec = SDevalBSDF(&sv, vjit, vsrc, ndp->sd); |
| 125 |
< |
if (ec) |
| 126 |
< |
objerror(ndp->mp, USER, transSDError(ec)); |
| 127 |
< |
|
| 128 |
< |
if (sv.cieY <= FTINY) /* not worth using? */ |
| 129 |
< |
return(0); |
| 130 |
< |
/* else we're good to go */ |
| 131 |
< |
cvt_sdcolor(cval, &sv); |
| 132 |
< |
return(1); |
| 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) */ |
| 201 |
|
/* |
| 202 |
|
* Compute scattering coefficient using BSDF. |
| 203 |
|
*/ |
| 204 |
< |
if (!direct_bsdf_OK(ctmp, ldir, np)) |
| 204 |
> |
if (!direct_bsdf_OK(ctmp, ldir, omega, np)) |
| 205 |
|
return; |
| 206 |
|
if (ldot > 0) { /* pattern only diffuse reflection */ |
| 207 |
|
COLOR ctmp1, ctmp2; |
| 256 |
|
/* |
| 257 |
|
* Compute reflection coefficient using BSDF. |
| 258 |
|
*/ |
| 259 |
< |
if (!direct_bsdf_OK(ctmp, ldir, np)) |
| 259 |
> |
if (!direct_bsdf_OK(ctmp, ldir, omega, np)) |
| 260 |
|
return; |
| 261 |
|
/* pattern only diffuse reflection */ |
| 262 |
|
dtmp = (np->pr->rod > 0) ? np->sd->rLambFront.cieY |
| 305 |
|
/* |
| 306 |
|
* Compute scattering coefficient using BSDF. |
| 307 |
|
*/ |
| 308 |
< |
if (!direct_bsdf_OK(ctmp, ldir, np)) |
| 308 |
> |
if (!direct_bsdf_OK(ctmp, ldir, omega, np)) |
| 309 |
|
return; |
| 310 |
|
/* full pattern on transmission */ |
| 311 |
|
multcolor(ctmp, np->pr->pcol); |
| 328 |
|
/* multiple samples? */ |
| 329 |
|
if (specjitter > 1.5) { |
| 330 |
|
nstarget = specjitter*ndp->pr->rweight + .5; |
| 331 |
< |
if (nstarget < 1) |
| 300 |
< |
nstarget = 1; |
| 331 |
> |
nstarget += !nstarget; |
| 332 |
|
} |
| 333 |
|
/* run through our samples */ |
| 334 |
|
for (nsent = 0; nsent < nstarget; nsent++) { |
| 335 |
< |
if (nstarget == 1) /* stratify random variable */ |
| 335 |
> |
if (nstarget == 1) { /* stratify random variable */ |
| 336 |
|
xrand = urand(ilhash(dimlist,ndims)+samplendx); |
| 337 |
< |
else |
| 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, 0); |
| 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)); |
| 380 |
|
|
| 381 |
|
if (sflags == SDsampSpT) { |
| 382 |
|
unsc = ndp->tunsamp; |
| 383 |
< |
dfp = ndp->sd->tf; |
| 383 |
> |
if (ndp->pr->rod > 0) |
| 384 |
> |
dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb; |
| 385 |
> |
else |
| 386 |
> |
dfp = (ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf; |
| 387 |
|
cvt_sdcolor(unsc, &ndp->sd->tLamb); |
| 388 |
|
} else /* sflags == SDsampSpR */ { |
| 389 |
|
unsc = ndp->runsamp; |
| 404 |
|
FVECT vjit; |
| 405 |
|
double d; |
| 406 |
|
COLOR ctmp; |
| 407 |
< |
bsdf_jitter(vjit, ndp, 1); |
| 407 |
> |
bsdf_jitter(vjit, ndp, ndp->sr_vpsa[1]); |
| 408 |
|
d = SDdirectHemi(vjit, sflags, ndp->sd); |
| 409 |
|
if (sflags == SDsampSpT) { |
| 410 |
|
copycolor(ctmp, ndp->pr->pcol); |
| 473 |
|
} else { |
| 474 |
|
if (m->oargs.nfargs < 6) { /* check invisible backside */ |
| 475 |
|
if (!backvis && (nd.sd->rb == NULL) & |
| 476 |
< |
(nd.sd->tf == NULL)) { |
| 476 |
> |
(nd.sd->tb == NULL)) { |
| 477 |
|
SDfreeCache(nd.sd); |
| 478 |
|
raytrans(r); |
| 479 |
|
return(1); |
| 503 |
|
upvec[1] = evalue(mf->ep[2]); |
| 504 |
|
upvec[2] = evalue(mf->ep[3]); |
| 505 |
|
/* return to world coords */ |
| 506 |
< |
if (mf->f != &unitxf) { |
| 507 |
< |
multv3(upvec, upvec, mf->f->xfm); |
| 508 |
< |
nd.thick *= mf->f->sca; |
| 506 |
> |
if (mf->fxp != &unitxf) { |
| 507 |
> |
multv3(upvec, upvec, mf->fxp->xfm); |
| 508 |
> |
nd.thick *= mf->fxp->sca; |
| 509 |
|
} |
| 510 |
|
raynormal(nd.pnorm, r); |
| 511 |
|
/* compute local BSDF xform */ |
| 518 |
|
} |
| 519 |
|
if (!ec) |
| 520 |
|
ec = SDinvXform(nd.fromloc, nd.toloc); |
| 484 |
– |
/* determine BSDF resolution */ |
| 485 |
– |
if (!ec) |
| 486 |
– |
ec = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL, |
| 487 |
– |
SDqueryMin+SDqueryMax, nd.sd); |
| 488 |
– |
nd.thru_psa = .0; |
| 489 |
– |
if (!ec && nd.thick != 0 && r->crtype & (SPECULAR|AMBIENT)) { |
| 490 |
– |
FVECT vthru; |
| 491 |
– |
vthru[0] = -nd.vray[0]; |
| 492 |
– |
vthru[1] = -nd.vray[1]; |
| 493 |
– |
vthru[2] = -nd.vray[2]; |
| 494 |
– |
ec = SDsizeBSDF(&nd.thru_psa, nd.vray, vthru, |
| 495 |
– |
SDqueryMin, nd.sd); |
| 496 |
– |
} |
| 521 |
|
if (ec) { |
| 522 |
< |
objerror(m, WARNING, transSDError(ec)); |
| 499 |
< |
SDfreeCache(nd.sd); |
| 522 |
> |
objerror(m, WARNING, "Illegal orientation vector"); |
| 523 |
|
return(1); |
| 524 |
|
} |
| 525 |
+ |
/* determine BSDF resolution */ |
| 526 |
+ |
ec = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL, SDqueryMin+SDqueryMax, nd.sd); |
| 527 |
+ |
if (ec) |
| 528 |
+ |
objerror(m, USER, transSDError(ec)); |
| 529 |
+ |
|
| 530 |
|
nd.sr_vpsa[0] = sqrt(nd.sr_vpsa[0]); |
| 531 |
|
nd.sr_vpsa[1] = sqrt(nd.sr_vpsa[1]); |
| 532 |
|
if (!hitfront) { /* perturb normal towards hit */ |
| 560 |
|
bnorm[2] = -nd.pnorm[2]; |
| 561 |
|
if (nd.thick != 0) { /* proxy with offset? */ |
| 562 |
|
VCOPY(vtmp, r->rop); |
| 563 |
< |
VSUM(r->rop, vtmp, r->ron, -nd.thick); |
| 563 |
> |
VSUM(r->rop, vtmp, r->ron, nd.thick); |
| 564 |
|
multambient(ctmp, r, bnorm); |
| 565 |
|
VCOPY(r->rop, vtmp); |
| 566 |
|
} else |
| 570 |
|
flipsurface(r); |
| 571 |
|
} |
| 572 |
|
/* add direct component */ |
| 573 |
< |
if ((bright(nd.tdiff) <= FTINY) & (nd.sd->tf == NULL)) { |
| 573 |
> |
if ((bright(nd.tdiff) <= FTINY) & (nd.sd->tf == NULL) & |
| 574 |
> |
(nd.sd->tb == NULL)) { |
| 575 |
|
direct(r, dir_brdf, &nd); /* reflection only */ |
| 576 |
|
} else if (nd.thick == 0) { |
| 577 |
|
direct(r, dir_bsdf, &nd); /* thin surface scattering */ |