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 |
|
} |
169 |
|
/* try loading variable resolution data */ |
170 |
|
lastErr = SDloadTre(sd, wtl); |
171 |
|
/* check our result */ |
172 |
< |
switch (lastErr) { |
173 |
< |
case SDEformat: |
174 |
< |
case SDEdata: |
175 |
< |
case SDEsupport: /* possibly we just tried the wrong format */ |
172 |
> |
if (lastErr == SDEsupport) /* try matrix BSDF if not tree data */ |
173 |
|
lastErr = SDloadMtx(sd, wtl); |
174 |
< |
break; |
178 |
< |
default: /* variable res. OK else serious error */ |
179 |
< |
break; |
180 |
< |
} |
174 |
> |
|
175 |
|
/* done with XML file */ |
176 |
|
ezxml_free(fl); |
177 |
|
|
399 |
|
|
400 |
|
/* Sample an individual BSDF component */ |
401 |
|
SDError |
402 |
< |
SDsampComponent(SDValue *sv, FVECT outVec, const FVECT inVec, |
409 |
< |
double randX, SDComponent *sdc) |
402 |
> |
SDsampComponent(SDValue *sv, FVECT ioVec, double randX, SDComponent *sdc) |
403 |
|
{ |
404 |
|
float coef[SDmaxCh]; |
405 |
|
SDError ec; |
406 |
+ |
FVECT inVec; |
407 |
|
const SDCDst *cd; |
408 |
|
double d; |
409 |
|
int n; |
410 |
|
/* check arguments */ |
411 |
< |
if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sdc == NULL)) |
411 |
> |
if ((sv == NULL) | (ioVec == NULL) | (sdc == NULL)) |
412 |
|
return SDEargument; |
413 |
|
/* get cumulative distribution */ |
414 |
+ |
VCOPY(inVec, ioVec); |
415 |
|
cd = (*sdc->func->getCDist)(inVec, sdc); |
416 |
|
if (cd == NULL) |
417 |
|
return SDEmemory; |
418 |
|
if (cd->cTotal <= 1e-7) { /* anything to sample? */ |
419 |
|
sv->spec = c_dfcolor; |
420 |
|
sv->cieY = .0; |
421 |
< |
memset(outVec, 0, 3*sizeof(double)); |
421 |
> |
memset(ioVec, 0, 3*sizeof(double)); |
422 |
|
return SDEnone; |
423 |
|
} |
424 |
|
sv->cieY = cd->cTotal; |
425 |
|
/* compute sample direction */ |
426 |
< |
ec = (*sdc->func->sampCDist)(outVec, randX, cd); |
426 |
> |
ec = (*sdc->func->sampCDist)(ioVec, randX, cd); |
427 |
|
if (ec) |
428 |
|
return ec; |
429 |
|
/* get BSDF color */ |
430 |
< |
n = (*sdc->func->getBSDFs)(coef, outVec, inVec, sdc->dist); |
430 |
> |
n = (*sdc->func->getBSDFs)(coef, ioVec, inVec, sdc); |
431 |
|
if (n <= 0) { |
432 |
|
strcpy(SDerrorDetail, "BSDF sample value error"); |
433 |
|
return SDEinternal; |
475 |
|
SDmultiSamp(outVec, 2, randX); |
476 |
|
SDsquare2disk(outVec, outVec[0], outVec[1]); |
477 |
|
outVec[2] = 1. - outVec[0]*outVec[0] - outVec[1]*outVec[1]; |
478 |
< |
if (outVec[2] > .0) /* a bit of paranoia */ |
478 |
> |
if (outVec[2] > 0) /* a bit of paranoia */ |
479 |
|
outVec[2] = sqrt(outVec[2]); |
480 |
|
if (!outFront) /* going out back? */ |
481 |
|
outVec[2] = -outVec[2]; |
483 |
|
|
484 |
|
/* Query projected solid angle coverage for non-diffuse BSDF direction */ |
485 |
|
SDError |
486 |
< |
SDsizeBSDF(double *projSA, const FVECT vec, int qflags, const SDData *sd) |
486 |
> |
SDsizeBSDF(double *projSA, const FVECT v1, const RREAL *v2, |
487 |
> |
int qflags, const SDData *sd) |
488 |
|
{ |
489 |
< |
SDSpectralDF *rdf; |
489 |
> |
SDSpectralDF *rdf, *tdf; |
490 |
|
SDError ec; |
491 |
|
int i; |
492 |
|
/* check arguments */ |
493 |
< |
if ((projSA == NULL) | (vec == NULL) | (sd == NULL)) |
493 |
> |
if ((projSA == NULL) | (v1 == NULL) | (sd == NULL)) |
494 |
|
return SDEargument; |
495 |
|
/* initialize extrema */ |
496 |
|
switch (qflags) { |
506 |
|
case 0: |
507 |
|
return SDEargument; |
508 |
|
} |
509 |
< |
if (vec[2] > .0) /* front surface query? */ |
509 |
> |
if (v1[2] > 0) /* front surface query? */ |
510 |
|
rdf = sd->rf; |
511 |
|
else |
512 |
|
rdf = sd->rb; |
513 |
+ |
tdf = sd->tf; |
514 |
+ |
if (v2 != NULL) /* bidirectional? */ |
515 |
+ |
if (v1[2] > 0 ^ v2[2] > 0) |
516 |
+ |
rdf = NULL; |
517 |
+ |
else |
518 |
+ |
tdf = NULL; |
519 |
|
ec = SDEdata; /* run through components */ |
520 |
|
for (i = (rdf==NULL) ? 0 : rdf->ncomp; i--; ) { |
521 |
< |
ec = (*rdf->comp[i].func->queryProjSA)(projSA, vec, qflags, |
522 |
< |
rdf->comp[i].dist); |
521 |
> |
ec = (*rdf->comp[i].func->queryProjSA)(projSA, v1, v2, |
522 |
> |
qflags, &rdf->comp[i]); |
523 |
|
if (ec) |
524 |
|
return ec; |
525 |
|
} |
526 |
< |
for (i = (sd->tf==NULL) ? 0 : sd->tf->ncomp; i--; ) { |
527 |
< |
ec = (*sd->tf->comp[i].func->queryProjSA)(projSA, vec, qflags, |
528 |
< |
sd->tf->comp[i].dist); |
526 |
> |
for (i = (tdf==NULL) ? 0 : tdf->ncomp; i--; ) { |
527 |
> |
ec = (*tdf->comp[i].func->queryProjSA)(projSA, v1, v2, |
528 |
> |
qflags, &tdf->comp[i]); |
529 |
|
if (ec) |
530 |
|
return ec; |
531 |
|
} |
549 |
|
if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sd == NULL)) |
550 |
|
return SDEargument; |
551 |
|
/* whose side are we on? */ |
552 |
< |
inFront = (inVec[2] > .0); |
553 |
< |
outFront = (outVec[2] > .0); |
552 |
> |
inFront = (inVec[2] > 0); |
553 |
> |
outFront = (outVec[2] > 0); |
554 |
|
/* start with diffuse portion */ |
555 |
|
if (inFront & outFront) { |
556 |
|
*sv = sd->rLambFront; |
567 |
|
i = (sdf != NULL) ? sdf->ncomp : 0; |
568 |
|
while (i-- > 0) { |
569 |
|
nch = (*sdf->comp[i].func->getBSDFs)(coef, outVec, inVec, |
570 |
< |
sdf->comp[i].dist); |
570 |
> |
&sdf->comp[i]); |
571 |
|
while (nch-- > 0) { |
572 |
|
c_cmix(&sv->spec, sv->cieY, &sv->spec, |
573 |
|
coef[nch], &sdf->comp[i].cspec[nch]); |
591 |
|
if ((inVec == NULL) | (sd == NULL)) |
592 |
|
return .0; |
593 |
|
/* gather diffuse components */ |
594 |
< |
if (inVec[2] > .0) { |
594 |
> |
if (inVec[2] > 0) { |
595 |
|
hsum = sd->rLambFront.cieY; |
596 |
|
rdf = sd->rf; |
597 |
|
} else /* !inFront */ { |
622 |
|
|
623 |
|
/* Sample BSDF direction based on the given random variable */ |
624 |
|
SDError |
625 |
< |
SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVec, |
624 |
< |
double randX, int sflags, const SDData *sd) |
625 |
> |
SDsampBSDF(SDValue *sv, FVECT ioVec, double randX, int sflags, const SDData *sd) |
626 |
|
{ |
627 |
|
SDError ec; |
628 |
+ |
FVECT inVec; |
629 |
|
int inFront; |
630 |
|
SDSpectralDF *rdf; |
631 |
|
double rdiff; |
634 |
|
SDComponent *sdc; |
635 |
|
const SDCDst **cdarr = NULL; |
636 |
|
/* check arguments */ |
637 |
< |
if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sd == NULL) | |
638 |
< |
(randX < .0) | (randX >= 1.)) |
637 |
> |
if ((sv == NULL) | (ioVec == NULL) | (sd == NULL) | |
638 |
> |
(randX < 0) | (randX >= 1.)) |
639 |
|
return SDEargument; |
640 |
|
/* whose side are we on? */ |
641 |
< |
inFront = (inVec[2] > .0); |
641 |
> |
VCOPY(inVec, ioVec); |
642 |
> |
inFront = (inVec[2] > 0); |
643 |
|
/* remember diffuse portions */ |
644 |
|
if (inFront) { |
645 |
|
*sv = sd->rLambFront; |
679 |
|
} |
680 |
|
if (sv->cieY <= 1e-7) { /* anything to sample? */ |
681 |
|
sv->cieY = .0; |
682 |
< |
memset(outVec, 0, 3*sizeof(double)); |
682 |
> |
memset(ioVec, 0, 3*sizeof(double)); |
683 |
|
return SDEnone; |
684 |
|
} |
685 |
|
/* scale random variable */ |
686 |
|
randX *= sv->cieY; |
687 |
|
/* diffuse reflection? */ |
688 |
|
if (randX < rdiff) { |
689 |
< |
SDdiffuseSamp(outVec, inFront, randX/rdiff); |
689 |
> |
SDdiffuseSamp(ioVec, inFront, randX/rdiff); |
690 |
|
goto done; |
691 |
|
} |
692 |
|
randX -= rdiff; |
694 |
|
if ((sflags & SDsampDf+SDsampT) == SDsampDf+SDsampT) { |
695 |
|
if (randX < sd->tLamb.cieY) { |
696 |
|
sv->spec = sd->tLamb.spec; |
697 |
< |
SDdiffuseSamp(outVec, !inFront, randX/sd->tLamb.cieY); |
697 |
> |
SDdiffuseSamp(ioVec, !inFront, randX/sd->tLamb.cieY); |
698 |
|
goto done; |
699 |
|
} |
700 |
|
randX -= sd->tLamb.cieY; |
706 |
|
return SDEinternal; |
707 |
|
/* compute sample direction */ |
708 |
|
sdc = (i < nr) ? &rdf->comp[i] : &sd->tf->comp[i-nr]; |
709 |
< |
ec = (*sdc->func->sampCDist)(outVec, randX/cdarr[i]->cTotal, cdarr[i]); |
709 |
> |
ec = (*sdc->func->sampCDist)(ioVec, randX/cdarr[i]->cTotal, cdarr[i]); |
710 |
|
if (ec) |
711 |
|
return ec; |
712 |
|
/* compute color */ |
713 |
< |
j = (*sdc->func->getBSDFs)(coef, outVec, inVec, sdc->dist); |
713 |
> |
j = (*sdc->func->getBSDFs)(coef, ioVec, inVec, sdc); |
714 |
|
if (j <= 0) { |
715 |
|
sprintf(SDerrorDetail, "BSDF \"%s\" sampling value error", |
716 |
|
sd->name); |
737 |
|
if ((vMtx == NULL) | (sNrm == NULL) | (uVec == NULL)) |
738 |
|
return SDEargument; |
739 |
|
VCOPY(vMtx[2], sNrm); |
740 |
< |
if (normalize(vMtx[2]) == .0) |
740 |
> |
if (normalize(vMtx[2]) == 0) |
741 |
|
return SDEargument; |
742 |
|
fcross(vMtx[0], uVec, vMtx[2]); |
743 |
< |
if (normalize(vMtx[0]) == .0) |
743 |
> |
if (normalize(vMtx[0]) == 0) |
744 |
|
return SDEargument; |
745 |
|
fcross(vMtx[1], vMtx[2], vMtx[0]); |
746 |
|
return SDEnone; |
760 |
|
mTmp[0][1] = vMtx[2][1]*vMtx[0][2] - vMtx[2][2]*vMtx[0][1]; |
761 |
|
mTmp[0][2] = vMtx[1][2]*vMtx[0][1] - vMtx[1][1]*vMtx[0][2]; |
762 |
|
d = vMtx[0][0]*mTmp[0][0] + vMtx[1][0]*mTmp[0][1] + vMtx[2][0]*mTmp[0][2]; |
763 |
< |
if (d == .0) { |
763 |
> |
if (d == 0) { |
764 |
|
strcpy(SDerrorDetail, "Zero determinant in matrix inversion"); |
765 |
|
return SDEargument; |
766 |
|
} |
787 |
|
if (vMtx == NULL) { /* assume they just want to normalize */ |
788 |
|
if (resVec != inpVec) |
789 |
|
VCOPY(resVec, inpVec); |
790 |
< |
return (normalize(resVec) > .0) ? SDEnone : SDEargument; |
790 |
> |
return (normalize(resVec) > 0) ? SDEnone : SDEargument; |
791 |
|
} |
792 |
|
vTmp[0] = DOT(vMtx[0], inpVec); |
793 |
|
vTmp[1] = DOT(vMtx[1], inpVec); |
794 |
|
vTmp[2] = DOT(vMtx[2], inpVec); |
795 |
< |
if (normalize(vTmp) == .0) |
795 |
> |
if (normalize(vTmp) == 0) |
796 |
|
return SDEargument; |
797 |
|
VCOPY(resVec, vTmp); |
798 |
|
return SDEnone; |
864 |
|
static int |
865 |
|
fequal(double a, double b) |
866 |
|
{ |
867 |
< |
if (b != .0) |
867 |
> |
if (b != 0) |
868 |
|
a = a/b - 1.; |
869 |
|
return((a <= 1e-6) & (a >= -1e-6)); |
870 |
|
} |
1182 |
|
hemi_total = .0; |
1183 |
|
for (i = dp->ninc; i--; ) { |
1184 |
|
dom = getBSDF_incohm(dp,i); |
1185 |
< |
if (dom <= .0) { |
1185 |
> |
if (dom <= 0) { |
1186 |
|
error(WARNING, "zero/negative incoming solid angle"); |
1187 |
|
continue; |
1188 |
|
} |
1205 |
|
hemi_total = .0; |
1206 |
|
for (o = dp->nout; o--; ) { |
1207 |
|
dom = getBSDF_outohm(dp,o); |
1208 |
< |
if (dom <= .0) { |
1208 |
> |
if (dom <= 0) { |
1209 |
|
error(WARNING, "zero/negative outgoing solid angle"); |
1210 |
|
continue; |
1211 |
|
} |
1229 |
|
hemi_total = .0; |
1230 |
|
for (o = dp->nout; o--; ) { |
1231 |
|
double f = BSDF_value(dp,i,o); |
1232 |
< |
if (f >= .0) |
1232 |
> |
if (f >= 0) |
1233 |
|
hemi_total += f*omega_oarr[o]; |
1234 |
|
else { |
1235 |
|
nneg += (f < -FTINY); |