--- ray/src/util/cmbsdf.c 2014/01/20 21:29:04 2.1 +++ ray/src/util/cmbsdf.c 2019/09/11 00:24:03 2.7 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: cmbsdf.c,v 2.1 2014/01/20 21:29:04 greg Exp $"; +static const char RCSid[] = "$Id: cmbsdf.c,v 2.7 2019/09/11 00:24:03 greg Exp $"; #endif /* * Load and convert BSDF into color coefficient matrix representation. @@ -15,7 +15,7 @@ static const char RCSid[] = "$Id: cmbsdf.c,v 2.1 2014/ /* Convert a BSDF to our matrix representation */ static CMATRIX * -cm_bsdf(const COLOR bsdfLamb, const COLOR specCol, const SDMat *bsdf) +cm_bsdf(const COLOR diffBSDF, const SDMat *bsdf) { CMATRIX *cm = cm_alloc(bsdf->nout, bsdf->ninc); int nbadohm = 0; @@ -28,16 +28,19 @@ cm_bsdf(const COLOR bsdfLamb, const COLOR specCol, con nbadohm += (dom <= 0); for (r = 0; r < cm->nrows; r++) { - float f = mBSDF_value(bsdf,c,r); + float f = mBSDF_value(bsdf,r,c); COLORV *mp = cm_lval(cm,r,c); /* check BSDF value */ if ((f <= 0) | (dom <= 0)) { nneg += (f < -FTINY); - f = .0f; - } - copycolor(mp, specCol); - scalecolor(mp, f); - addcolor(mp, bsdfLamb); + setcolor(mp, .0f, .0f, .0f); + } else if (bsdf->chroma != NULL) { + C_COLOR cxy; + c_decodeChroma(&cxy, mBSDF_chroma(bsdf,r,c)); + ccy2rgb(&cxy, f, mp); + } else + setcolor(mp, f, f, f); + addcolor(mp, diffBSDF); scalecolor(mp, dom); } } @@ -76,7 +79,7 @@ recip_in_from_out(const SDMat *bsdf, int out_recip) /* Convert a BSDF to our matrix representation, applying reciprocity */ static CMATRIX * -cm_bsdf_recip(const COLOR bsdfLamb, const COLOR specCol, const SDMat *bsdf) +cm_bsdf_recip(const COLOR diffBSDF, const SDMat *bsdf) { CMATRIX *cm = cm_alloc(bsdf->ninc, bsdf->nout); int nbadohm = 0; @@ -91,16 +94,19 @@ cm_bsdf_recip(const COLOR bsdfLamb, const COLOR specCo for (r = 0; r < cm->nrows; r++) { const int ri = recip_in_from_out(bsdf,r); - float f = mBSDF_value(bsdf,ri,ro); + float f = mBSDF_value(bsdf,ro,ri); COLORV *mp = cm_lval(cm,r,c); /* check BSDF value */ if ((f <= 0) | (dom <= 0)) { nneg += (f < -FTINY); - f = .0f; - } - copycolor(mp, specCol); - scalecolor(mp, f); - addcolor(mp, bsdfLamb); + setcolor(mp, .0f, .0f, .0f); + } else if (bsdf->chroma != NULL) { + C_COLOR cxy; + c_decodeChroma(&cxy, mBSDF_chroma(bsdf,ro,ri)); + ccy2rgb(&cxy, f, mp); + } else + setcolor(mp, f, f, f); + addcolor(mp, diffBSDF); scalecolor(mp, dom); } } @@ -113,46 +119,50 @@ cm_bsdf_recip(const COLOR bsdfLamb, const COLOR specCo return(cm); } -/* Load and convert a matrix BSDF from the given XML file */ +/* Return a Lambertian (diffuse) matrix */ +static CMATRIX * +cm_bsdf_Lamb(const COLOR diffBSDF) +{ + CMATRIX *cm = cm_alloc(145, 145); /* this is a hack */ + int r, c; + + for (c = 0; c < cm->ncols; c++) + for (r = 0; r < cm->nrows; r++) { + COLORV *mp = cm_lval(cm,r,c); + copycolor(mp, diffBSDF); + } + return(cm); +} + +/* Load and convert a matrix BTDF from the given XML file */ CMATRIX * -cm_loadBSDF(char *fname, COLOR cLamb) +cm_loadBTDF(char *fname) { CMATRIX *Tmat; - char *fpath; int recip; SDError ec; SDData myBSDF; SDSpectralDF *tdf; - COLOR bsdfLamb, specCol; - /* find path to BSDF file */ - fpath = getpath(fname, getrlibpath(), R_OK); - if (fpath == NULL) { - sprintf(errmsg, "cannot find BSDF file '%s'", fname); - error(USER, errmsg); - } + COLOR diffBSDF; + SDclearBSDF(&myBSDF, fname); /* load XML and check type */ - ec = SDloadFile(&myBSDF, fpath); + ec = SDloadFile(&myBSDF, fname); if (ec) error(USER, transSDError(ec)); - ccy2rgb(&myBSDF.tLamb.spec, myBSDF.tLamb.cieY/PI, bsdfLamb); + ccy2rgb(&myBSDF.tLamb.spec, myBSDF.tLamb.cieY/PI, diffBSDF); recip = (myBSDF.tb == NULL); tdf = recip ? myBSDF.tf : myBSDF.tb; if (tdf == NULL) { /* no non-Lambertian transmission? */ - if (cLamb != NULL) - copycolor(cLamb, bsdfLamb); SDfreeBSDF(&myBSDF); - return(NULL); + return(cm_bsdf_Lamb(diffBSDF)); } if (tdf->ncomp != 1 || tdf->comp[0].func != &SDhandleMtx) { - sprintf(errmsg, "unsupported BSDF '%s'", fpath); + sprintf(errmsg, "unsupported BSDF '%s'", fname); error(USER, errmsg); } /* convert BTDF to matrix */ - ccy2rgb(&tdf->comp[0].cspec[0], 1., specCol); - Tmat = recip ? cm_bsdf_recip(bsdfLamb, specCol, (SDMat *)tdf->comp[0].dist) - : cm_bsdf(bsdfLamb, specCol, (SDMat *)tdf->comp[0].dist); - if (cLamb != NULL) /* Lambertian is included */ - setcolor(cLamb, .0, .0, .0); + Tmat = recip ? cm_bsdf_recip(diffBSDF, (SDMat *)tdf->comp[0].dist) + : cm_bsdf(diffBSDF, (SDMat *)tdf->comp[0].dist); /* free BSDF and return */ SDfreeBSDF(&myBSDF); return(Tmat);