18 |
|
|
19 |
|
#include "random.h" |
20 |
|
|
21 |
< |
#define OCTSCALE 0.5 /* ceil((valid rad.)/(cube size)) */ |
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 */ |
53 |
|
#endif |
54 |
|
#endif |
55 |
|
#ifndef SORT_INTVL |
56 |
< |
#define SORT_INTVL (SORT_THRESH*32) |
56 |
> |
#define SORT_INTVL (SORT_THRESH*256) |
57 |
|
#endif |
58 |
|
#ifndef MAX_SORT_INTVL |
59 |
< |
#define MAX_SORT_INTVL (SORT_INTVL<<8) |
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 |
|
|
95 |
|
maxarad = thescene.cusize / 2.0; |
96 |
|
} else { |
97 |
|
minarad = thescene.cusize / ar; |
98 |
< |
maxarad = 16 * minarad; /* heuristic */ |
98 |
> |
maxarad = 64 * minarad; /* heuristic */ |
99 |
|
if (maxarad > thescene.cusize / 2.0) |
100 |
|
maxarad = thescene.cusize / 2.0; |
101 |
|
} |
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; |
246 |
|
int i; |
247 |
|
register int j; |
248 |
|
register AMBVAL *av; |
249 |
< |
/* do this node */ |
249 |
> |
|
250 |
|
wsum = 0.0; |
251 |
+ |
/* do this node */ |
252 |
|
for (av = at->alist; av != NULL; av = av->next) { |
253 |
|
if (tracktime) |
254 |
|
av->latick = ambclock++; |
262 |
|
/* |
263 |
|
* Ambient radius test. |
264 |
|
*/ |
265 |
< |
e1 = 0.0; |
266 |
< |
for (j = 0; j < 3; j++) { |
267 |
< |
d = av->pos[j] - r->rop[j]; |
268 |
< |
e1 += d * d; |
269 |
< |
} |
265 |
> |
d = av->pos[0] - r->rop[0]; |
266 |
> |
e1 = d * d; |
267 |
> |
d = av->pos[1] - r->rop[1]; |
268 |
> |
e1 += d * d; |
269 |
> |
d = av->pos[2] - r->rop[2]; |
270 |
> |
e1 += d * d; |
271 |
|
e1 /= av->rad * av->rad; |
272 |
|
if (e1 > ambacc*ambacc*1.21) |
273 |
|
continue; |
291 |
|
* Jittering final test reduces image artifacts. |
292 |
|
*/ |
293 |
|
wt = sqrt(e1) + sqrt(e2); |
294 |
< |
wt *= .9 + .2*urand(9015+samplendx); |
281 |
< |
if (wt > ambacc) |
294 |
> |
if (wt > ambacc*(.9+.2*urand(9015+samplendx))) |
295 |
|
continue; |
296 |
|
if (wt <= 1e-3) |
297 |
|
wt = 1e3; |
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 |
|
} |
647 |
|
* when we're thrashing, which is when we need to do it. |
648 |
|
*/ |
649 |
|
#ifdef DEBUG |
650 |
< |
sprintf(errmsg, "sorting %u ambient values...", nambvals); |
650 |
> |
sprintf(errmsg, "sorting %u ambient values at ambclock=%lu...", |
651 |
> |
nambvals, ambclock); |
652 |
|
eputs(errmsg); |
653 |
|
#endif |
654 |
|
i_avlist = 0; |
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 */ |