| 50 |
|
|
| 51 |
|
typedef struct { |
| 52 |
|
COLOR v; /* hemisphere sample value */ |
| 53 |
+ |
float d; /* reciprocal distance (1/rt) */ |
| 54 |
|
FVECT p; /* intersection point */ |
| 55 |
|
} AMBSAMP; /* sample value */ |
| 56 |
|
|
| 208 |
|
AMBSAMP *ap = &ambsam(hp,i,j); |
| 209 |
|
RAY ar; |
| 210 |
|
/* generate hemispherical sample */ |
| 211 |
< |
if (!getambsamp(&ar, hp, i, j, 0)) |
| 212 |
< |
goto badsample; |
| 213 |
< |
/* limit vertex distance */ |
| 211 |
> |
if (!getambsamp(&ar, hp, i, j, 0) || ar.rt <= FTINY) { |
| 212 |
> |
memset(ap, 0, sizeof(AMBSAMP)); |
| 213 |
> |
return(NULL); |
| 214 |
> |
} |
| 215 |
> |
ap->d = 1.0/ar.rt; /* limit vertex distance */ |
| 216 |
|
if (ar.rt > 10.0*thescene.cusize) |
| 217 |
|
ar.rt = 10.0*thescene.cusize; |
| 215 |
– |
else if (ar.rt <= FTINY) /* should never happen! */ |
| 216 |
– |
goto badsample; |
| 218 |
|
VSUM(ap->p, ar.rorg, ar.rdir, ar.rt); |
| 219 |
|
copycolor(ap->v, ar.rcol); |
| 220 |
|
return(ap); |
| 220 |
– |
badsample: |
| 221 |
– |
setcolor(ap->v, 0., 0., 0.); |
| 222 |
– |
VCOPY(ap->p, hp->rp->rop); |
| 223 |
– |
return(NULL); |
| 221 |
|
} |
| 222 |
|
|
| 223 |
|
|
| 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 */ |
| 314 |
|
vertex_flags(AMBHEMI *hp) |
| 315 |
|
{ |
| 316 |
|
uby8 *vflags = (uby8 *)calloc(hp->ns*hp->ns, sizeof(uby8)); |
| 320 |
– |
double *dist2a = (double *)malloc(sizeof(double)*hp->ns); |
| 317 |
|
uby8 *vf; |
| 318 |
+ |
AMBSAMP *ap; |
| 319 |
|
int i, j; |
| 320 |
|
|
| 321 |
< |
if ((vflags == NULL) | (dist2a == NULL)) |
| 321 |
> |
if (vflags == NULL) |
| 322 |
|
error(SYSTEM, "out of memory in vertex_flags()"); |
| 323 |
< |
vf = vflags; /* compute distances along first row */ |
| 324 |
< |
for (j = 0; j < hp->ns; j++) { |
| 325 |
< |
dist2a[j] = dist2(ambsam(hp,0,j).p, hp->rp->rop); |
| 326 |
< |
++vf; |
| 327 |
< |
if (!j) continue; |
| 331 |
< |
if (dist2a[j] >= dist2a[j-1]) |
| 332 |
< |
vf[0] |= 1<<VDB_x; |
| 323 |
> |
vf = vflags; |
| 324 |
> |
ap = hp->sa; /* compute farthest along first row */ |
| 325 |
> |
for (j = 0; j < hp->ns-1; j++, vf++, ap++) |
| 326 |
> |
if (ap[0].d <= ap[1].d) |
| 327 |
> |
vf[0] |= 1<<VDB_X; |
| 328 |
|
else |
| 329 |
< |
vf[-1] |= 1<<VDB_X; |
| 330 |
< |
} |
| 329 |
> |
vf[1] |= 1<<VDB_x; |
| 330 |
> |
++vf; ++ap; |
| 331 |
|
/* flag subsequent rows */ |
| 332 |
|
for (i = 1; i < hp->ns; i++) { |
| 333 |
< |
double d2n = dist2(ambsam(hp,i,0).p, hp->rp->rop); |
| 334 |
< |
for (j = 0; j < hp->ns-1; j++) { |
| 340 |
< |
double d2 = d2n; |
| 341 |
< |
if (d2 >= dist2a[j]) /* row before */ |
| 333 |
> |
for (j = 0; j < hp->ns-1; j++, vf++, ap++) { |
| 334 |
> |
if (ap[0].d <= ap[-hp->ns].d) /* row before */ |
| 335 |
|
vf[0] |= 1<<VDB_y; |
| 336 |
|
else |
| 337 |
|
vf[-hp->ns] |= 1<<VDB_Y; |
| 338 |
< |
dist2a[j] = d2n; |
| 346 |
< |
if (d2 >= dist2a[j+1]) /* diagonal we care about */ |
| 338 |
> |
if (ap[0].d <= ap[1-hp->ns].d) /* diagonal we care about */ |
| 339 |
|
vf[0] |= 1<<VDB_Xy; |
| 340 |
|
else |
| 341 |
|
vf[1-hp->ns] |= 1<<VDB_xY; |
| 342 |
< |
d2n = dist2(ambsam(hp,i,j+1).p, hp->rp->rop); |
| 351 |
< |
if (d2 >= d2n) /* column after */ |
| 342 |
> |
if (ap[0].d <= ap[1].d) /* column after */ |
| 343 |
|
vf[0] |= 1<<VDB_X; |
| 344 |
|
else |
| 345 |
|
vf[1] |= 1<<VDB_x; |
| 355 |
– |
++vf; |
| 346 |
|
} |
| 347 |
< |
if (d2n >= dist2a[j]) /* final column edge */ |
| 347 |
> |
if (ap[0].d <= ap[-hp->ns].d) /* final column edge */ |
| 348 |
|
vf[0] |= 1<<VDB_y; |
| 349 |
|
else |
| 350 |
|
vf[-hp->ns] |= 1<<VDB_Y; |
| 351 |
< |
dist2a[j] = d2n; |
| 362 |
< |
++vf; |
| 351 |
> |
++vf; ++ap; |
| 352 |
|
} |
| 364 |
– |
free(dist2a); |
| 353 |
|
return(vflags); |
| 354 |
|
} |
| 355 |
|
|
| 718 |
|
} |
| 719 |
|
|
| 720 |
|
|
| 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 |
+ |
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 |
+ |
uint32 flgs = 0; |
| 729 |
+ |
int i, j; |
| 730 |
+ |
/* circle around perimeter */ |
| 731 |
+ |
for (i = 0; i < hp->ns; i++) |
| 732 |
+ |
for (j = 0; j < hp->ns; j += !i|(i==hp->ns-1) ? 1 : hp->ns-1) { |
| 733 |
+ |
AMBSAMP *ap = &ambsam(hp,i,j); |
| 734 |
+ |
FVECT vec; |
| 735 |
+ |
double u, v; |
| 736 |
+ |
double ang, a1; |
| 737 |
+ |
int abp; |
| 738 |
+ |
if ((ap->d <= FTINY) | (ap->d >= max_d)) |
| 739 |
+ |
continue; /* too far or too near */ |
| 740 |
+ |
VSUB(vec, ap->p, hp->rp->rop); |
| 741 |
+ |
u = DOT(vec, uv[0]) * ap->d; |
| 742 |
+ |
v = DOT(vec, uv[1]) * ap->d; |
| 743 |
+ |
if ((r0*r0*u*u + r1*r1*v*v) * ap->d*ap->d <= 1.0) |
| 744 |
+ |
continue; /* occluder outside ellipse */ |
| 745 |
+ |
ang = atan2a(v, u); /* else set direction flags */ |
| 746 |
+ |
for (a1 = ang-.5*ang_res; a1 <= ang+.5*ang_res; a1 += ang_step) |
| 747 |
+ |
flgs |= 1L<<(int)(16/PI*(a1 + 2.*PI*(a1 < 0))); |
| 748 |
+ |
} |
| 749 |
+ |
return(flgs); |
| 750 |
+ |
} |
| 751 |
+ |
|
| 752 |
+ |
|
| 753 |
|
int |
| 754 |
|
doambient( /* compute ambient component */ |
| 755 |
|
COLOR rcol, /* input/output color */ |
| 758 |
|
FVECT uv[2], /* returned (optional) */ |
| 759 |
|
float ra[2], /* returned (optional) */ |
| 760 |
|
float pg[2], /* returned (optional) */ |
| 761 |
< |
float dg[2] /* returned (optional) */ |
| 761 |
> |
float dg[2], /* returned (optional) */ |
| 762 |
> |
uint32 *crlp /* returned (optional) */ |
| 763 |
|
) |
| 764 |
|
{ |
| 765 |
|
AMBHEMI *hp = inithemi(rcol, r, wt); |
| 779 |
|
pg[0] = pg[1] = 0.0; |
| 780 |
|
if (dg != NULL) |
| 781 |
|
dg[0] = dg[1] = 0.0; |
| 782 |
+ |
if (crlp != NULL) |
| 783 |
+ |
*crlp = 0; |
| 784 |
|
/* sample the hemisphere */ |
| 785 |
|
acol[0] = acol[1] = acol[2] = 0.0; |
| 786 |
|
cnt = 0; |
| 801 |
|
return(-1); /* return value w/o Hessian */ |
| 802 |
|
} |
| 803 |
|
cnt = ambssamp*wt + 0.5; /* perform super-sampling? */ |
| 804 |
< |
if (cnt > 0) |
| 804 |
> |
if (cnt > 8) |
| 805 |
|
ambsupersamp(acol, hp, cnt); |
| 806 |
|
copycolor(rcol, acol); /* final indirect irradiance/PI */ |
| 807 |
|
if ((ra == NULL) & (pg == NULL) & (dg == NULL)) { |
| 850 |
|
if (ra[0] > maxarad) |
| 851 |
|
ra[0] = maxarad; |
| 852 |
|
} |
| 853 |
+ |
if (crlp != NULL) /* flag encroached directions */ |
| 854 |
+ |
*crlp = ambcorral(hp, uv, ra[0]*ambacc, ra[1]*ambacc); |
| 855 |
|
if (pg != NULL) { /* cap gradient if necessary */ |
| 856 |
|
d = pg[0]*pg[0]*ra[0]*ra[0] + pg[1]*pg[1]*ra[1]*ra[1]; |
| 857 |
|
if (d > 1.0) { |