1 |
< |
/* Copyright (c) 1992 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1993 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
6 |
|
|
7 |
|
/* |
8 |
|
* ambient.c - routines dealing with ambient (inter-reflected) component. |
9 |
– |
* |
10 |
– |
* 5/9/86 |
9 |
|
*/ |
10 |
|
|
11 |
|
#include "ray.h" |
36 |
|
static AMBTREE atrunk; /* our ambient trunk node */ |
37 |
|
|
38 |
|
static FILE *ambfp = NULL; /* ambient file pointer */ |
39 |
+ |
static int nunflshed = 0; /* number of unflushed ambient values */ |
40 |
|
|
41 |
|
#define AMBFLUSH (BUFSIZ/AMBVALSIZ) |
42 |
|
|
45 |
|
#define newambtree() (AMBTREE *)calloc(8, sizeof(AMBTREE)) |
46 |
|
|
47 |
|
extern long ftell(), lseek(); |
48 |
< |
static int initambfile(), avsave(), avinsert(), ambsync(); |
48 |
> |
static int initambfile(), avsave(), avinsert(); |
49 |
|
|
50 |
|
|
51 |
|
setambres(ar) /* set ambient resolution */ |
74 |
|
/* init ambient limits */ |
75 |
|
setambres(ambres); |
76 |
|
/* open ambient file */ |
77 |
< |
if (afile != NULL) |
77 |
> |
if (afile != NULL) { |
78 |
|
if ((ambfp = fopen(afile, "r+")) != NULL) { |
79 |
|
initambfile(0); |
80 |
|
headlen = ftell(ambfp); |
90 |
|
afile); |
91 |
|
error(SYSTEM, errmsg); |
92 |
|
} |
93 |
+ |
nunflshed++; /* lie */ |
94 |
+ |
ambsync(); |
95 |
+ |
} |
96 |
|
} |
97 |
|
|
98 |
|
|
169 |
|
FVECT c0; |
170 |
|
double s; |
171 |
|
{ |
170 |
– |
extern double sqrt(); |
172 |
|
double d, e1, e2, wt, wsum; |
173 |
|
COLOR ct; |
174 |
|
FVECT ck0; |
324 |
|
fputformat(AMBFMT, ambfp); |
325 |
|
putc('\n', ambfp); |
326 |
|
putambmagic(ambfp); |
326 |
– |
fflush(ambfp); |
327 |
– |
#ifndef NIX |
328 |
– |
sync(); /* protect against NFS buffering */ |
329 |
– |
#endif |
327 |
|
} else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp)) |
328 |
|
error(USER, "bad ambient file"); |
329 |
|
} |
333 |
|
avsave(av) /* insert and save an ambient value */ |
334 |
|
AMBVAL *av; |
335 |
|
{ |
339 |
– |
static int nunflshed = 0; |
340 |
– |
|
336 |
|
avinsert(av, &atrunk, thescene.cuorg, thescene.cusize); |
337 |
|
if (ambfp == NULL) |
338 |
|
return; |
339 |
|
if (writambval(av, ambfp) < 0) |
340 |
|
goto writerr; |
341 |
< |
if (++nunflshed >= AMBFLUSH) { |
341 |
> |
if (++nunflshed >= AMBFLUSH) |
342 |
|
if (ambsync() == EOF) |
343 |
|
goto writerr; |
349 |
– |
nunflshed = 0; |
350 |
– |
} |
344 |
|
return; |
345 |
|
writerr: |
346 |
|
error(SYSTEM, "error writing ambient file"); |
384 |
|
} |
385 |
|
|
386 |
|
|
387 |
< |
#ifdef NIX |
387 |
> |
#ifdef F_SETLKW |
388 |
|
|
389 |
< |
static |
397 |
< |
ambsync() /* flush ambient file */ |
398 |
< |
{ |
399 |
< |
return(fflush(ambfp)); |
400 |
< |
} |
401 |
< |
|
402 |
< |
#else |
403 |
< |
|
404 |
< |
#include <fcntl.h> |
405 |
< |
#include <sys/types.h> |
406 |
< |
#include <sys/stat.h> |
407 |
< |
|
408 |
< |
static |
389 |
> |
int |
390 |
|
ambsync() /* synchronize ambient file */ |
391 |
|
{ |
392 |
|
static FILE *ambinp = NULL; |
393 |
+ |
static long lastpos = -1; |
394 |
|
struct flock fls; |
395 |
< |
struct stat sts; |
395 |
> |
long flen; |
396 |
|
AMBVAL avs; |
415 |
– |
long lastpos, flen; |
397 |
|
register int n; |
398 |
+ |
|
399 |
+ |
if (nunflshed == 0) |
400 |
+ |
return(0); |
401 |
|
/* gain exclusive access */ |
402 |
|
fls.l_type = F_WRLCK; |
403 |
|
fls.l_whence = 0; |
405 |
|
fls.l_len = 0L; |
406 |
|
if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0) |
407 |
|
error(SYSTEM, "cannot lock ambient file"); |
408 |
+ |
if (lastpos < 0) /* initializing */ |
409 |
+ |
goto syncend; |
410 |
|
/* see if file has grown */ |
411 |
< |
lastpos = lseek(fileno(ambfp), 0L, 1); /* get previous position */ |
412 |
< |
if (fstat(fileno(ambfp), &sts) < 0) /* get current length */ |
413 |
< |
error(SYSTEM, "cannot stat ambient file"); |
414 |
< |
flen = sts.st_size; |
429 |
< |
if (n = (flen - lastpos)/AMBVALSIZ) { /* file has grown */ |
430 |
< |
if (ambinp == NULL) /* use duplicate file */ |
411 |
> |
if ((flen = lseek(fileno(ambfp), 0L, 2)) < 0) |
412 |
> |
error(SYSTEM, "cannot seek on ambient file"); |
413 |
> |
if (n = flen - lastpos) { /* file has grown */ |
414 |
> |
if (ambinp == NULL) { /* use duplicate filedes */ |
415 |
|
ambinp = fdopen(dup(fileno(ambfp)), "r"); |
416 |
< |
while (n--) { /* load contributed values */ |
416 |
> |
if (ambinp == NULL) |
417 |
> |
error(SYSTEM, "fdopen failed in ambsync"); |
418 |
> |
} |
419 |
> |
if (fseek(ambinp, lastpos, 0) < 0) |
420 |
> |
error(SYSTEM, "fseek failed in ambsync"); |
421 |
> |
while (n >= AMBVALSIZ) { /* load contributed values */ |
422 |
|
readambval(&avs, ambinp); |
423 |
|
avinsert(&avs,&atrunk,thescene.cuorg,thescene.cusize); |
424 |
< |
} /* moves shared file pointer */ |
425 |
< |
if (n = (flen - lastpos)%AMBVALSIZ) /* alignment */ |
424 |
> |
n -= AMBVALSIZ; |
425 |
> |
} |
426 |
> |
if (n) /* alignment */ |
427 |
|
lseek(fileno(ambfp), flen-n, 0); |
428 |
|
} |
429 |
+ |
syncend: |
430 |
|
n = fflush(ambfp); /* calls write() at last */ |
431 |
+ |
lastpos = lseek(fileno(ambfp), 0L, 1); |
432 |
|
fls.l_type = F_UNLCK; /* release file */ |
433 |
|
fcntl(fileno(ambfp), F_SETLKW, &fls); |
434 |
+ |
nunflshed = 0; |
435 |
|
return(n); |
436 |
+ |
} |
437 |
+ |
|
438 |
+ |
#else |
439 |
+ |
|
440 |
+ |
int |
441 |
+ |
ambsync() /* flush ambient file */ |
442 |
+ |
{ |
443 |
+ |
if (nunflshed == 0) |
444 |
+ |
return(0); |
445 |
+ |
nunflshed = 0; |
446 |
+ |
return(fflush(ambfp)); |
447 |
|
} |
448 |
|
|
449 |
|
#endif |