45 |
|
SDError |
46 |
|
SDreportEnglish(SDError ec, FILE *fp) |
47 |
|
{ |
48 |
– |
if (fp == NULL) |
49 |
– |
return ec; |
48 |
|
if (!ec) |
49 |
|
return SDEnone; |
50 |
+ |
if ((ec < SDEnone) | (ec > SDEunknown)) { |
51 |
+ |
SDerrorDetail[0] = '\0'; |
52 |
+ |
ec = SDEunknown; |
53 |
+ |
} |
54 |
+ |
if (fp == NULL) |
55 |
+ |
return ec; |
56 |
|
fputs(SDerrorEnglish[ec], fp); |
57 |
|
if (SDerrorDetail[0]) { |
58 |
|
fputs(": ", fp); |
99 |
|
if ((geom = ezxml_child(wdb, "Thickness")) != NULL) |
100 |
|
sd->dim[2] = atof(ezxml_txt(geom)) * |
101 |
|
to_meters(ezxml_attr(geom, "unit")); |
102 |
< |
if ((sd->dim[0] < .0) | (sd->dim[1] < .0) | (sd->dim[2] < .0)) { |
102 |
> |
if ((sd->dim[0] < 0) | (sd->dim[1] < 0) | (sd->dim[2] < 0)) { |
103 |
|
sprintf(SDerrorDetail, "Negative size in \"%s\"", sd->name); |
104 |
|
return SDEdata; |
105 |
|
} |
168 |
|
return lastErr; |
169 |
|
/* try loading variable resolution data */ |
170 |
|
lastErr = SDloadTre(sd, wtl); |
171 |
+ |
#if 0 |
172 |
|
/* check our result */ |
173 |
|
switch (lastErr) { |
174 |
|
case SDEformat: |
179 |
|
default: /* variable res. OK else serious error */ |
180 |
|
break; |
181 |
|
} |
182 |
+ |
#endif |
183 |
|
/* done with XML file */ |
184 |
|
ezxml_free(fl); |
185 |
|
|
340 |
|
sdl->next = SDcacheList; |
341 |
|
SDcacheList = sdl; |
342 |
|
|
343 |
< |
sdl->refcnt++; |
343 |
> |
sdl->refcnt = 1; |
344 |
|
return &sdl->bsdf; |
345 |
|
} |
346 |
|
|
384 |
|
for (sdl = SDcacheList; sdl != NULL; sdl = (sdLast=sdl)->next) |
385 |
|
if (&sdl->bsdf == sd) |
386 |
|
break; |
387 |
< |
if (sdl == NULL || --sdl->refcnt) |
387 |
> |
if (sdl == NULL || (sdl->refcnt -= (sdl->refcnt > 0))) |
388 |
|
return; /* missing or still in use */ |
389 |
|
/* keep unreferenced data? */ |
390 |
|
if (SDisLoaded(sd) && SDretainSet) { |
407 |
|
|
408 |
|
/* Sample an individual BSDF component */ |
409 |
|
SDError |
410 |
< |
SDsampComponent(SDValue *sv, FVECT outVec, const FVECT inVec, |
405 |
< |
double randX, SDComponent *sdc) |
410 |
> |
SDsampComponent(SDValue *sv, FVECT ioVec, double randX, SDComponent *sdc) |
411 |
|
{ |
412 |
|
float coef[SDmaxCh]; |
413 |
|
SDError ec; |
414 |
+ |
FVECT inVec; |
415 |
|
const SDCDst *cd; |
416 |
|
double d; |
417 |
|
int n; |
418 |
|
/* check arguments */ |
419 |
< |
if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sdc == NULL)) |
419 |
> |
if ((sv == NULL) | (ioVec == NULL) | (sdc == NULL)) |
420 |
|
return SDEargument; |
421 |
|
/* get cumulative distribution */ |
422 |
+ |
VCOPY(inVec, ioVec); |
423 |
|
cd = (*sdc->func->getCDist)(inVec, sdc); |
424 |
|
if (cd == NULL) |
425 |
|
return SDEmemory; |
426 |
|
if (cd->cTotal <= 1e-7) { /* anything to sample? */ |
427 |
|
sv->spec = c_dfcolor; |
428 |
|
sv->cieY = .0; |
429 |
< |
memset(outVec, 0, 3*sizeof(double)); |
429 |
> |
memset(ioVec, 0, 3*sizeof(double)); |
430 |
|
return SDEnone; |
431 |
|
} |
432 |
|
sv->cieY = cd->cTotal; |
433 |
|
/* compute sample direction */ |
434 |
< |
ec = (*sdc->func->sampCDist)(outVec, randX, cd); |
434 |
> |
ec = (*sdc->func->sampCDist)(ioVec, randX, cd); |
435 |
|
if (ec) |
436 |
|
return ec; |
437 |
|
/* get BSDF color */ |
438 |
< |
n = (*sdc->func->getBSDFs)(coef, outVec, inVec, sdc->dist); |
438 |
> |
n = (*sdc->func->getBSDFs)(coef, ioVec, inVec, sdc); |
439 |
|
if (n <= 0) { |
440 |
|
strcpy(SDerrorDetail, "BSDF sample value error"); |
441 |
|
return SDEinternal; |
483 |
|
SDmultiSamp(outVec, 2, randX); |
484 |
|
SDsquare2disk(outVec, outVec[0], outVec[1]); |
485 |
|
outVec[2] = 1. - outVec[0]*outVec[0] - outVec[1]*outVec[1]; |
486 |
< |
if (outVec[2] > .0) /* a bit of paranoia */ |
486 |
> |
if (outVec[2] > 0) /* a bit of paranoia */ |
487 |
|
outVec[2] = sqrt(outVec[2]); |
488 |
|
if (!outFront) /* going out back? */ |
489 |
|
outVec[2] = -outVec[2]; |
491 |
|
|
492 |
|
/* Query projected solid angle coverage for non-diffuse BSDF direction */ |
493 |
|
SDError |
494 |
< |
SDsizeBSDF(double *projSA, const FVECT vec, int qflags, const SDData *sd) |
494 |
> |
SDsizeBSDF(double *projSA, const FVECT v1, const RREAL *v2, |
495 |
> |
int qflags, const SDData *sd) |
496 |
|
{ |
497 |
< |
SDSpectralDF *rdf; |
497 |
> |
SDSpectralDF *rdf, *tdf; |
498 |
|
SDError ec; |
499 |
|
int i; |
500 |
|
/* check arguments */ |
501 |
< |
if ((projSA == NULL) | (vec == NULL) | (sd == NULL)) |
501 |
> |
if ((projSA == NULL) | (v1 == NULL) | (sd == NULL)) |
502 |
|
return SDEargument; |
503 |
|
/* initialize extrema */ |
504 |
|
switch (qflags) { |
514 |
|
case 0: |
515 |
|
return SDEargument; |
516 |
|
} |
517 |
< |
if (vec[2] > .0) /* front surface query? */ |
517 |
> |
if (v1[2] > 0) /* front surface query? */ |
518 |
|
rdf = sd->rf; |
519 |
|
else |
520 |
|
rdf = sd->rb; |
521 |
+ |
tdf = sd->tf; |
522 |
+ |
if (v2 != NULL) /* bidirectional? */ |
523 |
+ |
if (v1[2] > 0 ^ v2[2] > 0) |
524 |
+ |
rdf = NULL; |
525 |
+ |
else |
526 |
+ |
tdf = NULL; |
527 |
|
ec = SDEdata; /* run through components */ |
528 |
|
for (i = (rdf==NULL) ? 0 : rdf->ncomp; i--; ) { |
529 |
< |
ec = (*rdf->comp[i].func->queryProjSA)(projSA, vec, qflags, |
530 |
< |
rdf->comp[i].dist); |
529 |
> |
ec = (*rdf->comp[i].func->queryProjSA)(projSA, v1, v2, |
530 |
> |
qflags, &rdf->comp[i]); |
531 |
|
if (ec) |
532 |
|
return ec; |
533 |
|
} |
534 |
< |
for (i = (sd->tf==NULL) ? 0 : sd->tf->ncomp; i--; ) { |
535 |
< |
ec = (*sd->tf->comp[i].func->queryProjSA)(projSA, vec, qflags, |
536 |
< |
sd->tf->comp[i].dist); |
534 |
> |
for (i = (tdf==NULL) ? 0 : tdf->ncomp; i--; ) { |
535 |
> |
ec = (*tdf->comp[i].func->queryProjSA)(projSA, v1, v2, |
536 |
> |
qflags, &tdf->comp[i]); |
537 |
|
if (ec) |
538 |
|
return ec; |
539 |
|
} |
557 |
|
if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sd == NULL)) |
558 |
|
return SDEargument; |
559 |
|
/* whose side are we on? */ |
560 |
< |
inFront = (inVec[2] > .0); |
561 |
< |
outFront = (outVec[2] > .0); |
560 |
> |
inFront = (inVec[2] > 0); |
561 |
> |
outFront = (outVec[2] > 0); |
562 |
|
/* start with diffuse portion */ |
563 |
|
if (inFront & outFront) { |
564 |
|
*sv = sd->rLambFront; |
575 |
|
i = (sdf != NULL) ? sdf->ncomp : 0; |
576 |
|
while (i-- > 0) { |
577 |
|
nch = (*sdf->comp[i].func->getBSDFs)(coef, outVec, inVec, |
578 |
< |
sdf->comp[i].dist); |
578 |
> |
&sdf->comp[i]); |
579 |
|
while (nch-- > 0) { |
580 |
|
c_cmix(&sv->spec, sv->cieY, &sv->spec, |
581 |
|
coef[nch], &sdf->comp[i].cspec[nch]); |
599 |
|
if ((inVec == NULL) | (sd == NULL)) |
600 |
|
return .0; |
601 |
|
/* gather diffuse components */ |
602 |
< |
if (inVec[2] > .0) { |
602 |
> |
if (inVec[2] > 0) { |
603 |
|
hsum = sd->rLambFront.cieY; |
604 |
|
rdf = sd->rf; |
605 |
|
} else /* !inFront */ { |
630 |
|
|
631 |
|
/* Sample BSDF direction based on the given random variable */ |
632 |
|
SDError |
633 |
< |
SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVec, |
620 |
< |
double randX, int sflags, const SDData *sd) |
633 |
> |
SDsampBSDF(SDValue *sv, FVECT ioVec, double randX, int sflags, const SDData *sd) |
634 |
|
{ |
635 |
|
SDError ec; |
636 |
+ |
FVECT inVec; |
637 |
|
int inFront; |
638 |
|
SDSpectralDF *rdf; |
639 |
|
double rdiff; |
642 |
|
SDComponent *sdc; |
643 |
|
const SDCDst **cdarr = NULL; |
644 |
|
/* check arguments */ |
645 |
< |
if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sd == NULL) | |
646 |
< |
(randX < .0) | (randX >= 1.)) |
645 |
> |
if ((sv == NULL) | (ioVec == NULL) | (sd == NULL) | |
646 |
> |
(randX < 0) | (randX >= 1.)) |
647 |
|
return SDEargument; |
648 |
|
/* whose side are we on? */ |
649 |
< |
inFront = (inVec[2] > .0); |
649 |
> |
VCOPY(inVec, ioVec); |
650 |
> |
inFront = (inVec[2] > 0); |
651 |
|
/* remember diffuse portions */ |
652 |
|
if (inFront) { |
653 |
|
*sv = sd->rLambFront; |
687 |
|
} |
688 |
|
if (sv->cieY <= 1e-7) { /* anything to sample? */ |
689 |
|
sv->cieY = .0; |
690 |
< |
memset(outVec, 0, 3*sizeof(double)); |
690 |
> |
memset(ioVec, 0, 3*sizeof(double)); |
691 |
|
return SDEnone; |
692 |
|
} |
693 |
|
/* scale random variable */ |
694 |
|
randX *= sv->cieY; |
695 |
|
/* diffuse reflection? */ |
696 |
|
if (randX < rdiff) { |
697 |
< |
SDdiffuseSamp(outVec, inFront, randX/rdiff); |
697 |
> |
SDdiffuseSamp(ioVec, inFront, randX/rdiff); |
698 |
|
goto done; |
699 |
|
} |
700 |
|
randX -= rdiff; |
702 |
|
if ((sflags & SDsampDf+SDsampT) == SDsampDf+SDsampT) { |
703 |
|
if (randX < sd->tLamb.cieY) { |
704 |
|
sv->spec = sd->tLamb.spec; |
705 |
< |
SDdiffuseSamp(outVec, !inFront, randX/sd->tLamb.cieY); |
705 |
> |
SDdiffuseSamp(ioVec, !inFront, randX/sd->tLamb.cieY); |
706 |
|
goto done; |
707 |
|
} |
708 |
|
randX -= sd->tLamb.cieY; |
714 |
|
return SDEinternal; |
715 |
|
/* compute sample direction */ |
716 |
|
sdc = (i < nr) ? &rdf->comp[i] : &sd->tf->comp[i-nr]; |
717 |
< |
ec = (*sdc->func->sampCDist)(outVec, randX/cdarr[i]->cTotal, cdarr[i]); |
717 |
> |
ec = (*sdc->func->sampCDist)(ioVec, randX/cdarr[i]->cTotal, cdarr[i]); |
718 |
|
if (ec) |
719 |
|
return ec; |
720 |
|
/* compute color */ |
721 |
< |
j = (*sdc->func->getBSDFs)(coef, outVec, inVec, sdc->dist); |
721 |
> |
j = (*sdc->func->getBSDFs)(coef, ioVec, inVec, sdc); |
722 |
|
if (j <= 0) { |
723 |
|
sprintf(SDerrorDetail, "BSDF \"%s\" sampling value error", |
724 |
|
sd->name); |
745 |
|
if ((vMtx == NULL) | (sNrm == NULL) | (uVec == NULL)) |
746 |
|
return SDEargument; |
747 |
|
VCOPY(vMtx[2], sNrm); |
748 |
< |
if (normalize(vMtx[2]) == .0) |
748 |
> |
if (normalize(vMtx[2]) == 0) |
749 |
|
return SDEargument; |
750 |
|
fcross(vMtx[0], uVec, vMtx[2]); |
751 |
< |
if (normalize(vMtx[0]) == .0) |
751 |
> |
if (normalize(vMtx[0]) == 0) |
752 |
|
return SDEargument; |
753 |
|
fcross(vMtx[1], vMtx[2], vMtx[0]); |
754 |
|
return SDEnone; |
768 |
|
mTmp[0][1] = vMtx[2][1]*vMtx[0][2] - vMtx[2][2]*vMtx[0][1]; |
769 |
|
mTmp[0][2] = vMtx[1][2]*vMtx[0][1] - vMtx[1][1]*vMtx[0][2]; |
770 |
|
d = vMtx[0][0]*mTmp[0][0] + vMtx[1][0]*mTmp[0][1] + vMtx[2][0]*mTmp[0][2]; |
771 |
< |
if (d == .0) { |
771 |
> |
if (d == 0) { |
772 |
|
strcpy(SDerrorDetail, "Zero determinant in matrix inversion"); |
773 |
|
return SDEargument; |
774 |
|
} |
795 |
|
if (vMtx == NULL) { /* assume they just want to normalize */ |
796 |
|
if (resVec != inpVec) |
797 |
|
VCOPY(resVec, inpVec); |
798 |
< |
return (normalize(resVec) > .0) ? SDEnone : SDEargument; |
798 |
> |
return (normalize(resVec) > 0) ? SDEnone : SDEargument; |
799 |
|
} |
800 |
|
vTmp[0] = DOT(vMtx[0], inpVec); |
801 |
|
vTmp[1] = DOT(vMtx[1], inpVec); |
802 |
|
vTmp[2] = DOT(vMtx[2], inpVec); |
803 |
< |
if (normalize(vTmp) == .0) |
803 |
> |
if (normalize(vTmp) == 0) |
804 |
|
return SDEargument; |
805 |
|
VCOPY(resVec, vTmp); |
806 |
|
return SDEnone; |
872 |
|
static int |
873 |
|
fequal(double a, double b) |
874 |
|
{ |
875 |
< |
if (b != .0) |
875 |
> |
if (b != 0) |
876 |
|
a = a/b - 1.; |
877 |
|
return((a <= 1e-6) & (a >= -1e-6)); |
878 |
|
} |
1190 |
|
hemi_total = .0; |
1191 |
|
for (i = dp->ninc; i--; ) { |
1192 |
|
dom = getBSDF_incohm(dp,i); |
1193 |
< |
if (dom <= .0) { |
1193 |
> |
if (dom <= 0) { |
1194 |
|
error(WARNING, "zero/negative incoming solid angle"); |
1195 |
|
continue; |
1196 |
|
} |
1213 |
|
hemi_total = .0; |
1214 |
|
for (o = dp->nout; o--; ) { |
1215 |
|
dom = getBSDF_outohm(dp,o); |
1216 |
< |
if (dom <= .0) { |
1216 |
> |
if (dom <= 0) { |
1217 |
|
error(WARNING, "zero/negative outgoing solid angle"); |
1218 |
|
continue; |
1219 |
|
} |
1237 |
|
hemi_total = .0; |
1238 |
|
for (o = dp->nout; o--; ) { |
1239 |
|
double f = BSDF_value(dp,i,o); |
1240 |
< |
if (f >= .0) |
1240 |
> |
if (f >= 0) |
1241 |
|
hemi_total += f*omega_oarr[o]; |
1242 |
|
else { |
1243 |
|
nneg += (f < -FTINY); |