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; /* total number of indirect values */ |
64 |
+ |
static unsigned int nambshare = 0; /* number of values from file */ |
65 |
|
static unsigned long ambclock = 0; /* ambient access clock */ |
58 |
– |
static unsigned int nambvals = 0; /* number of stored ambient values */ |
66 |
|
static unsigned long lastsort = 0; /* time of last value sort */ |
67 |
|
static long sortintvl = SORT_INTVL; /* time until next sort */ |
68 |
|
|
70 |
|
/* |
71 |
|
* Track access times unless we are sharing ambient values |
72 |
|
* through memory on a multiprocessor, when we want to avoid |
73 |
< |
* claiming our own memory (copy on write). |
73 |
> |
* claiming our own memory (copy on write). Go ahead anyway |
74 |
> |
* if more than two thirds of our values are unshared. |
75 |
|
*/ |
76 |
< |
#define tracktime (shm_boundary == NULL || ambfp == NULL) |
76 |
> |
#define tracktime (shm_boundary == NULL || nambvals > 3*nambshare) |
77 |
|
|
78 |
|
#define AMBFLUSH (BUFSIZ/AMBVALSIZ) |
79 |
|
|
111 |
|
setambacc(newa) /* set ambient accuracy */ |
112 |
|
double newa; |
113 |
|
{ |
114 |
< |
static double oldambacc = -1.0; |
114 |
> |
double ambdiff; |
115 |
|
|
116 |
< |
ambacc = newa < 0.0 ? 0.0 : newa; /* may be done already */ |
117 |
< |
if (oldambacc < -FTINY) |
118 |
< |
oldambacc = ambacc; /* do nothing first call */ |
119 |
< |
if (fabs(newa - oldambacc) < 0.01) |
120 |
< |
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 */ |
116 |
> |
if (newa < 0.0) |
117 |
> |
newa = 0.0; |
118 |
> |
ambdiff = fabs(newa - ambacc); |
119 |
> |
if (ambdiff >= .01 && (ambacc = newa) > FTINY && nambvals > 0) |
120 |
> |
sortambvals(1); /* rebuild tree */ |
121 |
|
} |
122 |
|
|
123 |
|
|
145 |
|
avinsert(avstore(&amb)); |
146 |
|
/* align */ |
147 |
|
fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1); |
148 |
+ |
nambshare = nambvals; |
149 |
|
} else if ((ambfp = fopen(afile, "w+")) != NULL) |
150 |
|
initambfile(1); |
151 |
|
else { |
179 |
|
} |
180 |
|
|
181 |
|
|
182 |
< |
ambient(acol, r) /* compute ambient component for ray */ |
182 |
> |
ambient(acol, r, nrm) /* compute ambient component for ray */ |
183 |
|
COLOR acol; |
184 |
|
register RAY *r; |
185 |
+ |
FVECT nrm; |
186 |
|
{ |
187 |
|
static int rdepth = 0; /* ambient recursion */ |
188 |
|
double d; |
201 |
|
rdepth++; |
202 |
|
d = doambient(acol, r, r->rweight, NULL, NULL); |
203 |
|
rdepth--; |
204 |
< |
if (d == 0.0) |
204 |
> |
if (d <= FTINY) |
205 |
|
goto dumbamb; |
206 |
|
return; |
207 |
|
} |
209 |
|
sortambvals(0); |
210 |
|
/* get ambient value */ |
211 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
212 |
< |
d = sumambient(acol, r, rdepth, |
212 |
> |
d = sumambient(acol, r, nrm, rdepth, |
213 |
|
&atrunk, thescene.cuorg, thescene.cusize); |
214 |
< |
if (d > FTINY) |
214 |
> |
if (d > FTINY) { |
215 |
|
scalecolor(acol, 1.0/d); |
216 |
< |
else { |
212 |
< |
d = makeambient(acol, r, rdepth++); |
213 |
< |
rdepth--; |
216 |
> |
return; |
217 |
|
} |
218 |
+ |
rdepth++; /* need to cache new value */ |
219 |
+ |
d = makeambient(acol, r, nrm, rdepth-1); |
220 |
+ |
rdepth--; |
221 |
|
if (d > FTINY) |
222 |
|
return; |
223 |
|
dumbamb: /* return global value */ |
224 |
|
copycolor(acol, ambval); |
225 |
+ |
#if AMBVWT |
226 |
+ |
if (nambvals == 0) |
227 |
+ |
return; |
228 |
+ |
scalecolor(acol, (double)AMBVWT); |
229 |
+ |
addcolor(acol, avsum); /* average in computations */ |
230 |
+ |
d = 1.0/(AMBVWT+nambvals); |
231 |
+ |
scalecolor(acol, d); |
232 |
+ |
#endif |
233 |
|
} |
234 |
|
|
235 |
|
|
236 |
|
double |
237 |
< |
sumambient(acol, r, al, at, c0, s) /* get interpolated ambient value */ |
237 |
> |
sumambient(acol, r, rn, al, at, c0, s) /* get interpolated ambient value */ |
238 |
|
COLOR acol; |
239 |
|
register RAY *r; |
240 |
+ |
FVECT rn; |
241 |
|
int al; |
242 |
|
AMBTREE *at; |
243 |
|
FVECT c0; |
301 |
|
else |
302 |
|
wt = 1.0 / wt; |
303 |
|
wsum += wt; |
304 |
< |
extambient(ct, av, r->rop, r->ron); |
304 |
> |
extambient(ct, av, r->rop, rn); |
305 |
|
scalecolor(ct, wt); |
306 |
|
addcolor(acol, ct); |
307 |
|
} |
320 |
|
break; |
321 |
|
} |
322 |
|
if (j == 3) |
323 |
< |
wsum += sumambient(acol, r, al, at->kid+i, ck0, s); |
323 |
> |
wsum += sumambient(acol, r, rn, al, at->kid+i, ck0, s); |
324 |
|
} |
325 |
|
return(wsum); |
326 |
|
} |
327 |
|
|
328 |
|
|
329 |
|
double |
330 |
< |
makeambient(acol, r, al) /* make a new ambient value */ |
330 |
> |
makeambient(acol, r, rn, al) /* make a new ambient value */ |
331 |
|
COLOR acol; |
332 |
|
register RAY *r; |
333 |
+ |
FVECT rn; |
334 |
|
int al; |
335 |
|
{ |
336 |
|
AMBVAL amb; |
337 |
|
FVECT gp, gd; |
338 |
|
/* compute weight */ |
339 |
|
amb.weight = pow(AVGREFL, (double)al); |
340 |
< |
if (r->rweight < 0.2*amb.weight) /* heuristic */ |
340 |
> |
if (r->rweight < 0.1*amb.weight) /* heuristic */ |
341 |
|
amb.weight = r->rweight; |
342 |
|
/* compute ambient */ |
343 |
|
amb.rad = doambient(acol, r, amb.weight, gp, gd); |
344 |
< |
if (amb.rad == 0.0) |
344 |
> |
if (amb.rad <= FTINY) |
345 |
|
return(0.0); |
346 |
|
/* store it */ |
347 |
|
VCOPY(amb.pos, r->rop); |
352 |
|
VCOPY(amb.gdir, gd); |
353 |
|
/* insert into tree */ |
354 |
|
avsave(&amb); /* and save to file */ |
355 |
+ |
if (rn != r->ron) |
356 |
+ |
extambient(acol, &amb, r->rop, rn); /* texture */ |
357 |
|
return(amb.rad); |
358 |
|
} |
359 |
|
|
444 |
|
copystruct(av, aval); |
445 |
|
av->latick = ambclock; |
446 |
|
av->next = NULL; |
447 |
+ |
addcolor(avsum, av->val); /* add to sum for averaging */ |
448 |
|
nambvals++; |
449 |
|
return(av); |
450 |
|
} |
681 |
|
free((char *)avlist2); |
682 |
|
/* compute new sort interval */ |
683 |
|
sortintvl = ambclock - lastsort; |
684 |
< |
if (sortintvl > MAX_SORT_INTVL) |
684 |
> |
if (sortintvl >= MAX_SORT_INTVL/2) |
685 |
|
sortintvl = MAX_SORT_INTVL; |
686 |
|
else |
687 |
|
sortintvl <<= 1; /* wait twice as long next */ |