1 |
– |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* ambient.c - routines dealing with ambient (inter-reflected) component. |
6 |
|
* |
7 |
< |
* The macro AMBFLUSH (if defined) is the number of ambient values |
11 |
< |
* to wait before flushing to the ambient file. |
12 |
< |
* |
13 |
< |
* 5/9/86 |
7 |
> |
* Declarations of external symbols in ambient.h |
8 |
|
*/ |
9 |
|
|
10 |
< |
#include "ray.h" |
10 |
> |
#include "copyright.h" |
11 |
|
|
12 |
< |
#include "octree.h" |
12 |
> |
#include <string.h> |
13 |
|
|
14 |
+ |
#include "platform.h" |
15 |
+ |
#include "ray.h" |
16 |
|
#include "otypes.h" |
17 |
< |
|
17 |
> |
#include "resolu.h" |
18 |
|
#include "ambient.h" |
23 |
– |
|
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 |
|
|
25 |
< |
typedef struct ambtree { |
29 |
< |
AMBVAL *alist; /* ambient value list */ |
30 |
< |
struct ambtree *kid; /* 8 child nodes */ |
31 |
< |
} AMBTREE; /* ambient octree */ |
25 |
> |
extern char *shm_boundary; /* memory sharing boundary */ |
26 |
|
|
27 |
< |
extern CUBE thescene; /* contains space boundaries */ |
27 |
> |
#ifndef MAXASET |
28 |
> |
#define MAXASET 2047 /* maximum number of elements in ambient set */ |
29 |
> |
#endif |
30 |
> |
OBJECT ambset[MAXASET+1]={0}; /* ambient include/exclude set */ |
31 |
|
|
32 |
< |
#define MAXASET 511 /* maximum number of elements in ambient set */ |
33 |
< |
OBJECT ambset[MAXASET+1]={0}; /* ambient include/exclude set */ |
32 |
> |
double maxarad; /* maximum ambient radius */ |
33 |
> |
double minarad; /* minimum ambient radius */ |
34 |
|
|
35 |
< |
double maxarad; /* maximum ambient radius */ |
39 |
< |
double minarad; /* minimum ambient radius */ |
35 |
> |
static AMBTREE atrunk; /* our ambient trunk node */ |
36 |
|
|
41 |
– |
static AMBTREE atrunk; /* our ambient trunk node */ |
42 |
– |
|
37 |
|
static FILE *ambfp = NULL; /* ambient file pointer */ |
38 |
+ |
static int nunflshed = 0; /* number of unflushed ambient values */ |
39 |
|
|
40 |
< |
#define newambval() (AMBVAL *)bmalloc(sizeof(AMBVAL)) |
40 |
> |
#ifndef SORT_THRESH |
41 |
> |
#ifdef SMLMEM |
42 |
> |
#define SORT_THRESH ((3L<<20)/sizeof(AMBVAL)) |
43 |
> |
#else |
44 |
> |
#define SORT_THRESH ((9L<<20)/sizeof(AMBVAL)) |
45 |
> |
#endif |
46 |
> |
#endif |
47 |
> |
#ifndef SORT_INTVL |
48 |
> |
#define SORT_INTVL (SORT_THRESH<<1) |
49 |
> |
#endif |
50 |
> |
#ifndef MAX_SORT_INTVL |
51 |
> |
#define MAX_SORT_INTVL (SORT_INTVL<<6) |
52 |
> |
#endif |
53 |
|
|
54 |
< |
#define newambtree() (AMBTREE *)calloc(8, sizeof(AMBTREE)) |
54 |
> |
static double avsum = 0.; /* computed ambient value sum (log) */ |
55 |
> |
static unsigned int navsum = 0; /* number of values in avsum */ |
56 |
> |
static unsigned int nambvals = 0; /* total number of indirect values */ |
57 |
> |
static unsigned int nambshare = 0; /* number of values from file */ |
58 |
> |
static unsigned long ambclock = 0; /* ambient access clock */ |
59 |
> |
static unsigned long lastsort = 0; /* time of last value sort */ |
60 |
> |
static long sortintvl = SORT_INTVL; /* time until next sort */ |
61 |
> |
static FILE *ambinp = NULL; /* auxiliary file for input */ |
62 |
> |
static long lastpos = -1; /* last flush position */ |
63 |
|
|
64 |
+ |
#define MAXACLOCK (1L<<30) /* clock turnover value */ |
65 |
+ |
/* |
66 |
+ |
* Track access times unless we are sharing ambient values |
67 |
+ |
* through memory on a multiprocessor, when we want to avoid |
68 |
+ |
* claiming our own memory (copy on write). Go ahead anyway |
69 |
+ |
* if more than two thirds of our values are unshared. |
70 |
+ |
* Compile with -Dtracktime=0 to turn this code off. |
71 |
+ |
*/ |
72 |
+ |
#ifndef tracktime |
73 |
+ |
#define tracktime (shm_boundary == NULL || nambvals > 3*nambshare) |
74 |
+ |
#endif |
75 |
|
|
76 |
< |
setambient(afile) /* initialize calculation */ |
77 |
< |
char *afile; |
76 |
> |
#define AMBFLUSH (BUFSIZ/AMBVALSIZ) |
77 |
> |
|
78 |
> |
#define newambval() (AMBVAL *)malloc(sizeof(AMBVAL)) |
79 |
> |
#define freeav(av) free((void *)av); |
80 |
> |
|
81 |
> |
static void initambfile(int creat); |
82 |
> |
static void avsave(AMBVAL *av); |
83 |
> |
static AMBVAL *avstore(AMBVAL *aval); |
84 |
> |
static AMBTREE *newambtree(void); |
85 |
> |
static void freeambtree(AMBTREE *atp); |
86 |
> |
|
87 |
> |
typedef void unloadtf_t(void *); |
88 |
> |
static unloadtf_t avinsert; |
89 |
> |
static unloadtf_t av2list; |
90 |
> |
static void unloadatree(AMBTREE *at, unloadtf_t *f); |
91 |
> |
|
92 |
> |
static int aposcmp(const void *avp1, const void *avp2); |
93 |
> |
static int avlmemi(AMBVAL *avaddr); |
94 |
> |
static void sortambvals(int always); |
95 |
> |
|
96 |
> |
#ifdef F_SETLKW |
97 |
> |
static void aflock(int typ); |
98 |
> |
#endif |
99 |
> |
|
100 |
> |
|
101 |
> |
extern void |
102 |
> |
setambres( /* set ambient resolution */ |
103 |
> |
int ar |
104 |
> |
) |
105 |
|
{ |
106 |
< |
long ftell(); |
107 |
< |
AMBVAL amb; |
106 |
> |
ambres = ar < 0 ? 0 : ar; /* may be done already */ |
107 |
> |
/* set min & max radii */ |
108 |
> |
if (ar <= 0) { |
109 |
> |
minarad = 0; |
110 |
> |
maxarad = thescene.cusize / 2.0; |
111 |
> |
} else { |
112 |
> |
minarad = thescene.cusize / ar; |
113 |
> |
maxarad = 64 * minarad; /* heuristic */ |
114 |
> |
if (maxarad > thescene.cusize / 2.0) |
115 |
> |
maxarad = thescene.cusize / 2.0; |
116 |
> |
} |
117 |
> |
if (minarad <= FTINY) |
118 |
> |
minarad = 10*FTINY; |
119 |
> |
if (maxarad <= minarad) |
120 |
> |
maxarad = 64 * minarad; |
121 |
> |
} |
122 |
|
|
56 |
– |
maxarad = thescene.cusize / 2.0; /* maximum radius */ |
57 |
– |
/* minimum radius */ |
58 |
– |
minarad = ambres > 0 ? thescene.cusize/ambres : 0.0; |
123 |
|
|
124 |
< |
/* open ambient file */ |
125 |
< |
if (afile != NULL) |
126 |
< |
if ((ambfp = fopen(afile, "r+")) != NULL) { |
127 |
< |
while (fread((char *)&amb,sizeof(AMBVAL),1,ambfp) == 1) |
128 |
< |
avinsert(&amb, &atrunk, thescene.cuorg, |
129 |
< |
thescene.cusize); |
130 |
< |
/* align */ |
131 |
< |
fseek(ambfp, -(ftell(ambfp)%sizeof(AMBVAL)), 1); |
132 |
< |
} else if ((ambfp = fopen(afile, "w")) == NULL) { |
133 |
< |
sprintf(errmsg, "cannot open ambient file \"%s\"", |
134 |
< |
afile); |
135 |
< |
error(SYSTEM, errmsg); |
124 |
> |
extern void |
125 |
> |
setambacc( /* set ambient accuracy */ |
126 |
> |
double newa |
127 |
> |
) |
128 |
> |
{ |
129 |
> |
double ambdiff; |
130 |
> |
|
131 |
> |
if (newa < 0.0) |
132 |
> |
newa = 0.0; |
133 |
> |
ambdiff = fabs(newa - ambacc); |
134 |
> |
if (ambdiff >= .01 && (ambacc = newa) > FTINY && nambvals > 0) |
135 |
> |
sortambvals(1); /* rebuild tree */ |
136 |
> |
} |
137 |
> |
|
138 |
> |
|
139 |
> |
extern void |
140 |
> |
setambient(void) /* initialize calculation */ |
141 |
> |
{ |
142 |
> |
int readonly = 0; |
143 |
> |
long pos, flen; |
144 |
> |
AMBVAL amb; |
145 |
> |
/* make sure we're fresh */ |
146 |
> |
ambdone(); |
147 |
> |
/* init ambient limits */ |
148 |
> |
setambres(ambres); |
149 |
> |
setambacc(ambacc); |
150 |
> |
if (ambfile == NULL || !ambfile[0]) |
151 |
> |
return; |
152 |
> |
if (ambacc <= FTINY) { |
153 |
> |
sprintf(errmsg, "zero ambient accuracy so \"%s\" not opened", |
154 |
> |
ambfile); |
155 |
> |
error(WARNING, errmsg); |
156 |
> |
return; |
157 |
> |
} |
158 |
> |
/* open ambient file */ |
159 |
> |
if ((ambfp = fopen(ambfile, "r+")) == NULL) |
160 |
> |
readonly = (ambfp = fopen(ambfile, "r")) != NULL; |
161 |
> |
if (ambfp != NULL) { |
162 |
> |
initambfile(0); /* file exists */ |
163 |
> |
pos = ftell(ambfp); |
164 |
> |
while (readambval(&amb, ambfp)) |
165 |
> |
avinsert(avstore(&amb)); |
166 |
> |
nambshare = nambvals; /* share loaded values */ |
167 |
> |
if (readonly) { |
168 |
> |
sprintf(errmsg, |
169 |
> |
"loaded %u values from read-only ambient file", |
170 |
> |
nambvals); |
171 |
> |
error(WARNING, errmsg); |
172 |
> |
fclose(ambfp); /* close file so no writes */ |
173 |
> |
ambfp = NULL; |
174 |
> |
return; /* avoid ambsync() */ |
175 |
|
} |
176 |
+ |
/* align file pointer */ |
177 |
+ |
pos += (long)nambvals*AMBVALSIZ; |
178 |
+ |
flen = lseek(fileno(ambfp), (off_t)0, SEEK_END); |
179 |
+ |
if (flen != pos) { |
180 |
+ |
sprintf(errmsg, |
181 |
+ |
"ignoring last %ld values in ambient file (corrupted)", |
182 |
+ |
(flen - pos)/AMBVALSIZ); |
183 |
+ |
error(WARNING, errmsg); |
184 |
+ |
fseek(ambfp, pos, 0); |
185 |
+ |
#ifndef _WIN32 /* XXX we need a replacement for that one */ |
186 |
+ |
ftruncate(fileno(ambfp), (off_t)pos); |
187 |
+ |
#endif |
188 |
+ |
} |
189 |
+ |
} else if ((ambfp = fopen(ambfile, "w+")) != NULL) { |
190 |
+ |
initambfile(1); /* else create new file */ |
191 |
+ |
} else { |
192 |
+ |
sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile); |
193 |
+ |
error(SYSTEM, errmsg); |
194 |
+ |
} |
195 |
+ |
nunflshed++; /* lie */ |
196 |
+ |
ambsync(); |
197 |
|
} |
198 |
|
|
199 |
|
|
200 |
< |
ambnotify(obj) /* record new modifier */ |
201 |
< |
OBJECT obj; |
200 |
> |
extern void |
201 |
> |
ambdone(void) /* close ambient file and free memory */ |
202 |
|
{ |
203 |
+ |
if (ambfp != NULL) { /* close ambient file */ |
204 |
+ |
ambsync(); |
205 |
+ |
fclose(ambfp); |
206 |
+ |
ambfp = NULL; |
207 |
+ |
if (ambinp != NULL) { |
208 |
+ |
fclose(ambinp); |
209 |
+ |
ambinp = NULL; |
210 |
+ |
} |
211 |
+ |
lastpos = -1; |
212 |
+ |
} |
213 |
+ |
/* free ambient tree */ |
214 |
+ |
unloadatree(&atrunk, free); |
215 |
+ |
/* reset state variables */ |
216 |
+ |
avsum = 0.; |
217 |
+ |
navsum = 0; |
218 |
+ |
nambvals = 0; |
219 |
+ |
nambshare = 0; |
220 |
+ |
ambclock = 0; |
221 |
+ |
lastsort = 0; |
222 |
+ |
sortintvl = SORT_INTVL; |
223 |
+ |
} |
224 |
+ |
|
225 |
+ |
|
226 |
+ |
extern void |
227 |
+ |
ambnotify( /* record new modifier */ |
228 |
+ |
OBJECT obj |
229 |
+ |
) |
230 |
+ |
{ |
231 |
|
static int hitlimit = 0; |
232 |
< |
register OBJREC *o = objptr(obj); |
232 |
> |
register OBJREC *o; |
233 |
|
register char **amblp; |
234 |
|
|
235 |
+ |
if (obj == OVOID) { /* starting over */ |
236 |
+ |
ambset[0] = 0; |
237 |
+ |
hitlimit = 0; |
238 |
+ |
return; |
239 |
+ |
} |
240 |
+ |
o = objptr(obj); |
241 |
|
if (hitlimit || !ismodifier(o->otype)) |
242 |
|
return; |
243 |
|
for (amblp = amblist; *amblp != NULL; amblp++) |
253 |
|
} |
254 |
|
|
255 |
|
|
256 |
< |
ambient(acol, r) /* compute ambient component for ray */ |
257 |
< |
COLOR acol; |
258 |
< |
register RAY *r; |
256 |
> |
extern void |
257 |
> |
multambient( /* compute ambient component & multiply by coef. */ |
258 |
> |
COLOR aval, |
259 |
> |
register RAY *r, |
260 |
> |
FVECT nrm |
261 |
> |
) |
262 |
|
{ |
263 |
|
static int rdepth = 0; /* ambient recursion */ |
264 |
< |
double d; |
264 |
> |
COLOR acol; |
265 |
> |
double d, l; |
266 |
|
|
267 |
|
if (ambdiv <= 0) /* no ambient calculation */ |
268 |
|
goto dumbamb; |
276 |
|
|
277 |
|
if (ambacc <= FTINY) { /* no ambient storage */ |
278 |
|
rdepth++; |
279 |
< |
d = doambient(acol, r, r->rweight, NULL, NULL); |
279 |
> |
d = doambient(acol, r, aval, intens(aval)*r->rweight, |
280 |
> |
NULL, NULL); |
281 |
|
rdepth--; |
282 |
< |
if (d == 0.0) |
282 |
> |
if (d <= FTINY) |
283 |
|
goto dumbamb; |
284 |
+ |
multcolor(aval, acol); |
285 |
|
return; |
286 |
|
} |
287 |
+ |
|
288 |
+ |
if (tracktime) /* sort to minimize thrashing */ |
289 |
+ |
sortambvals(0); |
290 |
|
/* get ambient value */ |
291 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
292 |
< |
d = sumambient(acol, r, rdepth, |
292 |
> |
d = sumambient(acol, r, nrm, rdepth, |
293 |
|
&atrunk, thescene.cuorg, thescene.cusize); |
294 |
< |
if (d > FTINY) |
294 |
> |
if (d > FTINY) { |
295 |
|
scalecolor(acol, 1.0/d); |
296 |
< |
else { |
297 |
< |
d = makeambient(acol, r, rdepth++); |
131 |
< |
rdepth--; |
296 |
> |
multcolor(aval, acol); |
297 |
> |
return; |
298 |
|
} |
299 |
< |
if (d > FTINY) |
299 |
> |
rdepth++; /* need to cache new value */ |
300 |
> |
d = makeambient(acol, r, aval, nrm, rdepth-1); |
301 |
> |
rdepth--; |
302 |
> |
if (d > FTINY) { |
303 |
> |
multcolor(aval, acol); /* got new value */ |
304 |
|
return; |
305 |
+ |
} |
306 |
|
dumbamb: /* return global value */ |
307 |
< |
copycolor(acol, ambval); |
307 |
> |
if ((ambvwt <= 0) | (navsum == 0)) { |
308 |
> |
multcolor(aval, ambval); |
309 |
> |
return; |
310 |
> |
} |
311 |
> |
l = bright(ambval); /* average in computations */ |
312 |
> |
if (l > FTINY) { |
313 |
> |
d = (log(l)*(double)ambvwt + avsum) / |
314 |
> |
(double)(ambvwt + navsum); |
315 |
> |
d = exp(d) / l; |
316 |
> |
scalecolor(aval, d); |
317 |
> |
multcolor(aval, ambval); /* apply color of ambval */ |
318 |
> |
} else { |
319 |
> |
d = exp( avsum / (double)navsum ); |
320 |
> |
scalecolor(aval, d); /* neutral color */ |
321 |
> |
} |
322 |
|
} |
323 |
|
|
324 |
|
|
325 |
< |
double |
326 |
< |
sumambient(acol, r, al, at, c0, s) /* get interpolated ambient value */ |
327 |
< |
COLOR acol; |
328 |
< |
register RAY *r; |
329 |
< |
int al; |
330 |
< |
AMBTREE *at; |
331 |
< |
FVECT c0; |
332 |
< |
double s; |
325 |
> |
extern double |
326 |
> |
sumambient( /* get interpolated ambient value */ |
327 |
> |
COLOR acol, |
328 |
> |
register RAY *r, |
329 |
> |
FVECT rn, |
330 |
> |
int al, |
331 |
> |
AMBTREE *at, |
332 |
> |
FVECT c0, |
333 |
> |
double s |
334 |
> |
) |
335 |
|
{ |
336 |
< |
extern double sqrt(); |
150 |
< |
double d, e1, e2, wt, wsum; |
336 |
> |
double d, e1, e2, wt, wsum; |
337 |
|
COLOR ct; |
338 |
|
FVECT ck0; |
339 |
|
int i; |
340 |
|
register int j; |
341 |
< |
register AMBVAL *av; |
342 |
< |
/* do this node */ |
341 |
> |
register AMBVAL *av; |
342 |
> |
|
343 |
|
wsum = 0.0; |
344 |
+ |
/* do this node */ |
345 |
|
for (av = at->alist; av != NULL; av = av->next) { |
346 |
+ |
double rn_dot = -2.0; |
347 |
+ |
if (tracktime) |
348 |
+ |
av->latick = ambclock; |
349 |
|
/* |
350 |
|
* Ambient level test. |
351 |
|
*/ |
352 |
< |
if (av->lvl > al || av->weight < r->rweight-FTINY) |
352 |
> |
if (av->lvl > al) /* list sorted, so this works */ |
353 |
> |
break; |
354 |
> |
if (av->weight < r->rweight-FTINY) |
355 |
|
continue; |
356 |
|
/* |
357 |
|
* Ambient radius test. |
358 |
|
*/ |
359 |
< |
e1 = 0.0; |
360 |
< |
for (j = 0; j < 3; j++) { |
361 |
< |
d = av->pos[j] - r->rop[j]; |
362 |
< |
e1 += d * d; |
363 |
< |
} |
359 |
> |
d = av->pos[0] - r->rop[0]; |
360 |
> |
e1 = d * d; |
361 |
> |
d = av->pos[1] - r->rop[1]; |
362 |
> |
e1 += d * d; |
363 |
> |
d = av->pos[2] - r->rop[2]; |
364 |
> |
e1 += d * d; |
365 |
|
e1 /= av->rad * av->rad; |
366 |
|
if (e1 > ambacc*ambacc*1.21) |
367 |
|
continue; |
368 |
|
/* |
369 |
< |
* Normal direction test. |
369 |
> |
* Direction test using closest normal. |
370 |
|
*/ |
371 |
< |
e2 = (1.0 - DOT(av->dir, r->ron)) * r->rweight; |
371 |
> |
d = DOT(av->dir, r->ron); |
372 |
> |
if (rn != r->ron) { |
373 |
> |
rn_dot = DOT(av->dir, rn); |
374 |
> |
if (rn_dot > 1.0-FTINY) |
375 |
> |
rn_dot = 1.0-FTINY; |
376 |
> |
if (rn_dot >= d-FTINY) { |
377 |
> |
d = rn_dot; |
378 |
> |
rn_dot = -2.0; |
379 |
> |
} |
380 |
> |
} |
381 |
> |
e2 = (1.0 - d) * r->rweight; |
382 |
|
if (e2 < 0.0) e2 = 0.0; |
383 |
|
if (e1 + e2 > ambacc*ambacc*1.21) |
384 |
|
continue; |
394 |
|
/* |
395 |
|
* Jittering final test reduces image artifacts. |
396 |
|
*/ |
397 |
< |
wt = sqrt(e1) + sqrt(e2); |
398 |
< |
wt *= .9 + .2*frandom(); |
399 |
< |
if (wt > ambacc) |
397 |
> |
e1 = sqrt(e1); |
398 |
> |
e2 = sqrt(e2); |
399 |
> |
wt = e1 + e2; |
400 |
> |
if (wt > ambacc*(.9+.2*urand(9015+samplendx))) |
401 |
|
continue; |
402 |
+ |
/* |
403 |
+ |
* Recompute directional error using perturbed normal |
404 |
+ |
*/ |
405 |
+ |
if (rn_dot > 0.0) { |
406 |
+ |
e2 = sqrt((1.0 - rn_dot)*r->rweight); |
407 |
+ |
wt = e1 + e2; |
408 |
+ |
} |
409 |
|
if (wt <= 1e-3) |
410 |
|
wt = 1e3; |
411 |
|
else |
412 |
|
wt = 1.0 / wt; |
413 |
|
wsum += wt; |
414 |
< |
extambient(ct, av, r->rop, r->ron); |
414 |
> |
extambient(ct, av, r->rop, rn); |
415 |
|
scalecolor(ct, wt); |
416 |
|
addcolor(acol, ct); |
417 |
|
} |
430 |
|
break; |
431 |
|
} |
432 |
|
if (j == 3) |
433 |
< |
wsum += sumambient(acol, r, al, at->kid+i, ck0, s); |
433 |
> |
wsum += sumambient(acol, r, rn, al, at->kid+i, ck0, s); |
434 |
|
} |
435 |
|
return(wsum); |
436 |
|
} |
437 |
|
|
438 |
|
|
439 |
< |
double |
440 |
< |
makeambient(acol, r, al) /* make a new ambient value */ |
441 |
< |
COLOR acol; |
442 |
< |
register RAY *r; |
443 |
< |
int al; |
439 |
> |
extern double |
440 |
> |
makeambient( /* make a new ambient value */ |
441 |
> |
COLOR acol, |
442 |
> |
RAY *r, |
443 |
> |
COLOR ac, |
444 |
> |
FVECT rn, |
445 |
> |
int al |
446 |
> |
) |
447 |
|
{ |
448 |
< |
AMBVAL amb; |
448 |
> |
AMBVAL amb; |
449 |
> |
double coef; |
450 |
|
FVECT gp, gd; |
451 |
|
/* compute weight */ |
452 |
|
amb.weight = pow(AVGREFL, (double)al); |
453 |
< |
if (r->rweight < 0.2*amb.weight) /* heuristic */ |
454 |
< |
amb.weight = r->rweight; |
453 |
> |
coef = intens(ac)*r->rweight; |
454 |
> |
if (coef < 0.1*amb.weight) /* heuristic */ |
455 |
> |
amb.weight = coef; |
456 |
|
/* compute ambient */ |
457 |
< |
amb.rad = doambient(acol, r, amb.weight, gp, gd); |
458 |
< |
if (amb.rad == 0.0) |
457 |
> |
amb.rad = doambient(acol, r, ac, amb.weight, gp, gd); |
458 |
> |
if (amb.rad <= FTINY) |
459 |
|
return(0.0); |
460 |
|
/* store it */ |
461 |
|
VCOPY(amb.pos, r->rop); |
465 |
|
VCOPY(amb.gpos, gp); |
466 |
|
VCOPY(amb.gdir, gd); |
467 |
|
/* insert into tree */ |
468 |
< |
avinsert(&amb, &atrunk, thescene.cuorg, thescene.cusize); |
469 |
< |
avsave(&amb); /* write to file */ |
468 |
> |
avsave(&amb); /* and save to file */ |
469 |
> |
if (rn != r->ron) |
470 |
> |
extambient(acol, &amb, r->rop, rn); /* texture */ |
471 |
|
return(amb.rad); |
472 |
|
} |
473 |
|
|
474 |
|
|
475 |
< |
extambient(cr, ap, pv, nv) /* extrapolate value at pv, nv */ |
476 |
< |
COLOR cr; |
477 |
< |
register AMBVAL *ap; |
478 |
< |
FVECT pv, nv; |
475 |
> |
extern void |
476 |
> |
extambient( /* extrapolate value at pv, nv */ |
477 |
> |
COLOR cr, |
478 |
> |
register AMBVAL *ap, |
479 |
> |
FVECT pv, |
480 |
> |
FVECT nv |
481 |
> |
) |
482 |
|
{ |
483 |
< |
FVECT v1, v2; |
483 |
> |
FVECT v1; |
484 |
|
register int i; |
485 |
< |
double d; |
485 |
> |
double d; |
486 |
|
|
487 |
|
d = 1.0; /* zeroeth order */ |
488 |
|
/* gradient due to translation */ |
489 |
|
for (i = 0; i < 3; i++) |
490 |
|
d += ap->gpos[i]*(pv[i]-ap->pos[i]); |
491 |
|
/* gradient due to rotation */ |
492 |
< |
VCOPY(v1, ap->dir); |
493 |
< |
fcross(v2, v1, nv); |
274 |
< |
d += DOT(ap->gdir, v2); |
492 |
> |
VCROSS(v1, ap->dir, nv); |
493 |
> |
d += DOT(ap->gdir, v1); |
494 |
|
if (d <= 0.0) { |
495 |
|
setcolor(cr, 0.0, 0.0, 0.0); |
496 |
|
return; |
500 |
|
} |
501 |
|
|
502 |
|
|
503 |
< |
static |
504 |
< |
avsave(av) /* save an ambient value */ |
505 |
< |
AMBVAL *av; |
503 |
> |
static void |
504 |
> |
initambfile( /* initialize ambient file */ |
505 |
> |
int creat |
506 |
> |
) |
507 |
|
{ |
508 |
< |
#ifdef AMBFLUSH |
509 |
< |
static int nunflshed = 0; |
508 |
> |
extern char *progname, *octname; |
509 |
> |
static char *mybuf = NULL; |
510 |
> |
|
511 |
> |
#ifdef F_SETLKW |
512 |
> |
aflock(creat ? F_WRLCK : F_RDLCK); |
513 |
|
#endif |
514 |
+ |
SET_FILE_BINARY(ambfp); |
515 |
+ |
if (mybuf == NULL) |
516 |
+ |
mybuf = (char *)bmalloc(BUFSIZ+8); |
517 |
+ |
setbuf(ambfp, mybuf); |
518 |
+ |
if (creat) { /* new file */ |
519 |
+ |
newheader("RADIANCE", ambfp); |
520 |
+ |
fprintf(ambfp, "%s -av %g %g %g -aw %d -ab %d -aa %g ", |
521 |
+ |
progname, colval(ambval,RED), |
522 |
+ |
colval(ambval,GRN), colval(ambval,BLU), |
523 |
+ |
ambvwt, ambounce, ambacc); |
524 |
+ |
fprintf(ambfp, "-ad %d -as %d -ar %d ", |
525 |
+ |
ambdiv, ambssamp, ambres); |
526 |
+ |
if (octname != NULL) |
527 |
+ |
printargs(1, &octname, ambfp); |
528 |
+ |
else |
529 |
+ |
fputc('\n', ambfp); |
530 |
+ |
fprintf(ambfp, "SOFTWARE= %s\n", VersionID); |
531 |
+ |
fputnow(ambfp); |
532 |
+ |
fputformat(AMBFMT, ambfp); |
533 |
+ |
putc('\n', ambfp); |
534 |
+ |
putambmagic(ambfp); |
535 |
+ |
} else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp)) |
536 |
+ |
error(USER, "bad ambient file"); |
537 |
+ |
} |
538 |
+ |
|
539 |
+ |
|
540 |
+ |
static void |
541 |
+ |
avsave( /* insert and save an ambient value */ |
542 |
+ |
AMBVAL *av |
543 |
+ |
) |
544 |
+ |
{ |
545 |
+ |
avinsert(avstore(av)); |
546 |
|
if (ambfp == NULL) |
547 |
|
return; |
548 |
< |
if (fwrite((char *)av, sizeof(AMBVAL), 1, ambfp) != 1) |
548 |
> |
if (writambval(av, ambfp) < 0) |
549 |
|
goto writerr; |
550 |
< |
#ifdef AMBFLUSH |
551 |
< |
if (++nunflshed >= AMBFLUSH) { |
297 |
< |
if (fflush(ambfp) == EOF) |
550 |
> |
if (++nunflshed >= AMBFLUSH) |
551 |
> |
if (ambsync() == EOF) |
552 |
|
goto writerr; |
299 |
– |
nunflshed = 0; |
300 |
– |
} |
301 |
– |
#endif |
553 |
|
return; |
554 |
|
writerr: |
555 |
< |
error(SYSTEM, "error writing ambient file"); |
555 |
> |
error(SYSTEM, "error writing to ambient file"); |
556 |
|
} |
557 |
|
|
558 |
|
|
559 |
< |
static |
560 |
< |
avinsert(aval, at, c0, s) /* insert ambient value in a tree */ |
561 |
< |
AMBVAL *aval; |
562 |
< |
register AMBTREE *at; |
312 |
< |
FVECT c0; |
313 |
< |
double s; |
559 |
> |
static AMBVAL * |
560 |
> |
avstore( /* allocate memory and store aval */ |
561 |
> |
register AMBVAL *aval |
562 |
> |
) |
563 |
|
{ |
564 |
+ |
register AMBVAL *av; |
565 |
+ |
double d; |
566 |
+ |
|
567 |
+ |
if ((av = newambval()) == NULL) |
568 |
+ |
error(SYSTEM, "out of memory in avstore"); |
569 |
+ |
*av = *aval; |
570 |
+ |
av->latick = ambclock; |
571 |
+ |
av->next = NULL; |
572 |
+ |
nambvals++; |
573 |
+ |
d = bright(av->val); |
574 |
+ |
if (d > FTINY) { /* add to log sum for averaging */ |
575 |
+ |
avsum += log(d); |
576 |
+ |
navsum++; |
577 |
+ |
} |
578 |
+ |
return(av); |
579 |
+ |
} |
580 |
+ |
|
581 |
+ |
|
582 |
+ |
#define ATALLOCSZ 512 /* #/8 trees to allocate at once */ |
583 |
+ |
|
584 |
+ |
static AMBTREE *atfreelist = NULL; /* free ambient tree structures */ |
585 |
+ |
|
586 |
+ |
|
587 |
+ |
static AMBTREE * |
588 |
+ |
newambtree(void) /* allocate 8 ambient tree structs */ |
589 |
+ |
{ |
590 |
+ |
register AMBTREE *atp, *upperlim; |
591 |
+ |
|
592 |
+ |
if (atfreelist == NULL) { /* get more nodes */ |
593 |
+ |
atfreelist = (AMBTREE *)malloc(ATALLOCSZ*8*sizeof(AMBTREE)); |
594 |
+ |
if (atfreelist == NULL) |
595 |
+ |
return(NULL); |
596 |
+ |
/* link new free list */ |
597 |
+ |
upperlim = atfreelist + 8*(ATALLOCSZ-1); |
598 |
+ |
for (atp = atfreelist; atp < upperlim; atp += 8) |
599 |
+ |
atp->kid = atp + 8; |
600 |
+ |
atp->kid = NULL; |
601 |
+ |
} |
602 |
+ |
atp = atfreelist; |
603 |
+ |
atfreelist = atp->kid; |
604 |
+ |
memset((char *)atp, '\0', 8*sizeof(AMBTREE)); |
605 |
+ |
return(atp); |
606 |
+ |
} |
607 |
+ |
|
608 |
+ |
|
609 |
+ |
static void |
610 |
+ |
freeambtree( /* free 8 ambient tree structs */ |
611 |
+ |
AMBTREE *atp |
612 |
+ |
) |
613 |
+ |
{ |
614 |
+ |
atp->kid = atfreelist; |
615 |
+ |
atfreelist = atp; |
616 |
+ |
} |
617 |
+ |
|
618 |
+ |
|
619 |
+ |
static void |
620 |
+ |
avinsert( /* insert ambient value in our tree */ |
621 |
+ |
void *av |
622 |
+ |
) |
623 |
+ |
{ |
624 |
+ |
register AMBTREE *at; |
625 |
+ |
register AMBVAL *ap; |
626 |
+ |
AMBVAL avh; |
627 |
|
FVECT ck0; |
628 |
+ |
double s; |
629 |
|
int branch; |
317 |
– |
register AMBVAL *av; |
630 |
|
register int i; |
631 |
|
|
632 |
< |
if ((av = newambval()) == NULL) |
633 |
< |
goto memerr; |
634 |
< |
copystruct(av, aval); |
635 |
< |
VCOPY(ck0, c0); |
636 |
< |
while (s*(OCTSCALE/2) > av->rad*ambacc) { |
632 |
> |
if (((AMBVAL*)av)->rad <= FTINY) |
633 |
> |
error(CONSISTENCY, "zero ambient radius in avinsert"); |
634 |
> |
at = &atrunk; |
635 |
> |
VCOPY(ck0, thescene.cuorg); |
636 |
> |
s = thescene.cusize; |
637 |
> |
while (s*(OCTSCALE/2) > ((AMBVAL*)av)->rad*ambacc) { |
638 |
|
if (at->kid == NULL) |
639 |
|
if ((at->kid = newambtree()) == NULL) |
640 |
< |
goto memerr; |
640 |
> |
error(SYSTEM, "out of memory in avinsert"); |
641 |
|
s *= 0.5; |
642 |
|
branch = 0; |
643 |
|
for (i = 0; i < 3; i++) |
644 |
< |
if (av->pos[i] > ck0[i] + s) { |
644 |
> |
if (((AMBVAL*)av)->pos[i] > ck0[i] + s) { |
645 |
|
ck0[i] += s; |
646 |
|
branch |= 1 << i; |
647 |
|
} |
648 |
|
at = at->kid + branch; |
649 |
|
} |
650 |
< |
av->next = at->alist; |
651 |
< |
at->alist = av; |
652 |
< |
return; |
653 |
< |
memerr: |
654 |
< |
error(SYSTEM, "out of memory in avinsert"); |
650 |
> |
avh.next = at->alist; /* order by increasing level */ |
651 |
> |
for (ap = &avh; ap->next != NULL; ap = ap->next) |
652 |
> |
if (ap->next->lvl >= ((AMBVAL*)av)->lvl) |
653 |
> |
break; |
654 |
> |
((AMBVAL*)av)->next = ap->next; |
655 |
> |
ap->next = (AMBVAL*)av; |
656 |
> |
at->alist = avh.next; |
657 |
|
} |
658 |
+ |
|
659 |
+ |
|
660 |
+ |
static void |
661 |
+ |
unloadatree( /* unload an ambient value tree */ |
662 |
+ |
register AMBTREE *at, |
663 |
+ |
unloadtf_t *f |
664 |
+ |
) |
665 |
+ |
{ |
666 |
+ |
register AMBVAL *av; |
667 |
+ |
register int i; |
668 |
+ |
/* transfer values at this node */ |
669 |
+ |
for (av = at->alist; av != NULL; av = at->alist) { |
670 |
+ |
at->alist = av->next; |
671 |
+ |
(*f)(av); |
672 |
+ |
} |
673 |
+ |
if (at->kid == NULL) |
674 |
+ |
return; |
675 |
+ |
for (i = 0; i < 8; i++) /* transfer and free children */ |
676 |
+ |
unloadatree(at->kid+i, f); |
677 |
+ |
freeambtree(at->kid); |
678 |
+ |
at->kid = NULL; |
679 |
+ |
} |
680 |
+ |
|
681 |
+ |
|
682 |
+ |
static struct avl { |
683 |
+ |
AMBVAL *p; |
684 |
+ |
unsigned long t; |
685 |
+ |
} *avlist1; /* ambient value list with ticks */ |
686 |
+ |
static AMBVAL **avlist2; /* memory positions for sorting */ |
687 |
+ |
static int i_avlist; /* index for lists */ |
688 |
+ |
|
689 |
+ |
static int alatcmp(const void *av1, const void *av2); |
690 |
+ |
|
691 |
+ |
static void |
692 |
+ |
av2list( |
693 |
+ |
void *av |
694 |
+ |
) |
695 |
+ |
{ |
696 |
+ |
#ifdef DEBUG |
697 |
+ |
if (i_avlist >= nambvals) |
698 |
+ |
error(CONSISTENCY, "too many ambient values in av2list1"); |
699 |
+ |
#endif |
700 |
+ |
avlist1[i_avlist].p = avlist2[i_avlist] = (AMBVAL*)av; |
701 |
+ |
avlist1[i_avlist++].t = ((AMBVAL*)av)->latick; |
702 |
+ |
} |
703 |
+ |
|
704 |
+ |
|
705 |
+ |
static int |
706 |
+ |
alatcmp( /* compare ambient values for MRA */ |
707 |
+ |
const void *av1, |
708 |
+ |
const void *av2 |
709 |
+ |
) |
710 |
+ |
{ |
711 |
+ |
register long lc = ((struct avl *)av2)->t - ((struct avl *)av1)->t; |
712 |
+ |
return(lc<0 ? -1 : lc>0 ? 1 : 0); |
713 |
+ |
} |
714 |
+ |
|
715 |
+ |
|
716 |
+ |
/* GW NOTE 2002/10/3: |
717 |
+ |
* I used to compare AMBVAL pointers, but found that this was the |
718 |
+ |
* cause of a serious consistency error with gcc, since the optimizer |
719 |
+ |
* uses some dangerous trick in pointer subtraction that |
720 |
+ |
* assumes pointers differ by exact struct size increments. |
721 |
+ |
*/ |
722 |
+ |
static int |
723 |
+ |
aposcmp( /* compare ambient value positions */ |
724 |
+ |
const void *avp1, |
725 |
+ |
const void *avp2 |
726 |
+ |
) |
727 |
+ |
{ |
728 |
+ |
register long diff = *(char * const *)avp1 - *(char * const *)avp2; |
729 |
+ |
if (diff < 0) |
730 |
+ |
return(-1); |
731 |
+ |
return(diff > 0); |
732 |
+ |
} |
733 |
+ |
|
734 |
+ |
#if 1 |
735 |
+ |
static int |
736 |
+ |
avlmemi( /* find list position from address */ |
737 |
+ |
AMBVAL *avaddr |
738 |
+ |
) |
739 |
+ |
{ |
740 |
+ |
register AMBVAL **avlpp; |
741 |
+ |
|
742 |
+ |
avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2, |
743 |
+ |
nambvals, sizeof(AMBVAL *), aposcmp); |
744 |
+ |
if (avlpp == NULL) |
745 |
+ |
error(CONSISTENCY, "address not found in avlmemi"); |
746 |
+ |
return(avlpp - avlist2); |
747 |
+ |
} |
748 |
+ |
#else |
749 |
+ |
#define avlmemi(avaddr) ((AMBVAL **)bsearch((char *)&avaddr,(char *)avlist2, \ |
750 |
+ |
nambvals,sizeof(AMBVAL *),aposcmp) - avlist2) |
751 |
+ |
#endif |
752 |
+ |
|
753 |
+ |
|
754 |
+ |
static void |
755 |
+ |
sortambvals( /* resort ambient values */ |
756 |
+ |
int always |
757 |
+ |
) |
758 |
+ |
{ |
759 |
+ |
AMBTREE oldatrunk; |
760 |
+ |
AMBVAL tav, *tap, *pnext; |
761 |
+ |
register int i, j; |
762 |
+ |
/* see if it's time yet */ |
763 |
+ |
if (!always && (ambclock++ < lastsort+sortintvl || |
764 |
+ |
nambvals < SORT_THRESH)) |
765 |
+ |
return; |
766 |
+ |
/* |
767 |
+ |
* The idea here is to minimize memory thrashing |
768 |
+ |
* in VM systems by improving reference locality. |
769 |
+ |
* We do this by periodically sorting our stored ambient |
770 |
+ |
* values in memory in order of most recently to least |
771 |
+ |
* recently accessed. This ordering was chosen so that new |
772 |
+ |
* ambient values (which tend to be less important) go into |
773 |
+ |
* higher memory with the infrequently accessed values. |
774 |
+ |
* Since we expect our values to need sorting less |
775 |
+ |
* frequently as the process continues, we double our |
776 |
+ |
* waiting interval after each call. |
777 |
+ |
* This routine is also called by setambacc() with |
778 |
+ |
* the "always" parameter set to 1 so that the ambient |
779 |
+ |
* tree will be rebuilt with the new accuracy parameter. |
780 |
+ |
*/ |
781 |
+ |
if (tracktime) { /* allocate pointer arrays to sort */ |
782 |
+ |
avlist2 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *)); |
783 |
+ |
avlist1 = (struct avl *)malloc(nambvals*sizeof(struct avl)); |
784 |
+ |
} else { |
785 |
+ |
avlist2 = NULL; |
786 |
+ |
avlist1 = NULL; |
787 |
+ |
} |
788 |
+ |
if (avlist1 == NULL) { /* no time tracking -- rebuild tree? */ |
789 |
+ |
if (avlist2 != NULL) |
790 |
+ |
free((void *)avlist2); |
791 |
+ |
if (always) { /* rebuild without sorting */ |
792 |
+ |
oldatrunk = atrunk; |
793 |
+ |
atrunk.alist = NULL; |
794 |
+ |
atrunk.kid = NULL; |
795 |
+ |
unloadatree(&oldatrunk, avinsert); |
796 |
+ |
} |
797 |
+ |
} else { /* sort memory by last access time */ |
798 |
+ |
/* |
799 |
+ |
* Sorting memory is tricky because it isn't contiguous. |
800 |
+ |
* We have to sort an array of pointers by MRA and also |
801 |
+ |
* by memory position. We then copy values in "loops" |
802 |
+ |
* to minimize memory hits. Nevertheless, we will visit |
803 |
+ |
* everyone at least twice, and this is an expensive process |
804 |
+ |
* when we're thrashing, which is when we need to do it. |
805 |
+ |
*/ |
806 |
+ |
#ifdef DEBUG |
807 |
+ |
sprintf(errmsg, "sorting %u ambient values at ambclock=%lu...", |
808 |
+ |
nambvals, ambclock); |
809 |
+ |
eputs(errmsg); |
810 |
+ |
#endif |
811 |
+ |
i_avlist = 0; |
812 |
+ |
unloadatree(&atrunk, av2list); /* empty current tree */ |
813 |
+ |
#ifdef DEBUG |
814 |
+ |
if (i_avlist < nambvals) |
815 |
+ |
error(CONSISTENCY, "missing ambient values in sortambvals"); |
816 |
+ |
#endif |
817 |
+ |
qsort((char *)avlist1, nambvals, sizeof(struct avl), alatcmp); |
818 |
+ |
qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp); |
819 |
+ |
for (i = 0; i < nambvals; i++) { |
820 |
+ |
if (avlist1[i].p == NULL) |
821 |
+ |
continue; |
822 |
+ |
tap = avlist2[i]; |
823 |
+ |
tav = *tap; |
824 |
+ |
for (j = i; (pnext = avlist1[j].p) != tap; |
825 |
+ |
j = avlmemi(pnext)) { |
826 |
+ |
*(avlist2[j]) = *pnext; |
827 |
+ |
avinsert(avlist2[j]); |
828 |
+ |
avlist1[j].p = NULL; |
829 |
+ |
} |
830 |
+ |
*(avlist2[j]) = tav; |
831 |
+ |
avinsert(avlist2[j]); |
832 |
+ |
avlist1[j].p = NULL; |
833 |
+ |
} |
834 |
+ |
free((void *)avlist1); |
835 |
+ |
free((void *)avlist2); |
836 |
+ |
/* compute new sort interval */ |
837 |
+ |
sortintvl = ambclock - lastsort; |
838 |
+ |
if (sortintvl >= MAX_SORT_INTVL/2) |
839 |
+ |
sortintvl = MAX_SORT_INTVL; |
840 |
+ |
else |
841 |
+ |
sortintvl <<= 1; /* wait twice as long next */ |
842 |
+ |
#ifdef DEBUG |
843 |
+ |
eputs("done\n"); |
844 |
+ |
#endif |
845 |
+ |
} |
846 |
+ |
if (ambclock >= MAXACLOCK) |
847 |
+ |
ambclock = MAXACLOCK/2; |
848 |
+ |
lastsort = ambclock; |
849 |
+ |
} |
850 |
+ |
|
851 |
+ |
|
852 |
+ |
#ifdef F_SETLKW |
853 |
+ |
|
854 |
+ |
static void |
855 |
+ |
aflock( /* lock/unlock ambient file */ |
856 |
+ |
int typ |
857 |
+ |
) |
858 |
+ |
{ |
859 |
+ |
static struct flock fls; /* static so initialized to zeroes */ |
860 |
+ |
|
861 |
+ |
fls.l_type = typ; |
862 |
+ |
if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0) |
863 |
+ |
error(SYSTEM, "cannot (un)lock ambient file"); |
864 |
+ |
} |
865 |
+ |
|
866 |
+ |
|
867 |
+ |
extern int |
868 |
+ |
ambsync(void) /* synchronize ambient file */ |
869 |
+ |
{ |
870 |
+ |
long flen; |
871 |
+ |
AMBVAL avs; |
872 |
+ |
register int n; |
873 |
+ |
|
874 |
+ |
if (nunflshed == 0) |
875 |
+ |
return(0); |
876 |
+ |
if (lastpos < 0) /* initializing (locked in initambfile) */ |
877 |
+ |
goto syncend; |
878 |
+ |
/* gain exclusive access */ |
879 |
+ |
aflock(F_WRLCK); |
880 |
+ |
/* see if file has grown */ |
881 |
+ |
if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0) |
882 |
+ |
goto seekerr; |
883 |
+ |
if ( (n = flen - lastpos) ) { /* file has grown */ |
884 |
+ |
if (ambinp == NULL) { /* use duplicate filedes */ |
885 |
+ |
ambinp = fdopen(dup(fileno(ambfp)), "r"); |
886 |
+ |
if (ambinp == NULL) |
887 |
+ |
error(SYSTEM, "fdopen failed in ambsync"); |
888 |
+ |
} |
889 |
+ |
if (fseek(ambinp, lastpos, 0) < 0) |
890 |
+ |
goto seekerr; |
891 |
+ |
while (n >= AMBVALSIZ) { /* load contributed values */ |
892 |
+ |
if (!readambval(&avs, ambinp)) { |
893 |
+ |
sprintf(errmsg, |
894 |
+ |
"ambient file \"%s\" corrupted near character %ld", |
895 |
+ |
ambfile, flen - n); |
896 |
+ |
error(WARNING, errmsg); |
897 |
+ |
break; |
898 |
+ |
} |
899 |
+ |
avinsert(avstore(&avs)); |
900 |
+ |
n -= AMBVALSIZ; |
901 |
+ |
} |
902 |
+ |
/*** seek always as safety measure |
903 |
+ |
if (n) ***/ /* alignment */ |
904 |
+ |
if (lseek(fileno(ambfp), (off_t)(flen-n), SEEK_SET) < 0) |
905 |
+ |
goto seekerr; |
906 |
+ |
} |
907 |
+ |
#ifdef DEBUG |
908 |
+ |
if (ambfp->_ptr - ambfp->_base != nunflshed*AMBVALSIZ) { |
909 |
+ |
sprintf(errmsg, "ambient file buffer at %d rather than %d", |
910 |
+ |
ambfp->_ptr - ambfp->_base, |
911 |
+ |
nunflshed*AMBVALSIZ); |
912 |
+ |
error(CONSISTENCY, errmsg); |
913 |
+ |
} |
914 |
+ |
#endif |
915 |
+ |
syncend: |
916 |
+ |
n = fflush(ambfp); /* calls write() at last */ |
917 |
+ |
if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0) |
918 |
+ |
goto seekerr; |
919 |
+ |
aflock(F_UNLCK); /* release file */ |
920 |
+ |
nunflshed = 0; |
921 |
+ |
return(n); |
922 |
+ |
seekerr: |
923 |
+ |
error(SYSTEM, "seek failed in ambsync"); |
924 |
+ |
return -1; /* pro forma return */ |
925 |
+ |
} |
926 |
+ |
|
927 |
+ |
#else |
928 |
+ |
|
929 |
+ |
extern int |
930 |
+ |
ambsync(void) /* flush ambient file */ |
931 |
+ |
{ |
932 |
+ |
if (nunflshed == 0) |
933 |
+ |
return(0); |
934 |
+ |
nunflshed = 0; |
935 |
+ |
return(fflush(ambfp)); |
936 |
+ |
} |
937 |
+ |
|
938 |
+ |
#endif |