--- ray/src/util/cmbsdf.c 2014/01/20 21:29:04 2.1 +++ ray/src/util/cmbsdf.c 2016/03/22 03:56:17 2.5 @@ -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.5 2016/03/22 03:56:17 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; @@ -33,11 +33,14 @@ cm_bsdf(const COLOR bsdfLamb, const COLOR specCol, con /* 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,c,r)); + 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; @@ -96,11 +99,14 @@ cm_bsdf_recip(const COLOR bsdfLamb, const COLOR specCo /* 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,ri,ro)); + ccy2rgb(&cxy, f, mp); + } else + setcolor(mp, f, f, f); + addcolor(mp, diffBSDF); scalecolor(mp, dom); } } @@ -113,9 +119,24 @@ 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; @@ -123,36 +144,31 @@ cm_loadBSDF(char *fname, COLOR cLamb) SDError ec; SDData myBSDF; SDSpectralDF *tdf; - COLOR bsdfLamb, specCol; + COLOR diffBSDF; /* 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); + error(SYSTEM, errmsg); } SDclearBSDF(&myBSDF, fname); /* load XML and check type */ ec = SDloadFile(&myBSDF, fpath); 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); 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);