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 |
|
|
176 |
|
} |
177 |
|
|
178 |
|
|
179 |
< |
ambient(acol, r) /* compute ambient component for ray */ |
179 |
> |
ambient(acol, r, nrm) /* compute ambient component for ray */ |
180 |
|
COLOR acol; |
181 |
|
register RAY *r; |
182 |
+ |
FVECT nrm; |
183 |
|
{ |
184 |
|
static int rdepth = 0; /* ambient recursion */ |
185 |
|
double d; |
198 |
|
rdepth++; |
199 |
|
d = doambient(acol, r, r->rweight, NULL, NULL); |
200 |
|
rdepth--; |
201 |
< |
if (d == 0.0) |
201 |
> |
if (d <= FTINY) |
202 |
|
goto dumbamb; |
203 |
|
return; |
204 |
|
} |
206 |
|
sortambvals(0); |
207 |
|
/* get ambient value */ |
208 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
209 |
< |
d = sumambient(acol, r, rdepth, |
209 |
> |
d = sumambient(acol, r, nrm, rdepth, |
210 |
|
&atrunk, thescene.cuorg, thescene.cusize); |
211 |
< |
if (d > FTINY) |
211 |
> |
if (d > FTINY) { |
212 |
|
scalecolor(acol, 1.0/d); |
213 |
< |
else { |
212 |
< |
d = makeambient(acol, r, rdepth++); |
213 |
< |
rdepth--; |
213 |
> |
return; |
214 |
|
} |
215 |
+ |
rdepth++; /* need to cache new value */ |
216 |
+ |
d = makeambient(acol, r, nrm, rdepth-1); |
217 |
+ |
rdepth--; |
218 |
|
if (d > FTINY) |
219 |
|
return; |
220 |
|
dumbamb: /* return global value */ |
221 |
|
copycolor(acol, ambval); |
222 |
+ |
#if AMBVWT |
223 |
+ |
if (nambvals == 0) |
224 |
+ |
return; |
225 |
+ |
scalecolor(acol, (double)AMBVWT); |
226 |
+ |
addcolor(acol, avsum); /* average in computations */ |
227 |
+ |
d = 1.0/(AMBVWT+nambvals); |
228 |
+ |
scalecolor(acol, d); |
229 |
+ |
#endif |
230 |
|
} |
231 |
|
|
232 |
|
|
233 |
|
double |
234 |
< |
sumambient(acol, r, al, at, c0, s) /* get interpolated ambient value */ |
234 |
> |
sumambient(acol, r, rn, al, at, c0, s) /* get interpolated ambient value */ |
235 |
|
COLOR acol; |
236 |
|
register RAY *r; |
237 |
+ |
FVECT rn; |
238 |
|
int al; |
239 |
|
AMBTREE *at; |
240 |
|
FVECT c0; |
298 |
|
else |
299 |
|
wt = 1.0 / wt; |
300 |
|
wsum += wt; |
301 |
< |
extambient(ct, av, r->rop, r->ron); |
301 |
> |
extambient(ct, av, r->rop, rn); |
302 |
|
scalecolor(ct, wt); |
303 |
|
addcolor(acol, ct); |
304 |
|
} |
317 |
|
break; |
318 |
|
} |
319 |
|
if (j == 3) |
320 |
< |
wsum += sumambient(acol, r, al, at->kid+i, ck0, s); |
320 |
> |
wsum += sumambient(acol, r, rn, al, at->kid+i, ck0, s); |
321 |
|
} |
322 |
|
return(wsum); |
323 |
|
} |
324 |
|
|
325 |
|
|
326 |
|
double |
327 |
< |
makeambient(acol, r, al) /* make a new ambient value */ |
327 |
> |
makeambient(acol, r, rn, al) /* make a new ambient value */ |
328 |
|
COLOR acol; |
329 |
|
register RAY *r; |
330 |
+ |
FVECT rn; |
331 |
|
int al; |
332 |
|
{ |
333 |
|
AMBVAL amb; |
334 |
|
FVECT gp, gd; |
335 |
|
/* compute weight */ |
336 |
|
amb.weight = pow(AVGREFL, (double)al); |
337 |
< |
if (r->rweight < 0.2*amb.weight) /* heuristic */ |
337 |
> |
if (r->rweight < 0.1*amb.weight) /* heuristic */ |
338 |
|
amb.weight = r->rweight; |
339 |
|
/* compute ambient */ |
340 |
|
amb.rad = doambient(acol, r, amb.weight, gp, gd); |
341 |
< |
if (amb.rad == 0.0) |
341 |
> |
if (amb.rad <= FTINY) |
342 |
|
return(0.0); |
343 |
|
/* store it */ |
344 |
|
VCOPY(amb.pos, r->rop); |
349 |
|
VCOPY(amb.gdir, gd); |
350 |
|
/* insert into tree */ |
351 |
|
avsave(&amb); /* and save to file */ |
352 |
+ |
if (rn != r->ron) |
353 |
+ |
extambient(acol, &amb, r->rop, rn); /* texture */ |
354 |
|
return(amb.rad); |
355 |
|
} |
356 |
|
|
441 |
|
copystruct(av, aval); |
442 |
|
av->latick = ambclock; |
443 |
|
av->next = NULL; |
444 |
+ |
addcolor(avsum, av->val); /* add to sum for averaging */ |
445 |
|
nambvals++; |
446 |
|
return(av); |
447 |
|
} |
678 |
|
free((char *)avlist2); |
679 |
|
/* compute new sort interval */ |
680 |
|
sortintvl = ambclock - lastsort; |
681 |
< |
if (sortintvl > MAX_SORT_INTVL) |
681 |
> |
if (sortintvl >= MAX_SORT_INTVL/2) |
682 |
|
sortintvl = MAX_SORT_INTVL; |
683 |
|
else |
684 |
|
sortintvl <<= 1; /* wait twice as long next */ |