--- ray/src/rt/m_bsdf.c 2011/08/21 22:38:12 2.14 +++ ray/src/rt/m_bsdf.c 2012/09/02 15:33:15 2.22 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: m_bsdf.c,v 2.14 2011/08/21 22:38:12 greg Exp $"; +static const char RCSid[] = "$Id: m_bsdf.c,v 2.22 2012/09/02 15:33:15 greg Exp $"; #endif /* * Shading for materials with BSDFs taken from XML data files @@ -82,10 +82,8 @@ typedef struct { /* Jitter ray sample according to projected solid angle and specjitter */ static void -bsdf_jitter(FVECT vres, BSDFDAT *ndp, int domax) +bsdf_jitter(FVECT vres, BSDFDAT *ndp, double sr_psa) { - double sr_psa = ndp->sr_vpsa[domax]; - VCOPY(vres, ndp->vray); if (specjitter < 1.) sr_psa *= specjitter; @@ -100,10 +98,10 @@ bsdf_jitter(FVECT vres, BSDFDAT *ndp, int domax) static int direct_bsdf_OK(COLOR cval, FVECT ldir, double omega, BSDFDAT *ndp) { - int nsamp = 1, ok = 0; + int nsamp, ok = 0; FVECT vsrc, vsmp, vjit; double tomega; - double sf, sd[2]; + double sf, tsr, sd[2]; COLOR csmp; SDValue sv; SDError ec; @@ -111,27 +109,27 @@ direct_bsdf_OK(COLOR cval, FVECT ldir, double omega, B /* transform source direction */ if (SDmapDir(vsrc, ndp->toloc, ldir) != SDEnone) return(0); + /* assign number of samples */ + 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) { double dx = vsrc[0] + ndp->vray[0]; double dy = vsrc[1] + ndp->vray[1]; - if (dx*dx + dy*dy <= omega*(1./PI)) + if (dx*dx + dy*dy <= omega+tomega) return(0); } - if (specjitter > FTINY) { /* assign number of samples */ - ec = SDsizeBSDF(&tomega, ndp->vray, vsrc, SDqueryMin, ndp->sd); - if (ec) - goto baderror; - sf = specjitter * ndp->pr->rweight; - if (tomega <= omega*.01) - nsamp = 100.*sf + .5; - else - nsamp = 4.*sf*omega/tomega + .5; - nsamp += !nsamp; - } - sf = sqrt(omega); + sf = specjitter * ndp->pr->rweight; + 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); + tsr = sqrt(tomega); for (i = nsamp; i--; ) { VCOPY(vsmp, vsrc); /* jitter query directions */ if (nsamp > 1) { @@ -143,7 +141,7 @@ direct_bsdf_OK(COLOR cval, FVECT ldir, double omega, B continue; } } - bsdf_jitter(vjit, ndp, 0); + bsdf_jitter(vjit, ndp, tsr); /* compute BSDF */ ec = SDevalBSDF(&sv, vjit, vsmp, ndp->sd); if (ec) @@ -159,6 +157,7 @@ direct_bsdf_OK(COLOR cval, FVECT ldir, double omega, B return(ok); baderror: objerror(ndp->mp, USER, transSDError(ec)); + return(0); /* gratis return */ } /* Compute source contribution for BSDF (reflected & transmitted) */ @@ -333,12 +332,15 @@ sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usep } /* run through our samples */ for (nsent = 0; nsent < nstarget; nsent++) { - if (nstarget == 1) /* stratify random variable */ + if (nstarget == 1) { /* stratify random variable */ xrand = urand(ilhash(dimlist,ndims)+samplendx); - else + if (specjitter < 1.) + xrand = .5 + specjitter*(xrand-.5); + } else { xrand = (nsent + frandom())/(double)nstarget; + } SDerrorDetail[0] = '\0'; /* sample direction & coef. */ - bsdf_jitter(vsmp, ndp, 0); + bsdf_jitter(vsmp, ndp, ndp->sr_vpsa[0]); ec = SDsampComponent(&bsv, vsmp, xrand, dcp); if (ec) objerror(ndp->mp, USER, transSDError(ec)); @@ -378,7 +380,10 @@ sample_sdf(BSDFDAT *ndp, int sflags) if (sflags == SDsampSpT) { unsc = ndp->tunsamp; - dfp = ndp->sd->tf; + 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; @@ -399,7 +404,7 @@ sample_sdf(BSDFDAT *ndp, int sflags) FVECT vjit; double d; COLOR ctmp; - bsdf_jitter(vjit, ndp, 1); + bsdf_jitter(vjit, ndp, ndp->sr_vpsa[1]); d = SDdirectHemi(vjit, sflags, ndp->sd); if (sflags == SDsampSpT) { copycolor(ctmp, ndp->pr->pcol); @@ -468,7 +473,7 @@ m_bsdf(OBJREC *m, RAY *r) } else { if (m->oargs.nfargs < 6) { /* check invisible backside */ if (!backvis && (nd.sd->rb == NULL) & - (nd.sd->tf == NULL)) { + (nd.sd->tb == NULL)) { SDfreeCache(nd.sd); raytrans(r); return(1); @@ -498,9 +503,9 @@ m_bsdf(OBJREC *m, RAY *r) upvec[1] = evalue(mf->ep[2]); upvec[2] = evalue(mf->ep[3]); /* return to world coords */ - if (mf->f != &unitxf) { - multv3(upvec, upvec, mf->f->xfm); - nd.thick *= mf->f->sca; + if (mf->fxp != &unitxf) { + multv3(upvec, upvec, mf->fxp->xfm); + nd.thick *= mf->fxp->sca; } raynormal(nd.pnorm, r); /* compute local BSDF xform */ @@ -513,15 +518,15 @@ m_bsdf(OBJREC *m, RAY *r) } if (!ec) ec = SDinvXform(nd.fromloc, nd.toloc); - /* determine BSDF resolution */ - if (!ec) - ec = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL, - SDqueryMin+SDqueryMax, nd.sd); if (ec) { - objerror(m, WARNING, transSDError(ec)); - SDfreeCache(nd.sd); + objerror(m, WARNING, "Illegal orientation vector"); return(1); } + /* determine BSDF resolution */ + ec = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL, SDqueryMin+SDqueryMax, nd.sd); + if (ec) + objerror(m, USER, transSDError(ec)); + nd.sr_vpsa[0] = sqrt(nd.sr_vpsa[0]); nd.sr_vpsa[1] = sqrt(nd.sr_vpsa[1]); if (!hitfront) { /* perturb normal towards hit */ @@ -555,7 +560,7 @@ m_bsdf(OBJREC *m, RAY *r) bnorm[2] = -nd.pnorm[2]; if (nd.thick != 0) { /* proxy with offset? */ VCOPY(vtmp, r->rop); - VSUM(r->rop, vtmp, r->ron, -nd.thick); + VSUM(r->rop, vtmp, r->ron, nd.thick); multambient(ctmp, r, bnorm); VCOPY(r->rop, vtmp); } else @@ -565,7 +570,8 @@ m_bsdf(OBJREC *m, RAY *r) flipsurface(r); } /* add direct component */ - if ((bright(nd.tdiff) <= FTINY) & (nd.sd->tf == NULL)) { + if ((bright(nd.tdiff) <= FTINY) & (nd.sd->tf == NULL) & + (nd.sd->tb == NULL)) { direct(r, dir_brdf, &nd); /* reflection only */ } else if (nd.thick == 0) { direct(r, dir_bsdf, &nd); /* thin surface scattering */