1 |
< |
/* Copyright (c) 1995 Regents of the University of California */ |
2 |
< |
|
3 |
< |
#ifndef lint |
4 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
< |
#endif |
6 |
< |
|
1 |
> |
static const char RCSid[] = "$Id$"; |
2 |
|
/* |
3 |
|
* ambient.c - routines dealing with ambient (inter-reflected) component. |
4 |
+ |
* |
5 |
+ |
* Declarations of external symbols in ambient.h |
6 |
|
*/ |
7 |
|
|
8 |
< |
#include "ray.h" |
8 |
> |
#include "copyright.h" |
9 |
|
|
10 |
< |
#include "octree.h" |
10 |
> |
#include <string.h> |
11 |
|
|
12 |
+ |
#include "platform.h" |
13 |
+ |
#include "ray.h" |
14 |
|
#include "otypes.h" |
15 |
< |
|
15 |
> |
#include "otspecial.h" |
16 |
> |
#include "resolu.h" |
17 |
|
#include "ambient.h" |
18 |
– |
|
18 |
|
#include "random.h" |
19 |
+ |
#include "pmapamb.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 |
24 |
|
|
28 |
– |
typedef struct ambtree { |
29 |
– |
AMBVAL *alist; /* ambient value list */ |
30 |
– |
struct ambtree *kid; /* 8 child nodes */ |
31 |
– |
} AMBTREE; /* ambient octree */ |
32 |
– |
|
33 |
– |
extern CUBE thescene; /* contains space boundaries */ |
34 |
– |
|
25 |
|
extern char *shm_boundary; /* memory sharing boundary */ |
26 |
|
|
27 |
< |
#define MAXASET 511 /* maximum number of elements in ambient set */ |
27 |
> |
#ifndef MAXASET |
28 |
> |
#define MAXASET 4095 /* maximum number of elements in ambient set */ |
29 |
> |
#endif |
30 |
|
OBJECT ambset[MAXASET+1]={0}; /* ambient include/exclude set */ |
31 |
|
|
32 |
|
double maxarad; /* maximum ambient radius */ |
38 |
|
static int nunflshed = 0; /* number of unflushed ambient values */ |
39 |
|
|
40 |
|
#ifndef SORT_THRESH |
41 |
< |
#ifdef BIGMEM |
42 |
< |
#define SORT_THRESH ((9L<<20)/sizeof(AMBVAL)) |
41 |
> |
#ifdef SMLMEM |
42 |
> |
#define SORT_THRESH ((16L<<20)/sizeof(AMBVAL)) |
43 |
|
#else |
44 |
< |
#define SORT_THRESH ((3L<<20)/sizeof(AMBVAL)) |
44 |
> |
#define SORT_THRESH ((64L<<20)/sizeof(AMBVAL)) |
45 |
|
#endif |
46 |
|
#endif |
47 |
|
#ifndef SORT_INTVL |
48 |
< |
#define SORT_INTVL (SORT_THRESH*256) |
48 |
> |
#define SORT_INTVL (SORT_THRESH<<1) |
49 |
|
#endif |
50 |
|
#ifndef MAX_SORT_INTVL |
51 |
< |
#define MAX_SORT_INTVL (SORT_INTVL<<4) |
51 |
> |
#define MAX_SORT_INTVL (SORT_INTVL<<6) |
52 |
|
#endif |
53 |
|
|
54 |
< |
static COLOR avsum = BLKCOLOR; /* computed ambient value sum */ |
55 |
< |
static unsigned int nambvals = 0; /* number of computed ambient values */ |
54 |
> |
|
55 |
> |
static double avsum = 0.; /* computed ambient value sum (log) */ |
56 |
> |
static unsigned int navsum = 0; /* number of values in avsum */ |
57 |
> |
static unsigned int nambvals = 0; /* total number of indirect values */ |
58 |
> |
static unsigned int nambshare = 0; /* number of values from file */ |
59 |
|
static unsigned long ambclock = 0; /* ambient access clock */ |
60 |
|
static unsigned long lastsort = 0; /* time of last value sort */ |
61 |
|
static long sortintvl = SORT_INTVL; /* time until next sort */ |
62 |
+ |
static FILE *ambinp = NULL; /* auxiliary file for input */ |
63 |
+ |
static long lastpos = -1; /* last flush position */ |
64 |
|
|
65 |
|
#define MAXACLOCK (1L<<30) /* clock turnover value */ |
66 |
|
/* |
67 |
|
* Track access times unless we are sharing ambient values |
68 |
|
* through memory on a multiprocessor, when we want to avoid |
69 |
< |
* claiming our own memory (copy on write). |
69 |
> |
* claiming our own memory (copy on write). Go ahead anyway |
70 |
> |
* if more than two thirds of our values are unshared. |
71 |
> |
* Compile with -Dtracktime=0 to turn this code off. |
72 |
|
*/ |
73 |
< |
#define tracktime (shm_boundary == NULL || ambfp == NULL) |
73 |
> |
#ifndef tracktime |
74 |
> |
#define tracktime (shm_boundary == NULL || nambvals > 3*nambshare) |
75 |
> |
#endif |
76 |
|
|
77 |
|
#define AMBFLUSH (BUFSIZ/AMBVALSIZ) |
78 |
|
|
79 |
< |
#define newambval() (AMBVAL *)bmalloc(sizeof(AMBVAL)) |
79 |
> |
#define newambval() (AMBVAL *)malloc(sizeof(AMBVAL)) |
80 |
|
|
81 |
< |
extern long ftell(), lseek(); |
82 |
< |
static int initambfile(), avsave(), avinsert(), sortambvals(); |
83 |
< |
static AMBVAL *avstore(); |
81 |
> |
#define tfunc(lwr, x, upr) (((x)-(lwr))/((upr)-(lwr))) |
82 |
> |
|
83 |
> |
static void initambfile(int creat); |
84 |
> |
static void avsave(AMBVAL *av); |
85 |
> |
static AMBVAL *avstore(AMBVAL *aval); |
86 |
> |
static AMBTREE *newambtree(void); |
87 |
> |
static void freeambtree(AMBTREE *atp); |
88 |
> |
|
89 |
> |
typedef void unloadtf_t(AMBVAL *); |
90 |
> |
static unloadtf_t avinsert; |
91 |
> |
static unloadtf_t av2list; |
92 |
> |
static unloadtf_t avfree; |
93 |
> |
static void unloadatree(AMBTREE *at, unloadtf_t *f); |
94 |
> |
|
95 |
> |
static int aposcmp(const void *avp1, const void *avp2); |
96 |
> |
static int avlmemi(AMBVAL *avaddr); |
97 |
> |
static void sortambvals(int always); |
98 |
> |
|
99 |
> |
static int plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang); |
100 |
> |
static double sumambient(COLOR acol, RAY *r, FVECT rn, int al, |
101 |
> |
AMBTREE *at, FVECT c0, double s); |
102 |
> |
static int makeambient(COLOR acol, RAY *r, FVECT rn, int al); |
103 |
> |
static int extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv, |
104 |
> |
FVECT uvw[3]); |
105 |
> |
|
106 |
|
#ifdef F_SETLKW |
107 |
< |
static aflock(); |
107 |
> |
static void aflock(int typ); |
108 |
|
#endif |
109 |
|
|
110 |
|
|
111 |
< |
setambres(ar) /* set ambient resolution */ |
112 |
< |
int ar; |
111 |
> |
void |
112 |
> |
setambres( /* set ambient resolution */ |
113 |
> |
int ar |
114 |
> |
) |
115 |
|
{ |
116 |
|
ambres = ar < 0 ? 0 : ar; /* may be done already */ |
117 |
|
/* set min & max radii */ |
118 |
|
if (ar <= 0) { |
119 |
|
minarad = 0; |
120 |
< |
maxarad = thescene.cusize / 2.0; |
120 |
> |
maxarad = thescene.cusize*0.2; |
121 |
|
} else { |
122 |
|
minarad = thescene.cusize / ar; |
123 |
< |
maxarad = 64 * minarad; /* heuristic */ |
124 |
< |
if (maxarad > thescene.cusize / 2.0) |
125 |
< |
maxarad = thescene.cusize / 2.0; |
123 |
> |
maxarad = 64.0 * minarad; /* heuristic */ |
124 |
> |
if (maxarad > thescene.cusize*0.2) |
125 |
> |
maxarad = thescene.cusize*0.2; |
126 |
|
} |
127 |
|
if (minarad <= FTINY) |
128 |
< |
minarad = 10*FTINY; |
128 |
> |
minarad = 10.0*FTINY; |
129 |
|
if (maxarad <= minarad) |
130 |
< |
maxarad = 64 * minarad; |
130 |
> |
maxarad = 64.0 * minarad; |
131 |
|
} |
132 |
|
|
133 |
|
|
134 |
< |
setambacc(newa) /* set ambient accuracy */ |
135 |
< |
double newa; |
134 |
> |
void |
135 |
> |
setambacc( /* set ambient accuracy */ |
136 |
> |
double newa |
137 |
> |
) |
138 |
|
{ |
139 |
< |
double ambdiff; |
140 |
< |
|
141 |
< |
if (newa < 0.0) |
142 |
< |
newa = 0.0; |
143 |
< |
ambdiff = fabs(newa - ambacc); |
144 |
< |
if (ambdiff >= .01 && (ambacc = newa) > FTINY && nambvals > 0) |
145 |
< |
sortambvals(1); /* rebuild tree */ |
139 |
> |
static double olda; /* remember previous setting here */ |
140 |
> |
|
141 |
> |
newa *= (newa > 0); |
142 |
> |
if (fabs(newa - olda) >= .05*(newa + olda)) { |
143 |
> |
ambacc = newa; |
144 |
> |
if (ambacc > FTINY && nambvals > 0) |
145 |
> |
sortambvals(1); /* rebuild tree */ |
146 |
> |
} |
147 |
|
} |
148 |
|
|
149 |
|
|
150 |
< |
setambient(afile) /* initialize calculation */ |
151 |
< |
char *afile; |
150 |
> |
void |
151 |
> |
setambient(void) /* initialize calculation */ |
152 |
|
{ |
153 |
< |
long headlen; |
153 |
> |
int readonly = 0; |
154 |
> |
long flen; |
155 |
|
AMBVAL amb; |
156 |
+ |
/* make sure we're fresh */ |
157 |
+ |
ambdone(); |
158 |
|
/* init ambient limits */ |
159 |
|
setambres(ambres); |
160 |
|
setambacc(ambacc); |
161 |
< |
if (afile == NULL) |
161 |
> |
if (ambfile == NULL || !ambfile[0]) |
162 |
|
return; |
163 |
|
if (ambacc <= FTINY) { |
164 |
|
sprintf(errmsg, "zero ambient accuracy so \"%s\" not opened", |
165 |
< |
afile); |
165 |
> |
ambfile); |
166 |
|
error(WARNING, errmsg); |
167 |
|
return; |
168 |
|
} |
169 |
|
/* open ambient file */ |
170 |
< |
if ((ambfp = fopen(afile, "r+")) != NULL) { |
171 |
< |
initambfile(0); |
172 |
< |
headlen = ftell(ambfp); |
170 |
> |
if ((ambfp = fopen(ambfile, "r+")) == NULL) |
171 |
> |
readonly = (ambfp = fopen(ambfile, "r")) != NULL; |
172 |
> |
if (ambfp != NULL) { |
173 |
> |
initambfile(0); /* file exists */ |
174 |
> |
lastpos = ftell(ambfp); |
175 |
|
while (readambval(&amb, ambfp)) |
176 |
< |
avinsert(avstore(&amb)); |
177 |
< |
/* align */ |
178 |
< |
fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1); |
179 |
< |
} else if ((ambfp = fopen(afile, "w+")) != NULL) |
180 |
< |
initambfile(1); |
181 |
< |
else { |
182 |
< |
sprintf(errmsg, "cannot open ambient file \"%s\"", afile); |
176 |
> |
avstore(&amb); |
177 |
> |
nambshare = nambvals; /* share loaded values */ |
178 |
> |
if (readonly) { |
179 |
> |
sprintf(errmsg, |
180 |
> |
"loaded %u values from read-only ambient file", |
181 |
> |
nambvals); |
182 |
> |
error(WARNING, errmsg); |
183 |
> |
fclose(ambfp); /* close file so no writes */ |
184 |
> |
ambfp = NULL; |
185 |
> |
return; /* avoid ambsync() */ |
186 |
> |
} |
187 |
> |
/* align file pointer */ |
188 |
> |
lastpos += (long)nambvals*AMBVALSIZ; |
189 |
> |
flen = lseek(fileno(ambfp), (off_t)0, SEEK_END); |
190 |
> |
if (flen != lastpos) { |
191 |
> |
sprintf(errmsg, |
192 |
> |
"ignoring last %ld values in ambient file (corrupted)", |
193 |
> |
(flen - lastpos)/AMBVALSIZ); |
194 |
> |
error(WARNING, errmsg); |
195 |
> |
fseek(ambfp, lastpos, SEEK_SET); |
196 |
> |
ftruncate(fileno(ambfp), (off_t)lastpos); |
197 |
> |
} |
198 |
> |
} else if ((ambfp = fopen(ambfile, "w+")) != NULL) { |
199 |
> |
initambfile(1); /* else create new file */ |
200 |
> |
fflush(ambfp); |
201 |
> |
lastpos = ftell(ambfp); |
202 |
> |
} else { |
203 |
> |
sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile); |
204 |
|
error(SYSTEM, errmsg); |
205 |
|
} |
206 |
< |
nunflshed++; /* lie */ |
207 |
< |
ambsync(); |
206 |
> |
#ifdef F_SETLKW |
207 |
> |
aflock(F_UNLCK); /* release file */ |
208 |
> |
#endif |
209 |
|
} |
210 |
|
|
211 |
|
|
212 |
< |
ambnotify(obj) /* record new modifier */ |
213 |
< |
OBJECT obj; |
212 |
> |
void |
213 |
> |
ambdone(void) /* close ambient file and free memory */ |
214 |
|
{ |
215 |
+ |
if (ambfp != NULL) { /* close ambient file */ |
216 |
+ |
ambsync(); |
217 |
+ |
fclose(ambfp); |
218 |
+ |
ambfp = NULL; |
219 |
+ |
if (ambinp != NULL) { |
220 |
+ |
fclose(ambinp); |
221 |
+ |
ambinp = NULL; |
222 |
+ |
} |
223 |
+ |
lastpos = -1; |
224 |
+ |
} |
225 |
+ |
/* free ambient tree */ |
226 |
+ |
unloadatree(&atrunk, avfree); |
227 |
+ |
/* reset state variables */ |
228 |
+ |
avsum = 0.; |
229 |
+ |
navsum = 0; |
230 |
+ |
nambvals = 0; |
231 |
+ |
nambshare = 0; |
232 |
+ |
ambclock = 0; |
233 |
+ |
lastsort = 0; |
234 |
+ |
sortintvl = SORT_INTVL; |
235 |
+ |
} |
236 |
+ |
|
237 |
+ |
|
238 |
+ |
void |
239 |
+ |
ambnotify( /* record new modifier */ |
240 |
+ |
OBJECT obj |
241 |
+ |
) |
242 |
+ |
{ |
243 |
|
static int hitlimit = 0; |
244 |
< |
register OBJREC *o = objptr(obj); |
245 |
< |
register char **amblp; |
244 |
> |
OBJREC *o; |
245 |
> |
char **amblp; |
246 |
|
|
247 |
+ |
if (obj == OVOID) { /* starting over */ |
248 |
+ |
ambset[0] = 0; |
249 |
+ |
hitlimit = 0; |
250 |
+ |
return; |
251 |
+ |
} |
252 |
+ |
o = objptr(obj); |
253 |
|
if (hitlimit || !ismodifier(o->otype)) |
254 |
|
return; |
255 |
|
for (amblp = amblist; *amblp != NULL; amblp++) |
265 |
|
} |
266 |
|
|
267 |
|
|
268 |
< |
ambient(acol, r) /* compute ambient component for ray */ |
269 |
< |
COLOR acol; |
270 |
< |
register RAY *r; |
268 |
> |
void |
269 |
> |
multambient( /* compute ambient component & multiply by coef. */ |
270 |
> |
COLOR aval, |
271 |
> |
RAY *r, |
272 |
> |
FVECT nrm |
273 |
> |
) |
274 |
|
{ |
275 |
+ |
static double logAvgAbsorp = 1; |
276 |
|
static int rdepth = 0; /* ambient recursion */ |
277 |
< |
double d; |
277 |
> |
COLOR acol, caustic; |
278 |
> |
int i, ok; |
279 |
> |
double d, l; |
280 |
|
|
281 |
+ |
/* PMAP: Factor in ambient from photon map, if enabled and ray is |
282 |
+ |
* ambient. Return as all ambient components accounted for, else |
283 |
+ |
* continue. */ |
284 |
+ |
if (ambPmap(aval, r, rdepth)) |
285 |
+ |
return; |
286 |
+ |
|
287 |
+ |
if (logAvgAbsorp > 0) /* exclude in -aw to avoid growth */ |
288 |
+ |
logAvgAbsorp = log(1.-AVGREFL); |
289 |
+ |
|
290 |
+ |
/* PMAP: Factor in specular-diffuse ambient (caustics) from photon |
291 |
+ |
* map, if enabled and ray is primary, else caustic is zero. Continue |
292 |
+ |
* with RADIANCE ambient calculation */ |
293 |
+ |
copycolor(caustic, aval); |
294 |
+ |
ambPmapCaustic(caustic, r, rdepth); |
295 |
+ |
|
296 |
|
if (ambdiv <= 0) /* no ambient calculation */ |
297 |
|
goto dumbamb; |
298 |
|
/* check number of bounces */ |
304 |
|
goto dumbamb; |
305 |
|
|
306 |
|
if (ambacc <= FTINY) { /* no ambient storage */ |
307 |
+ |
FVECT uvd[2]; |
308 |
+ |
float dgrad[2], *dgp = NULL; |
309 |
+ |
|
310 |
+ |
if (nrm != r->ron && DOT(nrm,r->ron) < 0.9999) |
311 |
+ |
dgp = dgrad; /* compute rotational grad. */ |
312 |
+ |
copycolor(acol, aval); |
313 |
|
rdepth++; |
314 |
< |
d = doambient(acol, r, r->rweight, NULL, NULL); |
314 |
> |
ok = doambient(acol, r, r->rweight, |
315 |
> |
uvd, NULL, NULL, dgp, NULL); |
316 |
|
rdepth--; |
317 |
< |
if (d <= FTINY) |
317 |
> |
if (!ok) |
318 |
|
goto dumbamb; |
319 |
+ |
if ((ok > 0) & (dgp != NULL)) { /* apply texture */ |
320 |
+ |
FVECT v1; |
321 |
+ |
VCROSS(v1, r->ron, nrm); |
322 |
+ |
d = 1.0; |
323 |
+ |
for (i = 3; i--; ) |
324 |
+ |
d += v1[i] * (dgp[0]*uvd[0][i] + dgp[1]*uvd[1][i]); |
325 |
+ |
if (d >= 0.05) |
326 |
+ |
scalecolor(acol, d); |
327 |
+ |
} |
328 |
+ |
copycolor(aval, acol); |
329 |
+ |
|
330 |
+ |
/* PMAP: add in caustic */ |
331 |
+ |
addcolor(aval, caustic); |
332 |
|
return; |
333 |
|
} |
334 |
< |
/* resort memory? */ |
335 |
< |
sortambvals(0); |
336 |
< |
/* get ambient value */ |
334 |
> |
|
335 |
> |
if (tracktime) /* sort to minimize thrashing */ |
336 |
> |
sortambvals(0); |
337 |
> |
/* interpolate ambient value */ |
338 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
339 |
< |
d = sumambient(acol, r, rdepth, |
339 |
> |
d = sumambient(acol, r, nrm, rdepth, |
340 |
|
&atrunk, thescene.cuorg, thescene.cusize); |
341 |
+ |
|
342 |
|
if (d > FTINY) { |
343 |
< |
scalecolor(acol, 1.0/d); |
343 |
> |
d = 1.0/d; |
344 |
> |
scalecolor(acol, d); |
345 |
> |
multcolor(aval, acol); |
346 |
> |
|
347 |
> |
/* PMAP: add in caustic */ |
348 |
> |
addcolor(aval, caustic); |
349 |
|
return; |
350 |
|
} |
351 |
+ |
|
352 |
|
rdepth++; /* need to cache new value */ |
353 |
< |
d = makeambient(acol, r, rdepth-1); |
353 |
> |
ok = makeambient(acol, r, nrm, rdepth-1); |
354 |
|
rdepth--; |
355 |
< |
if (d > FTINY) |
355 |
> |
|
356 |
> |
if (ok) { |
357 |
> |
multcolor(aval, acol); /* computed new value */ |
358 |
> |
|
359 |
> |
/* PMAP: add in caustic */ |
360 |
> |
addcolor(aval, caustic); |
361 |
|
return; |
362 |
+ |
} |
363 |
+ |
|
364 |
|
dumbamb: /* return global value */ |
365 |
< |
copycolor(acol, ambval); |
366 |
< |
#if AMBVWT |
367 |
< |
if (nambvals == 0) |
365 |
> |
if ((ambvwt <= 0) | (navsum == 0)) { |
366 |
> |
multcolor(aval, ambval); |
367 |
> |
|
368 |
> |
/* PMAP: add in caustic */ |
369 |
> |
addcolor(aval, caustic); |
370 |
|
return; |
371 |
< |
scalecolor(acol, (double)AMBVWT); |
372 |
< |
addcolor(acol, avsum); /* average in computations */ |
373 |
< |
d = 1.0/(AMBVWT+nambvals); |
374 |
< |
scalecolor(acol, d); |
375 |
< |
#endif |
371 |
> |
} |
372 |
> |
|
373 |
> |
l = bright(ambval); /* average in computations */ |
374 |
> |
if (l > FTINY) { |
375 |
> |
d = (log(l)*(double)ambvwt + avsum + logAvgAbsorp*navsum) / |
376 |
> |
(double)(ambvwt + navsum); |
377 |
> |
d = exp(d) / l; |
378 |
> |
scalecolor(aval, d); |
379 |
> |
multcolor(aval, ambval); /* apply color of ambval */ |
380 |
> |
} else { |
381 |
> |
d = exp( avsum/(double)navsum + logAvgAbsorp ); |
382 |
> |
scalecolor(aval, d); /* neutral color */ |
383 |
> |
} |
384 |
|
} |
385 |
|
|
386 |
|
|
387 |
< |
double |
388 |
< |
sumambient(acol, r, al, at, c0, s) /* get interpolated ambient value */ |
389 |
< |
COLOR acol; |
235 |
< |
register RAY *r; |
236 |
< |
int al; |
237 |
< |
AMBTREE *at; |
238 |
< |
FVECT c0; |
239 |
< |
double s; |
387 |
> |
/* Plug a potential leak where ambient cache value is occluded */ |
388 |
> |
static int |
389 |
> |
plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang) |
390 |
|
{ |
391 |
< |
double d, e1, e2, wt, wsum; |
392 |
< |
COLOR ct; |
393 |
< |
FVECT ck0; |
394 |
< |
int i; |
395 |
< |
register int j; |
246 |
< |
register AMBVAL *av; |
391 |
> |
const double cost70sq = 0.1169778; /* cos(70deg)^2 */ |
392 |
> |
RAY rtst; |
393 |
> |
FVECT vdif; |
394 |
> |
double normdot, ndotd, nadotd; |
395 |
> |
double a, b, c, t[2]; |
396 |
|
|
397 |
< |
wsum = 0.0; |
398 |
< |
/* do this node */ |
397 |
> |
ang += 2.*PI*(ang < 0); /* check direction flags */ |
398 |
> |
if ( !(ap->corral>>(int)(ang*(16./PI)) & 1) ) |
399 |
> |
return(0); |
400 |
> |
/* |
401 |
> |
* Generate test ray, targeting 20 degrees above sample point plane |
402 |
> |
* along surface normal from cache position. This should be high |
403 |
> |
* enough to miss local geometry we don't really care about. |
404 |
> |
*/ |
405 |
> |
VSUB(vdif, ap->pos, r->rop); |
406 |
> |
normdot = DOT(anorm, r->ron); |
407 |
> |
ndotd = DOT(vdif, r->ron); |
408 |
> |
nadotd = DOT(vdif, anorm); |
409 |
> |
a = normdot*normdot - cost70sq; |
410 |
> |
b = 2.0*(normdot*ndotd - nadotd*cost70sq); |
411 |
> |
c = ndotd*ndotd - DOT(vdif,vdif)*cost70sq; |
412 |
> |
if (quadratic(t, a, b, c) != 2) |
413 |
> |
return(1); /* should rarely happen */ |
414 |
> |
if (t[1] <= FTINY) |
415 |
> |
return(0); /* should fail behind test */ |
416 |
> |
rayorigin(&rtst, SHADOW, r, NULL); |
417 |
> |
VSUM(rtst.rdir, vdif, anorm, t[1]); /* further dist. > plane */ |
418 |
> |
rtst.rmax = normalize(rtst.rdir); /* short ray test */ |
419 |
> |
while (localhit(&rtst, &thescene)) { /* check for occluder */ |
420 |
> |
OBJREC *m = findmaterial(rtst.ro); |
421 |
> |
if (m != NULL && !istransp(m->otype) && !isBSDFproxy(m) && |
422 |
> |
(rtst.clipset == NULL || |
423 |
> |
!inset(rtst.clipset, rtst.ro->omod))) |
424 |
> |
return(1); /* plug light leak */ |
425 |
> |
VCOPY(rtst.rorg, rtst.rop); /* skip invisible surface */ |
426 |
> |
rtst.rmax -= rtst.rot; |
427 |
> |
rayclear(&rtst); |
428 |
> |
} |
429 |
> |
return(0); /* seems we're OK */ |
430 |
> |
} |
431 |
> |
|
432 |
> |
|
433 |
> |
static double |
434 |
> |
sumambient( /* get interpolated ambient value */ |
435 |
> |
COLOR acol, |
436 |
> |
RAY *r, |
437 |
> |
FVECT rn, |
438 |
> |
int al, |
439 |
> |
AMBTREE *at, |
440 |
> |
FVECT c0, |
441 |
> |
double s |
442 |
> |
) |
443 |
> |
{ /* initial limit is 10 degrees plus ambacc radians */ |
444 |
> |
const double minangle = 10.0 * PI/180.; |
445 |
> |
double maxangle = minangle + ambacc; |
446 |
> |
double wsum = 0.0; |
447 |
> |
FVECT ck0; |
448 |
> |
int i, j; |
449 |
> |
AMBVAL *av; |
450 |
> |
|
451 |
> |
if (at->kid != NULL) { /* sum children first */ |
452 |
> |
s *= 0.5; |
453 |
> |
for (i = 0; i < 8; i++) { |
454 |
> |
for (j = 0; j < 3; j++) { |
455 |
> |
ck0[j] = c0[j]; |
456 |
> |
if (1<<j & i) |
457 |
> |
ck0[j] += s; |
458 |
> |
if (r->rop[j] < ck0[j] - OCTSCALE*s) |
459 |
> |
break; |
460 |
> |
if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s) |
461 |
> |
break; |
462 |
> |
} |
463 |
> |
if (j == 3) |
464 |
> |
wsum += sumambient(acol, r, rn, al, |
465 |
> |
at->kid+i, ck0, s); |
466 |
> |
} |
467 |
> |
/* good enough? */ |
468 |
> |
if (wsum >= 0.05 && s > minarad*10.0) |
469 |
> |
return(wsum); |
470 |
> |
} |
471 |
> |
/* adjust maximum angle */ |
472 |
> |
if (at->alist != NULL && (at->alist->lvl <= al) & (r->rweight < 0.6)) |
473 |
> |
maxangle = (maxangle - PI/2.)*pow(r->rweight,0.13) + PI/2.; |
474 |
> |
/* sum this node */ |
475 |
|
for (av = at->alist; av != NULL; av = av->next) { |
476 |
+ |
double u, v, d, delta_r2, delta_t2; |
477 |
+ |
COLOR ct; |
478 |
+ |
FVECT uvw[3]; |
479 |
+ |
/* record access */ |
480 |
|
if (tracktime) |
481 |
< |
av->latick = ambclock++; |
481 |
> |
av->latick = ambclock; |
482 |
|
/* |
483 |
< |
* Ambient level test. |
483 |
> |
* Ambient level test |
484 |
|
*/ |
485 |
< |
if (av->lvl > al) /* list sorted, so this works */ |
485 |
> |
if (av->lvl > al || /* list sorted, so this works */ |
486 |
> |
(av->lvl == al) & (av->weight < 0.9*r->rweight)) |
487 |
|
break; |
488 |
< |
if (av->weight < r->rweight-FTINY) |
488 |
> |
/* |
489 |
> |
* Direction test using unperturbed normal |
490 |
> |
*/ |
491 |
> |
decodedir(uvw[2], av->ndir); |
492 |
> |
d = DOT(uvw[2], r->ron); |
493 |
> |
if (d <= 0.0) /* >= 90 degrees */ |
494 |
|
continue; |
495 |
+ |
delta_r2 = 2.0 - 2.0*d; /* approx. radians^2 */ |
496 |
+ |
if (delta_r2 >= maxangle*maxangle) |
497 |
+ |
continue; |
498 |
|
/* |
499 |
< |
* Ambient radius test. |
499 |
> |
* Modified ray behind test |
500 |
|
*/ |
501 |
< |
d = av->pos[0] - r->rop[0]; |
502 |
< |
e1 = d * d; |
503 |
< |
d = av->pos[1] - r->rop[1]; |
266 |
< |
e1 += d * d; |
267 |
< |
d = av->pos[2] - r->rop[2]; |
268 |
< |
e1 += d * d; |
269 |
< |
e1 /= av->rad * av->rad; |
270 |
< |
if (e1 > ambacc*ambacc*1.21) |
501 |
> |
VSUB(ck0, r->rop, av->pos); |
502 |
> |
d = DOT(ck0, uvw[2]); |
503 |
> |
if (d < -minarad*ambacc-.001) |
504 |
|
continue; |
505 |
+ |
d /= av->rad[0]; |
506 |
+ |
delta_t2 = d*d; |
507 |
+ |
if (delta_t2 >= ambacc*ambacc) |
508 |
+ |
continue; |
509 |
|
/* |
510 |
< |
* Normal direction test. |
510 |
> |
* Elliptical radii test based on Hessian |
511 |
|
*/ |
512 |
< |
e2 = (1.0 - DOT(av->dir, r->ron)) * r->rweight; |
513 |
< |
if (e2 < 0.0) e2 = 0.0; |
514 |
< |
if (e1 + e2 > ambacc*ambacc*1.21) |
512 |
> |
decodedir(uvw[0], av->udir); |
513 |
> |
VCROSS(uvw[1], uvw[2], uvw[0]); |
514 |
> |
d = (u = DOT(ck0, uvw[0])) / av->rad[0]; |
515 |
> |
delta_t2 += d*d; |
516 |
> |
d = (v = DOT(ck0, uvw[1])) / av->rad[1]; |
517 |
> |
delta_t2 += d*d; |
518 |
> |
if (delta_t2 >= ambacc*ambacc) |
519 |
|
continue; |
520 |
|
/* |
521 |
< |
* Ray behind test. |
521 |
> |
* Test for potential light leak |
522 |
|
*/ |
523 |
< |
d = 0.0; |
283 |
< |
for (j = 0; j < 3; j++) |
284 |
< |
d += (r->rop[j] - av->pos[j]) * |
285 |
< |
(av->dir[j] + r->ron[j]); |
286 |
< |
if (d*0.5 < -minarad*ambacc-.001) |
523 |
> |
if (av->corral && plugaleak(r, av, uvw[2], atan2a(v,u))) |
524 |
|
continue; |
525 |
|
/* |
526 |
< |
* Jittering final test reduces image artifacts. |
526 |
> |
* Extrapolate value and compute final weight (hat function) |
527 |
|
*/ |
528 |
< |
wt = sqrt(e1) + sqrt(e2); |
292 |
< |
if (wt > ambacc*(.9+.2*urand(9015+samplendx))) |
528 |
> |
if (!extambient(ct, av, r->rop, rn, uvw)) |
529 |
|
continue; |
530 |
< |
if (wt <= 1e-3) |
531 |
< |
wt = 1e3; |
532 |
< |
else |
297 |
< |
wt = 1.0 / wt; |
298 |
< |
wsum += wt; |
299 |
< |
extambient(ct, av, r->rop, r->ron); |
300 |
< |
scalecolor(ct, wt); |
530 |
> |
d = tfunc(maxangle, sqrt(delta_r2), 0.0) * |
531 |
> |
tfunc(ambacc, sqrt(delta_t2), 0.0); |
532 |
> |
scalecolor(ct, d); |
533 |
|
addcolor(acol, ct); |
534 |
+ |
wsum += d; |
535 |
|
} |
303 |
– |
if (at->kid == NULL) |
304 |
– |
return(wsum); |
305 |
– |
/* do children */ |
306 |
– |
s *= 0.5; |
307 |
– |
for (i = 0; i < 8; i++) { |
308 |
– |
for (j = 0; j < 3; j++) { |
309 |
– |
ck0[j] = c0[j]; |
310 |
– |
if (1<<j & i) |
311 |
– |
ck0[j] += s; |
312 |
– |
if (r->rop[j] < ck0[j] - OCTSCALE*s) |
313 |
– |
break; |
314 |
– |
if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s) |
315 |
– |
break; |
316 |
– |
} |
317 |
– |
if (j == 3) |
318 |
– |
wsum += sumambient(acol, r, al, at->kid+i, ck0, s); |
319 |
– |
} |
536 |
|
return(wsum); |
537 |
|
} |
538 |
|
|
539 |
|
|
540 |
< |
double |
541 |
< |
makeambient(acol, r, al) /* make a new ambient value */ |
542 |
< |
COLOR acol; |
543 |
< |
register RAY *r; |
544 |
< |
int al; |
540 |
> |
static int |
541 |
> |
makeambient( /* make a new ambient value for storage */ |
542 |
> |
COLOR acol, |
543 |
> |
RAY *r, |
544 |
> |
FVECT rn, |
545 |
> |
int al |
546 |
> |
) |
547 |
|
{ |
548 |
|
AMBVAL amb; |
549 |
< |
FVECT gp, gd; |
550 |
< |
/* compute weight */ |
551 |
< |
amb.weight = pow(AVGREFL, (double)al); |
552 |
< |
if (r->rweight < 0.1*amb.weight) /* heuristic */ |
553 |
< |
amb.weight = r->rweight; |
549 |
> |
FVECT uvw[3]; |
550 |
> |
int i; |
551 |
> |
|
552 |
> |
amb.weight = 1.0; /* compute weight */ |
553 |
> |
for (i = al; i-- > 0; ) |
554 |
> |
amb.weight *= AVGREFL; |
555 |
> |
if (r->rweight < 0.1*amb.weight) /* heuristic override */ |
556 |
> |
amb.weight = 1.25*r->rweight; |
557 |
> |
setcolor(acol, AVGREFL, AVGREFL, AVGREFL); |
558 |
|
/* compute ambient */ |
559 |
< |
amb.rad = doambient(acol, r, amb.weight, gp, gd); |
560 |
< |
if (amb.rad <= FTINY) |
561 |
< |
return(0.0); |
562 |
< |
/* store it */ |
559 |
> |
i = doambient(acol, r, amb.weight, |
560 |
> |
uvw, amb.rad, amb.gpos, amb.gdir, &amb.corral); |
561 |
> |
scalecolor(acol, 1./AVGREFL); /* undo assumed reflectance */ |
562 |
> |
if (i <= 0 || amb.rad[0] <= FTINY) /* no Hessian or zero radius */ |
563 |
> |
return(i); |
564 |
> |
/* store value */ |
565 |
|
VCOPY(amb.pos, r->rop); |
566 |
< |
VCOPY(amb.dir, r->ron); |
566 |
> |
amb.ndir = encodedir(r->ron); |
567 |
> |
amb.udir = encodedir(uvw[0]); |
568 |
|
amb.lvl = al; |
569 |
|
copycolor(amb.val, acol); |
345 |
– |
VCOPY(amb.gpos, gp); |
346 |
– |
VCOPY(amb.gdir, gd); |
570 |
|
/* insert into tree */ |
571 |
|
avsave(&amb); /* and save to file */ |
572 |
< |
return(amb.rad); |
572 |
> |
if (rn != r->ron) { /* texture */ |
573 |
> |
VCOPY(uvw[2], r->ron); |
574 |
> |
extambient(acol, &amb, r->rop, rn, uvw); |
575 |
> |
} |
576 |
> |
return(1); |
577 |
|
} |
578 |
|
|
579 |
|
|
580 |
< |
extambient(cr, ap, pv, nv) /* extrapolate value at pv, nv */ |
581 |
< |
COLOR cr; |
582 |
< |
register AMBVAL *ap; |
583 |
< |
FVECT pv, nv; |
580 |
> |
static int |
581 |
> |
extambient( /* extrapolate value at pv, nv */ |
582 |
> |
COLOR cr, |
583 |
> |
AMBVAL *ap, |
584 |
> |
FVECT pv, |
585 |
> |
FVECT nv, |
586 |
> |
FVECT uvw[3] |
587 |
> |
) |
588 |
|
{ |
589 |
< |
FVECT v1, v2; |
590 |
< |
register int i; |
591 |
< |
double d; |
589 |
> |
const double min_d = 0.05; |
590 |
> |
const double max_d = 20.; |
591 |
> |
static FVECT my_uvw[3]; |
592 |
> |
FVECT v1; |
593 |
> |
int i; |
594 |
> |
double d = 1.0; /* zeroeth order */ |
595 |
|
|
596 |
< |
d = 1.0; /* zeroeth order */ |
597 |
< |
/* gradient due to translation */ |
598 |
< |
for (i = 0; i < 3; i++) |
599 |
< |
d += ap->gpos[i]*(pv[i]-ap->pos[i]); |
600 |
< |
/* gradient due to rotation */ |
367 |
< |
VCOPY(v1, ap->dir); |
368 |
< |
fcross(v2, v1, nv); |
369 |
< |
d += DOT(ap->gdir, v2); |
370 |
< |
if (d <= 0.0) { |
371 |
< |
setcolor(cr, 0.0, 0.0, 0.0); |
372 |
< |
return; |
596 |
> |
if (uvw == NULL) { /* need local coordinates? */ |
597 |
> |
decodedir(my_uvw[2], ap->ndir); |
598 |
> |
decodedir(my_uvw[0], ap->udir); |
599 |
> |
VCROSS(my_uvw[1], my_uvw[2], my_uvw[0]); |
600 |
> |
uvw = my_uvw; |
601 |
|
} |
602 |
+ |
for (i = 3; i--; ) /* gradient due to translation */ |
603 |
+ |
d += (pv[i] - ap->pos[i]) * |
604 |
+ |
(ap->gpos[0]*uvw[0][i] + ap->gpos[1]*uvw[1][i]); |
605 |
+ |
|
606 |
+ |
VCROSS(v1, uvw[2], nv); /* gradient due to rotation */ |
607 |
+ |
for (i = 3; i--; ) |
608 |
+ |
d += v1[i] * (ap->gdir[0]*uvw[0][i] + ap->gdir[1]*uvw[1][i]); |
609 |
+ |
|
610 |
+ |
if (d < min_d) /* clamp min/max scaling */ |
611 |
+ |
d = min_d; |
612 |
+ |
else if (d > max_d) |
613 |
+ |
d = max_d; |
614 |
|
copycolor(cr, ap->val); |
615 |
|
scalecolor(cr, d); |
616 |
+ |
return(d > min_d); |
617 |
|
} |
618 |
|
|
619 |
|
|
620 |
< |
static |
621 |
< |
initambfile(creat) /* initialize ambient file */ |
622 |
< |
int creat; |
620 |
> |
static void |
621 |
> |
avinsert( /* insert ambient value in our tree */ |
622 |
> |
AMBVAL *av |
623 |
> |
) |
624 |
|
{ |
625 |
< |
extern char *progname, *octname, VersionID[]; |
625 |
> |
AMBTREE *at; |
626 |
> |
AMBVAL *ap; |
627 |
> |
AMBVAL avh; |
628 |
> |
FVECT ck0; |
629 |
> |
double s; |
630 |
> |
int branch; |
631 |
> |
int i; |
632 |
|
|
633 |
+ |
if (av->rad[0] <= FTINY) |
634 |
+ |
error(CONSISTENCY, "zero ambient radius in avinsert"); |
635 |
+ |
at = &atrunk; |
636 |
+ |
VCOPY(ck0, thescene.cuorg); |
637 |
+ |
s = thescene.cusize; |
638 |
+ |
while (s*(OCTSCALE/2) > av->rad[1]*ambacc) { |
639 |
+ |
if (at->kid == NULL) |
640 |
+ |
if ((at->kid = newambtree()) == NULL) |
641 |
+ |
error(SYSTEM, "out of memory in avinsert"); |
642 |
+ |
s *= 0.5; |
643 |
+ |
branch = 0; |
644 |
+ |
for (i = 0; i < 3; i++) |
645 |
+ |
if (av->pos[i] > ck0[i] + s) { |
646 |
+ |
ck0[i] += s; |
647 |
+ |
branch |= 1 << i; |
648 |
+ |
} |
649 |
+ |
at = at->kid + branch; |
650 |
+ |
} |
651 |
+ |
avh.next = at->alist; /* order by increasing level */ |
652 |
+ |
for (ap = &avh; ap->next != NULL; ap = ap->next) |
653 |
+ |
if ( ap->next->lvl > av->lvl || |
654 |
+ |
(ap->next->lvl == av->lvl) & |
655 |
+ |
(ap->next->weight <= av->weight) ) |
656 |
+ |
break; |
657 |
+ |
av->next = ap->next; |
658 |
+ |
ap->next = (AMBVAL*)av; |
659 |
+ |
at->alist = avh.next; |
660 |
+ |
} |
661 |
+ |
|
662 |
+ |
|
663 |
+ |
static void |
664 |
+ |
initambfile( /* initialize ambient file */ |
665 |
+ |
int cre8 |
666 |
+ |
) |
667 |
+ |
{ |
668 |
+ |
extern char *progname, *octname; |
669 |
+ |
static char *mybuf = NULL; |
670 |
+ |
|
671 |
|
#ifdef F_SETLKW |
672 |
< |
aflock(creat ? F_WRLCK : F_RDLCK); |
672 |
> |
aflock(cre8 ? F_WRLCK : F_RDLCK); |
673 |
|
#endif |
674 |
< |
#ifdef MSDOS |
675 |
< |
setmode(fileno(ambfp), O_BINARY); |
676 |
< |
#endif |
677 |
< |
setbuf(ambfp, bmalloc(BUFSIZ+8)); |
678 |
< |
if (creat) { /* new file */ |
674 |
> |
SET_FILE_BINARY(ambfp); |
675 |
> |
if (mybuf == NULL) |
676 |
> |
mybuf = (char *)bmalloc(BUFSIZ+8); |
677 |
> |
setbuf(ambfp, mybuf); |
678 |
> |
if (cre8) { /* new file */ |
679 |
|
newheader("RADIANCE", ambfp); |
680 |
< |
fprintf(ambfp, "%s -av %g %g %g -ab %d -aa %g ", |
680 |
> |
fprintf(ambfp, "%s -av %g %g %g -aw %d -ab %d -aa %g ", |
681 |
|
progname, colval(ambval,RED), |
682 |
|
colval(ambval,GRN), colval(ambval,BLU), |
683 |
< |
ambounce, ambacc); |
684 |
< |
fprintf(ambfp, "-ad %d -as %d -ar %d %s\n", |
685 |
< |
ambdiv, ambssamp, ambres, |
686 |
< |
octname==NULL ? "" : octname); |
683 |
> |
ambvwt, ambounce, ambacc); |
684 |
> |
fprintf(ambfp, "-ad %d -as %d -ar %d ", |
685 |
> |
ambdiv, ambssamp, ambres); |
686 |
> |
if (octname != NULL) |
687 |
> |
fputs(octname, ambfp); |
688 |
> |
fputc('\n', ambfp); |
689 |
|
fprintf(ambfp, "SOFTWARE= %s\n", VersionID); |
690 |
+ |
fputnow(ambfp); |
691 |
|
fputformat(AMBFMT, ambfp); |
692 |
< |
putc('\n', ambfp); |
692 |
> |
fputc('\n', ambfp); |
693 |
|
putambmagic(ambfp); |
694 |
|
} else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp)) |
695 |
|
error(USER, "bad ambient file"); |
696 |
|
} |
697 |
|
|
698 |
|
|
699 |
< |
static |
700 |
< |
avsave(av) /* insert and save an ambient value */ |
701 |
< |
AMBVAL *av; |
699 |
> |
static void |
700 |
> |
avsave( /* insert and save an ambient value */ |
701 |
> |
AMBVAL *av |
702 |
> |
) |
703 |
|
{ |
704 |
< |
avinsert(avstore(av)); |
704 |
> |
avstore(av); |
705 |
|
if (ambfp == NULL) |
706 |
|
return; |
707 |
|
if (writambval(av, ambfp) < 0) |
711 |
|
goto writerr; |
712 |
|
return; |
713 |
|
writerr: |
714 |
< |
error(SYSTEM, "error writing ambient file"); |
714 |
> |
error(SYSTEM, "error writing to ambient file"); |
715 |
|
} |
716 |
|
|
717 |
|
|
718 |
|
static AMBVAL * |
719 |
< |
avstore(aval) /* allocate memory and store aval */ |
720 |
< |
register AMBVAL *aval; |
719 |
> |
avstore( /* allocate memory and save aval */ |
720 |
> |
AMBVAL *aval |
721 |
> |
) |
722 |
|
{ |
723 |
< |
register AMBVAL *av; |
723 |
> |
AMBVAL *av; |
724 |
> |
double d; |
725 |
|
|
726 |
|
if ((av = newambval()) == NULL) |
727 |
|
error(SYSTEM, "out of memory in avstore"); |
728 |
< |
copystruct(av, aval); |
728 |
> |
*av = *aval; |
729 |
|
av->latick = ambclock; |
730 |
|
av->next = NULL; |
439 |
– |
addcolor(avsum, av->val); /* add to sum for averaging */ |
731 |
|
nambvals++; |
732 |
+ |
d = bright(av->val); |
733 |
+ |
if (d > FTINY) { /* add to log sum for averaging */ |
734 |
+ |
avsum += log(d); |
735 |
+ |
navsum++; |
736 |
+ |
} |
737 |
+ |
avinsert(av); /* insert in our cache tree */ |
738 |
|
return(av); |
739 |
|
} |
740 |
|
|
744 |
|
static AMBTREE *atfreelist = NULL; /* free ambient tree structures */ |
745 |
|
|
746 |
|
|
747 |
< |
static |
748 |
< |
AMBTREE * |
452 |
< |
newambtree() /* allocate 8 ambient tree structs */ |
747 |
> |
static AMBTREE * |
748 |
> |
newambtree(void) /* allocate 8 ambient tree structs */ |
749 |
|
{ |
750 |
< |
register AMBTREE *atp, *upperlim; |
750 |
> |
AMBTREE *atp, *upperlim; |
751 |
|
|
752 |
|
if (atfreelist == NULL) { /* get more nodes */ |
753 |
< |
atfreelist = (AMBTREE *)bmalloc(ATALLOCSZ*8*sizeof(AMBTREE)); |
753 |
> |
atfreelist = (AMBTREE *)malloc(ATALLOCSZ*8*sizeof(AMBTREE)); |
754 |
|
if (atfreelist == NULL) |
755 |
|
return(NULL); |
756 |
|
/* link new free list */ |
761 |
|
} |
762 |
|
atp = atfreelist; |
763 |
|
atfreelist = atp->kid; |
764 |
< |
bzero((char *)atp, 8*sizeof(AMBTREE)); |
764 |
> |
memset(atp, 0, 8*sizeof(AMBTREE)); |
765 |
|
return(atp); |
766 |
|
} |
767 |
|
|
768 |
|
|
769 |
< |
static |
770 |
< |
freeambtree(atp) /* free 8 ambient tree structs */ |
771 |
< |
AMBTREE *atp; |
769 |
> |
static void |
770 |
> |
freeambtree( /* free 8 ambient tree structs */ |
771 |
> |
AMBTREE *atp |
772 |
> |
) |
773 |
|
{ |
774 |
|
atp->kid = atfreelist; |
775 |
|
atfreelist = atp; |
776 |
|
} |
777 |
|
|
778 |
|
|
779 |
< |
static |
780 |
< |
avinsert(av) /* insert ambient value in our tree */ |
781 |
< |
register AMBVAL *av; |
779 |
> |
static void |
780 |
> |
unloadatree( /* unload an ambient value tree */ |
781 |
> |
AMBTREE *at, |
782 |
> |
unloadtf_t *f |
783 |
> |
) |
784 |
|
{ |
785 |
< |
register AMBTREE *at; |
786 |
< |
register AMBVAL *ap; |
488 |
< |
AMBVAL avh; |
489 |
< |
FVECT ck0; |
490 |
< |
double s; |
491 |
< |
int branch; |
492 |
< |
register int i; |
493 |
< |
|
494 |
< |
if (av->rad <= FTINY) |
495 |
< |
error(CONSISTENCY, "zero ambient radius in avinsert"); |
496 |
< |
at = &atrunk; |
497 |
< |
VCOPY(ck0, thescene.cuorg); |
498 |
< |
s = thescene.cusize; |
499 |
< |
while (s*(OCTSCALE/2) > av->rad*ambacc) { |
500 |
< |
if (at->kid == NULL) |
501 |
< |
if ((at->kid = newambtree()) == NULL) |
502 |
< |
error(SYSTEM, "out of memory in avinsert"); |
503 |
< |
s *= 0.5; |
504 |
< |
branch = 0; |
505 |
< |
for (i = 0; i < 3; i++) |
506 |
< |
if (av->pos[i] > ck0[i] + s) { |
507 |
< |
ck0[i] += s; |
508 |
< |
branch |= 1 << i; |
509 |
< |
} |
510 |
< |
at = at->kid + branch; |
511 |
< |
} |
512 |
< |
avh.next = at->alist; /* order by increasing level */ |
513 |
< |
for (ap = &avh; ap->next != NULL; ap = ap->next) |
514 |
< |
if (ap->next->lvl >= av->lvl) |
515 |
< |
break; |
516 |
< |
av->next = ap->next; |
517 |
< |
ap->next = av; |
518 |
< |
at->alist = avh.next; |
519 |
< |
} |
520 |
< |
|
521 |
< |
|
522 |
< |
static |
523 |
< |
unloadatree(at, f) /* unload an ambient value tree */ |
524 |
< |
register AMBTREE *at; |
525 |
< |
int (*f)(); |
526 |
< |
{ |
527 |
< |
register AMBVAL *av; |
528 |
< |
register int i; |
785 |
> |
AMBVAL *av; |
786 |
> |
int i; |
787 |
|
/* transfer values at this node */ |
788 |
|
for (av = at->alist; av != NULL; av = at->alist) { |
789 |
|
at->alist = av->next; |
790 |
+ |
av->next = NULL; |
791 |
|
(*f)(av); |
792 |
|
} |
793 |
|
if (at->kid == NULL) |
799 |
|
} |
800 |
|
|
801 |
|
|
802 |
< |
static AMBVAL **avlist1, **avlist2; /* ambient value lists for sorting */ |
802 |
> |
static struct avl { |
803 |
> |
AMBVAL *p; |
804 |
> |
unsigned long t; |
805 |
> |
} *avlist1; /* ambient value list with ticks */ |
806 |
> |
static AMBVAL **avlist2; /* memory positions for sorting */ |
807 |
|
static int i_avlist; /* index for lists */ |
808 |
|
|
809 |
+ |
static int alatcmp(const void *av1, const void *av2); |
810 |
|
|
811 |
< |
static |
812 |
< |
av2list(av) |
549 |
< |
AMBVAL *av; |
811 |
> |
static void |
812 |
> |
avfree(AMBVAL *av) |
813 |
|
{ |
814 |
+ |
free(av); |
815 |
+ |
} |
816 |
+ |
|
817 |
+ |
static void |
818 |
+ |
av2list( |
819 |
+ |
AMBVAL *av |
820 |
+ |
) |
821 |
+ |
{ |
822 |
|
#ifdef DEBUG |
823 |
|
if (i_avlist >= nambvals) |
824 |
|
error(CONSISTENCY, "too many ambient values in av2list1"); |
825 |
|
#endif |
826 |
< |
avlist1[i_avlist] = avlist2[i_avlist] = av; |
827 |
< |
i_avlist++; |
826 |
> |
avlist1[i_avlist].p = avlist2[i_avlist] = (AMBVAL*)av; |
827 |
> |
avlist1[i_avlist++].t = av->latick; |
828 |
|
} |
829 |
|
|
830 |
|
|
831 |
|
static int |
832 |
< |
alatcmp(avp1, avp2) /* compare ambient values for MRA */ |
833 |
< |
AMBVAL **avp1, **avp2; |
832 |
> |
alatcmp( /* compare ambient values for MRA */ |
833 |
> |
const void *av1, |
834 |
> |
const void *av2 |
835 |
> |
) |
836 |
|
{ |
837 |
< |
return((**avp2).latick - (**avp1).latick); |
837 |
> |
long lc = ((struct avl *)av2)->t - ((struct avl *)av1)->t; |
838 |
> |
return(lc<0 ? -1 : lc>0 ? 1 : 0); |
839 |
|
} |
840 |
|
|
841 |
|
|
842 |
+ |
/* GW NOTE 2002/10/3: |
843 |
+ |
* I used to compare AMBVAL pointers, but found that this was the |
844 |
+ |
* cause of a serious consistency error with gcc, since the optimizer |
845 |
+ |
* uses some dangerous trick in pointer subtraction that |
846 |
+ |
* assumes pointers differ by exact struct size increments. |
847 |
+ |
*/ |
848 |
|
static int |
849 |
< |
aposcmp(avp1, avp2) /* compare ambient value positions */ |
850 |
< |
AMBVAL **avp1, **avp2; |
849 |
> |
aposcmp( /* compare ambient value positions */ |
850 |
> |
const void *avp1, |
851 |
> |
const void *avp2 |
852 |
> |
) |
853 |
|
{ |
854 |
< |
return(*avp1 - *avp2); |
854 |
> |
long diff = *(char * const *)avp1 - *(char * const *)avp2; |
855 |
> |
if (diff < 0) |
856 |
> |
return(-1); |
857 |
> |
return(diff > 0); |
858 |
|
} |
859 |
|
|
860 |
|
|
576 |
– |
#ifdef DEBUG |
861 |
|
static int |
862 |
< |
avlmemi(avaddr) /* find list position from address */ |
863 |
< |
AMBVAL *avaddr; |
862 |
> |
avlmemi( /* find list position from address */ |
863 |
> |
AMBVAL *avaddr |
864 |
> |
) |
865 |
|
{ |
866 |
< |
register AMBVAL **avlpp; |
866 |
> |
AMBVAL **avlpp; |
867 |
|
|
868 |
< |
avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2, |
868 |
> |
avlpp = (AMBVAL **)bsearch(&avaddr, avlist2, |
869 |
|
nambvals, sizeof(AMBVAL *), aposcmp); |
870 |
|
if (avlpp == NULL) |
871 |
|
error(CONSISTENCY, "address not found in avlmemi"); |
872 |
|
return(avlpp - avlist2); |
873 |
|
} |
589 |
– |
#else |
590 |
– |
#define avlmemi(avaddr) ((AMBVAL **)bsearch((char *)&avaddr,(char *)avlist2, \ |
591 |
– |
nambvals,sizeof(AMBVAL *),aposcmp) - avlist2) |
592 |
– |
#endif |
874 |
|
|
875 |
|
|
876 |
< |
static |
877 |
< |
sortambvals(always) /* resort ambient values */ |
878 |
< |
int always; |
876 |
> |
static void |
877 |
> |
sortambvals( /* resort ambient values */ |
878 |
> |
int always |
879 |
> |
) |
880 |
|
{ |
881 |
|
AMBTREE oldatrunk; |
882 |
|
AMBVAL tav, *tap, *pnext; |
883 |
< |
register int i, j; |
883 |
> |
int i, j; |
884 |
|
/* see if it's time yet */ |
885 |
< |
if (!always && (ambclock < lastsort+sortintvl || |
885 |
> |
if (!always && (ambclock++ < lastsort+sortintvl || |
886 |
|
nambvals < SORT_THRESH)) |
887 |
|
return; |
888 |
|
/* |
901 |
|
* tree will be rebuilt with the new accuracy parameter. |
902 |
|
*/ |
903 |
|
if (tracktime) { /* allocate pointer arrays to sort */ |
622 |
– |
avlist1 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *)); |
904 |
|
avlist2 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *)); |
905 |
< |
} else |
906 |
< |
avlist1 = avlist2 = NULL; |
907 |
< |
if (avlist2 == NULL) { /* no time tracking -- rebuild tree? */ |
908 |
< |
if (avlist1 != NULL) |
909 |
< |
free((char *)avlist1); |
905 |
> |
avlist1 = (struct avl *)malloc(nambvals*sizeof(struct avl)); |
906 |
> |
} else { |
907 |
> |
avlist2 = NULL; |
908 |
> |
avlist1 = NULL; |
909 |
> |
} |
910 |
> |
if (avlist1 == NULL) { /* no time tracking -- rebuild tree? */ |
911 |
> |
if (avlist2 != NULL) |
912 |
> |
free(avlist2); |
913 |
|
if (always) { /* rebuild without sorting */ |
914 |
< |
copystruct(&oldatrunk, &atrunk); |
914 |
> |
oldatrunk = atrunk; |
915 |
|
atrunk.alist = NULL; |
916 |
|
atrunk.kid = NULL; |
917 |
|
unloadatree(&oldatrunk, avinsert); |
936 |
|
if (i_avlist < nambvals) |
937 |
|
error(CONSISTENCY, "missing ambient values in sortambvals"); |
938 |
|
#endif |
939 |
< |
qsort((char *)avlist1, nambvals, sizeof(AMBVAL *), alatcmp); |
940 |
< |
qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp); |
939 |
> |
qsort(avlist1, nambvals, sizeof(struct avl), alatcmp); |
940 |
> |
qsort(avlist2, nambvals, sizeof(AMBVAL *), aposcmp); |
941 |
|
for (i = 0; i < nambvals; i++) { |
942 |
< |
if (avlist1[i] == NULL) |
942 |
> |
if (avlist1[i].p == NULL) |
943 |
|
continue; |
944 |
|
tap = avlist2[i]; |
945 |
< |
copystruct(&tav, tap); |
946 |
< |
for (j = i; (pnext = avlist1[j]) != tap; |
945 |
> |
tav = *tap; |
946 |
> |
for (j = i; (pnext = avlist1[j].p) != tap; |
947 |
|
j = avlmemi(pnext)) { |
948 |
< |
copystruct(avlist2[j], pnext); |
948 |
> |
*(avlist2[j]) = *pnext; |
949 |
|
avinsert(avlist2[j]); |
950 |
< |
avlist1[j] = NULL; |
950 |
> |
avlist1[j].p = NULL; |
951 |
|
} |
952 |
< |
copystruct(avlist2[j], &tav); |
952 |
> |
*(avlist2[j]) = tav; |
953 |
|
avinsert(avlist2[j]); |
954 |
< |
avlist1[j] = NULL; |
954 |
> |
avlist1[j].p = NULL; |
955 |
|
} |
956 |
< |
free((char *)avlist1); |
957 |
< |
free((char *)avlist2); |
956 |
> |
free(avlist1); |
957 |
> |
free(avlist2); |
958 |
|
/* compute new sort interval */ |
959 |
|
sortintvl = ambclock - lastsort; |
960 |
|
if (sortintvl >= MAX_SORT_INTVL/2) |
973 |
|
|
974 |
|
#ifdef F_SETLKW |
975 |
|
|
976 |
< |
static |
977 |
< |
aflock(typ) /* lock/unlock ambient file */ |
978 |
< |
int typ; |
976 |
> |
static void |
977 |
> |
aflock( /* lock/unlock ambient file */ |
978 |
> |
int typ |
979 |
> |
) |
980 |
|
{ |
981 |
|
static struct flock fls; /* static so initialized to zeroes */ |
982 |
|
|
983 |
+ |
if (typ == fls.l_type) /* already called? */ |
984 |
+ |
return; |
985 |
+ |
|
986 |
|
fls.l_type = typ; |
987 |
< |
if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0) |
988 |
< |
error(SYSTEM, "cannot (un)lock ambient file"); |
987 |
> |
do |
988 |
> |
if (fcntl(fileno(ambfp), F_SETLKW, &fls) != -1) |
989 |
> |
return; |
990 |
> |
while (errno == EINTR); |
991 |
> |
|
992 |
> |
error(SYSTEM, "cannot (un)lock ambient file"); |
993 |
|
} |
994 |
|
|
995 |
|
|
996 |
|
int |
997 |
< |
ambsync() /* synchronize ambient file */ |
997 |
> |
ambsync(void) /* synchronize ambient file */ |
998 |
|
{ |
707 |
– |
static FILE *ambinp = NULL; |
708 |
– |
static long lastpos = -1; |
999 |
|
long flen; |
1000 |
|
AMBVAL avs; |
1001 |
< |
register int n; |
1001 |
> |
int n; |
1002 |
|
|
1003 |
< |
if (nunflshed == 0) |
1003 |
> |
if (ambfp == NULL) /* no ambient file? */ |
1004 |
|
return(0); |
1005 |
< |
if (lastpos < 0) /* initializing (locked in initambfile) */ |
1006 |
< |
goto syncend; |
717 |
< |
/* gain exclusive access */ |
718 |
< |
aflock(F_WRLCK); |
1005 |
> |
/* gain appropriate access */ |
1006 |
> |
aflock(nunflshed ? F_WRLCK : F_RDLCK); |
1007 |
|
/* see if file has grown */ |
1008 |
< |
if ((flen = lseek(fileno(ambfp), 0L, 2)) < 0) |
1008 |
> |
if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0) |
1009 |
|
goto seekerr; |
1010 |
< |
if (n = flen - lastpos) { /* file has grown */ |
1011 |
< |
if (ambinp == NULL) { /* use duplicate filedes */ |
1012 |
< |
ambinp = fdopen(dup(fileno(ambfp)), "r"); |
1010 |
> |
if ((n = flen - lastpos) > 0) { /* file has grown */ |
1011 |
> |
if (ambinp == NULL) { /* get new file pointer */ |
1012 |
> |
ambinp = fopen(ambfile, "rb"); |
1013 |
|
if (ambinp == NULL) |
1014 |
< |
error(SYSTEM, "fdopen failed in ambsync"); |
1014 |
> |
error(SYSTEM, "fopen failed in ambsync"); |
1015 |
|
} |
1016 |
< |
if (fseek(ambinp, lastpos, 0) < 0) |
1016 |
> |
if (fseek(ambinp, lastpos, SEEK_SET) < 0) |
1017 |
|
goto seekerr; |
1018 |
|
while (n >= AMBVALSIZ) { /* load contributed values */ |
1019 |
< |
readambval(&avs, ambinp); |
1020 |
< |
avinsert(avstore(&avs)); |
1019 |
> |
if (!readambval(&avs, ambinp)) { |
1020 |
> |
sprintf(errmsg, |
1021 |
> |
"ambient file \"%s\" corrupted near character %ld", |
1022 |
> |
ambfile, flen - n); |
1023 |
> |
error(WARNING, errmsg); |
1024 |
> |
break; |
1025 |
> |
} |
1026 |
> |
avstore(&avs); |
1027 |
|
n -= AMBVALSIZ; |
1028 |
|
} |
1029 |
< |
/*** seek always as safety measure |
1030 |
< |
if (n) ***/ /* alignment */ |
1031 |
< |
if (lseek(fileno(ambfp), flen-n, 0) < 0) |
738 |
< |
goto seekerr; |
1029 |
> |
lastpos = flen - n; /* check alignment */ |
1030 |
> |
if (n && lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0) |
1031 |
> |
goto seekerr; |
1032 |
|
} |
740 |
– |
#ifdef DEBUG |
741 |
– |
if (ambfp->_ptr - ambfp->_base != nunflshed*AMBVALSIZ) { |
742 |
– |
sprintf(errmsg, "ambient file buffer at %d rather than %d", |
743 |
– |
ambfp->_ptr - ambfp->_base, |
744 |
– |
nunflshed*AMBVALSIZ); |
745 |
– |
error(CONSISTENCY, errmsg); |
746 |
– |
} |
747 |
– |
#endif |
748 |
– |
syncend: |
1033 |
|
n = fflush(ambfp); /* calls write() at last */ |
1034 |
< |
if ((lastpos = lseek(fileno(ambfp), 0L, 1)) < 0) |
751 |
< |
goto seekerr; |
1034 |
> |
lastpos += (long)nunflshed*AMBVALSIZ; |
1035 |
|
aflock(F_UNLCK); /* release file */ |
1036 |
|
nunflshed = 0; |
1037 |
|
return(n); |
1038 |
|
seekerr: |
1039 |
|
error(SYSTEM, "seek failed in ambsync"); |
1040 |
+ |
return(EOF); /* pro forma return */ |
1041 |
|
} |
1042 |
|
|
1043 |
< |
#else |
1043 |
> |
#else /* ! F_SETLKW */ |
1044 |
|
|
1045 |
|
int |
1046 |
< |
ambsync() /* flush ambient file */ |
1046 |
> |
ambsync(void) /* flush ambient file */ |
1047 |
|
{ |
1048 |
< |
if (nunflshed == 0) |
1048 |
> |
if (ambfp == NULL) |
1049 |
|
return(0); |
1050 |
|
nunflshed = 0; |
1051 |
|
return(fflush(ambfp)); |
1052 |
|
} |
1053 |
|
|
1054 |
< |
#endif |
1054 |
> |
#endif /* ! F_SETLKW */ |