| 23 |
|
|
| 24 |
|
#ifdef NEWAMB |
| 25 |
|
|
| 26 |
– |
/* #define AHEM_MARG 1.2 /* hem margin */ |
| 27 |
– |
|
| 26 |
|
extern void SDsquare2disk(double ds[2], double seedx, double seedy); |
| 27 |
|
|
| 28 |
|
/* vertex direction bit positions */ |
| 272 |
|
ambsupersamp(double acol[3], AMBHEMI *hp, int cnt) |
| 273 |
|
{ |
| 274 |
|
float *earr = getambdiffs(hp); |
| 275 |
< |
double e2sum = 0; |
| 275 |
> |
double e2sum = 0.0; |
| 276 |
|
AMBSAMP *ap; |
| 277 |
|
RAY ar; |
| 278 |
< |
COLOR asum; |
| 278 |
> |
double asum[3]; |
| 279 |
|
float *ep; |
| 280 |
|
int i, j, n; |
| 281 |
|
|
| 288 |
|
for (ap = hp->sa, i = 0; i < hp->ns; i++) |
| 289 |
|
for (j = 0; j < hp->ns; j++, ap++) { |
| 290 |
|
int nss = *ep/e2sum*cnt + frandom(); |
| 291 |
< |
setcolor(asum, 0., 0., 0.); |
| 291 |
> |
asum[0] = asum[1] = asum[2] = 0.0; |
| 292 |
|
for (n = 1; n <= nss; n++) { |
| 293 |
|
if (!getambsamp(&ar, hp, i, j, n)) { |
| 294 |
|
nss = n-1; |
| 299 |
|
if (nss) { /* update returned ambient value */ |
| 300 |
|
const double ssf = 1./(nss + 1); |
| 301 |
|
for (n = 3; n--; ) |
| 302 |
< |
acol[n] += ssf*colval(asum,n) + |
| 302 |
> |
acol[n] += ssf*asum[n] + |
| 303 |
|
(ssf - 1.)*colval(ap->v,n); |
| 304 |
|
} |
| 305 |
|
e2sum -= *ep++; /* update remainders */ |
| 718 |
|
} |
| 719 |
|
|
| 720 |
|
|
| 721 |
< |
/* Make sure radii don't extend beyond what we see in our periphery */ |
| 722 |
< |
static int |
| 723 |
< |
hem_radii(AMBHEMI *hp, FVECT uv[2], float ra[2]) |
| 721 |
> |
/* Compute potential light leak direction flags for cache value */ |
| 722 |
> |
static uint32 |
| 723 |
> |
ambcorral(AMBHEMI *hp, FVECT uv[2], const double r0, const double r1) |
| 724 |
|
{ |
| 725 |
< |
#ifdef AHEM_MARG |
| 726 |
< |
#define MAXDACCUM 47 |
| 727 |
< |
const double hemarg = AHEM_MARG*ambacc; /* hem margin */ |
| 728 |
< |
float radivisor2[MAXDACCUM+1]; |
| 729 |
< |
int i, j, k = hp->ns/10 + 1; /* around 5%ile */ |
| 730 |
< |
const int n2accum = (k < MAXDACCUM) ? k : MAXDACCUM ; |
| 731 |
< |
int na = 0; |
| 732 |
< |
double d; |
| 733 |
< |
/* circle around perimeter */ |
| 725 |
> |
const double max_d = 1.0/(minarad*ambacc + 0.001); |
| 726 |
> |
const double ang_res = 0.5*PI/(hp->ns-1); |
| 727 |
> |
const double ang_step = ang_res/((int)(16/PI*ang_res) + (1+FTINY)); |
| 728 |
> |
double avg_d = 0; |
| 729 |
> |
uint32 flgs = 0; |
| 730 |
> |
int i, j; |
| 731 |
> |
/* check distances above us */ |
| 732 |
> |
for (i = hp->ns*3/4; i-- > hp->ns>>2; ) |
| 733 |
> |
for (j = hp->ns*3/4; j-- > hp->ns>>2; ) |
| 734 |
> |
avg_d += ambsam(hp,i,j).d; |
| 735 |
> |
avg_d *= 4.0/(hp->ns*hp->ns); |
| 736 |
> |
if (avg_d >= max_d) /* too close to corral? */ |
| 737 |
> |
return(0); |
| 738 |
> |
/* else circle around perimeter */ |
| 739 |
|
for (i = 0; i < hp->ns; i++) |
| 740 |
|
for (j = 0; j < hp->ns; j += !i|(i==hp->ns-1) ? 1 : hp->ns-1) { |
| 741 |
|
AMBSAMP *ap = &ambsam(hp,i,j); |
| 739 |
– |
double radiv2 = 0; |
| 742 |
|
FVECT vec; |
| 743 |
< |
if (ap->d <= FTINY) |
| 744 |
< |
continue; |
| 743 |
> |
double u, v; |
| 744 |
> |
double ang, a1; |
| 745 |
> |
int abp; |
| 746 |
> |
if ((ap->d <= FTINY) | (ap->d >= max_d)) |
| 747 |
> |
continue; /* too far or too near */ |
| 748 |
|
VSUB(vec, ap->p, hp->rp->rop); |
| 749 |
< |
for (k = 2; k--; ) { |
| 750 |
< |
d = ap->d * DOT(vec, uv[k]) * ra[k]; |
| 751 |
< |
radiv2 += d*d; |
| 752 |
< |
} |
| 753 |
< |
radiv2 *= hemarg*hemarg * ap->d * ap->d; |
| 754 |
< |
if (radiv2 <= 1.0) |
| 755 |
< |
continue; |
| 751 |
< |
/* insert in percentile list */ |
| 752 |
< |
for (k = na; k && radiv2 > radivisor2[k-1]; k--) |
| 753 |
< |
radivisor2[k] = radivisor2[k-1]; |
| 754 |
< |
radivisor2[k] = radiv2; |
| 755 |
< |
na += (na < n2accum); |
| 749 |
> |
u = DOT(vec, uv[0]) * ap->d; |
| 750 |
> |
v = DOT(vec, uv[1]) * ap->d; |
| 751 |
> |
if ((r0*r0*u*u + r1*r1*v*v) * ap->d*ap->d <= 1.0) |
| 752 |
> |
continue; /* occluder outside ellipse */ |
| 753 |
> |
ang = atan2a(v, u); /* else set direction flags */ |
| 754 |
> |
for (a1 = ang-.5*ang_res; a1 <= ang+.5*ang_res; a1 += ang_step) |
| 755 |
> |
flgs |= 1L<<(int)(16/PI*(a1 + 2.*PI*(a1 < 0))); |
| 756 |
|
} |
| 757 |
< |
if (na < n2accum) /* current radii are OK? */ |
| 758 |
< |
return(0); |
| 759 |
< |
/* else apply divisor */ |
| 760 |
< |
d = 1.0/sqrt(radivisor2[na-1]); |
| 761 |
< |
ra[0] *= d; |
| 762 |
< |
ra[1] *= d; |
| 763 |
< |
return(1); |
| 764 |
< |
#undef MAXDACCUM |
| 765 |
< |
#else |
| 766 |
< |
return(0); |
| 767 |
< |
#endif |
| 757 |
> |
return(flgs); |
| 758 |
|
} |
| 759 |
|
|
| 760 |
|
|
| 766 |
|
FVECT uv[2], /* returned (optional) */ |
| 767 |
|
float ra[2], /* returned (optional) */ |
| 768 |
|
float pg[2], /* returned (optional) */ |
| 769 |
< |
float dg[2] /* returned (optional) */ |
| 769 |
> |
float dg[2], /* returned (optional) */ |
| 770 |
> |
uint32 *crlp /* returned (optional) */ |
| 771 |
|
) |
| 772 |
|
{ |
| 773 |
|
AMBHEMI *hp = inithemi(rcol, r, wt); |
| 787 |
|
pg[0] = pg[1] = 0.0; |
| 788 |
|
if (dg != NULL) |
| 789 |
|
dg[0] = dg[1] = 0.0; |
| 790 |
+ |
if (crlp != NULL) |
| 791 |
+ |
*crlp = 0; |
| 792 |
|
/* sample the hemisphere */ |
| 793 |
|
acol[0] = acol[1] = acol[2] = 0.0; |
| 794 |
|
cnt = 0; |
| 809 |
|
return(-1); /* return value w/o Hessian */ |
| 810 |
|
} |
| 811 |
|
cnt = ambssamp*wt + 0.5; /* perform super-sampling? */ |
| 812 |
< |
if (cnt > 0) |
| 812 |
> |
if (cnt > 8) |
| 813 |
|
ambsupersamp(acol, hp, cnt); |
| 814 |
|
copycolor(rcol, acol); /* final indirect irradiance/PI */ |
| 815 |
|
if ((ra == NULL) & (pg == NULL) & (dg == NULL)) { |
| 845 |
|
if (ra[0] > ra[1]) |
| 846 |
|
ra[0] = ra[1]; |
| 847 |
|
} |
| 855 |
– |
hem_radii(hp, uv, ra); |
| 848 |
|
if (ra[0] < minarad) { |
| 849 |
|
ra[0] = minarad; |
| 850 |
|
if (ra[1] < minarad) |
| 858 |
|
if (ra[0] > maxarad) |
| 859 |
|
ra[0] = maxarad; |
| 860 |
|
} |
| 861 |
+ |
if (crlp != NULL) /* flag encroached directions */ |
| 862 |
+ |
*crlp = ambcorral(hp, uv, ra[0]*ambacc, ra[1]*ambacc); |
| 863 |
|
if (pg != NULL) { /* cap gradient if necessary */ |
| 864 |
|
d = pg[0]*pg[0]*ra[0]*ra[0] + pg[1]*pg[1]*ra[1]*ra[1]; |
| 865 |
|
if (d > 1.0) { |