79 |
|
COLOR cthru; /* "through" component multiplier */ |
80 |
|
SDData *sd; /* loaded BSDF data */ |
81 |
|
COLOR rdiff; /* diffuse reflection */ |
82 |
+ |
COLOR runsamp; /* BSDF hemispherical reflection */ |
83 |
|
COLOR tdiff; /* diffuse transmission */ |
84 |
+ |
COLOR tunsamp; /* BSDF hemispherical transmission */ |
85 |
|
} BSDFDAT; /* BSDF material data */ |
86 |
|
|
87 |
|
#define cvt_sdcolor(cv, svp) ccy2rgb(&(svp)->spec, (svp)->cieY, cv) |
116 |
|
|
117 |
|
setcolor(ndp->cthru, .0, .0, .0); /* starting assumption */ |
118 |
|
|
119 |
+ |
if (!(ndp->pr->crtype & (SPECULAR|AMBIENT|SHADOW))) |
120 |
+ |
return; /* simply don't need to know */ |
121 |
+ |
|
122 |
|
if (ndp->pr->rod > 0) |
123 |
|
dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb; |
124 |
|
else |
139 |
|
tdir[0] = -ndp->vray[0] + dir2check[i][0]*srchrad; |
140 |
|
tdir[1] = -ndp->vray[1] + dir2check[i][1]*srchrad; |
141 |
|
tdir[2] = -ndp->vray[2]; |
142 |
< |
if (normalize(tdir) == 0) |
138 |
< |
continue; |
142 |
> |
normalize(tdir); |
143 |
|
ec = SDevalBSDF(&sv, tdir, ndp->vray, ndp->sd); |
144 |
|
if (ec) |
145 |
|
goto baderror; |
192 |
|
{ |
193 |
|
int nsamp, ok = 0; |
194 |
|
FVECT vsrc, vsmp, vjit; |
195 |
< |
double tomega; |
195 |
> |
double tomega, tomega2; |
196 |
|
double sf, tsr, sd[2]; |
197 |
|
COLOR csmp, cdiff; |
198 |
|
double diffY; |
199 |
|
SDValue sv; |
200 |
|
SDError ec; |
201 |
|
int i; |
202 |
+ |
/* in case we fail */ |
203 |
+ |
setcolor(cval, .0, .0, .0); |
204 |
|
/* transform source direction */ |
205 |
|
if (SDmapDir(vsrc, ndp->toloc, ldir) != SDEnone) |
206 |
|
return(0); |
229 |
|
diffY = .0; |
230 |
|
setcolor(cdiff, .0, .0, .0); |
231 |
|
} |
232 |
< |
/* assign number of samples */ |
232 |
> |
/* need projected solid angles */ |
233 |
> |
omega *= fabs(vsrc[2]); |
234 |
|
ec = SDsizeBSDF(&tomega, ndp->vray, vsrc, SDqueryMin, ndp->sd); |
235 |
|
if (ec) |
236 |
|
goto baderror; |
237 |
|
/* check indirect over-counting */ |
238 |
< |
if ((ndp->thick != 0 || bright(ndp->cthru) > FTINY) |
239 |
< |
&& ndp->pr->crtype & (SPECULAR|AMBIENT) |
240 |
< |
&& (vsrc[2] > 0) ^ (ndp->vray[2] > 0)) { |
238 |
> |
if (ndp->pr->crtype & (SPECULAR|AMBIENT) |
239 |
> |
&& (vsrc[2] > 0) ^ (ndp->vray[2] > 0) |
240 |
> |
&& bright(ndp->cthru) > FTINY) { |
241 |
|
double dx = vsrc[0] + ndp->vray[0]; |
242 |
|
double dy = vsrc[1] + ndp->vray[1]; |
243 |
< |
if (dx*dx + dy*dy <= omega+tomega) |
243 |
> |
if (dx*dx + dy*dy <= (4./PI)*(omega + tomega + |
244 |
> |
2.*sqrt(omega*tomega))) |
245 |
|
return(0); |
246 |
|
} |
247 |
+ |
/* assign number of samples */ |
248 |
|
sf = specjitter * ndp->pr->rweight; |
249 |
|
if (tomega <= .0) |
250 |
|
nsamp = 1; |
253 |
|
else |
254 |
|
nsamp = 4.*sf*omega/tomega + .5; |
255 |
|
nsamp += !nsamp; |
256 |
< |
setcolor(cval, .0, .0, .0); /* sample our source area */ |
248 |
< |
sf = sqrt(omega); |
256 |
> |
sf = sqrt(omega); /* sample our source area */ |
257 |
|
tsr = sqrt(tomega); |
258 |
|
for (i = nsamp; i--; ) { |
259 |
|
VCOPY(vsmp, vsrc); /* jitter query directions */ |
261 |
|
multisamp(sd, 2, (i + frandom())/(double)nsamp); |
262 |
|
vsmp[0] += (sd[0] - .5)*sf; |
263 |
|
vsmp[1] += (sd[1] - .5)*sf; |
264 |
< |
if (normalize(vsmp) == 0) { |
257 |
< |
--nsamp; |
258 |
< |
continue; |
259 |
< |
} |
264 |
> |
normalize(vsmp); |
265 |
|
} |
266 |
|
bsdf_jitter(vjit, ndp, tsr); |
267 |
|
/* compute BSDF */ |
268 |
|
ec = SDevalBSDF(&sv, vjit, vsmp, ndp->sd); |
269 |
|
if (ec) |
270 |
|
goto baderror; |
271 |
< |
if (sv.cieY - diffY <= FTINY) { |
267 |
< |
addcolor(cval, cdiff); |
271 |
> |
if (sv.cieY - diffY <= FTINY) |
272 |
|
continue; /* no specular part */ |
273 |
< |
} |
273 |
> |
/* check for variable resolution */ |
274 |
> |
ec = SDsizeBSDF(&tomega2, vjit, vsmp, SDqueryMin, ndp->sd); |
275 |
> |
if (ec) |
276 |
> |
goto baderror; |
277 |
> |
if (tomega2 < .12*tomega) |
278 |
> |
continue; /* not safe to include */ |
279 |
|
cvt_sdcolor(csmp, &sv); |
280 |
|
addcolor(cval, csmp); /* else average it in */ |
281 |
|
++ok; |
282 |
|
} |
283 |
< |
if (!ok) { |
284 |
< |
setcolor(cval, .0, .0, .0); |
285 |
< |
return(0); /* no valid specular samples */ |
286 |
< |
} |
278 |
< |
sf = 1./(double)nsamp; |
283 |
> |
if (!ok) /* no valid specular samples? */ |
284 |
> |
return(0); |
285 |
> |
|
286 |
> |
sf = 1./(double)ok; /* compute average BSDF */ |
287 |
|
scalecolor(cval, sf); |
288 |
|
/* subtract diffuse contribution */ |
289 |
|
for (i = 3*(diffY > FTINY); i--; ) |
317 |
|
|
318 |
|
if (ldot > 0 && bright(np->rdiff) > FTINY) { |
319 |
|
/* |
320 |
< |
* Compute added diffuse reflected component. |
320 |
> |
* Compute diffuse reflected component |
321 |
|
*/ |
322 |
|
copycolor(ctmp, np->rdiff); |
323 |
|
dtmp = ldot * omega * (1./PI); |
326 |
|
} |
327 |
|
if (ldot < 0 && bright(np->tdiff) > FTINY) { |
328 |
|
/* |
329 |
< |
* Compute added diffuse transmission. |
329 |
> |
* Compute diffuse transmission |
330 |
|
*/ |
331 |
|
copycolor(ctmp, np->tdiff); |
332 |
|
dtmp = -ldot * omega * (1.0/PI); |
336 |
|
if (ambRayInPmap(np->pr)) |
337 |
|
return; /* specular already in photon map */ |
338 |
|
/* |
339 |
< |
* Compute specular scattering coefficient using BSDF. |
339 |
> |
* Compute specular scattering coefficient using BSDF |
340 |
|
*/ |
341 |
|
if (!direct_specular_OK(ctmp, ldir, omega, np)) |
342 |
|
return; |
372 |
|
|
373 |
|
if (bright(np->rdiff) > FTINY) { |
374 |
|
/* |
375 |
< |
* Compute added diffuse reflected component. |
375 |
> |
* Compute diffuse reflected component |
376 |
|
*/ |
377 |
|
copycolor(ctmp, np->rdiff); |
378 |
|
dtmp = ldot * omega * (1./PI); |
382 |
|
if (ambRayInPmap(np->pr)) |
383 |
|
return; /* specular already in photon map */ |
384 |
|
/* |
385 |
< |
* Compute specular reflection coefficient using BSDF. |
385 |
> |
* Compute specular reflection coefficient using BSDF |
386 |
|
*/ |
387 |
|
if (!direct_specular_OK(ctmp, ldir, omega, np)) |
388 |
|
return; |
414 |
|
|
415 |
|
if (bright(np->tdiff) > FTINY) { |
416 |
|
/* |
417 |
< |
* Compute added diffuse transmission. |
417 |
> |
* Compute diffuse transmission |
418 |
|
*/ |
419 |
|
copycolor(ctmp, np->tdiff); |
420 |
|
dtmp = -ldot * omega * (1.0/PI); |
424 |
|
if (ambRayInPmap(np->pr)) |
425 |
|
return; /* specular already in photon map */ |
426 |
|
/* |
427 |
< |
* Compute specular scattering coefficient using BSDF. |
427 |
> |
* Compute specular scattering coefficient using BSDF |
428 |
|
*/ |
429 |
|
if (!direct_specular_OK(ctmp, ldir, omega, np)) |
430 |
|
return; |
500 |
|
COLORV *unsc; |
501 |
|
|
502 |
|
if (sflags == SDsampSpT) { |
503 |
< |
unsc = ndp->tdiff; |
503 |
> |
unsc = ndp->tunsamp; |
504 |
|
if (ndp->pr->rod > 0) |
505 |
|
dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb; |
506 |
|
else |
507 |
|
dfp = (ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf; |
508 |
|
} else /* sflags == SDsampSpR */ { |
509 |
< |
unsc = ndp->rdiff; |
509 |
> |
unsc = ndp->runsamp; |
510 |
|
if (ndp->pr->rod > 0) |
511 |
|
dfp = ndp->sd->rf; |
512 |
|
else |
513 |
|
dfp = ndp->sd->rb; |
514 |
|
} |
515 |
+ |
setcolor(unsc, 0., 0., 0.); |
516 |
|
if (dfp == NULL) /* no specular component? */ |
517 |
|
return(0); |
518 |
|
/* below sampling threshold? */ |
520 |
|
if (dfp->maxHemi > FTINY) { /* XXX no color from BSDF */ |
521 |
|
FVECT vjit; |
522 |
|
double d; |
514 |
– |
COLOR ctmp; |
523 |
|
bsdf_jitter(vjit, ndp, ndp->sr_vpsa[1]); |
524 |
|
d = SDdirectHemi(vjit, sflags, ndp->sd); |
525 |
|
if (sflags == SDsampSpT) { |
526 |
< |
copycolor(ctmp, ndp->pr->pcol); |
527 |
< |
scalecolor(ctmp, d); |
526 |
> |
copycolor(unsc, ndp->pr->pcol); |
527 |
> |
scalecolor(unsc, d); |
528 |
|
} else /* no pattern on reflection */ |
529 |
< |
setcolor(ctmp, d, d, d); |
522 |
< |
addcolor(unsc, ctmp); |
529 |
> |
setcolor(unsc, d, d, d); |
530 |
|
} |
531 |
|
return(0); |
532 |
|
} |
670 |
|
/* sample transmission */ |
671 |
|
sample_sdf(&nd, SDsampSpT); |
672 |
|
/* compute indirect diffuse */ |
673 |
< |
if (bright(nd.rdiff) > FTINY) { /* ambient from reflection */ |
673 |
> |
copycolor(ctmp, nd.rdiff); |
674 |
> |
addcolor(ctmp, nd.runsamp); |
675 |
> |
if (bright(ctmp) > FTINY) { /* ambient from reflection */ |
676 |
|
if (!hitfront) |
677 |
|
flipsurface(r); |
669 |
– |
copycolor(ctmp, nd.rdiff); |
678 |
|
multambient(ctmp, r, nd.pnorm); |
679 |
|
addcolor(r->rcol, ctmp); |
680 |
|
if (!hitfront) |
681 |
|
flipsurface(r); |
682 |
|
} |
683 |
< |
if (bright(nd.tdiff) > FTINY) { /* ambient from other side */ |
683 |
> |
copycolor(ctmp, nd.tdiff); |
684 |
> |
addcolor(ctmp, nd.tunsamp); |
685 |
> |
if (bright(ctmp) > FTINY) { /* ambient from other side */ |
686 |
|
FVECT bnorm; |
687 |
|
if (hitfront) |
688 |
|
flipsurface(r); |
689 |
|
bnorm[0] = -nd.pnorm[0]; |
690 |
|
bnorm[1] = -nd.pnorm[1]; |
691 |
|
bnorm[2] = -nd.pnorm[2]; |
682 |
– |
copycolor(ctmp, nd.tdiff); |
692 |
|
if (nd.thick != 0) { /* proxy with offset? */ |
693 |
|
VCOPY(vtmp, r->rop); |
694 |
|
VSUM(r->rop, vtmp, r->ron, nd.thick); |