16 |
|
#include "random.h" |
17 |
|
|
18 |
|
|
19 |
< |
static int |
20 |
< |
ambcmp(d1, d2) /* decreasing order */ |
21 |
< |
AMBSAMP *d1, *d2; |
19 |
> |
int |
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 |
> |
int ns; |
28 |
> |
double d; |
29 |
> |
register int i; |
30 |
> |
/* set number of divisions */ |
31 |
> |
hp->nt = sqrt(ambdiv * wt / PI) + 0.5; |
32 |
> |
i = ambacc > FTINY ? 3 : 1; /* minimum number of samples */ |
33 |
> |
if (hp->nt < i) |
34 |
> |
hp->nt = i; |
35 |
> |
hp->np = PI * hp->nt + 0.5; |
36 |
> |
/* set number of super-samples */ |
37 |
> |
ns = ambssamp * wt + 0.5; |
38 |
> |
/* assign coefficient */ |
39 |
> |
d = 1.0/(hp->nt*hp->np + ns); /* XXX weight not uniform if ns > 0 */ |
40 |
> |
copycolor(hp->acoef, ac); |
41 |
> |
scalecolor(hp->acoef, d); |
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 |
> |
return(ns); |
55 |
|
} |
56 |
|
|
57 |
|
|
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 |
– |
|
58 |
|
int |
59 |
< |
divsample(dp, h, r) /* sample a division */ |
60 |
< |
register AMBSAMP *dp; |
61 |
< |
AMBHEMI *h; |
62 |
< |
RAY *r; |
59 |
> |
divsample( /* sample a division */ |
60 |
> |
register AMBSAMP *dp, |
61 |
> |
AMBHEMI *h, |
62 |
> |
RAY *r |
63 |
> |
) |
64 |
|
{ |
65 |
|
RAY ar; |
66 |
|
int hlist[3]; |
69 |
|
double b2; |
70 |
|
double phi; |
71 |
|
register int i; |
72 |
< |
|
73 |
< |
if (rayorigin(&ar, r, AMBIENT, AVGREFL) < 0) |
72 |
> |
/* assign coefficient */ |
73 |
> |
if (ambacc <= FTINY) /* no storage, so report accurately */ |
74 |
> |
copycolor(ar.rcoef, h->acoef); |
75 |
> |
else /* else lie for sake of cache */ |
76 |
> |
setcolor(ar.rcoef, AVGREFL, AVGREFL, AVGREFL); |
77 |
> |
if (rayorigin(&ar, AMBIENT, r, ar.rcoef) < 0) |
78 |
|
return(-1); |
79 |
+ |
copycolor(ar.rcoef, h->acoef); /* correct coefficient rtrace output */ |
80 |
|
hlist[0] = r->rno; |
81 |
|
hlist[1] = dp->t; |
82 |
|
hlist[2] = dp->p; |
108 |
|
} |
109 |
|
|
110 |
|
|
111 |
+ |
static int |
112 |
+ |
ambcmp( /* decreasing order */ |
113 |
+ |
const void *p1, |
114 |
+ |
const void *p2 |
115 |
+ |
) |
116 |
+ |
{ |
117 |
+ |
const AMBSAMP *d1 = (const AMBSAMP *)p1; |
118 |
+ |
const AMBSAMP *d2 = (const AMBSAMP *)p2; |
119 |
+ |
|
120 |
+ |
if (d1->k < d2->k) |
121 |
+ |
return(1); |
122 |
+ |
if (d1->k > d2->k) |
123 |
+ |
return(-1); |
124 |
+ |
return(0); |
125 |
+ |
} |
126 |
+ |
|
127 |
+ |
|
128 |
+ |
static int |
129 |
+ |
ambnorm( /* standard order */ |
130 |
+ |
const void *p1, |
131 |
+ |
const void *p2 |
132 |
+ |
) |
133 |
+ |
{ |
134 |
+ |
const AMBSAMP *d1 = (const AMBSAMP *)p1; |
135 |
+ |
const AMBSAMP *d2 = (const AMBSAMP *)p2; |
136 |
+ |
register int c; |
137 |
+ |
|
138 |
+ |
if ( (c = d1->t - d2->t) ) |
139 |
+ |
return(c); |
140 |
+ |
return(d1->p - d2->p); |
141 |
+ |
} |
142 |
+ |
|
143 |
+ |
|
144 |
|
double |
145 |
< |
doambient(acol, r, wt, pg, dg) /* compute ambient component */ |
146 |
< |
COLOR acol; |
147 |
< |
RAY *r; |
148 |
< |
double wt; |
149 |
< |
FVECT pg, dg; |
145 |
> |
doambient( /* compute ambient component */ |
146 |
> |
COLOR acol, |
147 |
> |
RAY *r, |
148 |
> |
COLOR ac, |
149 |
> |
double wt, |
150 |
> |
FVECT pg, |
151 |
> |
FVECT dg |
152 |
> |
) |
153 |
|
{ |
154 |
|
double b, d; |
155 |
|
AMBHEMI hemi; |
162 |
|
/* initialize color */ |
163 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
164 |
|
/* initialize hemisphere */ |
165 |
< |
inithemi(&hemi, r, wt); |
165 |
> |
ns = inithemi(&hemi, r, ac, wt); |
166 |
|
ndivs = hemi.nt * hemi.np; |
167 |
|
if (ndivs == 0) |
168 |
|
return(0.0); |
169 |
< |
/* set number of super-samples */ |
113 |
< |
ns = ambssamp * wt + 0.5; |
169 |
> |
/* allocate super-samples */ |
170 |
|
if (ns > 0 || pg != NULL || dg != NULL) { |
171 |
|
div = (AMBSAMP *)malloc(ndivs*sizeof(AMBSAMP)); |
172 |
|
if (div == NULL) |
279 |
|
|
280 |
|
|
281 |
|
void |
282 |
< |
inithemi(hp, r, wt) /* initialize sampling hemisphere */ |
283 |
< |
register AMBHEMI *hp; |
284 |
< |
RAY *r; |
285 |
< |
double wt; |
282 |
> |
comperrs( /* compute initial error estimates */ |
283 |
> |
AMBSAMP *da, /* assumes standard ordering */ |
284 |
> |
register AMBHEMI *hp |
285 |
> |
) |
286 |
|
{ |
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 |
– |
{ |
287 |
|
double b, b2; |
288 |
|
int i, j; |
289 |
|
register AMBSAMP *dp; |
331 |
|
|
332 |
|
|
333 |
|
void |
334 |
< |
posgradient(gv, da, hp) /* compute position gradient */ |
335 |
< |
FVECT gv; |
336 |
< |
AMBSAMP *da; /* assumes standard ordering */ |
337 |
< |
register AMBHEMI *hp; |
334 |
> |
posgradient( /* compute position gradient */ |
335 |
> |
FVECT gv, |
336 |
> |
AMBSAMP *da, /* assumes standard ordering */ |
337 |
> |
register AMBHEMI *hp |
338 |
> |
) |
339 |
|
{ |
340 |
|
register int i, j; |
341 |
|
double nextsine, lastsine, b, d; |
389 |
|
|
390 |
|
|
391 |
|
void |
392 |
< |
dirgradient(gv, da, hp) /* compute direction gradient */ |
393 |
< |
FVECT gv; |
394 |
< |
AMBSAMP *da; /* assumes standard ordering */ |
395 |
< |
register AMBHEMI *hp; |
392 |
> |
dirgradient( /* compute direction gradient */ |
393 |
> |
FVECT gv, |
394 |
> |
AMBSAMP *da, /* assumes standard ordering */ |
395 |
> |
register AMBHEMI *hp |
396 |
> |
) |
397 |
|
{ |
398 |
|
register int i, j; |
399 |
|
double mag; |