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 |
24 |
|
|
25 |
|
typedef struct ambtree { |
26 |
|
AMBVAL *alist; /* ambient value list */ |
57 |
|
#endif |
58 |
|
|
59 |
|
static COLOR avsum = BLKCOLOR; /* computed ambient value sum */ |
60 |
< |
static unsigned int nambvals = 0; /* number of computed ambient values */ |
60 |
> |
static unsigned int nambvals = 0; /* total number of indirect values */ |
61 |
> |
static unsigned int nambshare = 0; /* number of values from file */ |
62 |
|
static unsigned long ambclock = 0; /* ambient access clock */ |
63 |
|
static unsigned long lastsort = 0; /* time of last value sort */ |
64 |
|
static long sortintvl = SORT_INTVL; /* time until next sort */ |
67 |
|
/* |
68 |
|
* Track access times unless we are sharing ambient values |
69 |
|
* through memory on a multiprocessor, when we want to avoid |
70 |
< |
* claiming our own memory (copy on write). |
70 |
> |
* claiming our own memory (copy on write). Go ahead anyway |
71 |
> |
* if more than two thirds of our values are unshared. |
72 |
|
*/ |
73 |
< |
#define tracktime (shm_boundary == NULL || ambfp == NULL) |
73 |
> |
#define tracktime (shm_boundary == NULL || nambvals > 3*nambshare) |
74 |
|
|
75 |
|
#define AMBFLUSH (BUFSIZ/AMBVALSIZ) |
76 |
|
|
108 |
|
setambacc(newa) /* set ambient accuracy */ |
109 |
|
double newa; |
110 |
|
{ |
111 |
< |
static double oldambacc = -1.0; |
111 |
> |
double ambdiff; |
112 |
|
|
113 |
< |
ambacc = newa < 0.0 ? 0.0 : newa; /* may be done already */ |
114 |
< |
if (oldambacc < -FTINY) |
115 |
< |
oldambacc = ambacc; /* do nothing first call */ |
116 |
< |
if (fabs(newa - oldambacc) < 0.01) |
117 |
< |
return; /* insignificant -- don't bother */ |
119 |
< |
if (ambacc <= FTINY) |
120 |
< |
return; /* cannot build new tree */ |
121 |
< |
/* else need to rebuild tree */ |
122 |
< |
sortambvals(1); |
123 |
< |
oldambacc = ambacc; /* remeber setting for next call */ |
113 |
> |
if (newa < 0.0) |
114 |
> |
newa = 0.0; |
115 |
> |
ambdiff = fabs(newa - ambacc); |
116 |
> |
if (ambdiff >= .01 && (ambacc = newa) > FTINY && nambvals > 0) |
117 |
> |
sortambvals(1); /* rebuild tree */ |
118 |
|
} |
119 |
|
|
120 |
|
|
142 |
|
avinsert(avstore(&amb)); |
143 |
|
/* align */ |
144 |
|
fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1); |
145 |
+ |
nambshare = nambvals; |
146 |
|
} else if ((ambfp = fopen(afile, "w+")) != NULL) |
147 |
|
initambfile(1); |
148 |
|
else { |
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; |
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) { |
212 |
|
scalecolor(acol, 1.0/d); |
213 |
|
return; |
214 |
|
} |
215 |
< |
rdepth++; |
216 |
< |
d = makeambient(acol, r, rdepth-1); |
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 |
227 |
< |
if (nambvals == 0) |
222 |
> |
if (ambvwt <= 0 | nambvals == 0) |
223 |
|
return; |
224 |
< |
scalecolor(acol, (double)AMBVWT); |
224 |
> |
scalecolor(acol, (double)ambvwt); |
225 |
|
addcolor(acol, avsum); /* average in computations */ |
226 |
< |
d = 1.0/(AMBVWT+nambvals); |
226 |
> |
d = 1.0/(ambvwt+nambvals); |
227 |
|
scalecolor(acol, d); |
233 |
– |
#endif |
228 |
|
} |
229 |
|
|
230 |
|
|
231 |
|
double |
232 |
< |
sumambient(acol, r, al, at, c0, s) /* get interpolated ambient value */ |
232 |
> |
sumambient(acol, r, rn, al, at, c0, s) /* get interpolated ambient value */ |
233 |
|
COLOR acol; |
234 |
|
register RAY *r; |
235 |
+ |
FVECT rn; |
236 |
|
int al; |
237 |
|
AMBTREE *at; |
238 |
|
FVECT c0; |
296 |
|
else |
297 |
|
wt = 1.0 / wt; |
298 |
|
wsum += wt; |
299 |
< |
extambient(ct, av, r->rop, r->ron); |
299 |
> |
extambient(ct, av, r->rop, rn); |
300 |
|
scalecolor(ct, wt); |
301 |
|
addcolor(acol, ct); |
302 |
|
} |
315 |
|
break; |
316 |
|
} |
317 |
|
if (j == 3) |
318 |
< |
wsum += sumambient(acol, r, al, at->kid+i, ck0, s); |
318 |
> |
wsum += sumambient(acol, r, rn, al, at->kid+i, ck0, s); |
319 |
|
} |
320 |
|
return(wsum); |
321 |
|
} |
322 |
|
|
323 |
|
|
324 |
|
double |
325 |
< |
makeambient(acol, r, al) /* make a new ambient value */ |
325 |
> |
makeambient(acol, r, rn, al) /* make a new ambient value */ |
326 |
|
COLOR acol; |
327 |
|
register RAY *r; |
328 |
+ |
FVECT rn; |
329 |
|
int al; |
330 |
|
{ |
331 |
|
AMBVAL amb; |
332 |
|
FVECT gp, gd; |
333 |
|
/* compute weight */ |
334 |
|
amb.weight = pow(AVGREFL, (double)al); |
335 |
< |
if (r->rweight < 0.2*amb.weight) /* heuristic */ |
335 |
> |
if (r->rweight < 0.1*amb.weight) /* heuristic */ |
336 |
|
amb.weight = r->rweight; |
337 |
|
/* compute ambient */ |
338 |
|
amb.rad = doambient(acol, r, amb.weight, gp, gd); |
339 |
< |
if (amb.rad == 0.0) |
339 |
> |
if (amb.rad <= FTINY) |
340 |
|
return(0.0); |
341 |
|
/* store it */ |
342 |
|
VCOPY(amb.pos, r->rop); |
347 |
|
VCOPY(amb.gdir, gd); |
348 |
|
/* insert into tree */ |
349 |
|
avsave(&amb); /* and save to file */ |
350 |
+ |
if (rn != r->ron) |
351 |
+ |
extambient(acol, &amb, r->rop, rn); /* texture */ |
352 |
|
return(amb.rad); |
353 |
|
} |
354 |
|
|