18 |
|
|
19 |
|
#include "random.h" |
20 |
|
|
21 |
+ |
#ifndef OCTSCALE |
22 |
|
#define OCTSCALE 1.0 /* ceil((valid rad.)/(cube size)) */ |
23 |
+ |
#endif |
24 |
+ |
#ifndef AMBVWT |
25 |
+ |
#define AMBVWT 250 /* relative ambient value weight (# calcs) */ |
26 |
+ |
#endif |
27 |
|
|
28 |
|
typedef struct ambtree { |
29 |
|
AMBVAL *alist; /* ambient value list */ |
59 |
|
#define MAX_SORT_INTVL (SORT_INTVL<<4) |
60 |
|
#endif |
61 |
|
|
62 |
+ |
static COLOR avsum = BLKCOLOR; /* computed ambient value sum */ |
63 |
+ |
static unsigned int nambvals = 0; /* number of computed ambient values */ |
64 |
|
static unsigned long ambclock = 0; /* ambient access clock */ |
58 |
– |
static unsigned int nambvals = 0; /* number of stored ambient values */ |
65 |
|
static unsigned long lastsort = 0; /* time of last value sort */ |
66 |
|
static long sortintvl = SORT_INTVL; /* time until next sort */ |
67 |
|
|
109 |
|
setambacc(newa) /* set ambient accuracy */ |
110 |
|
double newa; |
111 |
|
{ |
112 |
< |
static double oldambacc = -1.0; |
112 |
> |
double ambdiff; |
113 |
|
|
114 |
< |
ambacc = newa < 0.0 ? 0.0 : newa; /* may be done already */ |
115 |
< |
if (oldambacc < -FTINY) |
116 |
< |
oldambacc = ambacc; /* do nothing first call */ |
117 |
< |
if (fabs(newa - oldambacc) < 0.01) |
118 |
< |
return; /* insignificant -- don't bother */ |
113 |
< |
if (ambacc <= FTINY) |
114 |
< |
return; /* cannot build new tree */ |
115 |
< |
/* else need to rebuild tree */ |
116 |
< |
sortambvals(1); |
117 |
< |
oldambacc = ambacc; /* remeber setting for next call */ |
114 |
> |
if (newa < 0.0) |
115 |
> |
newa = 0.0; |
116 |
> |
ambdiff = fabs(newa - ambacc); |
117 |
> |
if (ambdiff >= .01 && (ambacc = newa) > FTINY && nambvals > 0) |
118 |
> |
sortambvals(1); /* rebuild tree */ |
119 |
|
} |
120 |
|
|
121 |
|
|
197 |
|
rdepth++; |
198 |
|
d = doambient(acol, r, r->rweight, NULL, NULL); |
199 |
|
rdepth--; |
200 |
< |
if (d == 0.0) |
200 |
> |
if (d <= FTINY) |
201 |
|
goto dumbamb; |
202 |
|
return; |
203 |
|
} |
207 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
208 |
|
d = sumambient(acol, r, rdepth, |
209 |
|
&atrunk, thescene.cuorg, thescene.cusize); |
210 |
< |
if (d > FTINY) |
210 |
> |
if (d > FTINY) { |
211 |
|
scalecolor(acol, 1.0/d); |
212 |
< |
else { |
212 |
< |
d = makeambient(acol, r, rdepth++); |
213 |
< |
rdepth--; |
212 |
> |
return; |
213 |
|
} |
214 |
+ |
rdepth++; /* need to cache new value */ |
215 |
+ |
d = makeambient(acol, r, rdepth-1); |
216 |
+ |
rdepth--; |
217 |
|
if (d > FTINY) |
218 |
|
return; |
219 |
|
dumbamb: /* return global value */ |
220 |
|
copycolor(acol, ambval); |
221 |
+ |
#if AMBVWT |
222 |
+ |
if (nambvals == 0) |
223 |
+ |
return; |
224 |
+ |
scalecolor(acol, (double)AMBVWT); |
225 |
+ |
addcolor(acol, avsum); /* average in computations */ |
226 |
+ |
d = 1.0/(AMBVWT+nambvals); |
227 |
+ |
scalecolor(acol, d); |
228 |
+ |
#endif |
229 |
|
} |
230 |
|
|
231 |
|
|
289 |
|
* Jittering final test reduces image artifacts. |
290 |
|
*/ |
291 |
|
wt = sqrt(e1) + sqrt(e2); |
292 |
< |
wt *= .9 + .2*urand(9015+samplendx); |
283 |
< |
if (wt > ambacc) |
292 |
> |
if (wt > ambacc*(.9+.2*urand(9015+samplendx))) |
293 |
|
continue; |
294 |
|
if (wt <= 1e-3) |
295 |
|
wt = 1e3; |
331 |
|
FVECT gp, gd; |
332 |
|
/* compute weight */ |
333 |
|
amb.weight = pow(AVGREFL, (double)al); |
334 |
< |
if (r->rweight < 0.2*amb.weight) /* heuristic */ |
334 |
> |
if (r->rweight < 0.1*amb.weight) /* heuristic */ |
335 |
|
amb.weight = r->rweight; |
336 |
|
/* compute ambient */ |
337 |
|
amb.rad = doambient(acol, r, amb.weight, gp, gd); |
338 |
< |
if (amb.rad == 0.0) |
338 |
> |
if (amb.rad <= FTINY) |
339 |
|
return(0.0); |
340 |
|
/* store it */ |
341 |
|
VCOPY(amb.pos, r->rop); |
436 |
|
copystruct(av, aval); |
437 |
|
av->latick = ambclock; |
438 |
|
av->next = NULL; |
439 |
+ |
addcolor(avsum, av->val); /* add to sum for averaging */ |
440 |
|
nambvals++; |
441 |
|
return(av); |
442 |
|
} |
673 |
|
free((char *)avlist2); |
674 |
|
/* compute new sort interval */ |
675 |
|
sortintvl = ambclock - lastsort; |
676 |
< |
if (sortintvl > MAX_SORT_INTVL) |
676 |
> |
if (sortintvl >= MAX_SORT_INTVL/2) |
677 |
|
sortintvl = MAX_SORT_INTVL; |
678 |
|
else |
679 |
|
sortintvl <<= 1; /* wait twice as long next */ |