10 |
|
* |
11 |
|
*/ |
12 |
|
|
13 |
+ |
#define _USE_MATH_DEFINES |
14 |
|
#include <stdio.h> |
15 |
|
#include <stdlib.h> |
16 |
+ |
#include <string.h> |
17 |
|
#include <math.h> |
18 |
|
#include "ezxml.h" |
19 |
|
#include "hilbert.h" |
171 |
|
/* try loading variable resolution data */ |
172 |
|
lastErr = SDloadTre(sd, wtl); |
173 |
|
/* check our result */ |
174 |
< |
switch (lastErr) { |
173 |
< |
case SDEformat: |
174 |
< |
case SDEdata: |
175 |
< |
case SDEsupport: /* possibly we just tried the wrong format */ |
174 |
> |
if (lastErr == SDEsupport) /* try matrix BSDF if not tree data */ |
175 |
|
lastErr = SDloadMtx(sd, wtl); |
176 |
< |
break; |
178 |
< |
default: /* variable res. OK else serious error */ |
179 |
< |
break; |
180 |
< |
} |
176 |
> |
|
177 |
|
/* done with XML file */ |
178 |
|
ezxml_free(fl); |
179 |
|
|
401 |
|
|
402 |
|
/* Sample an individual BSDF component */ |
403 |
|
SDError |
404 |
< |
SDsampComponent(SDValue *sv, FVECT outVec, const FVECT inVec, |
409 |
< |
double randX, SDComponent *sdc) |
404 |
> |
SDsampComponent(SDValue *sv, FVECT ioVec, double randX, SDComponent *sdc) |
405 |
|
{ |
406 |
|
float coef[SDmaxCh]; |
407 |
|
SDError ec; |
408 |
+ |
FVECT inVec; |
409 |
|
const SDCDst *cd; |
410 |
|
double d; |
411 |
|
int n; |
412 |
|
/* check arguments */ |
413 |
< |
if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sdc == NULL)) |
413 |
> |
if ((sv == NULL) | (ioVec == NULL) | (sdc == NULL)) |
414 |
|
return SDEargument; |
415 |
|
/* get cumulative distribution */ |
416 |
+ |
VCOPY(inVec, ioVec); |
417 |
|
cd = (*sdc->func->getCDist)(inVec, sdc); |
418 |
|
if (cd == NULL) |
419 |
|
return SDEmemory; |
420 |
|
if (cd->cTotal <= 1e-7) { /* anything to sample? */ |
421 |
|
sv->spec = c_dfcolor; |
422 |
|
sv->cieY = .0; |
423 |
< |
memset(outVec, 0, 3*sizeof(double)); |
423 |
> |
memset(ioVec, 0, 3*sizeof(double)); |
424 |
|
return SDEnone; |
425 |
|
} |
426 |
|
sv->cieY = cd->cTotal; |
427 |
|
/* compute sample direction */ |
428 |
< |
ec = (*sdc->func->sampCDist)(outVec, randX, cd); |
428 |
> |
ec = (*sdc->func->sampCDist)(ioVec, randX, cd); |
429 |
|
if (ec) |
430 |
|
return ec; |
431 |
|
/* get BSDF color */ |
432 |
< |
n = (*sdc->func->getBSDFs)(coef, outVec, inVec, sdc->dist); |
432 |
> |
n = (*sdc->func->getBSDFs)(coef, ioVec, inVec, sdc); |
433 |
|
if (n <= 0) { |
434 |
|
strcpy(SDerrorDetail, "BSDF sample value error"); |
435 |
|
return SDEinternal; |
492 |
|
SDError ec; |
493 |
|
int i; |
494 |
|
/* check arguments */ |
495 |
< |
if ((projSA == NULL) | (v1 == NULL)) |
495 |
> |
if ((projSA == NULL) | (v1 == NULL) | (sd == NULL)) |
496 |
|
return SDEargument; |
497 |
|
/* initialize extrema */ |
498 |
|
switch (qflags) { |
512 |
|
rdf = sd->rf; |
513 |
|
else |
514 |
|
rdf = sd->rb; |
515 |
< |
tdf = NULL; /* transmitted component? */ |
516 |
< |
if (v2 != NULL && v1[2] > 0 ^ v2[2] > 0) { |
517 |
< |
rdf = NULL; |
518 |
< |
tdf = sd->tf; |
519 |
< |
} |
515 |
> |
tdf = sd->tf; |
516 |
> |
if (v2 != NULL) /* bidirectional? */ |
517 |
> |
if (v1[2] > 0 ^ v2[2] > 0) |
518 |
> |
rdf = NULL; |
519 |
> |
else |
520 |
> |
tdf = NULL; |
521 |
|
ec = SDEdata; /* run through components */ |
522 |
|
for (i = (rdf==NULL) ? 0 : rdf->ncomp; i--; ) { |
523 |
|
ec = (*rdf->comp[i].func->queryProjSA)(projSA, v1, v2, |
524 |
< |
qflags, rdf->comp[i].dist); |
524 |
> |
qflags, &rdf->comp[i]); |
525 |
|
if (ec) |
526 |
|
return ec; |
527 |
|
} |
528 |
|
for (i = (tdf==NULL) ? 0 : tdf->ncomp; i--; ) { |
529 |
|
ec = (*tdf->comp[i].func->queryProjSA)(projSA, v1, v2, |
530 |
< |
qflags, tdf->comp[i].dist); |
530 |
> |
qflags, &tdf->comp[i]); |
531 |
|
if (ec) |
532 |
|
return ec; |
533 |
|
} |
569 |
|
i = (sdf != NULL) ? sdf->ncomp : 0; |
570 |
|
while (i-- > 0) { |
571 |
|
nch = (*sdf->comp[i].func->getBSDFs)(coef, outVec, inVec, |
572 |
< |
sdf->comp[i].dist); |
572 |
> |
&sdf->comp[i]); |
573 |
|
while (nch-- > 0) { |
574 |
|
c_cmix(&sv->spec, sv->cieY, &sv->spec, |
575 |
|
coef[nch], &sdf->comp[i].cspec[nch]); |
624 |
|
|
625 |
|
/* Sample BSDF direction based on the given random variable */ |
626 |
|
SDError |
627 |
< |
SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVec, |
630 |
< |
double randX, int sflags, const SDData *sd) |
627 |
> |
SDsampBSDF(SDValue *sv, FVECT ioVec, double randX, int sflags, const SDData *sd) |
628 |
|
{ |
629 |
|
SDError ec; |
630 |
+ |
FVECT inVec; |
631 |
|
int inFront; |
632 |
|
SDSpectralDF *rdf; |
633 |
|
double rdiff; |
636 |
|
SDComponent *sdc; |
637 |
|
const SDCDst **cdarr = NULL; |
638 |
|
/* check arguments */ |
639 |
< |
if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sd == NULL) | |
639 |
> |
if ((sv == NULL) | (ioVec == NULL) | (sd == NULL) | |
640 |
|
(randX < 0) | (randX >= 1.)) |
641 |
|
return SDEargument; |
642 |
|
/* whose side are we on? */ |
643 |
+ |
VCOPY(inVec, ioVec); |
644 |
|
inFront = (inVec[2] > 0); |
645 |
|
/* remember diffuse portions */ |
646 |
|
if (inFront) { |
681 |
|
} |
682 |
|
if (sv->cieY <= 1e-7) { /* anything to sample? */ |
683 |
|
sv->cieY = .0; |
684 |
< |
memset(outVec, 0, 3*sizeof(double)); |
684 |
> |
memset(ioVec, 0, 3*sizeof(double)); |
685 |
|
return SDEnone; |
686 |
|
} |
687 |
|
/* scale random variable */ |
688 |
|
randX *= sv->cieY; |
689 |
|
/* diffuse reflection? */ |
690 |
|
if (randX < rdiff) { |
691 |
< |
SDdiffuseSamp(outVec, inFront, randX/rdiff); |
691 |
> |
SDdiffuseSamp(ioVec, inFront, randX/rdiff); |
692 |
|
goto done; |
693 |
|
} |
694 |
|
randX -= rdiff; |
696 |
|
if ((sflags & SDsampDf+SDsampT) == SDsampDf+SDsampT) { |
697 |
|
if (randX < sd->tLamb.cieY) { |
698 |
|
sv->spec = sd->tLamb.spec; |
699 |
< |
SDdiffuseSamp(outVec, !inFront, randX/sd->tLamb.cieY); |
699 |
> |
SDdiffuseSamp(ioVec, !inFront, randX/sd->tLamb.cieY); |
700 |
|
goto done; |
701 |
|
} |
702 |
|
randX -= sd->tLamb.cieY; |
708 |
|
return SDEinternal; |
709 |
|
/* compute sample direction */ |
710 |
|
sdc = (i < nr) ? &rdf->comp[i] : &sd->tf->comp[i-nr]; |
711 |
< |
ec = (*sdc->func->sampCDist)(outVec, randX/cdarr[i]->cTotal, cdarr[i]); |
711 |
> |
ec = (*sdc->func->sampCDist)(ioVec, randX/cdarr[i]->cTotal, cdarr[i]); |
712 |
|
if (ec) |
713 |
|
return ec; |
714 |
|
/* compute color */ |
715 |
< |
j = (*sdc->func->getBSDFs)(coef, outVec, inVec, sdc->dist); |
715 |
> |
j = (*sdc->func->getBSDFs)(coef, ioVec, inVec, sdc); |
716 |
|
if (j <= 0) { |
717 |
|
sprintf(SDerrorDetail, "BSDF \"%s\" sampling value error", |
718 |
|
sd->name); |