--- ray/src/util/cmbsdf.c 2014/01/20 22:18:29 2.2 +++ ray/src/util/cmbsdf.c 2021/01/19 23:32:00 2.9 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: cmbsdf.c,v 2.2 2014/01/20 22:18:29 greg Exp $"; +static const char RCSid[] = "$Id: cmbsdf.c,v 2.9 2021/01/19 23:32:00 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.2 2014/ /* Convert a BSDF to our matrix representation */ static CMATRIX * -cm_bsdf(const COLOR diffBSDF, 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,15 +28,18 @@ cm_bsdf(const COLOR diffBSDF, 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); + 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 diffBSDF, 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,15 +94,18 @@ cm_bsdf_recip(const COLOR diffBSDF, 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); + 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); } @@ -117,54 +123,91 @@ cm_bsdf_recip(const COLOR diffBSDF, const COLOR specCo static CMATRIX * cm_bsdf_Lamb(const COLOR diffBSDF) { - CMATRIX *cm = cm_alloc(145, 145); /* this is a hack */ - int r, c; + CMATRIX *cm = cm_alloc(145, 145); /* this is a hack */ + ANGLE_BASIS *abase = abase_list; + int r, c; - for (c = 0; c < cm->ncols; c++) + while (abase->nangles != cm->nrows) + if (++abase >= abase_list+nabases) + error(INTERNAL, "Code error in cm_bsdf_Lamb()"); + + for (c = 0; c < cm->ncols; c++) { + const double dom = io_getohm(c,abase); for (r = 0; r < cm->nrows; r++) { COLORV *mp = cm_lval(cm,r,c); copycolor(mp, diffBSDF); + scalecolor(mp, dom); } + } return(cm); } /* Load and convert a matrix BTDF from the given XML file */ CMATRIX * -cm_loadBTDF(char *fname) +cm_loadBTDF(const char *fname) { CMATRIX *Tmat; - char *fpath; int recip; SDError ec; SDData myBSDF; SDSpectralDF *tdf; - COLOR diffBSDF, 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 diffBTDF; + 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, diffBSDF); + ccy2rgb(&myBSDF.tLamb.spec, myBSDF.tLamb.cieY/PI, diffBTDF); recip = (myBSDF.tb == NULL); tdf = recip ? myBSDF.tf : myBSDF.tb; if (tdf == NULL) { /* no non-Lambertian transmission? */ SDfreeBSDF(&myBSDF); - return(cm_bsdf_Lamb(diffBSDF)); + return(cm_bsdf_Lamb(diffBTDF)); } 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(diffBSDF, specCol, (SDMat *)tdf->comp[0].dist) - : cm_bsdf(diffBSDF, specCol, (SDMat *)tdf->comp[0].dist); + Tmat = recip ? cm_bsdf_recip(diffBTDF, (SDMat *)tdf->comp[0].dist) + : cm_bsdf(diffBTDF, (SDMat *)tdf->comp[0].dist); /* free BSDF and return */ SDfreeBSDF(&myBSDF); return(Tmat); +} + +/* Load and convert a matrix BRDF from the given XML file */ +CMATRIX * +cm_loadBRDF(const char *fname, int backside) +{ + CMATRIX *Rmat; + SDError ec; + SDData myBSDF; + SDSpectralDF *rdf; + COLOR diffBRDF; + + SDclearBSDF(&myBSDF, fname); /* load XML and check type */ + ec = SDloadFile(&myBSDF, fname); + if (ec) + error(USER, transSDError(ec)); + if (backside) { + ccy2rgb(&myBSDF.rLambBack.spec, myBSDF.rLambBack.cieY/PI, diffBRDF); + rdf = myBSDF.rb; + } else { + ccy2rgb(&myBSDF.rLambFront.spec, myBSDF.rLambFront.cieY/PI, diffBRDF); + rdf = myBSDF.rf; + } + if (rdf == NULL) { /* no non-Lambertian reflection? */ + SDfreeBSDF(&myBSDF); + return(cm_bsdf_Lamb(diffBRDF)); + } + if (rdf->ncomp != 1 || rdf->comp[0].func != &SDhandleMtx) { + sprintf(errmsg, "unsupported BSDF '%s'", fname); + error(USER, errmsg); + } + /* convert BRDF to matrix */ + Rmat = cm_bsdf(diffBRDF, (SDMat *)rdf->comp[0].dist); + /* free BSDF and return */ + SDfreeBSDF(&myBSDF); + return(Rmat); }