| 26 |
|
#endif |
| 27 |
|
|
| 28 |
|
typedef struct { |
| 29 |
– |
COLOR v; /* hemisphere sample value */ |
| 30 |
– |
float d; /* reciprocal distance */ |
| 29 |
|
FVECT p; /* intersection point */ |
| 30 |
+ |
float d; /* reciprocal distance */ |
| 31 |
+ |
SCOLOR v; /* hemisphere sample value */ |
| 32 |
|
} AMBSAMP; /* sample value */ |
| 33 |
|
|
| 34 |
|
typedef struct { |
| 35 |
|
RAY *rp; /* originating ray sample */ |
| 36 |
|
int ns; /* number of samples per axis */ |
| 37 |
|
int sampOK; /* acquired full sample set? */ |
| 38 |
< |
COLOR acoef; /* division contribution coefficient */ |
| 39 |
< |
double acol[3]; /* accumulated color */ |
| 38 |
> |
int atyp; /* RAMBIENT or TAMBIENT */ |
| 39 |
> |
SCOLOR acoef; /* division contribution coefficient */ |
| 40 |
> |
SCOLOR acol; /* accumulated color */ |
| 41 |
> |
FVECT onrm; /* oriented unperturbed surface normal */ |
| 42 |
|
FVECT ux, uy; /* tangent axis unit vectors */ |
| 43 |
|
AMBSAMP sa[1]; /* sample array (extends struct) */ |
| 44 |
|
} AMBHEMI; /* ambient sample hemisphere */ |
| 105 |
|
/* generate hemispherical sample */ |
| 106 |
|
/* ambient coefficient for weight */ |
| 107 |
|
if (ambacc > FTINY) |
| 108 |
< |
setcolor(ar.rcoef, AVGREFL, AVGREFL, AVGREFL); |
| 108 |
> |
setscolor(ar.rcoef, AVGREFL, AVGREFL, AVGREFL); |
| 109 |
|
else |
| 110 |
< |
copycolor(ar.rcoef, hp->acoef); |
| 111 |
< |
if (rayorigin(&ar, AMBIENT, hp->rp, ar.rcoef) < 0) |
| 110 |
> |
copyscolor(ar.rcoef, hp->acoef); |
| 111 |
> |
if (rayorigin(&ar, hp->atyp, hp->rp, ar.rcoef) < 0) |
| 112 |
|
return(0); |
| 113 |
|
if (ambacc > FTINY) { |
| 114 |
< |
multcolor(ar.rcoef, hp->acoef); |
| 115 |
< |
scalecolor(ar.rcoef, 1./AVGREFL); |
| 114 |
> |
smultscolor(ar.rcoef, hp->acoef); |
| 115 |
> |
scalescolor(ar.rcoef, 1./AVGREFL); |
| 116 |
|
} |
| 117 |
|
hlist[0] = hp->rp->rno; |
| 118 |
|
hlist[1] = j; |
| 124 |
|
for (ii = 3; ii--; ) |
| 125 |
|
ar.rdir[ii] = spt[0]*hp->ux[ii] + |
| 126 |
|
spt[1]*hp->uy[ii] + |
| 127 |
< |
zd*hp->rp->ron[ii]; |
| 127 |
> |
zd*hp->onrm[ii]; |
| 128 |
|
checknorm(ar.rdir); |
| 129 |
|
/* avoid coincident samples */ |
| 130 |
|
if (!n && ambcollision(hp, i, j, ar.rdir)) { |
| 137 |
|
zd = raydistance(&ar); |
| 138 |
|
if (zd <= FTINY) |
| 139 |
|
return(0); /* should never happen */ |
| 140 |
< |
multcolor(ar.rcol, ar.rcoef); /* apply coefficient */ |
| 140 |
> |
smultscolor(ar.rcol, ar.rcoef); /* apply coefficient */ |
| 141 |
|
if (zd*ap->d < 1.0) /* new/closer distance? */ |
| 142 |
|
ap->d = 1.0/zd; |
| 143 |
|
if (!n) { /* record first vertex & value */ |
| 144 |
|
if (zd > 10.0*thescene.cusize + 1000.) |
| 145 |
|
zd = 10.0*thescene.cusize + 1000.; |
| 146 |
|
VSUM(ap->p, ar.rorg, ar.rdir, zd); |
| 147 |
< |
copycolor(ap->v, ar.rcol); |
| 147 |
> |
copyscolor(ap->v, ar.rcol); |
| 148 |
|
} else { /* else update recorded value */ |
| 149 |
< |
hp->acol[RED] -= colval(ap->v,RED); |
| 148 |
< |
hp->acol[GRN] -= colval(ap->v,GRN); |
| 149 |
< |
hp->acol[BLU] -= colval(ap->v,BLU); |
| 149 |
> |
sopscolor(hp->acol, -=, ap->v); |
| 150 |
|
zd = 1.0/(double)(n+1); |
| 151 |
< |
scalecolor(ar.rcol, zd); |
| 151 |
> |
scalescolor(ar.rcol, zd); |
| 152 |
|
zd *= (double)n; |
| 153 |
< |
scalecolor(ap->v, zd); |
| 154 |
< |
addcolor(ap->v, ar.rcol); |
| 153 |
> |
scalescolor(ap->v, zd); |
| 154 |
> |
saddscolor(ap->v, ar.rcol); |
| 155 |
|
} |
| 156 |
< |
addcolor(hp->acol, ap->v); /* add to our sum */ |
| 156 |
> |
saddscolor(hp->acol, ap->v); /* add to our sum */ |
| 157 |
|
return(1); |
| 158 |
|
} |
| 159 |
|
|
| 174 |
|
/* sum squared neighbor diffs */ |
| 175 |
|
for (ap = hp->sa, ep = earr, i = 0; i < hp->ns; i++) |
| 176 |
|
for (j = 0; j < hp->ns; j++, ap++, ep++) { |
| 177 |
< |
b = bright(ap[0].v); |
| 177 |
> |
b = pbright(ap[0].v); |
| 178 |
|
if (i) { /* from above */ |
| 179 |
< |
b1 = bright(ap[-hp->ns].v); |
| 179 |
> |
b1 = pbright(ap[-hp->ns].v); |
| 180 |
|
d2 = b - b1; |
| 181 |
< |
d2 *= d2*normf/(b + b1); |
| 181 |
> |
d2 *= d2*normf/(b + b1 + FTINY); |
| 182 |
|
ep[0] += d2; |
| 183 |
|
ep[-hp->ns] += d2; |
| 184 |
|
} |
| 185 |
|
if (!j) continue; |
| 186 |
|
/* from behind */ |
| 187 |
< |
b1 = bright(ap[-1].v); |
| 187 |
> |
b1 = pbright(ap[-1].v); |
| 188 |
|
d2 = b - b1; |
| 189 |
< |
d2 *= d2*normf/(b + b1); |
| 189 |
> |
d2 *= d2*normf/(b + b1 + FTINY); |
| 190 |
|
ep[0] += d2; |
| 191 |
|
ep[-1] += d2; |
| 192 |
|
if (!i) continue; |
| 193 |
|
/* diagonal */ |
| 194 |
< |
b1 = bright(ap[-hp->ns-1].v); |
| 194 |
> |
b1 = pbright(ap[-hp->ns-1].v); |
| 195 |
|
d2 = b - b1; |
| 196 |
< |
d2 *= d2*normf/(b + b1); |
| 196 |
> |
d2 *= d2*normf/(b + b1 + FTINY); |
| 197 |
|
ep[0] += d2; |
| 198 |
|
ep[-hp->ns-1] += d2; |
| 199 |
|
} |
| 245 |
|
|
| 246 |
|
static AMBHEMI * |
| 247 |
|
samp_hemi( /* sample indirect hemisphere */ |
| 248 |
< |
COLOR rcol, |
| 248 |
> |
SCOLOR rcol, |
| 249 |
|
RAY *r, |
| 250 |
|
double wt |
| 251 |
|
) |
| 252 |
|
{ |
| 253 |
+ |
int backside = (wt < 0); |
| 254 |
|
AMBHEMI *hp; |
| 255 |
|
double d; |
| 256 |
|
int n, i, j; |
| 257 |
|
/* insignificance check */ |
| 258 |
< |
if (bright(rcol) <= FTINY) |
| 258 |
> |
d = sintens(rcol); |
| 259 |
> |
if (d <= FTINY) |
| 260 |
|
return(NULL); |
| 261 |
|
/* set number of divisions */ |
| 262 |
+ |
if (backside) wt = -wt; |
| 263 |
|
if (ambacc <= FTINY && |
| 264 |
< |
wt > (d = 0.8*intens(rcol)*r->rweight/(ambdiv*minweight))) |
| 264 |
> |
wt > (d *= 0.8*r->rweight/(ambdiv*minweight))) |
| 265 |
|
wt = d; /* avoid ray termination */ |
| 266 |
|
n = sqrt(ambdiv * wt) + 0.5; |
| 267 |
|
i = 1 + (MINADIV-1)*(ambacc > FTINY); |
| 271 |
|
hp = (AMBHEMI *)malloc(sizeof(AMBHEMI) + sizeof(AMBSAMP)*(n*n - 1)); |
| 272 |
|
if (hp == NULL) |
| 273 |
|
error(SYSTEM, "out of memory in samp_hemi"); |
| 274 |
+ |
|
| 275 |
+ |
if (backside) { |
| 276 |
+ |
hp->atyp = TAMBIENT; |
| 277 |
+ |
hp->onrm[0] = -r->ron[0]; |
| 278 |
+ |
hp->onrm[1] = -r->ron[1]; |
| 279 |
+ |
hp->onrm[2] = -r->ron[2]; |
| 280 |
+ |
} else { |
| 281 |
+ |
hp->atyp = RAMBIENT; |
| 282 |
+ |
VCOPY(hp->onrm, r->ron); |
| 283 |
+ |
} |
| 284 |
|
hp->rp = r; |
| 285 |
|
hp->ns = n; |
| 286 |
< |
hp->acol[RED] = hp->acol[GRN] = hp->acol[BLU] = 0.0; |
| 286 |
> |
scolorblack(hp->acol); |
| 287 |
|
memset(hp->sa, 0, sizeof(AMBSAMP)*n*n); |
| 288 |
|
hp->sampOK = 0; |
| 289 |
|
/* assign coefficient */ |
| 290 |
< |
copycolor(hp->acoef, rcol); |
| 290 |
> |
copyscolor(hp->acoef, rcol); |
| 291 |
|
d = 1.0/(n*n); |
| 292 |
< |
scalecolor(hp->acoef, d); |
| 292 |
> |
scalescolor(hp->acoef, d); |
| 293 |
|
/* make tangent plane axes */ |
| 294 |
< |
if (!getperpendicular(hp->ux, r->ron, 1)) |
| 294 |
> |
if (!getperpendicular(hp->ux, hp->onrm, 1)) |
| 295 |
|
error(CONSISTENCY, "bad ray direction in samp_hemi"); |
| 296 |
< |
VCROSS(hp->uy, r->ron, hp->ux); |
| 296 |
> |
VCROSS(hp->uy, hp->onrm, hp->ux); |
| 297 |
|
/* sample divisions */ |
| 298 |
|
for (i = hp->ns; i--; ) |
| 299 |
|
for (j = hp->ns; j--; ) |
| 300 |
|
hp->sampOK += ambsample(hp, i, j, 0); |
| 301 |
< |
copycolor(rcol, hp->acol); |
| 301 |
> |
copyscolor(rcol, hp->acol); |
| 302 |
|
if (!hp->sampOK) { /* utter failure? */ |
| 303 |
|
free(hp); |
| 304 |
|
return(NULL); |
| 312 |
|
n = ambssamp*wt + 0.5; |
| 313 |
|
if (n > 8) { /* perform super-sampling? */ |
| 314 |
|
ambsupersamp(hp, n); |
| 315 |
< |
copycolor(rcol, hp->acol); |
| 315 |
> |
copyscolor(rcol, hp->acol); |
| 316 |
|
} |
| 317 |
|
return(hp); /* all is well */ |
| 318 |
|
} |
| 324 |
|
{ |
| 325 |
|
if (hp->sa[n1].d <= hp->sa[n2].d) { |
| 326 |
|
if (hp->sa[n1].d <= hp->sa[n3].d) |
| 327 |
< |
return(colval(hp->sa[n1].v,CIEY)); |
| 328 |
< |
return(colval(hp->sa[n3].v,CIEY)); |
| 327 |
> |
return(hp->sa[n1].v[0]); |
| 328 |
> |
return(hp->sa[n3].v[0]); |
| 329 |
|
} |
| 330 |
|
if (hp->sa[n2].d <= hp->sa[n3].d) |
| 331 |
< |
return(colval(hp->sa[n2].v,CIEY)); |
| 332 |
< |
return(colval(hp->sa[n3].v,CIEY)); |
| 331 |
> |
return(hp->sa[n2].v[0]); |
| 332 |
> |
return(hp->sa[n3].v[0]); |
| 333 |
|
} |
| 334 |
|
|
| 335 |
|
|
| 558 |
|
for (j = 0; j < hp->ns-1; j++) { |
| 559 |
|
comp_fftri(&fftr, hp, AI(hp,0,j), AI(hp,0,j+1)); |
| 560 |
|
if (hessrow != NULL) |
| 561 |
< |
comp_hessian(hessrow[j], &fftr, hp->rp->ron); |
| 561 |
> |
comp_hessian(hessrow[j], &fftr, hp->onrm); |
| 562 |
|
if (gradrow != NULL) |
| 563 |
< |
comp_gradient(gradrow[j], &fftr, hp->rp->ron); |
| 563 |
> |
comp_gradient(gradrow[j], &fftr, hp->onrm); |
| 564 |
|
} |
| 565 |
|
/* sum each row of triangles */ |
| 566 |
|
for (i = 0; i < hp->ns-1; i++) { |
| 568 |
|
FVECT gradcol; |
| 569 |
|
comp_fftri(&fftr, hp, AI(hp,i,0), AI(hp,i+1,0)); |
| 570 |
|
if (hessrow != NULL) |
| 571 |
< |
comp_hessian(hesscol, &fftr, hp->rp->ron); |
| 571 |
> |
comp_hessian(hesscol, &fftr, hp->onrm); |
| 572 |
|
if (gradrow != NULL) |
| 573 |
< |
comp_gradient(gradcol, &fftr, hp->rp->ron); |
| 573 |
> |
comp_gradient(gradcol, &fftr, hp->onrm); |
| 574 |
|
for (j = 0; j < hp->ns-1; j++) { |
| 575 |
|
FVECT hessdia[3]; /* compute triangle contributions */ |
| 576 |
|
FVECT graddia; |
| 580 |
|
/* diagonal (inner) edge */ |
| 581 |
|
comp_fftri(&fftr, hp, AI(hp,i,j+1), AI(hp,i+1,j)); |
| 582 |
|
if (hessrow != NULL) { |
| 583 |
< |
comp_hessian(hessdia, &fftr, hp->rp->ron); |
| 583 |
> |
comp_hessian(hessdia, &fftr, hp->onrm); |
| 584 |
|
rev_hessian(hesscol); |
| 585 |
|
add2hessian(hessian, hessrow[j], hessdia, hesscol, backg); |
| 586 |
|
} |
| 587 |
|
if (gradrow != NULL) { |
| 588 |
< |
comp_gradient(graddia, &fftr, hp->rp->ron); |
| 588 |
> |
comp_gradient(graddia, &fftr, hp->onrm); |
| 589 |
|
rev_gradient(gradcol); |
| 590 |
|
add2gradient(gradient, gradrow[j], graddia, gradcol, backg); |
| 591 |
|
} |
| 592 |
|
/* initialize edge in next row */ |
| 593 |
|
comp_fftri(&fftr, hp, AI(hp,i+1,j+1), AI(hp,i+1,j)); |
| 594 |
|
if (hessrow != NULL) |
| 595 |
< |
comp_hessian(hessrow[j], &fftr, hp->rp->ron); |
| 595 |
> |
comp_hessian(hessrow[j], &fftr, hp->onrm); |
| 596 |
|
if (gradrow != NULL) |
| 597 |
< |
comp_gradient(gradrow[j], &fftr, hp->rp->ron); |
| 597 |
> |
comp_gradient(gradrow[j], &fftr, hp->onrm); |
| 598 |
|
/* new column edge & paired triangle */ |
| 599 |
|
backg = back_ambval(hp, AI(hp,i+1,j+1), |
| 600 |
|
AI(hp,i+1,j), AI(hp,i,j+1)); |
| 601 |
|
comp_fftri(&fftr, hp, AI(hp,i,j+1), AI(hp,i+1,j+1)); |
| 602 |
|
if (hessrow != NULL) { |
| 603 |
< |
comp_hessian(hesscol, &fftr, hp->rp->ron); |
| 603 |
> |
comp_hessian(hesscol, &fftr, hp->onrm); |
| 604 |
|
rev_hessian(hessdia); |
| 605 |
|
add2hessian(hessian, hessrow[j], hessdia, hesscol, backg); |
| 606 |
|
if (i < hp->ns-2) |
| 607 |
|
rev_hessian(hessrow[j]); |
| 608 |
|
} |
| 609 |
|
if (gradrow != NULL) { |
| 610 |
< |
comp_gradient(gradcol, &fftr, hp->rp->ron); |
| 610 |
> |
comp_gradient(gradcol, &fftr, hp->onrm); |
| 611 |
|
rev_gradient(graddia); |
| 612 |
|
add2gradient(gradient, gradrow[j], graddia, gradcol, backg); |
| 613 |
|
if (i < hp->ns-2) |
| 643 |
|
/* use vector for azimuth + 90deg */ |
| 644 |
|
VSUB(vd, ap->p, hp->rp->rop); |
| 645 |
|
/* brightness over cosine factor */ |
| 646 |
< |
gfact = colval(ap->v,CIEY) / DOT(hp->rp->ron, vd); |
| 646 |
> |
gfact = ap->v[0] / DOT(hp->onrm, vd); |
| 647 |
|
/* sine = proj_radius/vd_length */ |
| 648 |
|
dgsum[0] -= DOT(uv[1], vd) * gfact; |
| 649 |
|
dgsum[1] += DOT(uv[0], vd) * gfact; |
| 699 |
|
|
| 700 |
|
int |
| 701 |
|
doambient( /* compute ambient component */ |
| 702 |
< |
COLOR rcol, /* input/output color */ |
| 702 |
> |
SCOLOR rcol, /* input/output color */ |
| 703 |
|
RAY *r, |
| 704 |
< |
double wt, |
| 704 |
> |
double wt, /* negative for back side */ |
| 705 |
|
FVECT uv[2], /* returned (optional) */ |
| 706 |
|
float ra[2], /* returned (optional) */ |
| 707 |
|
float pg[2], /* returned (optional) */ |
| 733 |
|
free(hp); /* Hessian not requested/possible */ |
| 734 |
|
return(-1); /* value-only return value */ |
| 735 |
|
} |
| 736 |
< |
if ((d = bright(rcol)) > FTINY) { /* normalize Y values */ |
| 737 |
< |
d = 0.99*(hp->ns*hp->ns)/d; |
| 736 |
> |
if ((d = scolor_mean(rcol)) > FTINY) { |
| 737 |
> |
d = 0.99*(hp->ns*hp->ns)/d; /* normalize avg. values */ |
| 738 |
|
K = 0.01; |
| 739 |
|
} else { /* or fall back on geometric Hessian */ |
| 740 |
|
K = 1.0; |
| 742 |
|
dg = NULL; |
| 743 |
|
crlp = NULL; |
| 744 |
|
} |
| 745 |
< |
ap = hp->sa; /* relative Y channel from here on... */ |
| 745 |
> |
ap = hp->sa; /* single channel from here on... */ |
| 746 |
|
for (i = hp->ns*hp->ns; i--; ap++) |
| 747 |
< |
colval(ap->v,CIEY) = bright(ap->v)*d + K; |
| 747 |
> |
ap->v[0] = scolor_mean(ap->v)*d + K; |
| 748 |
|
|
| 749 |
|
if (uv == NULL) /* make sure we have axis pointers */ |
| 750 |
|
uv = my_uv; |
| 768 |
|
if (ra[1] < minarad) |
| 769 |
|
ra[1] = minarad; |
| 770 |
|
} |
| 771 |
< |
ra[0] *= d = 1.0/sqrt(wt); |
| 771 |
> |
ra[0] *= d = 1.0/sqrt(fabs(wt)); |
| 772 |
|
if ((ra[1] *= d) > 2.0*ra[0]) |
| 773 |
|
ra[1] = 2.0*ra[0]; |
| 774 |
|
if (ra[1] > maxarad) { |