| 12 |
|
#include "cone.h" |
| 13 |
|
#include "source.h" |
| 14 |
|
|
| 15 |
+ |
#ifndef NBSDFSAMPS |
| 16 |
+ |
#define NBSDFSAMPS 256 /* BSDF resampling count */ |
| 17 |
+ |
#endif |
| 18 |
|
|
| 19 |
|
COLORV * distarr = NULL; /* distribution array */ |
| 20 |
|
int distsiz = 0; |
| 21 |
|
COLORV * direct_discount = NULL; /* amount to take off direct */ |
| 22 |
|
|
| 23 |
+ |
|
| 24 |
|
void |
| 25 |
|
newdist( /* allocate & clear distribution array */ |
| 26 |
|
int siz |
| 657 |
|
/* clean up */ |
| 658 |
|
freecone(ob); |
| 659 |
|
return(1); |
| 660 |
+ |
} |
| 661 |
+ |
|
| 662 |
+ |
|
| 663 |
+ |
void |
| 664 |
+ |
redistribute( /* pass distarr ray sums through BSDF */ |
| 665 |
+ |
struct BSDF_data *b, |
| 666 |
+ |
int nalt, |
| 667 |
+ |
int nazi, |
| 668 |
+ |
FVECT u, |
| 669 |
+ |
FVECT v, |
| 670 |
+ |
FVECT w, |
| 671 |
+ |
MAT4 xm |
| 672 |
+ |
) |
| 673 |
+ |
{ |
| 674 |
+ |
int nout = 0; |
| 675 |
+ |
MAT4 mymat, inmat; |
| 676 |
+ |
COLORV *idist; |
| 677 |
+ |
COLORV *cp; |
| 678 |
+ |
FVECT dv; |
| 679 |
+ |
double wt; |
| 680 |
+ |
int i, j, k, c, o; |
| 681 |
+ |
COLOR col, cinc; |
| 682 |
+ |
/* copy incoming distribution */ |
| 683 |
+ |
if (b->ninc > distsiz) |
| 684 |
+ |
error(INTERNAL, "error 1 in redistribute"); |
| 685 |
+ |
idist = (COLORV *)malloc(sizeof(COLOR)*b->ninc); |
| 686 |
+ |
if (idist == NULL) |
| 687 |
+ |
error(SYSTEM, "out of memory in redistribute"); |
| 688 |
+ |
memcpy(idist, distarr, sizeof(COLOR)*b->ninc); |
| 689 |
+ |
/* compose direction transform */ |
| 690 |
+ |
for (i = 3; i--; ) { |
| 691 |
+ |
mymat[i][0] = u[i]; |
| 692 |
+ |
mymat[i][1] = v[i]; |
| 693 |
+ |
mymat[i][2] = w[i]; |
| 694 |
+ |
mymat[i][3] = 0.; |
| 695 |
+ |
} |
| 696 |
+ |
mymat[3][0] = mymat[3][1] = mymat[3][2] = 0.; |
| 697 |
+ |
mymat[3][3] = 1.; |
| 698 |
+ |
if (xm != NULL) |
| 699 |
+ |
multmat4(mymat, xm, mymat); |
| 700 |
+ |
for (i = 3; i--; ) { /* make sure it's normalized */ |
| 701 |
+ |
wt = 1./sqrt( mymat[0][i]*mymat[0][i] + |
| 702 |
+ |
mymat[1][i]*mymat[1][i] + |
| 703 |
+ |
mymat[2][i]*mymat[2][i] ); |
| 704 |
+ |
for (j = 3; j--; ) |
| 705 |
+ |
mymat[j][i] *= wt; |
| 706 |
+ |
} |
| 707 |
+ |
if (!invmat4(inmat, mymat)) /* need inverse as well */ |
| 708 |
+ |
error(INTERNAL, "cannot invert BSDF transform"); |
| 709 |
+ |
newdist(nalt*nazi); /* resample distribution */ |
| 710 |
+ |
for (i = b->ninc; i--; ) { |
| 711 |
+ |
int direct_out = -1; |
| 712 |
+ |
COLOR cdir; |
| 713 |
+ |
getBSDF_incvec(dv, b, i); /* compute incident irrad. */ |
| 714 |
+ |
multv3(dv, dv, mymat); |
| 715 |
+ |
if (dv[2] < 0.0) { |
| 716 |
+ |
dv[0] = -dv[0]; dv[1] = -dv[1]; dv[2] = -dv[2]; |
| 717 |
+ |
direct_out += (direct_discount != NULL); |
| 718 |
+ |
} |
| 719 |
+ |
wt = getBSDF_incohm(b, i); |
| 720 |
+ |
wt *= dv[2]; /* solid_angle*cosine(theta) */ |
| 721 |
+ |
cp = &idist[3*i]; |
| 722 |
+ |
copycolor(cinc, cp); |
| 723 |
+ |
scalecolor(cinc, wt); |
| 724 |
+ |
if (!direct_out) { /* discount direct contr. */ |
| 725 |
+ |
cp = &direct_discount[3*i]; |
| 726 |
+ |
copycolor(cdir, cp); |
| 727 |
+ |
scalecolor(cdir, -wt); |
| 728 |
+ |
if (b->nout != b->ninc) |
| 729 |
+ |
direct_out = flatindex(dv, nalt, nazi); |
| 730 |
+ |
else |
| 731 |
+ |
direct_out = i; /* assumes dist. mirroring */ |
| 732 |
+ |
} |
| 733 |
+ |
for (k = nalt; k--; ) /* loop over distribution */ |
| 734 |
+ |
for (j = nazi; j--; ) { |
| 735 |
+ |
int rstart = random(); |
| 736 |
+ |
for (c = NBSDFSAMPS; c--; ) { |
| 737 |
+ |
double sp[2]; |
| 738 |
+ |
multisamp(sp, 2, urand(rstart+c)); |
| 739 |
+ |
flatdir(dv, (k + sp[0])/nalt, |
| 740 |
+ |
(j + .5 - sp[1])/nazi); |
| 741 |
+ |
multv3(dv, dv, inmat); |
| 742 |
+ |
/* evaluate BSDF @ outgoing */ |
| 743 |
+ |
o = getBSDF_outndx(b, dv); |
| 744 |
+ |
if (o < 0) { |
| 745 |
+ |
nout++; |
| 746 |
+ |
continue; |
| 747 |
+ |
} |
| 748 |
+ |
wt = BSDF_value(b, i, o) * (1./NBSDFSAMPS); |
| 749 |
+ |
copycolor(col, cinc); |
| 750 |
+ |
if (b->nout != b->ninc) |
| 751 |
+ |
o = k*nazi + j; |
| 752 |
+ |
if (o == direct_out) |
| 753 |
+ |
addcolor(col, cdir); /* minus direct */ |
| 754 |
+ |
scalecolor(col, wt); |
| 755 |
+ |
cp = &distarr[3*o]; |
| 756 |
+ |
addcolor(cp, col); /* sum into distribution */ |
| 757 |
+ |
} |
| 758 |
+ |
} |
| 759 |
+ |
} |
| 760 |
+ |
free(idist); /* free temp space */ |
| 761 |
+ |
if (nout) { |
| 762 |
+ |
sprintf(errmsg, "missing %.1f%% of BSDF directions", |
| 763 |
+ |
100.*nout/(b->ninc*nalt*nazi*NBSDFSAMPS)); |
| 764 |
+ |
error(WARNING, errmsg); |
| 765 |
+ |
} |
| 766 |
|
} |