--- ray/src/rt/m_bsdf.c 2017/05/16 02:52:15 2.35 +++ ray/src/rt/m_bsdf.c 2021/08/25 16:12:21 2.64 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: m_bsdf.c,v 2.35 2017/05/16 02:52:15 greg Exp $"; +static const char RCSid[] = "$Id: m_bsdf.c,v 2.64 2021/08/25 16:12:21 greg Exp $"; #endif /* * Shading for materials with BSDFs taken from XML data files @@ -8,6 +8,7 @@ static const char RCSid[] = "$Id: m_bsdf.c,v 2.35 2017 #include "copyright.h" #include "ray.h" +#include "otypes.h" #include "ambient.h" #include "source.h" #include "func.h" @@ -16,9 +17,9 @@ static const char RCSid[] = "$Id: m_bsdf.c,v 2.35 2017 #include "pmapmat.h" /* - * Arguments to this material include optional diffuse colors. + * Arguments to this material include optional diffuse colors. * String arguments include the BSDF and function files. - * A non-zero thickness causes the strange but useful behavior + * For the MAT_BSDF type, a non-zero thickness causes the useful behavior * of translating transmitted rays this distance beneath the surface * (opposite the surface normal) to bypass any intervening geometry. * Translation only affects scattered, non-source-directed samples. @@ -35,11 +36,13 @@ static const char RCSid[] = "$Id: m_bsdf.c,v 2.35 2017 * hides geometry in front of the surface when rays hit from behind, * and applies only the transmission and backside reflectance properties. * Reflection is ignored on the hidden side, as those rays pass through. - * When thickness is set to zero, shadow rays will be blocked unless - * a BTDF has a strong "through" component in the source direction. - * A separate test prevents over-counting by dropping specular & ambient - * samples that are too close to this "through" direction. The same - * restriction applies for the proxy case (thickness != 0). + * For the MAT_ABSDF type, we check for a strong "through" component. + * Such a component will cause direct rays to pass through unscattered. + * A separate test prevents over-counting by dropping samples that are + * too close to this "through" direction. BSDFs with such a through direction + * will also have a view component, meaning they are somewhat see-through. + * A MAT_BSDF type with zero thickness behaves the same as a MAT_ABSDF + * type with no strong through component. * The "up" vector for the BSDF is given by three variables, defined * (along with the thickness) by the named function file, or '.' if none. * Together with the surface normal, this defines the local coordinate @@ -52,6 +55,13 @@ static const char RCSid[] = "$Id: m_bsdf.c,v 2.35 2017 * not multiplied. However, patterns affect this material as a multiplier * on everything except non-diffuse reflection. * + * Arguments for MAT_ABSDF are: + * 5+ BSDFfile ux uy uz funcfile transform + * 0 + * 0|3|6|9 rdf gdf bdf + * rdb gdb bdb + * rdt gdt bdt + * * Arguments for MAT_BSDF are: * 6+ thick BSDFfile ux uy uz funcfile transform * 0 @@ -76,44 +86,64 @@ typedef struct { RREAL toloc[3][3]; /* world to local BSDF coords */ RREAL fromloc[3][3]; /* local BSDF coords to world */ double thick; /* surface thickness */ - COLOR cthru; /* "through" component multiplier */ + COLOR cthru; /* "through" component for MAT_ABSDF */ + COLOR cthru_surr; /* surround for "through" component */ SDData *sd; /* loaded BSDF data */ COLOR rdiff; /* diffuse reflection */ + COLOR runsamp; /* BSDF hemispherical reflection */ COLOR tdiff; /* diffuse transmission */ + COLOR tunsamp; /* BSDF hemispherical transmission */ } BSDFDAT; /* BSDF material data */ #define cvt_sdcolor(cv, svp) ccy2rgb(&(svp)->spec, (svp)->cieY, cv) -/* Compute "through" component color */ +typedef struct { + double vy; /* brightness (for sorting) */ + FVECT tdir; /* through sample direction (normalized) */ + COLOR vcol; /* BTDF color */ +} PEAKSAMP; /* BTDF peak sample */ + +/* Comparison function to put near-peak values in descending order */ +static int +cmp_psamp(const void *p1, const void *p2) +{ + double diff = (*(const PEAKSAMP *)p1).vy - (*(const PEAKSAMP *)p2).vy; + if (diff > 0) return(-1); + if (diff < 0) return(1); + return(0); +} + +/* Compute "through" component color for MAT_ABSDF */ static void compute_through(BSDFDAT *ndp) { -#define NDIR2CHECK 13 +#define NDIR2CHECK 29 static const float dir2check[NDIR2CHECK][2] = { - {0, 0}, - {-0.8, 0}, - {0, 0.8}, - {0, -0.8}, - {0.8, 0}, - {-0.8, 0.8}, - {-0.8, -0.8}, - {0.8, 0.8}, - {0.8, -0.8}, - {-1.6, 0}, - {0, 1.6}, - {0, -1.6}, - {1.6, 0}, + {0, 0}, {-0.6, 0}, {0, 0.6}, + {0, -0.6}, {0.6, 0}, {-0.6, 0.6}, + {-0.6, -0.6}, {0.6, 0.6}, {0.6, -0.6}, + {-1.2, 0}, {0, 1.2}, {0, -1.2}, + {1.2, 0}, {-1.2, 1.2}, {-1.2, -1.2}, + {1.2, 1.2}, {1.2, -1.2}, {-1.8, 0}, + {0, 1.8}, {0, -1.8}, {1.8, 0}, + {-1.8, 1.8}, {-1.8, -1.8}, {1.8, 1.8}, + {1.8, -1.8}, {-2.4, 0}, {0, 2.4}, + {0, -2.4}, {2.4, 0}, }; - const double peak_over = 2.0; +#define neighbors(i,j) \ + ((dir2check[i][0]-dir2check[j][0])*(dir2check[i][0]-dir2check[j][0]) + \ + (dir2check[i][1]-dir2check[j][1])*(dir2check[i][1]-dir2check[j][1]) <= 0.73) + const double peak_over = 1.5; + PEAKSAMP psamp[NDIR2CHECK]; SDSpectralDF *dfp; FVECT pdir; double tomega, srchrad; - COLOR vpeak, vsum; - int nsum, i; + double tomsum, tomsurr; + COLOR vpeak, vsurr; + double vypeak; + int i, j, ns; SDError ec; - setcolor(ndp->cthru, .0, .0, .0); /* starting assumption */ - if (ndp->pr->rod > 0) dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb; else @@ -123,48 +153,68 @@ compute_through(BSDFDAT *ndp) return; /* no specular transmission */ if (bright(ndp->pr->pcol) <= FTINY) return; /* pattern is black, here */ - srchrad = sqrt(dfp->minProjSA); /* else search for peak */ - setcolor(vpeak, .0, .0, .0); - setcolor(vsum, .0, .0, .0); - nsum = 0; + srchrad = sqrt(dfp->minProjSA); /* else evaluate peak */ for (i = 0; i < NDIR2CHECK; i++) { - FVECT tdir; SDValue sv; - COLOR vcol; - tdir[0] = -ndp->vray[0] + dir2check[i][0]*srchrad; - tdir[1] = -ndp->vray[1] + dir2check[i][1]*srchrad; - tdir[2] = -ndp->vray[2]; - if (normalize(tdir) == 0) - continue; - ec = SDevalBSDF(&sv, tdir, ndp->vray, ndp->sd); + psamp[i].tdir[0] = -ndp->vray[0] + dir2check[i][0]*srchrad; + psamp[i].tdir[1] = -ndp->vray[1] + dir2check[i][1]*srchrad; + psamp[i].tdir[2] = -ndp->vray[2]; + normalize(psamp[i].tdir); + ec = SDevalBSDF(&sv, psamp[i].tdir, ndp->vray, ndp->sd); if (ec) goto baderror; - cvt_sdcolor(vcol, &sv); - addcolor(vsum, vcol); - ++nsum; - if (bright(vcol) > bright(vpeak)) { - copycolor(vpeak, vcol); - VCOPY(pdir, tdir); + cvt_sdcolor(psamp[i].vcol, &sv); + psamp[i].vy = sv.cieY; + } + qsort(psamp, NDIR2CHECK, sizeof(PEAKSAMP), cmp_psamp); + if (psamp[0].vy <= FTINY) + return; /* zero area */ + setcolor(vpeak, 0, 0, 0); + setcolor(vsurr, 0, 0, 0); + vypeak = tomsum = tomsurr = 0; /* combine top unique values */ + ns = 0; + for (i = 0; i < NDIR2CHECK; i++) { + for (j = i; j--; ) /* check for duplicate sample */ + if (psamp[j].vy == psamp[i].vy && neighbors(i,j)) + break; + if (j >= 0) + continue; /* skip duplicate */ + + ec = SDsizeBSDF(&tomega, psamp[i].tdir, ndp->vray, + SDqueryMin, ndp->sd); + if (ec) + goto baderror; + /* not really a peak? */ + if (tomega > 1.5*dfp->minProjSA || + vypeak > 8.*psamp[i].vy*ns) { + if (!i) return; /* abort */ + scalecolor(psamp[i].vcol, tomega); + addcolor(vsurr, psamp[i].vcol); + tomsurr += tomega; + continue; } + scalecolor(psamp[i].vcol, tomega); + addcolor(vpeak, psamp[i].vcol); + tomsum += tomega; + vypeak += psamp[i].vy; + ++ns; } - ec = SDsizeBSDF(&tomega, pdir, ndp->vray, SDqueryMin, ndp->sd); - if (ec) - goto baderror; - if (tomega > 1.5*dfp->minProjSA) - return; /* not really a peak? */ - if ((bright(vpeak) - ndp->sd->tLamb.cieY*(1./PI))*tomega <= .007) - return; /* < 0.7% transmission */ - for (i = 3; i--; ) /* remove peak from average */ - colval(vsum,i) -= colval(vpeak,i); - --nsum; - if (peak_over*bright(vsum) >= nsum*bright(vpeak)) - return; /* not peaky enough */ - copycolor(ndp->cthru, vpeak); /* else use it */ - scalecolor(ndp->cthru, tomega); + if (vypeak*tomsurr < peak_over*bright(vsurr)*ns) + return; /* peak not peaky enough */ + if ((vypeak/ns - (ndp->vray[2] > 0 ? ndp->sd->tLambFront.cieY + : ndp->sd->tLambBack.cieY)*(1./PI))*tomsum <= .001) + return; /* < 0.1% transmission */ + copycolor(ndp->cthru, vpeak); /* already scaled by omega */ multcolor(ndp->cthru, ndp->pr->pcol); /* modify by pattern */ + if (tomsurr > FTINY) { /* surround contribution? */ + scalecolor(vsurr, 1./tomsurr); /* this one is avg. BTDF */ + copycolor(ndp->cthru_surr, vsurr); + multcolor(ndp->cthru_surr, ndp->pr->pcol); + } return; baderror: objerror(ndp->mp, USER, transSDError(ec)); +#undef neighbors #undef NDIR2CHECK } @@ -186,15 +236,18 @@ bsdf_jitter(FVECT vres, BSDFDAT *ndp, double sr_psa) static int direct_specular_OK(COLOR cval, FVECT ldir, double omega, BSDFDAT *ndp) { - int nsamp, ok = 0; + int nsamp; + double wtot = 0; FVECT vsrc, vsmp, vjit; - double tomega; + double tomega, tomega2; double sf, tsr, sd[2]; COLOR csmp, cdiff; double diffY; SDValue sv; SDError ec; int i; + /* in case we fail */ + setcolor(cval, 0, 0, 0); /* transform source direction */ if (SDmapDir(vsrc, ndp->toloc, ldir) != SDEnone) return(0); @@ -210,42 +263,55 @@ direct_specular_OK(COLOR cval, FVECT ldir, double omeg return(0); /* all diffuse */ sv = ndp->sd->rLambBack; break; - default: + case 1: if ((ndp->sd->tf == NULL) & (ndp->sd->tb == NULL)) return(0); /* all diffuse */ - sv = ndp->sd->tLamb; + sv = ndp->sd->tLambFront; break; + case 2: + if ((ndp->sd->tf == NULL) & (ndp->sd->tb == NULL)) + return(0); /* all diffuse */ + sv = ndp->sd->tLambBack; + break; } if (sv.cieY > FTINY) { diffY = sv.cieY *= 1./PI; cvt_sdcolor(cdiff, &sv); } else { - diffY = .0; - setcolor(cdiff, .0, .0, .0); + diffY = 0; + setcolor(cdiff, 0, 0, 0); } - /* assign number of samples */ + /* need projected solid angle */ + omega *= fabs(vsrc[2]); + /* check indirect over-counting */ + if ((vsrc[2] > 0) ^ (ndp->vray[2] > 0) && bright(ndp->cthru) > FTINY) { + double dx = vsrc[0] + ndp->vray[0]; + double dy = vsrc[1] + ndp->vray[1]; + SDSpectralDF *dfp = (ndp->pr->rod > 0) ? + ((ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb) : + ((ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf) ; + + if (dx*dx + dy*dy <= (2.5*4./PI)*(omega + dfp->minProjSA + + 2.*sqrt(omega*dfp->minProjSA))) { + if (bright(ndp->cthru_surr) <= FTINY) + return(0); + copycolor(cval, ndp->cthru_surr); + return(1); /* return non-zero surround BTDF */ + } + } ec = SDsizeBSDF(&tomega, ndp->vray, vsrc, SDqueryMin, ndp->sd); if (ec) goto baderror; - /* check indirect over-counting */ - if ((ndp->thick != 0 || bright(ndp->cthru) > FTINY) - && ndp->pr->crtype & (SPECULAR|AMBIENT) - && (vsrc[2] > 0) ^ (ndp->vray[2] > 0)) { - double dx = vsrc[0] + ndp->vray[0]; - double dy = vsrc[1] + ndp->vray[1]; - if (dx*dx + dy*dy <= omega+tomega) - return(0); - } + /* assign number of samples */ sf = specjitter * ndp->pr->rweight; - if (tomega <= .0) + if (tomega <= 0) nsamp = 1; else if (25.*tomega <= omega) nsamp = 100.*sf + .5; else nsamp = 4.*sf*omega/tomega + .5; nsamp += !nsamp; - setcolor(cval, .0, .0, .0); /* sample our source area */ - sf = sqrt(omega); + sf = sqrt(omega); /* sample our source area */ tsr = sqrt(tomega); for (i = nsamp; i--; ) { VCOPY(vsmp, vsrc); /* jitter query directions */ @@ -253,34 +319,40 @@ direct_specular_OK(COLOR cval, FVECT ldir, double omeg multisamp(sd, 2, (i + frandom())/(double)nsamp); vsmp[0] += (sd[0] - .5)*sf; vsmp[1] += (sd[1] - .5)*sf; - if (normalize(vsmp) == 0) { - --nsamp; - continue; - } + normalize(vsmp); } bsdf_jitter(vjit, ndp, tsr); /* compute BSDF */ ec = SDevalBSDF(&sv, vjit, vsmp, ndp->sd); if (ec) goto baderror; - if (sv.cieY - diffY <= FTINY) { - addcolor(cval, cdiff); + if (sv.cieY - diffY <= FTINY) continue; /* no specular part */ - } + /* check for variable resolution */ + ec = SDsizeBSDF(&tomega2, vjit, vsmp, SDqueryMin, ndp->sd); + if (ec) + goto baderror; + if (tomega2 < .12*tomega) + continue; /* not safe to include */ cvt_sdcolor(csmp, &sv); - addcolor(cval, csmp); /* else average it in */ - ++ok; +#if 0 + if (sf < 2.5*tsr) { /* weight by BSDF for small sources */ + scalecolor(csmp, sv.cieY); + wtot += sv.cieY; + } else +#endif + wtot += 1.; + addcolor(cval, csmp); } - if (!ok) { - setcolor(cval, .0, .0, .0); - return(0); /* no valid specular samples */ - } - sf = 1./(double)nsamp; + if (wtot <= FTINY) /* no valid specular samples? */ + return(0); + + sf = 1./wtot; /* weighted average BSDF */ scalecolor(cval, sf); /* subtract diffuse contribution */ for (i = 3*(diffY > FTINY); i--; ) - if ((colval(cval,i) -= colval(cdiff,i)) < .0) - colval(cval,i) = .0; + if ((colval(cval,i) -= colval(cdiff,i)) < 0) + colval(cval,i) = 0; return(1); baderror: objerror(ndp->mp, USER, transSDError(ec)); @@ -301,7 +373,7 @@ dir_bsdf( double dtmp; COLOR ctmp; - setcolor(cval, .0, .0, .0); + setcolor(cval, 0, 0, 0); ldot = DOT(np->pnorm, ldir); if ((-FTINY <= ldot) & (ldot <= FTINY)) @@ -309,7 +381,7 @@ dir_bsdf( if (ldot > 0 && bright(np->rdiff) > FTINY) { /* - * Compute added diffuse reflected component. + * Compute diffuse reflected component */ copycolor(ctmp, np->rdiff); dtmp = ldot * omega * (1./PI); @@ -318,7 +390,7 @@ dir_bsdf( } if (ldot < 0 && bright(np->tdiff) > FTINY) { /* - * Compute added diffuse transmission. + * Compute diffuse transmission */ copycolor(ctmp, np->tdiff); dtmp = -ldot * omega * (1.0/PI); @@ -328,7 +400,7 @@ dir_bsdf( if (ambRayInPmap(np->pr)) return; /* specular already in photon map */ /* - * Compute specular scattering coefficient using BSDF. + * Compute specular scattering coefficient using BSDF */ if (!direct_specular_OK(ctmp, ldir, omega, np)) return; @@ -355,7 +427,7 @@ dir_brdf( double dtmp; COLOR ctmp, ctmp1, ctmp2; - setcolor(cval, .0, .0, .0); + setcolor(cval, 0, 0, 0); ldot = DOT(np->pnorm, ldir); @@ -364,7 +436,7 @@ dir_brdf( if (bright(np->rdiff) > FTINY) { /* - * Compute added diffuse reflected component. + * Compute diffuse reflected component */ copycolor(ctmp, np->rdiff); dtmp = ldot * omega * (1./PI); @@ -374,7 +446,7 @@ dir_brdf( if (ambRayInPmap(np->pr)) return; /* specular already in photon map */ /* - * Compute specular reflection coefficient using BSDF. + * Compute specular reflection coefficient using BSDF */ if (!direct_specular_OK(ctmp, ldir, omega, np)) return; @@ -397,7 +469,7 @@ dir_btdf( double dtmp; COLOR ctmp; - setcolor(cval, .0, .0, .0); + setcolor(cval, 0, 0, 0); ldot = DOT(np->pnorm, ldir); @@ -406,7 +478,7 @@ dir_btdf( if (bright(np->tdiff) > FTINY) { /* - * Compute added diffuse transmission. + * Compute diffuse transmission */ copycolor(ctmp, np->tdiff); dtmp = -ldot * omega * (1.0/PI); @@ -416,7 +488,7 @@ dir_btdf( if (ambRayInPmap(np->pr)) return; /* specular already in photon map */ /* - * Compute specular scattering coefficient using BSDF. + * Compute specular scattering coefficient using BSDF */ if (!direct_specular_OK(ctmp, ldir, omega, np)) return; @@ -429,56 +501,71 @@ dir_btdf( /* Sample separate BSDF component */ static int -sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usepat) +sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int xmit) { - int nstarget = 1; - int nsent; - SDError ec; - SDValue bsv; - double xrand; - FVECT vsmp; - RAY sr; + const int hasthru = (xmit && + !(ndp->pr->crtype & (SPECULAR|AMBIENT)) + && bright(ndp->cthru) > FTINY); + int nstarget = 1; + int nsent = 0; + int n; + SDError ec; + SDValue bsv; + double xrand; + FVECT vsmp, vinc; + RAY sr; /* multiple samples? */ if (specjitter > 1.5) { nstarget = specjitter*ndp->pr->rweight + .5; nstarget += !nstarget; } /* run through our samples */ - for (nsent = 0; nsent < nstarget; nsent++) { + for (n = 0; n < nstarget; n++) { if (nstarget == 1) { /* stratify random variable */ xrand = urand(ilhash(dimlist,ndims)+samplendx); if (specjitter < 1.) xrand = .5 + specjitter*(xrand-.5); } else { - xrand = (nsent + frandom())/(double)nstarget; + xrand = (n + frandom())/(double)nstarget; } SDerrorDetail[0] = '\0'; /* sample direction & coef. */ bsdf_jitter(vsmp, ndp, ndp->sr_vpsa[0]); + VCOPY(vinc, vsmp); /* to compare after */ ec = SDsampComponent(&bsv, vsmp, xrand, dcp); if (ec) objerror(ndp->mp, USER, transSDError(ec)); if (bsv.cieY <= FTINY) /* zero component? */ break; - /* map vector to world */ + if (hasthru) { /* check for view ray */ + double dx = vinc[0] + vsmp[0]; + double dy = vinc[1] + vsmp[1]; + if (dx*dx + dy*dy <= ndp->sr_vpsa[0]*ndp->sr_vpsa[0]) + continue; /* exclude view sample */ + } + /* map non-view sample->world */ if (SDmapDir(sr.rdir, ndp->fromloc, vsmp) != SDEnone) break; /* spawn a specular ray */ if (nstarget > 1) bsv.cieY /= (double)nstarget; cvt_sdcolor(sr.rcoef, &bsv); /* use sample color */ - if (usepat) /* apply pattern? */ + if (xmit) /* apply pattern on transmit */ multcolor(sr.rcoef, ndp->pr->pcol); if (rayorigin(&sr, SPECULAR, ndp->pr, sr.rcoef) < 0) { - if (maxdepth > 0) - break; - continue; /* Russian roulette victim */ + if (!n & (nstarget > 1)) { + n = nstarget; /* avoid infinitue loop */ + nstarget = nstarget*sr.rweight/minweight; + if (n == nstarget) break; + n = -1; /* moved target */ + } + continue; /* try again */ } - /* need to offset origin? */ - if (ndp->thick != 0 && (ndp->pr->rod > 0) ^ (vsmp[2] > 0)) + if (xmit && ndp->thick != 0) /* need to offset origin? */ VSUM(sr.rorg, sr.rorg, ndp->pr->ron, -ndp->thick); rayvalue(&sr); /* send & evaluate sample */ multcolor(sr.rcol, sr.rcoef); addcolor(ndp->pr->rcol, sr.rcol); + ++nsent; } return(nsent); } @@ -487,45 +574,64 @@ sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usep static int sample_sdf(BSDFDAT *ndp, int sflags) { + int hasthru = (sflags == SDsampSpT && + !(ndp->pr->crtype & (SPECULAR|AMBIENT)) + && bright(ndp->cthru) > FTINY); int n, ntotal = 0; + double b = 0; SDSpectralDF *dfp; COLORV *unsc; if (sflags == SDsampSpT) { - unsc = ndp->tdiff; + unsc = ndp->tunsamp; if (ndp->pr->rod > 0) dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb; else dfp = (ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf; } else /* sflags == SDsampSpR */ { - unsc = ndp->rdiff; + unsc = ndp->runsamp; if (ndp->pr->rod > 0) dfp = ndp->sd->rf; else dfp = ndp->sd->rb; } + setcolor(unsc, 0, 0, 0); if (dfp == NULL) /* no specular component? */ return(0); - /* below sampling threshold? */ - if (dfp->maxHemi <= specthresh+FTINY) { - if (dfp->maxHemi > FTINY) { /* XXX no color from BSDF */ - FVECT vjit; - double d; - COLOR ctmp; - bsdf_jitter(vjit, ndp, ndp->sr_vpsa[1]); - d = SDdirectHemi(vjit, sflags, ndp->sd); + + if (hasthru) { /* separate view sample? */ + RAY tr; + if (rayorigin(&tr, TRANS, ndp->pr, ndp->cthru) == 0) { + VCOPY(tr.rdir, ndp->pr->rdir); + rayvalue(&tr); + multcolor(tr.rcol, tr.rcoef); + addcolor(ndp->pr->rcol, tr.rcol); + ndp->pr->rxt = ndp->pr->rot + raydistance(&tr); + ++ntotal; + b = bright(ndp->cthru); + } else + hasthru = 0; + } + if (dfp->maxHemi - b <= FTINY) { /* have specular to sample? */ + b = 0; + } else { + FVECT vjit; + bsdf_jitter(vjit, ndp, ndp->sr_vpsa[1]); + b = SDdirectHemi(vjit, sflags, ndp->sd) - b; + if (b < 0) b = 0; + } + if (b <= specthresh+FTINY) { /* below sampling threshold? */ + if (b > FTINY) { /* XXX no color from BSDF */ if (sflags == SDsampSpT) { - copycolor(ctmp, ndp->pr->pcol); - scalecolor(ctmp, d); + copycolor(unsc, ndp->pr->pcol); + scalecolor(unsc, b); } else /* no pattern on reflection */ - setcolor(ctmp, d, d, d); - addcolor(unsc, ctmp); + setcolor(unsc, b, b, b); } - return(0); + return(ntotal); } - /* else need to sample */ - dimlist[ndims++] = (int)(size_t)ndp->mp; - ndims++; + dimlist[ndims] = (int)(size_t)ndp->mp; /* else sample specular */ + ndims += 2; for (n = dfp->ncomp; n--; ) { /* loop over components */ dimlist[ndims-1] = n + 9438; ntotal += sample_sdcomp(ndp, &dfp->comp[n], sflags==SDsampSpT); @@ -538,6 +644,7 @@ sample_sdf(BSDFDAT *ndp, int sflags) int m_bsdf(OBJREC *m, RAY *r) { + int hasthick = (m->otype == MAT_BSDF); int hitfront; COLOR ctmp; SDError ec; @@ -545,18 +652,21 @@ m_bsdf(OBJREC *m, RAY *r) MFUNC *mf; BSDFDAT nd; /* check arguments */ - if ((m->oargs.nsargs < 6) | (m->oargs.nfargs > 9) | + if ((m->oargs.nsargs < hasthick+5) | (m->oargs.nfargs > 9) | (m->oargs.nfargs % 3)) objerror(m, USER, "bad # arguments"); /* record surface struck */ hitfront = (r->rod > 0); /* load cal file */ - mf = getfunc(m, 5, 0x1d, 1); + mf = hasthick ? getfunc(m, 5, 0x1d, 1) + : getfunc(m, 4, 0xe, 1) ; setfunc(m, r); - /* get thickness */ - nd.thick = evalue(mf->ep[0]); - if ((-FTINY <= nd.thick) & (nd.thick <= FTINY)) - nd.thick = .0; + nd.thick = 0; /* set thickness */ + if (hasthick) { + nd.thick = evalue(mf->ep[0]); + if ((-FTINY <= nd.thick) & (nd.thick <= FTINY)) + nd.thick = 0; + } /* check backface visibility */ if (!hitfront & !backvis) { raytrans(r); @@ -569,14 +679,18 @@ m_bsdf(OBJREC *m, RAY *r) raytrans(r); /* hide our proxy */ return(1); } + if (hasthick && r->crtype & SHADOW) /* early shadow check #1 */ + return(1); nd.mp = m; nd.pr = r; /* get BSDF data */ - nd.sd = loadBSDF(m->oargs.sarg[1]); - /* early shadow check */ - if (r->crtype & SHADOW && (nd.sd->tf == NULL) & (nd.sd->tb == NULL)) + nd.sd = loadBSDF(m->oargs.sarg[hasthick]); + /* early shadow check #2 */ + if (r->crtype & SHADOW && (nd.sd->tf == NULL) & (nd.sd->tb == NULL)) { + SDfreeCache(nd.sd); return(1); - /* diffuse reflectance */ + } + /* diffuse components */ if (hitfront) { cvt_sdcolor(nd.rdiff, &nd.sd->rLambFront); if (m->oargs.nfargs >= 3) { @@ -585,6 +699,7 @@ m_bsdf(OBJREC *m, RAY *r) m->oargs.farg[2]); addcolor(nd.rdiff, ctmp); } + cvt_sdcolor(nd.tdiff, &nd.sd->tLambFront); } else { cvt_sdcolor(nd.rdiff, &nd.sd->rLambBack); if (m->oargs.nfargs >= 6) { @@ -593,10 +708,9 @@ m_bsdf(OBJREC *m, RAY *r) m->oargs.farg[5]); addcolor(nd.rdiff, ctmp); } + cvt_sdcolor(nd.tdiff, &nd.sd->tLambBack); } - /* diffuse transmittance */ - cvt_sdcolor(nd.tdiff, &nd.sd->tLamb); - if (m->oargs.nfargs >= 9) { + if (m->oargs.nfargs >= 9) { /* add diffuse transmittance? */ setcolor(ctmp, m->oargs.farg[6], m->oargs.farg[7], m->oargs.farg[8]); @@ -608,9 +722,9 @@ m_bsdf(OBJREC *m, RAY *r) multcolor(nd.rdiff, r->pcol); multcolor(nd.tdiff, r->pcol); /* get up vector */ - upvec[0] = evalue(mf->ep[1]); - upvec[1] = evalue(mf->ep[2]); - upvec[2] = evalue(mf->ep[3]); + upvec[0] = evalue(mf->ep[hasthick+0]); + upvec[1] = evalue(mf->ep[hasthick+1]); + upvec[2] = evalue(mf->ep[hasthick+2]); /* return to world coords */ if (mf->fxp != &unitxf) { multv3(upvec, upvec, mf->fxp->xfm); @@ -631,18 +745,24 @@ m_bsdf(OBJREC *m, RAY *r) } if (ec) { objerror(m, WARNING, "Illegal orientation vector"); + SDfreeCache(nd.sd); return(1); } - compute_through(&nd); /* compute through component */ - if (r->crtype & SHADOW) { - RAY tr; /* attempt to pass shadow ray */ - if (rayorigin(&tr, TRANS, r, nd.cthru) < 0) - return(1); /* blocked */ - VCOPY(tr.rdir, r->rdir); - rayvalue(&tr); /* transmit with scaling */ - multcolor(tr.rcol, tr.rcoef); - copycolor(r->rcol, tr.rcol); - return(1); /* we're done */ + setcolor(nd.cthru, 0, 0, 0); /* consider through component */ + setcolor(nd.cthru_surr, 0, 0, 0); + if (m->otype == MAT_ABSDF) { + compute_through(&nd); + if (r->crtype & SHADOW) { + RAY tr; /* attempt to pass shadow ray */ + SDfreeCache(nd.sd); + if (rayorigin(&tr, TRANS, r, nd.cthru) < 0) + return(1); /* no through component */ + VCOPY(tr.rdir, r->rdir); + rayvalue(&tr); /* transmit with scaling */ + multcolor(tr.rcol, tr.rcoef); + copycolor(r->rcol, tr.rcol); + return(1); /* we're done */ + } } ec = SDinvXform(nd.fromloc, nd.toloc); if (!ec) /* determine BSDF resolution */ @@ -663,23 +783,25 @@ m_bsdf(OBJREC *m, RAY *r) /* sample transmission */ sample_sdf(&nd, SDsampSpT); /* compute indirect diffuse */ - if (bright(nd.rdiff) > FTINY) { /* ambient from reflection */ + copycolor(ctmp, nd.rdiff); + addcolor(ctmp, nd.runsamp); + if (bright(ctmp) > FTINY) { /* ambient from reflection */ if (!hitfront) flipsurface(r); - copycolor(ctmp, nd.rdiff); multambient(ctmp, r, nd.pnorm); addcolor(r->rcol, ctmp); if (!hitfront) flipsurface(r); } - if (bright(nd.tdiff) > FTINY) { /* ambient from other side */ + copycolor(ctmp, nd.tdiff); + addcolor(ctmp, nd.tunsamp); + if (bright(ctmp) > FTINY) { /* ambient from other side */ FVECT bnorm; if (hitfront) flipsurface(r); bnorm[0] = -nd.pnorm[0]; bnorm[1] = -nd.pnorm[1]; bnorm[2] = -nd.pnorm[2]; - copycolor(ctmp, nd.tdiff); if (nd.thick != 0) { /* proxy with offset? */ VCOPY(vtmp, r->rop); VSUM(r->rop, vtmp, r->ron, nd.thick);