--- ray/src/rt/m_bsdf.c 2018/02/12 18:46:29 2.46 +++ ray/src/rt/m_bsdf.c 2021/08/27 03:09:27 2.65 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: m_bsdf.c,v 2.46 2018/02/12 18:46:29 greg Exp $"; +static const char RCSid[] = "$Id: m_bsdf.c,v 2.65 2021/08/27 03:09:27 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.46 2018 #include "copyright.h" #include "ray.h" +#include "otypes.h" #include "ambient.h" #include "source.h" #include "func.h" @@ -18,7 +19,7 @@ static const char RCSid[] = "$Id: m_bsdf.c,v 2.46 2018 /* * 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.46 2018 * 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. + * 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.46 2018 * 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,7 +86,8 @@ 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 */ @@ -86,39 +97,49 @@ typedef struct { #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 = 1.5; + PEAKSAMP psamp[NDIR2CHECK]; SDSpectralDF *dfp; FVECT pdir; double tomega, srchrad; - COLOR vpeak, vsum; - int i; + double tomsum, tomsurr; + COLOR vpeak, vsurr; + double vypeak; + int i, ns; SDError ec; - setcolor(ndp->cthru, 0, 0, 0); /* starting assumption */ - - if (ndp->pr->crtype & (SPECULAR|AMBIENT) && !(ndp->pr->crtype & SHADOW)) - return; /* no need for through comp. */ - if (ndp->pr->rod > 0) dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb; else @@ -128,45 +149,59 @@ 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); - pdir[2] = 0.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]; - normalize(tdir); - 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); - if (sv.cieY > 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 BTDF here */ + 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++) { + if (i && psamp[i].vy == psamp[i-1].vy) + continue; /* assume duplicate sample */ + + ec = SDsizeBSDF(&tomega, psamp[i].tdir, ndp->vray, + SDqueryMin, ndp->sd); + if (ec) + goto baderror; + + scalecolor(psamp[i].vcol, tomega); + /* not part of peak? */ + if (tomega > 1.5*dfp->minProjSA || + vypeak > 8.*psamp[i].vy*ns) { + if (!i) return; /* abort */ + addcolor(vsurr, psamp[i].vcol); + tomsurr += tomega; + continue; } + addcolor(vpeak, psamp[i].vcol); + tomsum += tomega; + vypeak += psamp[i].vy; + ++ns; } - if (pdir[2] == 0.0) - return; /* zero neighborhood */ - ec = SDsizeBSDF(&tomega, pdir, ndp->vray, SDqueryMin, ndp->sd); - if (ec) - goto baderror; - if (tomega > 1.5*dfp->minProjSA) - return; /* not really a peak? */ - tomega /= fabs(pdir[2]); /* remove cosine factor */ - if ((bright(vpeak) - ndp->sd->tLamb.cieY*(1./PI))*tomega <= .001) - return; /* < 0.1% transmission */ - for (i = 3; i--; ) /* remove peak from average */ - colval(vsum,i) -= colval(vpeak,i); - if (peak_over*bright(vsum) >= (NDIR2CHECK-1)*bright(vpeak)) - return; /* not peaky enough */ - copycolor(ndp->cthru, vpeak); /* else use it */ - scalecolor(ndp->cthru, tomega); + if (tomsurr <= FTINY) /* no surround implies no peak */ + return; + if ((vypeak/ns - (ndp->vray[2] > 0 ? ndp->sd->tLambFront.cieY + : ndp->sd->tLambBack.cieY)*(1./PI))*tomsum < .0005) + return; /* < 0.05% transmission */ + copycolor(ndp->cthru, vpeak); /* already scaled by omega */ multcolor(ndp->cthru, ndp->pr->pcol); /* modify by pattern */ + scalecolor(vsurr, 1./tomsurr); /* surround is avg. BTDF */ + copycolor(ndp->cthru_surr, vsurr); + multcolor(ndp->cthru_surr, ndp->pr->pcol); return; baderror: objerror(ndp->mp, USER, transSDError(ec)); @@ -218,11 +253,16 @@ 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; @@ -231,19 +271,27 @@ direct_specular_OK(COLOR cval, FVECT ldir, double omeg diffY = 0; setcolor(cdiff, 0, 0, 0); } - /* need projected solid angles */ + /* need projected solid angle */ omega *= fabs(vsrc[2]); - ec = SDsizeBSDF(&tomega, ndp->vray, vsrc, SDqueryMin, ndp->sd); - if (ec) - goto baderror; /* 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]; - if (dx*dx + dy*dy <= (4./PI)*(omega + tomega + - 2.*sqrt(omega*tomega))) - return(0); + 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; /* assign number of samples */ sf = specjitter * ndp->pr->rweight; if (tomega <= 0) @@ -277,12 +325,13 @@ direct_specular_OK(COLOR cval, FVECT ldir, double omeg if (tomega2 < .12*tomega) continue; /* not safe to include */ cvt_sdcolor(csmp, &sv); - - if (sf < 2.5*tsr) { /* weight by Y for small sources */ +#if 0 + if (sf < 2.5*tsr) { /* weight by BSDF for small sources */ scalecolor(csmp, sv.cieY); wtot += sv.cieY; } else - wtot += 1.; +#endif + wtot += 1.; addcolor(cval, csmp); } if (wtot <= FTINY) /* no valid specular samples? */ @@ -444,7 +493,9 @@ dir_btdf( static int sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int xmit) { - const int hasthru = (xmit && bright(ndp->cthru) > FTINY); + const int hasthru = (xmit && + !(ndp->pr->crtype & (SPECULAR|AMBIENT)) + && bright(ndp->cthru) > FTINY); int nstarget = 1; int nsent = 0; int n; @@ -491,9 +542,13 @@ sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int xmit 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 */ } if (xmit && ndp->thick != 0) /* need to offset origin? */ VSUM(sr.rorg, sr.rorg, ndp->pr->ron, -ndp->thick); @@ -510,7 +565,8 @@ static int sample_sdf(BSDFDAT *ndp, int sflags) { int hasthru = (sflags == SDsampSpT && - bright(ndp->cthru) > FTINY); + !(ndp->pr->crtype & (SPECULAR|AMBIENT)) + && bright(ndp->cthru) > FTINY); int n, ntotal = 0; double b = 0; SDSpectralDF *dfp; @@ -540,6 +596,7 @@ sample_sdf(BSDFDAT *ndp, int sflags) 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 @@ -577,6 +634,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; @@ -584,18 +642,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); @@ -608,14 +669,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) { @@ -624,6 +689,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) { @@ -632,10 +698,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]); @@ -647,9 +712,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); @@ -670,18 +735,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); /* 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 */ + 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 */