--- ray/src/rt/m_bsdf.c 2013/07/04 15:14:45 2.24 +++ ray/src/rt/m_bsdf.c 2017/05/18 17:59:37 2.37 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: m_bsdf.c,v 2.24 2013/07/04 15:14:45 greg Exp $"; +static const char RCSid[] = "$Id: m_bsdf.c,v 2.37 2017/05/18 17:59:37 greg Exp $"; #endif /* * Shading for materials with BSDFs taken from XML data files @@ -13,6 +13,7 @@ static const char RCSid[] = "$Id: m_bsdf.c,v 2.24 2013 #include "func.h" #include "bsdf.h" #include "random.h" +#include "pmapmat.h" /* * Arguments to this material include optional diffuse colors. @@ -22,11 +23,10 @@ static const char RCSid[] = "$Id: m_bsdf.c,v 2.24 2013 * (opposite the surface normal) to bypass any intervening geometry. * Translation only affects scattered, non-source-directed samples. * A non-zero thickness has the further side-effect that an unscattered - * (view) ray will pass right through our material if it has any - * non-diffuse transmission, making the BSDF surface invisible. This - * shows the proxied geometry instead. Thickness has the further - * effect of turning off reflection on the hidden side so that rays - * heading in the opposite direction pass unimpeded through the BSDF + * (view) ray will pass right through our material, making the BSDF + * surface invisible and showing the proxied geometry instead. Thickness + * has the further effect of turning off reflection on the reverse side so + * rays heading in the opposite direction pass unimpeded through the BSDF * surface. A paired surface may be placed on the opposide side of * the detail geometry, less than this thickness away, if a two-way * proxy is desired. Note that the sign of the thickness is important. @@ -35,6 +35,11 @@ static const char RCSid[] = "$Id: m_bsdf.c,v 2.24 2013 * 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). * 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 @@ -42,7 +47,7 @@ static const char RCSid[] = "$Id: m_bsdf.c,v 2.24 2013 * We do not reorient the surface, so if the BSDF has no back-side * reflectance and none is given in the real arguments, a BSDF surface * with zero thickness will appear black when viewed from behind - * unless backface visibility is off. + * unless backface visibility is on, when it becomes invisible. * The diffuse arguments are added to components in the BSDF file, * not multiplied. However, patterns affect this material as a multiplier * on everything except non-diffuse reflection. @@ -58,7 +63,7 @@ static const char RCSid[] = "$Id: m_bsdf.c,v 2.24 2013 /* * Note that our reverse ray-tracing process means that the positions * of incoming and outgoing vectors may be reversed in our calls - * to the BSDF library. This is fine, since the bidirectional nature + * to the BSDF library. This is usually fine, since the bidirectional nature * of the BSDF (that's what the 'B' stands for) means it all works out. */ @@ -71,15 +76,97 @@ 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 */ SDData *sd; /* loaded BSDF data */ - COLOR runsamp; /* BSDF hemispherical reflection */ - COLOR rdiff; /* added diffuse reflection */ - COLOR tunsamp; /* BSDF hemispherical transmission */ - COLOR tdiff; /* added diffuse transmission */ + COLOR rdiff; /* diffuse reflection */ + COLOR tdiff; /* diffuse transmission */ } BSDFDAT; /* BSDF material data */ #define cvt_sdcolor(cv, svp) ccy2rgb(&(svp)->spec, (svp)->cieY, cv) +/* Compute "through" component color */ +static void +compute_through(BSDFDAT *ndp) +{ +#define NDIR2CHECK 13 + 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}, + }; + const double peak_over = 2.0; + SDSpectralDF *dfp; + FVECT pdir; + double tomega, srchrad; + COLOR vpeak, vsum; + int nsum, i; + 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 + dfp = (ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf; + + if (dfp == NULL) + 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; + 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]; + normalize(tdir); + ec = SDevalBSDF(&sv, 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); + } + } + 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); + multcolor(ndp->cthru, ndp->pr->pcol); /* modify by pattern */ + return; +baderror: + objerror(ndp->mp, USER, transSDError(ec)); +#undef NDIR2CHECK +} + /* Jitter ray sample according to projected solid angle and specjitter */ static void bsdf_jitter(FVECT vres, BSDFDAT *ndp, double sr_psa) @@ -94,33 +181,65 @@ bsdf_jitter(FVECT vres, BSDFDAT *ndp, double sr_psa) normalize(vres); } -/* Evaluate BSDF for direct component, returning true if OK to proceed */ +/* Get BSDF specular for direct component, returning true if OK to proceed */ static int -direct_bsdf_OK(COLOR cval, FVECT ldir, double omega, BSDFDAT *ndp) +direct_specular_OK(COLOR cval, FVECT ldir, double omega, BSDFDAT *ndp) { int nsamp, ok = 0; FVECT vsrc, vsmp, vjit; - double tomega; + double tomega, tomega2; double sf, tsr, sd[2]; - COLOR csmp; + 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); - /* assign number of samples */ + /* will discount diffuse portion */ + switch ((vsrc[2] > 0)<<1 | (ndp->vray[2] > 0)) { + case 3: + if (ndp->sd->rf == NULL) + return(0); /* all diffuse */ + sv = ndp->sd->rLambFront; + break; + case 0: + if (ndp->sd->rb == NULL) + return(0); /* all diffuse */ + sv = ndp->sd->rLambBack; + break; + default: + if ((ndp->sd->tf == NULL) & (ndp->sd->tb == NULL)) + return(0); /* all diffuse */ + sv = ndp->sd->tLamb; + break; + } + if (sv.cieY > FTINY) { + diffY = sv.cieY *= 1./PI; + cvt_sdcolor(cdiff, &sv); + } else { + diffY = .0; + setcolor(cdiff, .0, .0, .0); + } + /* need projected solid angles */ + omega *= fabs(vsrc[2]); ec = SDsizeBSDF(&tomega, ndp->vray, vsrc, SDqueryMin, ndp->sd); if (ec) goto baderror; /* check indirect over-counting */ - if (ndp->thick != 0 && ndp->pr->crtype & (SPECULAR|AMBIENT) - && vsrc[2] > 0 ^ ndp->vray[2] > 0) { + 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) + if (dx*dx + dy*dy <= (4./PI)*(omega + tomega + + 2.*sqrt(omega*tomega))) return(0); } + /* assign number of samples */ sf = specjitter * ndp->pr->rweight; if (tomega <= .0) nsamp = 1; @@ -129,8 +248,7 @@ direct_bsdf_OK(COLOR cval, FVECT ldir, double omega, B 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 */ @@ -138,25 +256,35 @@ direct_bsdf_OK(COLOR cval, FVECT ldir, double omega, B 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 <= FTINY) /* worth using? */ - continue; + 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); /* average it in */ + addcolor(cval, csmp); /* else average it in */ ++ok; } - sf = 1./(double)nsamp; + if (!ok) /* no valid specular samples? */ + return(0); + + sf = 1./(double)ok; /* compute average BSDF */ scalecolor(cval, sf); - return(ok); + /* subtract diffuse contribution */ + for (i = 3*(diffY > FTINY); i--; ) + if ((colval(cval,i) -= colval(cdiff,i)) < .0) + colval(cval,i) = .0; + return(1); baderror: objerror(ndp->mp, USER, transSDError(ec)); return(0); /* gratis return */ @@ -200,27 +328,18 @@ dir_bsdf( scalecolor(ctmp, dtmp); addcolor(cval, ctmp); } + if (ambRayInPmap(np->pr)) + return; /* specular already in photon map */ /* - * Compute scattering coefficient using BSDF. + * Compute specular scattering coefficient using BSDF. */ - if (!direct_bsdf_OK(ctmp, ldir, omega, np)) + if (!direct_specular_OK(ctmp, ldir, omega, np)) return; - if (ldot > 0) { /* pattern only diffuse reflection */ - COLOR ctmp1, ctmp2; - dtmp = (np->pr->rod > 0) ? np->sd->rLambFront.cieY - : np->sd->rLambBack.cieY; - /* diffuse fraction */ - dtmp /= PI * bright(ctmp); - copycolor(ctmp2, np->pr->pcol); - scalecolor(ctmp2, dtmp); - setcolor(ctmp1, 1.-dtmp, 1.-dtmp, 1.-dtmp); - addcolor(ctmp1, ctmp2); - multcolor(ctmp, ctmp1); /* apply derated pattern */ - dtmp = ldot * omega; - } else { /* full pattern on transmission */ + if (ldot < 0) { /* pattern for specular transmission */ multcolor(ctmp, np->pr->pcol); dtmp = -ldot * omega; - } + } else + dtmp = ldot * omega; scalecolor(ctmp, dtmp); addcolor(cval, ctmp); } @@ -255,20 +374,13 @@ dir_brdf( scalecolor(ctmp, dtmp); addcolor(cval, ctmp); } + if (ambRayInPmap(np->pr)) + return; /* specular already in photon map */ /* - * Compute reflection coefficient using BSDF. + * Compute specular reflection coefficient using BSDF. */ - if (!direct_bsdf_OK(ctmp, ldir, omega, np)) + if (!direct_specular_OK(ctmp, ldir, omega, np)) return; - /* pattern only diffuse reflection */ - dtmp = (np->pr->rod > 0) ? np->sd->rLambFront.cieY - : np->sd->rLambBack.cieY; - dtmp /= PI * bright(ctmp); /* diffuse fraction */ - copycolor(ctmp2, np->pr->pcol); - scalecolor(ctmp2, dtmp); - setcolor(ctmp1, 1.-dtmp, 1.-dtmp, 1.-dtmp); - addcolor(ctmp1, ctmp2); - multcolor(ctmp, ctmp1); /* apply derated pattern */ dtmp = ldot * omega; scalecolor(ctmp, dtmp); addcolor(cval, ctmp); @@ -304,10 +416,12 @@ dir_btdf( scalecolor(ctmp, dtmp); addcolor(cval, ctmp); } + if (ambRayInPmap(np->pr)) + return; /* specular already in photon map */ /* - * Compute scattering coefficient using BSDF. + * Compute specular scattering coefficient using BSDF. */ - if (!direct_bsdf_OK(ctmp, ldir, omega, np)) + if (!direct_specular_OK(ctmp, ldir, omega, np)) return; /* full pattern on transmission */ multcolor(ctmp, np->pr->pcol); @@ -363,7 +477,7 @@ sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usep continue; /* Russian roulette victim */ } /* need to offset origin? */ - if (ndp->thick != 0 && ndp->pr->rod > 0 ^ vsmp[2] > 0) + if (ndp->thick != 0 && (ndp->pr->rod > 0) ^ (vsmp[2] > 0)) VSUM(sr.rorg, sr.rorg, ndp->pr->ron, -ndp->thick); rayvalue(&sr); /* send & evaluate sample */ multcolor(sr.rcol, sr.rcoef); @@ -381,23 +495,18 @@ sample_sdf(BSDFDAT *ndp, int sflags) COLORV *unsc; if (sflags == SDsampSpT) { - unsc = ndp->tunsamp; + unsc = ndp->tdiff; 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; - cvt_sdcolor(unsc, &ndp->sd->tLamb); } else /* sflags == SDsampSpR */ { - unsc = ndp->runsamp; - if (ndp->pr->rod > 0) { + unsc = ndp->rdiff; + if (ndp->pr->rod > 0) dfp = ndp->sd->rf; - cvt_sdcolor(unsc, &ndp->sd->rLambFront); - } else { + else dfp = ndp->sd->rb; - cvt_sdcolor(unsc, &ndp->sd->rLambBack); - } } - multcolor(unsc, ndp->pr->pcol); if (dfp == NULL) /* no specular component? */ return(0); /* below sampling threshold? */ @@ -446,55 +555,56 @@ m_bsdf(OBJREC *m, RAY *r) hitfront = (r->rod > 0); /* load cal file */ mf = getfunc(m, 5, 0x1d, 1); + setfunc(m, r); /* get thickness */ nd.thick = evalue(mf->ep[0]); if ((-FTINY <= nd.thick) & (nd.thick <= FTINY)) nd.thick = .0; - /* check shadow */ - if (r->crtype & SHADOW) { - if (nd.thick != 0) - raytrans(r); /* pass-through */ - return(1); /* or shadow */ + /* check backface visibility */ + if (!hitfront & !backvis) { + raytrans(r); + return(1); } /* check other rays to pass */ - if (nd.thick != 0 && (!(r->crtype & (SPECULAR|AMBIENT)) || - nd.thick > 0 ^ hitfront)) { + if (nd.thick != 0 && (r->crtype & SHADOW || + !(r->crtype & (SPECULAR|AMBIENT)) || + (nd.thick > 0) ^ hitfront)) { raytrans(r); /* hide our proxy */ 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)) + return(1); /* diffuse reflectance */ if (hitfront) { - if (m->oargs.nfargs < 3) - setcolor(nd.rdiff, .0, .0, .0); - else - setcolor(nd.rdiff, m->oargs.farg[0], + cvt_sdcolor(nd.rdiff, &nd.sd->rLambFront); + if (m->oargs.nfargs >= 3) { + setcolor(ctmp, m->oargs.farg[0], m->oargs.farg[1], m->oargs.farg[2]); + addcolor(nd.rdiff, ctmp); + } } else { - if (m->oargs.nfargs < 6) { /* check invisible backside */ - if (!backvis && (nd.sd->rb == NULL) & - (nd.sd->tb == NULL)) { - SDfreeCache(nd.sd); - raytrans(r); - return(1); - } - setcolor(nd.rdiff, .0, .0, .0); - } else - setcolor(nd.rdiff, m->oargs.farg[3], + cvt_sdcolor(nd.rdiff, &nd.sd->rLambBack); + if (m->oargs.nfargs >= 6) { + setcolor(ctmp, m->oargs.farg[3], m->oargs.farg[4], m->oargs.farg[5]); + addcolor(nd.rdiff, ctmp); + } } /* diffuse transmittance */ - if (m->oargs.nfargs < 9) - setcolor(nd.tdiff, .0, .0, .0); - else - setcolor(nd.tdiff, m->oargs.farg[6], + cvt_sdcolor(nd.tdiff, &nd.sd->tLamb); + if (m->oargs.nfargs >= 9) { + setcolor(ctmp, m->oargs.farg[6], m->oargs.farg[7], m->oargs.farg[8]); - nd.mp = m; - nd.pr = r; + addcolor(nd.tdiff, ctmp); + } /* get modifiers */ raytexture(r, m->omod); /* modify diffuse values */ @@ -522,14 +632,25 @@ m_bsdf(OBJREC *m, RAY *r) nd.vray[2] = -r->rdir[2]; ec = SDmapDir(nd.vray, nd.toloc, nd.vray); } - if (!ec) - ec = SDinvXform(nd.fromloc, nd.toloc); if (ec) { objerror(m, WARNING, "Illegal orientation vector"); return(1); } - /* determine BSDF resolution */ - ec = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL, SDqueryMin+SDqueryMax, nd.sd); + 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 */ + } + ec = SDinvXform(nd.fromloc, nd.toloc); + if (!ec) /* determine BSDF resolution */ + ec = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL, + SDqueryMin+SDqueryMax, nd.sd); if (ec) objerror(m, USER, transSDError(ec)); @@ -545,25 +666,23 @@ m_bsdf(OBJREC *m, RAY *r) /* sample transmission */ sample_sdf(&nd, SDsampSpT); /* compute indirect diffuse */ - copycolor(ctmp, nd.rdiff); - addcolor(ctmp, nd.runsamp); - if (bright(ctmp) > FTINY) { /* ambient from reflection */ + if (bright(nd.rdiff) > 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); } - copycolor(ctmp, nd.tdiff); - addcolor(ctmp, nd.tunsamp); - if (bright(ctmp) > FTINY) { /* ambient from other side */ + if (bright(nd.tdiff) > 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);