16 |
|
#include "random.h" |
17 |
|
|
18 |
|
|
19 |
< |
static int |
20 |
< |
ambcmp(d1, d2) /* decreasing order */ |
21 |
< |
AMBSAMP *d1, *d2; |
19 |
> |
void |
20 |
> |
inithemi( /* initialize sampling hemisphere */ |
21 |
> |
register AMBHEMI *hp, |
22 |
> |
RAY *r, |
23 |
> |
COLOR ac, |
24 |
> |
double wt |
25 |
> |
) |
26 |
|
{ |
27 |
< |
if (d1->k < d2->k) |
28 |
< |
return(1); |
29 |
< |
if (d1->k > d2->k) |
30 |
< |
return(-1); |
31 |
< |
return(0); |
27 |
> |
register int i; |
28 |
> |
/* set number of divisions */ |
29 |
> |
hp->nt = sqrt(ambdiv * wt * (1./PI/AVGREFL)) + 0.5; |
30 |
> |
i = ambacc > FTINY ? 3 : 1; /* minimum number of samples */ |
31 |
> |
if (hp->nt < i) |
32 |
> |
hp->nt = i; |
33 |
> |
hp->np = PI * hp->nt + 0.5; |
34 |
> |
/* set number of super-samples */ |
35 |
> |
hp->ns = ambssamp * wt + 0.5; |
36 |
> |
/* assign coefficients */ |
37 |
> |
copycolor(hp->acoef, ac); |
38 |
> |
if (wt >= r->rweight) |
39 |
> |
hp->drc = 1.; |
40 |
> |
else |
41 |
> |
hp->drc = wt / r->rweight; |
42 |
> |
/* make axes */ |
43 |
> |
VCOPY(hp->uz, r->ron); |
44 |
> |
hp->uy[0] = hp->uy[1] = hp->uy[2] = 0.0; |
45 |
> |
for (i = 0; i < 3; i++) |
46 |
> |
if (hp->uz[i] < 0.6 && hp->uz[i] > -0.6) |
47 |
> |
break; |
48 |
> |
if (i >= 3) |
49 |
> |
error(CONSISTENCY, "bad ray direction in inithemi"); |
50 |
> |
hp->uy[i] = 1.0; |
51 |
> |
fcross(hp->ux, hp->uy, hp->uz); |
52 |
> |
normalize(hp->ux); |
53 |
> |
fcross(hp->uy, hp->uz, hp->ux); |
54 |
|
} |
55 |
|
|
56 |
|
|
31 |
– |
static int |
32 |
– |
ambnorm(d1, d2) /* standard order */ |
33 |
– |
AMBSAMP *d1, *d2; |
34 |
– |
{ |
35 |
– |
register int c; |
36 |
– |
|
37 |
– |
if ( (c = d1->t - d2->t) ) |
38 |
– |
return(c); |
39 |
– |
return(d1->p - d2->p); |
40 |
– |
} |
41 |
– |
|
42 |
– |
|
57 |
|
int |
58 |
< |
divsample(dp, h, r) /* sample a division */ |
59 |
< |
register AMBSAMP *dp; |
60 |
< |
AMBHEMI *h; |
61 |
< |
RAY *r; |
58 |
> |
divsample( /* sample a division */ |
59 |
> |
register AMBSAMP *dp, |
60 |
> |
AMBHEMI *h, |
61 |
> |
RAY *r |
62 |
> |
) |
63 |
|
{ |
64 |
|
RAY ar; |
65 |
|
int hlist[3]; |
68 |
|
double b2; |
69 |
|
double phi; |
70 |
|
register int i; |
71 |
< |
|
72 |
< |
if (rayorigin(&ar, r, AMBIENT, AVGREFL) < 0) |
71 |
> |
/* ambient coefficient for weight */ |
72 |
> |
setcolor(ar.rcoef, h->drc, h->drc, h->drc); |
73 |
> |
if (rayorigin(&ar, AMBIENT, r, ar.rcoef) < 0) |
74 |
|
return(-1); |
75 |
+ |
copycolor(ar.rcoef, h->acoef); /* correct coefficient for trace */ |
76 |
+ |
b2 = 1.0/(h->nt*h->np + h->ns); /* XXX not uniform if ns > 0 */ |
77 |
+ |
scalecolor(ar.rcoef, b2); |
78 |
|
hlist[0] = r->rno; |
79 |
|
hlist[1] = dp->t; |
80 |
|
hlist[2] = dp->p; |
106 |
|
} |
107 |
|
|
108 |
|
|
109 |
+ |
static int |
110 |
+ |
ambcmp( /* decreasing order */ |
111 |
+ |
const void *p1, |
112 |
+ |
const void *p2 |
113 |
+ |
) |
114 |
+ |
{ |
115 |
+ |
const AMBSAMP *d1 = (const AMBSAMP *)p1; |
116 |
+ |
const AMBSAMP *d2 = (const AMBSAMP *)p2; |
117 |
+ |
|
118 |
+ |
if (d1->k < d2->k) |
119 |
+ |
return(1); |
120 |
+ |
if (d1->k > d2->k) |
121 |
+ |
return(-1); |
122 |
+ |
return(0); |
123 |
+ |
} |
124 |
+ |
|
125 |
+ |
|
126 |
+ |
static int |
127 |
+ |
ambnorm( /* standard order */ |
128 |
+ |
const void *p1, |
129 |
+ |
const void *p2 |
130 |
+ |
) |
131 |
+ |
{ |
132 |
+ |
const AMBSAMP *d1 = (const AMBSAMP *)p1; |
133 |
+ |
const AMBSAMP *d2 = (const AMBSAMP *)p2; |
134 |
+ |
register int c; |
135 |
+ |
|
136 |
+ |
if ( (c = d1->t - d2->t) ) |
137 |
+ |
return(c); |
138 |
+ |
return(d1->p - d2->p); |
139 |
+ |
} |
140 |
+ |
|
141 |
+ |
|
142 |
|
double |
143 |
< |
doambient(acol, r, wt, pg, dg) /* compute ambient component */ |
144 |
< |
COLOR acol; |
145 |
< |
RAY *r; |
146 |
< |
double wt; |
147 |
< |
FVECT pg, dg; |
143 |
> |
doambient( /* compute ambient component */ |
144 |
> |
COLOR acol, |
145 |
> |
RAY *r, |
146 |
> |
COLOR ac, |
147 |
> |
double wt, |
148 |
> |
FVECT pg, |
149 |
> |
FVECT dg |
150 |
> |
) |
151 |
|
{ |
152 |
|
double b, d; |
153 |
|
AMBHEMI hemi; |
155 |
|
AMBSAMP dnew; |
156 |
|
register AMBSAMP *dp; |
157 |
|
double arad; |
158 |
< |
int ndivs, ns; |
158 |
> |
int ndivs; |
159 |
|
register int i, j; |
160 |
|
/* initialize color */ |
161 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
162 |
|
/* initialize hemisphere */ |
163 |
< |
inithemi(&hemi, r, wt); |
163 |
> |
inithemi(&hemi, r, ac, wt); |
164 |
|
ndivs = hemi.nt * hemi.np; |
165 |
|
if (ndivs == 0) |
166 |
|
return(0.0); |
167 |
< |
/* set number of super-samples */ |
168 |
< |
ns = ambssamp * wt + 0.5; |
114 |
< |
if (ns > 0 || pg != NULL || dg != NULL) { |
167 |
> |
/* allocate super-samples */ |
168 |
> |
if (hemi.ns > 0 || pg != NULL || dg != NULL) { |
169 |
|
div = (AMBSAMP *)malloc(ndivs*sizeof(AMBSAMP)); |
170 |
|
if (div == NULL) |
171 |
|
error(SYSTEM, "out of memory in doambient"); |
189 |
|
else |
190 |
|
addcolor(acol, dp->v); |
191 |
|
} |
192 |
< |
if (ns > 0 && arad > FTINY && ndivs/arad < minarad) |
193 |
< |
ns = 0; /* close enough */ |
194 |
< |
else if (ns > 0) { /* else perform super-sampling */ |
192 |
> |
if (hemi.ns > 0 && arad > FTINY && ndivs/arad < minarad) |
193 |
> |
hemi.ns = 0; /* close enough */ |
194 |
> |
else if (hemi.ns > 0) { /* else perform super-sampling */ |
195 |
|
comperrs(div, &hemi); /* compute errors */ |
196 |
|
qsort(div, ndivs, sizeof(AMBSAMP), ambcmp); /* sort divs */ |
197 |
|
/* super-sample */ |
198 |
< |
for (i = ns; i > 0; i--) { |
198 |
> |
for (i = hemi.ns; i > 0; i--) { |
199 |
|
dnew = *div; |
200 |
|
if (divsample(&dnew, &hemi, r) < 0) |
201 |
|
goto oopsy; |
252 |
|
if (arad <= FTINY) |
253 |
|
arad = maxarad; |
254 |
|
else |
255 |
< |
arad = (ndivs+ns)/arad; |
255 |
> |
arad = (ndivs+hemi.ns)/arad; |
256 |
|
if (pg != NULL) { /* reduce radius if gradient large */ |
257 |
|
d = DOT(pg,pg); |
258 |
|
if (d*arad*arad > 1.0) |
277 |
|
|
278 |
|
|
279 |
|
void |
280 |
< |
inithemi(hp, r, wt) /* initialize sampling hemisphere */ |
281 |
< |
register AMBHEMI *hp; |
282 |
< |
RAY *r; |
283 |
< |
double wt; |
280 |
> |
comperrs( /* compute initial error estimates */ |
281 |
> |
AMBSAMP *da, /* assumes standard ordering */ |
282 |
> |
register AMBHEMI *hp |
283 |
> |
) |
284 |
|
{ |
231 |
– |
register int i; |
232 |
– |
/* set number of divisions */ |
233 |
– |
if (wt < (.25*PI)/ambdiv+FTINY) { |
234 |
– |
hp->nt = hp->np = 0; |
235 |
– |
return; /* zero samples */ |
236 |
– |
} |
237 |
– |
hp->nt = sqrt(ambdiv * wt / PI) + 0.5; |
238 |
– |
hp->np = PI * hp->nt + 0.5; |
239 |
– |
/* make axes */ |
240 |
– |
VCOPY(hp->uz, r->ron); |
241 |
– |
hp->uy[0] = hp->uy[1] = hp->uy[2] = 0.0; |
242 |
– |
for (i = 0; i < 3; i++) |
243 |
– |
if (hp->uz[i] < 0.6 && hp->uz[i] > -0.6) |
244 |
– |
break; |
245 |
– |
if (i >= 3) |
246 |
– |
error(CONSISTENCY, "bad ray direction in inithemi"); |
247 |
– |
hp->uy[i] = 1.0; |
248 |
– |
fcross(hp->ux, hp->uy, hp->uz); |
249 |
– |
normalize(hp->ux); |
250 |
– |
fcross(hp->uy, hp->uz, hp->ux); |
251 |
– |
} |
252 |
– |
|
253 |
– |
|
254 |
– |
void |
255 |
– |
comperrs(da, hp) /* compute initial error estimates */ |
256 |
– |
AMBSAMP *da; /* assumes standard ordering */ |
257 |
– |
register AMBHEMI *hp; |
258 |
– |
{ |
285 |
|
double b, b2; |
286 |
|
int i, j; |
287 |
|
register AMBSAMP *dp; |
329 |
|
|
330 |
|
|
331 |
|
void |
332 |
< |
posgradient(gv, da, hp) /* compute position gradient */ |
333 |
< |
FVECT gv; |
334 |
< |
AMBSAMP *da; /* assumes standard ordering */ |
335 |
< |
register AMBHEMI *hp; |
332 |
> |
posgradient( /* compute position gradient */ |
333 |
> |
FVECT gv, |
334 |
> |
AMBSAMP *da, /* assumes standard ordering */ |
335 |
> |
register AMBHEMI *hp |
336 |
> |
) |
337 |
|
{ |
338 |
|
register int i, j; |
339 |
|
double nextsine, lastsine, b, d; |
387 |
|
|
388 |
|
|
389 |
|
void |
390 |
< |
dirgradient(gv, da, hp) /* compute direction gradient */ |
391 |
< |
FVECT gv; |
392 |
< |
AMBSAMP *da; /* assumes standard ordering */ |
393 |
< |
register AMBHEMI *hp; |
390 |
> |
dirgradient( /* compute direction gradient */ |
391 |
> |
FVECT gv, |
392 |
> |
AMBSAMP *da, /* assumes standard ordering */ |
393 |
> |
register AMBHEMI *hp |
394 |
> |
) |
395 |
|
{ |
396 |
|
register int i, j; |
397 |
|
double mag; |