--- ray/src/common/bsdf.c 2012/10/13 20:15:43 2.43 +++ ray/src/common/bsdf.c 2015/02/08 22:14:50 2.50 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: bsdf.c,v 2.43 2012/10/13 20:15:43 greg Exp $"; +static const char RCSid[] = "$Id: bsdf.c,v 2.50 2015/02/08 22:14:50 greg Exp $"; #endif /* * bsdf.c @@ -35,6 +35,9 @@ const char *SDerrorEnglish[] = { "Unknown error" }; +/* Pointer to error list in preferred language */ +const char **SDerrorList = SDerrorEnglish; + /* Additional information on last error (ASCII English) */ char SDerrorDetail[256]; @@ -47,9 +50,9 @@ struct SDCache_s *SDcacheList = NULL; /* Retain BSDFs in cache list */ int SDretainSet = SDretainNone; -/* Report any error to the indicated stream (in English) */ +/* Report any error to the indicated stream */ SDError -SDreportEnglish(SDError ec, FILE *fp) +SDreportError(SDError ec, FILE *fp) { if (!ec) return SDEnone; @@ -59,7 +62,7 @@ SDreportEnglish(SDError ec, FILE *fp) } if (fp == NULL) return ec; - fputs(SDerrorEnglish[ec], fp); + fputs(SDerrorList[ec], fp); if (SDerrorDetail[0]) { fputs(": ", fp); fputs(SDerrorDetail, fp); @@ -196,7 +199,7 @@ SDloadFile(SDData *sd, const char *fname) } wtl = ezxml_child(ezxml_child(fl, "Optical"), "Layer"); if (wtl == NULL) { - sprintf(SDerrorDetail, "BSDF \"%s\": no optical layers'", + sprintf(SDerrorDetail, "BSDF \"%s\": no optical layers", sd->name); ezxml_free(fl); return SDEformat; @@ -420,11 +423,11 @@ SDcacheFile(const char *fname) return NULL; SDerrorDetail[0] = '\0'; if ((sd = SDgetCache(fname)) == NULL) { - SDreportEnglish(SDEmemory, stderr); + SDreportError(SDEmemory, stderr); return NULL; } if (!SDisLoaded(sd) && (ec = SDloadFile(sd, fname))) { - SDreportEnglish(ec, stderr); + SDreportError(ec, stderr); SDfreeCache(sd); return NULL; } @@ -485,16 +488,15 @@ SDsampComponent(SDValue *sv, FVECT ioVec, double randX return SDEargument; /* get cumulative distribution */ VCOPY(inVec, ioVec); + sv->cieY = 0; cd = (*sdc->func->getCDist)(inVec, sdc); - if (cd == NULL) - return SDEmemory; - if (cd->cTotal <= 1e-6) { /* anything to sample? */ + if (cd != NULL) + sv->cieY = cd->cTotal; + if (sv->cieY <= 1e-6) { /* nothing to sample? */ sv->spec = c_dfcolor; - sv->cieY = .0; memset(ioVec, 0, 3*sizeof(double)); return SDEnone; } - sv->cieY = cd->cTotal; /* compute sample direction */ ec = (*sdc->func->sampCDist)(ioVec, randX, cd); if (ec) @@ -556,8 +558,7 @@ SDdiffuseSamp(FVECT outVec, int outFront, double randX SDmultiSamp(outVec, 2, randX); SDsquare2disk(outVec, outVec[0], outVec[1]); outVec[2] = 1. - outVec[0]*outVec[0] - outVec[1]*outVec[1]; - if (outVec[2] > 0) /* a bit of paranoia */ - outVec[2] = sqrt(outVec[2]); + outVec[2] = sqrt(outVec[2]*(outVec[2]>0)); if (!outFront) /* going out back? */ outVec[2] = -outVec[2]; } @@ -616,7 +617,8 @@ SDsizeBSDF(double *projSA, const FVECT v1, const RREAL projSA[0] = M_PI; if (qflags == SDqueryMin+SDqueryMax) projSA[1] = M_PI; - } + } else if (qflags == SDqueryMin+SDqueryMax && projSA[0] > projSA[1]) + projSA[0] = projSA[1]; return SDEnone; } @@ -753,18 +755,14 @@ SDsampBSDF(SDValue *sv, FVECT ioVec, double randX, int return SDEmemory; while (j-- > 0) { /* non-diffuse transmission */ cdarr[i+j] = (*tdf->comp[j].func->getCDist)(inVec, &tdf->comp[j]); - if (cdarr[i+j] == NULL) { - free(cdarr); - return SDEmemory; - } + if (cdarr[i+j] == NULL) + cdarr[i+j] = &SDemptyCD; sv->cieY += cdarr[i+j]->cTotal; } while (i-- > 0) { /* non-diffuse reflection */ cdarr[i] = (*rdf->comp[i].func->getCDist)(inVec, &rdf->comp[i]); - if (cdarr[i] == NULL) { - free(cdarr); - return SDEmemory; - } + if (cdarr[i] == NULL) + cdarr[i] = &SDemptyCD; sv->cieY += cdarr[i]->cTotal; } if (sv->cieY <= 1e-6) { /* anything to sample? */ @@ -790,7 +788,7 @@ SDsampBSDF(SDValue *sv, FVECT ioVec, double randX, int randX -= sd->tLamb.cieY; } /* else one of cumulative dist. */ - for (i = 0; i < n && randX < cdarr[i]->cTotal; i++) + for (i = 0; i < n && randX > cdarr[i]->cTotal; i++) randX -= cdarr[i]->cTotal; if (i >= n) return SDEinternal;