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. |
83 |
|
|
84 |
|
/* Jitter ray sample according to projected solid angle and specjitter */ |
85 |
|
static void |
86 |
< |
bsdf_jitter(FVECT vres, BSDFDAT *ndp, int domax) |
86 |
> |
bsdf_jitter(FVECT vres, BSDFDAT *ndp, double sr_psa) |
87 |
|
{ |
87 |
– |
double sr_psa = ndp->sr_vpsa[domax]; |
88 |
– |
|
88 |
|
VCOPY(vres, ndp->vray); |
89 |
|
if (specjitter < 1.) |
90 |
|
sr_psa *= specjitter; |
99 |
|
static int |
100 |
|
direct_bsdf_OK(COLOR cval, FVECT ldir, double omega, BSDFDAT *ndp) |
101 |
|
{ |
102 |
< |
int nsamp = 1, ok = 0; |
102 |
> |
int nsamp, ok = 0; |
103 |
|
FVECT vsrc, vsmp, vjit; |
104 |
|
double tomega; |
105 |
< |
double sf, sd[2]; |
105 |
> |
double sf, tsr, sd[2]; |
106 |
|
COLOR csmp; |
107 |
|
SDValue sv; |
108 |
|
SDError ec; |
110 |
|
/* transform source direction */ |
111 |
|
if (SDmapDir(vsrc, ndp->toloc, ldir) != SDEnone) |
112 |
|
return(0); |
113 |
+ |
/* assign number of samples */ |
114 |
+ |
ec = SDsizeBSDF(&tomega, ndp->vray, vsrc, SDqueryMin, ndp->sd); |
115 |
+ |
if (ec) |
116 |
+ |
goto baderror; |
117 |
|
/* check indirect over-counting */ |
118 |
|
if (ndp->thick != 0 && ndp->pr->crtype & (SPECULAR|AMBIENT) |
119 |
|
&& vsrc[2] > 0 ^ ndp->vray[2] > 0) { |
120 |
|
double dx = vsrc[0] + ndp->vray[0]; |
121 |
|
double dy = vsrc[1] + ndp->vray[1]; |
122 |
< |
if (dx*dx + dy*dy <= omega*(1./PI)) |
122 |
> |
if (dx*dx + dy*dy <= omega+tomega) |
123 |
|
return(0); |
124 |
|
} |
125 |
< |
if (specjitter > FTINY) { /* assign number of samples */ |
126 |
< |
ec = SDsizeBSDF(&tomega, ndp->vray, vsrc, SDqueryMin, ndp->sd); |
127 |
< |
if (ec) |
128 |
< |
goto baderror; |
129 |
< |
sf = specjitter * ndp->pr->rweight; |
130 |
< |
if (tomega <= omega*.01) |
131 |
< |
nsamp = 100.*sf + .5; |
132 |
< |
else |
130 |
< |
nsamp = 4.*sf*omega/tomega + .5; |
131 |
< |
nsamp += !nsamp; |
132 |
< |
} |
133 |
< |
sf = sqrt(omega); |
125 |
> |
sf = specjitter * ndp->pr->rweight; |
126 |
> |
if (tomega <= .0) |
127 |
> |
nsamp = 1; |
128 |
> |
else if (25.*tomega <= omega) |
129 |
> |
nsamp = 100.*sf + .5; |
130 |
> |
else |
131 |
> |
nsamp = 4.*sf*omega/tomega + .5; |
132 |
> |
nsamp += !nsamp; |
133 |
|
setcolor(cval, .0, .0, .0); /* sample our source area */ |
134 |
+ |
sf = sqrt(omega); |
135 |
+ |
tsr = sqrt(tomega); |
136 |
|
for (i = nsamp; i--; ) { |
137 |
|
VCOPY(vsmp, vsrc); /* jitter query directions */ |
138 |
|
if (nsamp > 1) { |
144 |
|
continue; |
145 |
|
} |
146 |
|
} |
147 |
< |
bsdf_jitter(vjit, ndp, 0); |
147 |
> |
bsdf_jitter(vjit, ndp, tsr); |
148 |
|
/* compute BSDF */ |
149 |
|
ec = SDevalBSDF(&sv, vjit, vsmp, ndp->sd); |
150 |
|
if (ec) |
160 |
|
return(ok); |
161 |
|
baderror: |
162 |
|
objerror(ndp->mp, USER, transSDError(ec)); |
163 |
+ |
return(0); /* gratis return */ |
164 |
|
} |
165 |
|
|
166 |
|
/* Compute source contribution for BSDF (reflected & transmitted) */ |
335 |
|
} |
336 |
|
/* run through our samples */ |
337 |
|
for (nsent = 0; nsent < nstarget; nsent++) { |
338 |
< |
if (nstarget == 1) /* stratify random variable */ |
338 |
> |
if (nstarget == 1) { /* stratify random variable */ |
339 |
|
xrand = urand(ilhash(dimlist,ndims)+samplendx); |
340 |
< |
else |
340 |
> |
if (specjitter < 1.) |
341 |
> |
xrand = .5 + specjitter*(xrand-.5); |
342 |
> |
} else { |
343 |
|
xrand = (nsent + frandom())/(double)nstarget; |
344 |
+ |
} |
345 |
|
SDerrorDetail[0] = '\0'; /* sample direction & coef. */ |
346 |
< |
bsdf_jitter(vsmp, ndp, 0); |
346 |
> |
bsdf_jitter(vsmp, ndp, ndp->sr_vpsa[0]); |
347 |
|
ec = SDsampComponent(&bsv, vsmp, xrand, dcp); |
348 |
|
if (ec) |
349 |
|
objerror(ndp->mp, USER, transSDError(ec)); |
383 |
|
|
384 |
|
if (sflags == SDsampSpT) { |
385 |
|
unsc = ndp->tunsamp; |
386 |
< |
dfp = ndp->sd->tf; |
386 |
> |
if (ndp->pr->rod > 0) |
387 |
> |
dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb; |
388 |
> |
else |
389 |
> |
dfp = (ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf; |
390 |
|
cvt_sdcolor(unsc, &ndp->sd->tLamb); |
391 |
|
} else /* sflags == SDsampSpR */ { |
392 |
|
unsc = ndp->runsamp; |
407 |
|
FVECT vjit; |
408 |
|
double d; |
409 |
|
COLOR ctmp; |
410 |
< |
bsdf_jitter(vjit, ndp, 1); |
410 |
> |
bsdf_jitter(vjit, ndp, ndp->sr_vpsa[1]); |
411 |
|
d = SDdirectHemi(vjit, sflags, ndp->sd); |
412 |
|
if (sflags == SDsampSpT) { |
413 |
|
copycolor(ctmp, ndp->pr->pcol); |
447 |
|
hitfront = (r->rod > 0); |
448 |
|
/* load cal file */ |
449 |
|
mf = getfunc(m, 5, 0x1d, 1); |
450 |
+ |
setfunc(m, r); |
451 |
|
/* get thickness */ |
452 |
|
nd.thick = evalue(mf->ep[0]); |
453 |
|
if ((-FTINY <= nd.thick) & (nd.thick <= FTINY)) |
458 |
|
raytrans(r); /* pass-through */ |
459 |
|
return(1); /* or shadow */ |
460 |
|
} |
461 |
+ |
/* check backface visibility */ |
462 |
+ |
if (!hitfront & !backvis) { |
463 |
+ |
raytrans(r); |
464 |
+ |
return(1); |
465 |
+ |
} |
466 |
|
/* check other rays to pass */ |
467 |
|
if (nd.thick != 0 && (!(r->crtype & (SPECULAR|AMBIENT)) || |
468 |
|
nd.thick > 0 ^ hitfront)) { |
469 |
|
raytrans(r); /* hide our proxy */ |
470 |
|
return(1); |
471 |
|
} |
472 |
+ |
|
473 |
+ |
/* PMAP: skip ambient ray if accounted for by photon map */ |
474 |
+ |
if (ambRayInPmap(r)) |
475 |
+ |
return(1); |
476 |
+ |
|
477 |
|
/* get BSDF data */ |
478 |
|
nd.sd = loadBSDF(m->oargs.sarg[1]); |
479 |
|
/* diffuse reflectance */ |
485 |
|
m->oargs.farg[1], |
486 |
|
m->oargs.farg[2]); |
487 |
|
} else { |
488 |
< |
if (m->oargs.nfargs < 6) { /* check invisible backside */ |
470 |
< |
if (!backvis && (nd.sd->rb == NULL) & |
471 |
< |
(nd.sd->tf == NULL)) { |
472 |
< |
SDfreeCache(nd.sd); |
473 |
< |
raytrans(r); |
474 |
< |
return(1); |
475 |
< |
} |
488 |
> |
if (m->oargs.nfargs < 6) |
489 |
|
setcolor(nd.rdiff, .0, .0, .0); |
490 |
< |
} else |
490 |
> |
else |
491 |
|
setcolor(nd.rdiff, m->oargs.farg[3], |
492 |
|
m->oargs.farg[4], |
493 |
|
m->oargs.farg[5]); |
511 |
|
upvec[1] = evalue(mf->ep[2]); |
512 |
|
upvec[2] = evalue(mf->ep[3]); |
513 |
|
/* return to world coords */ |
514 |
< |
if (mf->f != &unitxf) { |
515 |
< |
multv3(upvec, upvec, mf->f->xfm); |
516 |
< |
nd.thick *= mf->f->sca; |
514 |
> |
if (mf->fxp != &unitxf) { |
515 |
> |
multv3(upvec, upvec, mf->fxp->xfm); |
516 |
> |
nd.thick *= mf->fxp->sca; |
517 |
|
} |
518 |
+ |
if (r->rox != NULL) { |
519 |
+ |
multv3(upvec, upvec, r->rox->f.xfm); |
520 |
+ |
nd.thick *= r->rox->f.sca; |
521 |
+ |
} |
522 |
|
raynormal(nd.pnorm, r); |
523 |
|
/* compute local BSDF xform */ |
524 |
|
ec = SDcompXform(nd.toloc, nd.pnorm, upvec); |
530 |
|
} |
531 |
|
if (!ec) |
532 |
|
ec = SDinvXform(nd.fromloc, nd.toloc); |
516 |
– |
/* determine BSDF resolution */ |
517 |
– |
if (!ec) |
518 |
– |
ec = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL, |
519 |
– |
SDqueryMin+SDqueryMax, nd.sd); |
533 |
|
if (ec) { |
534 |
< |
objerror(m, WARNING, transSDError(ec)); |
522 |
< |
SDfreeCache(nd.sd); |
534 |
> |
objerror(m, WARNING, "Illegal orientation vector"); |
535 |
|
return(1); |
536 |
|
} |
537 |
+ |
/* determine BSDF resolution */ |
538 |
+ |
ec = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL, SDqueryMin+SDqueryMax, nd.sd); |
539 |
+ |
if (ec) |
540 |
+ |
objerror(m, USER, transSDError(ec)); |
541 |
+ |
|
542 |
|
nd.sr_vpsa[0] = sqrt(nd.sr_vpsa[0]); |
543 |
|
nd.sr_vpsa[1] = sqrt(nd.sr_vpsa[1]); |
544 |
|
if (!hitfront) { /* perturb normal towards hit */ |
572 |
|
bnorm[2] = -nd.pnorm[2]; |
573 |
|
if (nd.thick != 0) { /* proxy with offset? */ |
574 |
|
VCOPY(vtmp, r->rop); |
575 |
< |
VSUM(r->rop, vtmp, r->ron, -nd.thick); |
575 |
> |
VSUM(r->rop, vtmp, r->ron, nd.thick); |
576 |
|
multambient(ctmp, r, bnorm); |
577 |
|
VCOPY(r->rop, vtmp); |
578 |
|
} else |
582 |
|
flipsurface(r); |
583 |
|
} |
584 |
|
/* add direct component */ |
585 |
< |
if ((bright(nd.tdiff) <= FTINY) & (nd.sd->tf == NULL)) { |
585 |
> |
if ((bright(nd.tdiff) <= FTINY) & (nd.sd->tf == NULL) & |
586 |
> |
(nd.sd->tb == NULL)) { |
587 |
|
direct(r, dir_brdf, &nd); /* reflection only */ |
588 |
|
} else if (nd.thick == 0) { |
589 |
|
direct(r, dir_bsdf, &nd); /* thin surface scattering */ |