--- ray/src/common/bsdf.c 2011/04/20 14:44:05 2.23 +++ ray/src/common/bsdf.c 2011/06/03 18:12:58 2.29 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: bsdf.c,v 2.23 2011/04/20 14:44:05 greg Exp $"; +static const char RCSid[] = "$Id: bsdf.c,v 2.29 2011/06/03 18:12:58 greg Exp $"; #endif /* * bsdf.c @@ -169,15 +169,9 @@ SDloadFile(SDData *sd, const char *fname) /* try loading variable resolution data */ lastErr = SDloadTre(sd, wtl); /* check our result */ - switch (lastErr) { - case SDEformat: - case SDEdata: - case SDEsupport: /* possibly we just tried the wrong format */ + if (lastErr == SDEsupport) /* try matrix BSDF if not tree data */ lastErr = SDloadMtx(sd, wtl); - break; - default: /* variable res. OK else serious error */ - break; - } + /* done with XML file */ ezxml_free(fl); @@ -405,34 +399,35 @@ SDfreeCache(const SDData *sd) /* Sample an individual BSDF component */ SDError -SDsampComponent(SDValue *sv, FVECT outVec, const FVECT inVec, - double randX, SDComponent *sdc) +SDsampComponent(SDValue *sv, FVECT ioVec, double randX, SDComponent *sdc) { float coef[SDmaxCh]; SDError ec; + FVECT inVec; const SDCDst *cd; double d; int n; /* check arguments */ - if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sdc == NULL)) + if ((sv == NULL) | (ioVec == NULL) | (sdc == NULL)) return SDEargument; /* get cumulative distribution */ + VCOPY(inVec, ioVec); cd = (*sdc->func->getCDist)(inVec, sdc); if (cd == NULL) return SDEmemory; if (cd->cTotal <= 1e-7) { /* anything to sample? */ sv->spec = c_dfcolor; sv->cieY = .0; - memset(outVec, 0, 3*sizeof(double)); + memset(ioVec, 0, 3*sizeof(double)); return SDEnone; } sv->cieY = cd->cTotal; /* compute sample direction */ - ec = (*sdc->func->sampCDist)(outVec, randX, cd); + ec = (*sdc->func->sampCDist)(ioVec, randX, cd); if (ec) return ec; /* get BSDF color */ - n = (*sdc->func->getBSDFs)(coef, outVec, inVec, sdc->dist); + n = (*sdc->func->getBSDFs)(coef, ioVec, inVec, sdc); if (n <= 0) { strcpy(SDerrorDetail, "BSDF sample value error"); return SDEinternal; @@ -495,7 +490,7 @@ SDsizeBSDF(double *projSA, const FVECT v1, const RREAL SDError ec; int i; /* check arguments */ - if ((projSA == NULL) | (v1 == NULL)) + if ((projSA == NULL) | (v1 == NULL) | (sd == NULL)) return SDEargument; /* initialize extrema */ switch (qflags) { @@ -515,21 +510,22 @@ SDsizeBSDF(double *projSA, const FVECT v1, const RREAL rdf = sd->rf; else rdf = sd->rb; - tdf = NULL; /* transmitted component? */ - if (v2 != NULL && v1[2] > 0 ^ v2[2] > 0) { - rdf = NULL; - tdf = sd->tf; - } + tdf = sd->tf; + if (v2 != NULL) /* bidirectional? */ + if (v1[2] > 0 ^ v2[2] > 0) + rdf = NULL; + else + tdf = NULL; ec = SDEdata; /* run through components */ for (i = (rdf==NULL) ? 0 : rdf->ncomp; i--; ) { ec = (*rdf->comp[i].func->queryProjSA)(projSA, v1, v2, - qflags, rdf->comp[i].dist); + qflags, &rdf->comp[i]); if (ec) return ec; } for (i = (tdf==NULL) ? 0 : tdf->ncomp; i--; ) { ec = (*tdf->comp[i].func->queryProjSA)(projSA, v1, v2, - qflags, tdf->comp[i].dist); + qflags, &tdf->comp[i]); if (ec) return ec; } @@ -571,7 +567,7 @@ SDevalBSDF(SDValue *sv, const FVECT outVec, const FVEC i = (sdf != NULL) ? sdf->ncomp : 0; while (i-- > 0) { nch = (*sdf->comp[i].func->getBSDFs)(coef, outVec, inVec, - sdf->comp[i].dist); + &sdf->comp[i]); while (nch-- > 0) { c_cmix(&sv->spec, sv->cieY, &sv->spec, coef[nch], &sdf->comp[i].cspec[nch]); @@ -626,10 +622,10 @@ SDdirectHemi(const FVECT inVec, int sflags, const SDDa /* Sample BSDF direction based on the given random variable */ SDError -SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVec, - double randX, int sflags, const SDData *sd) +SDsampBSDF(SDValue *sv, FVECT ioVec, double randX, int sflags, const SDData *sd) { SDError ec; + FVECT inVec; int inFront; SDSpectralDF *rdf; double rdiff; @@ -638,10 +634,11 @@ SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVe SDComponent *sdc; const SDCDst **cdarr = NULL; /* check arguments */ - if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sd == NULL) | + if ((sv == NULL) | (ioVec == NULL) | (sd == NULL) | (randX < 0) | (randX >= 1.)) return SDEargument; /* whose side are we on? */ + VCOPY(inVec, ioVec); inFront = (inVec[2] > 0); /* remember diffuse portions */ if (inFront) { @@ -682,14 +679,14 @@ SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVe } if (sv->cieY <= 1e-7) { /* anything to sample? */ sv->cieY = .0; - memset(outVec, 0, 3*sizeof(double)); + memset(ioVec, 0, 3*sizeof(double)); return SDEnone; } /* scale random variable */ randX *= sv->cieY; /* diffuse reflection? */ if (randX < rdiff) { - SDdiffuseSamp(outVec, inFront, randX/rdiff); + SDdiffuseSamp(ioVec, inFront, randX/rdiff); goto done; } randX -= rdiff; @@ -697,7 +694,7 @@ SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVe if ((sflags & SDsampDf+SDsampT) == SDsampDf+SDsampT) { if (randX < sd->tLamb.cieY) { sv->spec = sd->tLamb.spec; - SDdiffuseSamp(outVec, !inFront, randX/sd->tLamb.cieY); + SDdiffuseSamp(ioVec, !inFront, randX/sd->tLamb.cieY); goto done; } randX -= sd->tLamb.cieY; @@ -709,11 +706,11 @@ SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVe return SDEinternal; /* compute sample direction */ sdc = (i < nr) ? &rdf->comp[i] : &sd->tf->comp[i-nr]; - ec = (*sdc->func->sampCDist)(outVec, randX/cdarr[i]->cTotal, cdarr[i]); + ec = (*sdc->func->sampCDist)(ioVec, randX/cdarr[i]->cTotal, cdarr[i]); if (ec) return ec; /* compute color */ - j = (*sdc->func->getBSDFs)(coef, outVec, inVec, sdc->dist); + j = (*sdc->func->getBSDFs)(coef, ioVec, inVec, sdc); if (j <= 0) { sprintf(SDerrorDetail, "BSDF \"%s\" sampling value error", sd->name);