--- ray/src/rt/ambient.c 1997/08/30 10:17:01 2.43 +++ ray/src/rt/ambient.c 1999/01/10 12:45:19 2.46 @@ -80,7 +80,6 @@ static long sortintvl = SORT_INTVL; /* time until nex #define newambval() (AMBVAL *)bmalloc(sizeof(AMBVAL)) -extern long ftell(), lseek(); static int initambfile(), avsave(), avinsert(), sortambvals(), avlmemi(); static AMBVAL *avstore(); #ifdef F_SETLKW @@ -125,6 +124,7 @@ double newa; setambient(afile) /* initialize calculation */ char *afile; { + int readonly = 0; long pos, flen; AMBVAL amb; /* init ambient limits */ @@ -139,12 +139,24 @@ char *afile; return; } /* open ambient file */ - if ((ambfp = fopen(afile, "r+")) != NULL) { - initambfile(0); + if ((ambfp = fopen(afile, "r+")) == NULL) + readonly = (ambfp = fopen(afile, "r")) != NULL; + if (ambfp != NULL) { + initambfile(0); /* file exists */ pos = ftell(ambfp); while (readambval(&amb, ambfp)) avinsert(avstore(&amb)); - /* align */ + nambshare = nambvals; /* share loaded values */ + if (readonly) { + sprintf(errmsg, + "loaded %u values from read-only ambient file", + nambvals); + error(WARNING, errmsg); + fclose(ambfp); /* close file so no writes */ + ambfp = NULL; + return; /* avoid ambsync() */ + } + /* align file pointer */ pos += (long)nambvals*AMBVALSIZ; flen = lseek(fileno(ambfp), 0L, 2); if (flen != pos) { @@ -155,10 +167,9 @@ char *afile; fseek(ambfp, pos, 0); ftruncate(fileno(ambfp), pos); } - nambshare = nambvals; - } else if ((ambfp = fopen(afile, "w+")) != NULL) - initambfile(1); - else { + } else if ((ambfp = fopen(afile, "w+")) != NULL) { + initambfile(1); /* else create new file */ + } else { sprintf(errmsg, "cannot open ambient file \"%s\"", afile); error(SYSTEM, errmsg); } @@ -378,7 +389,7 @@ COLOR cr; register AMBVAL *ap; FVECT pv, nv; { - FVECT v1, v2; + FVECT v1; register int i; double d; @@ -387,9 +398,8 @@ FVECT pv, nv; for (i = 0; i < 3; i++) d += ap->gpos[i]*(pv[i]-ap->pos[i]); /* gradient due to rotation */ - VCOPY(v1, ap->dir); - fcross(v2, v1, nv); - d += DOT(ap->gdir, v2); + VCROSS(v1, ap->dir, nv); + d += DOT(ap->gdir, v1); if (d <= 0.0) { setcolor(cr, 0.0, 0.0, 0.0); return;