21 |
|
#include "ambient.h" |
22 |
|
#include "random.h" |
23 |
|
|
24 |
< |
#ifdef NEWAMB |
24 |
> |
#ifndef OLDAMB |
25 |
|
|
26 |
|
extern void SDsquare2disk(double ds[2], double seedx, double seedy); |
27 |
|
|
51 |
|
|
52 |
|
|
53 |
|
static int |
54 |
+ |
ambcollision( /* proposed direciton collides? */ |
55 |
+ |
AMBHEMI *hp, |
56 |
+ |
int i, |
57 |
+ |
int j, |
58 |
+ |
FVECT dv |
59 |
+ |
) |
60 |
+ |
{ |
61 |
+ |
const double cos_thresh = 0.9999995; /* about 3.44 arcminutes */ |
62 |
+ |
int ii, jj; |
63 |
+ |
|
64 |
+ |
for (ii = i-1; ii <= i+1; ii++) { |
65 |
+ |
if (ii < 0) continue; |
66 |
+ |
if (ii >= hp->ns) break; |
67 |
+ |
for (jj = j-1; jj <= j+1; jj++) { |
68 |
+ |
AMBSAMP *ap; |
69 |
+ |
FVECT avec; |
70 |
+ |
double dprod; |
71 |
+ |
if (jj < 0) continue; |
72 |
+ |
if (jj >= hp->ns) break; |
73 |
+ |
if ((ii==i) & (jj==j)) continue; |
74 |
+ |
ap = &ambsam(hp,ii,jj); |
75 |
+ |
if (ap->d <= .5/FHUGE) continue; |
76 |
+ |
VSUB(avec, ap->p, hp->rp->rop); |
77 |
+ |
dprod = DOT(avec, dv); |
78 |
+ |
if (dprod >= cos_thresh*VLEN(avec)) |
79 |
+ |
return(1); /* collision */ |
80 |
+ |
} |
81 |
+ |
} |
82 |
+ |
return(0); |
83 |
+ |
} |
84 |
+ |
|
85 |
+ |
|
86 |
+ |
static int |
87 |
|
ambsample( /* initial ambient division sample */ |
88 |
|
AMBHEMI *hp, |
89 |
|
int i, |
101 |
|
setcolor(ar.rcoef, AVGREFL, AVGREFL, AVGREFL); |
102 |
|
else |
103 |
|
copycolor(ar.rcoef, hp->acoef); |
104 |
< |
if (rayorigin(&ar, AMBIENT, hp->rp, ar.rcoef) < 0) { |
72 |
< |
if (!n) memset(ap, 0, sizeof(AMBSAMP)); |
104 |
> |
if (rayorigin(&ar, AMBIENT, hp->rp, ar.rcoef) < 0) |
105 |
|
return(0); |
74 |
– |
} |
106 |
|
if (ambacc > FTINY) { |
107 |
|
multcolor(ar.rcoef, hp->acoef); |
108 |
|
scalecolor(ar.rcoef, 1./AVGREFL); |
111 |
|
hlist[1] = j; |
112 |
|
hlist[2] = i; |
113 |
|
multisamp(spt, 2, urand(ilhash(hlist,3)+n)); |
114 |
< |
if (!n) { /* avoid border samples for n==0 */ |
84 |
< |
if ((spt[0] < 0.1) | (spt[0] >= 0.9)) |
85 |
< |
spt[0] = 0.1 + 0.8*frandom(); |
86 |
< |
if ((spt[1] < 0.1) | (spt[1] >= 0.9)) |
87 |
< |
spt[1] = 0.1 + 0.8*frandom(); |
88 |
< |
} |
114 |
> |
resample: |
115 |
|
SDsquare2disk(spt, (j+spt[1])/hp->ns, (i+spt[0])/hp->ns); |
116 |
|
zd = sqrt(1. - spt[0]*spt[0] - spt[1]*spt[1]); |
117 |
|
for (ii = 3; ii--; ) |
119 |
|
spt[1]*hp->uy[ii] + |
120 |
|
zd*hp->rp->ron[ii]; |
121 |
|
checknorm(ar.rdir); |
122 |
+ |
/* avoid coincident samples */ |
123 |
+ |
if (!n && ambcollision(hp, i, j, ar.rdir)) { |
124 |
+ |
spt[0] = frandom(); spt[1] = frandom(); |
125 |
+ |
goto resample; |
126 |
+ |
} |
127 |
|
dimlist[ndims++] = AI(hp,i,j) + 90171; |
128 |
|
rayvalue(&ar); /* evaluate ray */ |
129 |
|
ndims--; |
130 |
|
if (ar.rt <= FTINY) |
131 |
|
return(0); /* should never happen */ |
132 |
|
multcolor(ar.rcol, ar.rcoef); /* apply coefficient */ |
133 |
< |
if (!n || ar.rt*ap->d < 1.0) /* new/closer distance? */ |
133 |
> |
if (ar.rt*ap->d < 1.0) /* new/closer distance? */ |
134 |
|
ap->d = 1.0/ar.rt; |
135 |
|
if (!n) { /* record first vertex & value */ |
136 |
|
if (ar.rt > 10.0*thescene.cusize) |
211 |
|
float *earr = getambdiffs(hp); |
212 |
|
double e2rem = 0; |
213 |
|
AMBSAMP *ap; |
183 |
– |
RAY ar; |
214 |
|
float *ep; |
215 |
|
int i, j, n, nss; |
216 |
|
|
225 |
|
if (e2rem <= FTINY) |
226 |
|
goto done; /* nothing left to do */ |
227 |
|
nss = *ep/e2rem*cnt + frandom(); |
228 |
< |
for (n = 1; n <= nss; n++) |
229 |
< |
cnt -= ambsample(hp, i, j, n); |
228 |
> |
for (n = 1; n <= nss && ambsample(hp,i,j,n); n++) |
229 |
> |
--cnt; |
230 |
|
e2rem -= *ep++; /* update remainder */ |
231 |
|
} |
232 |
|
done: |
259 |
|
hp->rp = r; |
260 |
|
hp->ns = n; |
261 |
|
hp->acol[RED] = hp->acol[GRN] = hp->acol[BLU] = 0.0; |
262 |
+ |
memset(hp->sa, 0, sizeof(AMBSAMP)*n*n); |
263 |
|
hp->sampOK = 0; |
264 |
|
/* assign coefficient */ |
265 |
|
copycolor(hp->acoef, rcol); |
266 |
|
d = 1.0/(n*n); |
267 |
|
scalecolor(hp->acoef, d); |
268 |
|
/* make tangent plane axes */ |
269 |
< |
hp->uy[0] = 0.5 - frandom(); |
239 |
< |
hp->uy[1] = 0.5 - frandom(); |
240 |
< |
hp->uy[2] = 0.5 - frandom(); |
241 |
< |
for (i = 3; i--; ) |
242 |
< |
if ((-0.6 < r->ron[i]) & (r->ron[i] < 0.6)) |
243 |
< |
break; |
244 |
< |
if (i < 0) |
269 |
> |
if (!getperpendicular(hp->ux, r->ron, 1)) |
270 |
|
error(CONSISTENCY, "bad ray direction in samp_hemi"); |
246 |
– |
hp->uy[i] = 1.0; |
247 |
– |
VCROSS(hp->ux, hp->uy, r->ron); |
248 |
– |
normalize(hp->ux); |
271 |
|
VCROSS(hp->uy, r->ron, hp->ux); |
272 |
|
/* sample divisions */ |
273 |
|
for (i = hp->ns; i--; ) |
631 |
|
ambcorral(AMBHEMI *hp, FVECT uv[2], const double r0, const double r1) |
632 |
|
{ |
633 |
|
const double max_d = 1.0/(minarad*ambacc + 0.001); |
634 |
< |
const double ang_res = 0.5*PI/(hp->ns-1); |
635 |
< |
const double ang_step = ang_res/((int)(16/PI*ang_res) + (1+FTINY)); |
634 |
> |
const double ang_res = 0.5*PI/hp->ns; |
635 |
> |
const double ang_step = ang_res/((int)(16/PI*ang_res) + 1.01); |
636 |
|
double avg_d = 0; |
637 |
|
uint32 flgs = 0; |
638 |
|
FVECT vec; |
639 |
< |
double d, u, v; |
639 |
> |
double u, v; |
640 |
|
double ang, a1; |
641 |
|
int i, j; |
642 |
|
/* don't bother for a few samples */ |
643 |
< |
if (hp->ns < 12) |
643 |
> |
if (hp->ns < 8) |
644 |
|
return(0); |
645 |
|
/* check distances overhead */ |
646 |
|
for (i = hp->ns*3/4; i-- > hp->ns>>2; ) |
658 |
|
if ((ap->d <= FTINY) | (ap->d >= max_d)) |
659 |
|
continue; /* too far or too near */ |
660 |
|
VSUB(vec, ap->p, hp->rp->rop); |
661 |
< |
d = DOT(vec, hp->rp->ron); |
662 |
< |
d = 1.0/sqrt(DOT(vec,vec) - d*d); |
663 |
< |
u = DOT(vec, uv[0]) * d; |
642 |
< |
v = DOT(vec, uv[1]) * d; |
643 |
< |
if ((r0*r0*u*u + r1*r1*v*v) * ap->d*ap->d <= 1.0) |
661 |
> |
u = DOT(vec, uv[0]); |
662 |
> |
v = DOT(vec, uv[1]); |
663 |
> |
if ((r0*r0*u*u + r1*r1*v*v) * ap->d*ap->d <= u*u + v*v) |
664 |
|
continue; /* occluder outside ellipse */ |
665 |
|
ang = atan2a(v, u); /* else set direction flags */ |
666 |
< |
for (a1 = ang-.5*ang_res; a1 <= ang+.5*ang_res; a1 += ang_step) |
666 |
> |
for (a1 = ang-ang_res; a1 <= ang+ang_res; a1 += ang_step) |
667 |
|
flgs |= 1L<<(int)(16/PI*(a1 + 2.*PI*(a1 < 0))); |
668 |
|
} |
669 |
|
/* add low-angle incident (< 20deg) */ |
716 |
|
return(0); |
717 |
|
|
718 |
|
if ((ra == NULL) & (pg == NULL) & (dg == NULL) || |
719 |
< |
(hp->sampOK < 0) | (hp->ns < 4)) { |
719 |
> |
(hp->sampOK < 0) | (hp->ns < 6)) { |
720 |
|
free(hp); /* Hessian not requested/possible */ |
721 |
|
return(-1); /* value-only return value */ |
722 |
|
} |
764 |
|
ra[0] = maxarad; |
765 |
|
} |
766 |
|
/* flag encroached directions */ |
767 |
< |
if ((wt >= 0.89*AVGREFL) & (crlp != NULL)) |
767 |
> |
if (crlp != NULL) |
768 |
|
*crlp = ambcorral(hp, uv, ra[0]*ambacc, ra[1]*ambacc); |
769 |
|
if (pg != NULL) { /* cap gradient if necessary */ |
770 |
|
d = pg[0]*pg[0]*ra[0]*ra[0] + pg[1]*pg[1]*ra[1]*ra[1]; |