56 |
|
#define MAX_SORT_INTVL (SORT_INTVL<<6) |
57 |
|
#endif |
58 |
|
|
59 |
< |
static COLOR avsum = BLKCOLOR; /* computed ambient value sum */ |
59 |
> |
static double avsum = 0.; /* computed ambient value sum (log) */ |
60 |
> |
static unsigned int navsum = 0; /* number of values in avsum */ |
61 |
|
static unsigned int nambvals = 0; /* total number of indirect values */ |
62 |
|
static unsigned int nambshare = 0; /* number of values from file */ |
63 |
|
static unsigned long ambclock = 0; /* ambient access clock */ |
70 |
|
* through memory on a multiprocessor, when we want to avoid |
71 |
|
* claiming our own memory (copy on write). Go ahead anyway |
72 |
|
* if more than two thirds of our values are unshared. |
73 |
+ |
* Compile with -Dtracktime=0 to turn this code off. |
74 |
|
*/ |
75 |
+ |
#ifndef tracktime |
76 |
|
#define tracktime (shm_boundary == NULL || nambvals > 3*nambshare) |
77 |
+ |
#endif |
78 |
|
|
79 |
|
#define AMBFLUSH (BUFSIZ/AMBVALSIZ) |
80 |
|
|
81 |
|
#define newambval() (AMBVAL *)bmalloc(sizeof(AMBVAL)) |
82 |
|
|
79 |
– |
extern long ftell(), lseek(); |
83 |
|
static int initambfile(), avsave(), avinsert(), sortambvals(), avlmemi(); |
84 |
|
static AMBVAL *avstore(); |
85 |
|
#ifdef F_SETLKW |
124 |
|
setambient(afile) /* initialize calculation */ |
125 |
|
char *afile; |
126 |
|
{ |
127 |
+ |
int readonly = 0; |
128 |
|
long pos, flen; |
129 |
|
AMBVAL amb; |
130 |
|
/* init ambient limits */ |
139 |
|
return; |
140 |
|
} |
141 |
|
/* open ambient file */ |
142 |
< |
if ((ambfp = fopen(afile, "r+")) != NULL) { |
143 |
< |
initambfile(0); |
142 |
> |
if ((ambfp = fopen(afile, "r+")) == NULL) |
143 |
> |
readonly = (ambfp = fopen(afile, "r")) != NULL; |
144 |
> |
if (ambfp != NULL) { |
145 |
> |
initambfile(0); /* file exists */ |
146 |
|
pos = ftell(ambfp); |
147 |
|
while (readambval(&amb, ambfp)) |
148 |
|
avinsert(avstore(&amb)); |
149 |
< |
/* align */ |
149 |
> |
nambshare = nambvals; /* share loaded values */ |
150 |
> |
if (readonly) { |
151 |
> |
sprintf(errmsg, |
152 |
> |
"loaded %u values from read-only ambient file", |
153 |
> |
nambvals); |
154 |
> |
error(WARNING, errmsg); |
155 |
> |
fclose(ambfp); /* close file so no writes */ |
156 |
> |
ambfp = NULL; |
157 |
> |
return; /* avoid ambsync() */ |
158 |
> |
} |
159 |
> |
/* align file pointer */ |
160 |
|
pos += (long)nambvals*AMBVALSIZ; |
161 |
|
flen = lseek(fileno(ambfp), 0L, 2); |
162 |
|
if (flen != pos) { |
163 |
< |
error(WARNING, |
163 |
> |
sprintf(errmsg, |
164 |
|
"ignoring last %ld values in ambient file (corrupted)", |
165 |
|
(flen - pos)/AMBVALSIZ); |
166 |
+ |
error(WARNING, errmsg); |
167 |
|
fseek(ambfp, pos, 0); |
168 |
|
ftruncate(fileno(ambfp), pos); |
169 |
|
} |
170 |
< |
nambshare = nambvals; |
171 |
< |
} else if ((ambfp = fopen(afile, "w+")) != NULL) |
172 |
< |
initambfile(1); |
156 |
< |
else { |
170 |
> |
} else if ((ambfp = fopen(afile, "w+")) != NULL) { |
171 |
> |
initambfile(1); /* else create new file */ |
172 |
> |
} else { |
173 |
|
sprintf(errmsg, "cannot open ambient file \"%s\"", afile); |
174 |
|
error(SYSTEM, errmsg); |
175 |
|
} |
206 |
|
FVECT nrm; |
207 |
|
{ |
208 |
|
static int rdepth = 0; /* ambient recursion */ |
209 |
< |
double d; |
209 |
> |
double d, l; |
210 |
|
|
211 |
|
if (ambdiv <= 0) /* no ambient calculation */ |
212 |
|
goto dumbamb; |
244 |
|
return; |
245 |
|
dumbamb: /* return global value */ |
246 |
|
copycolor(acol, ambval); |
247 |
< |
if (ambvwt <= 0 | nambvals == 0) |
247 |
> |
if (ambvwt <= 0 | navsum == 0) |
248 |
|
return; |
249 |
< |
scalecolor(acol, (double)ambvwt); |
250 |
< |
addcolor(acol, avsum); /* average in computations */ |
251 |
< |
d = 1.0/(ambvwt+nambvals); |
252 |
< |
scalecolor(acol, d); |
249 |
> |
l = bright(ambval); /* average in computations */ |
250 |
> |
if (l > FTINY) { |
251 |
> |
d = (log(l)*(double)ambvwt + avsum) / |
252 |
> |
(double)(ambvwt + navsum); |
253 |
> |
d = exp(d) / l; |
254 |
> |
scalecolor(acol, d); /* apply color of ambval */ |
255 |
> |
} else { |
256 |
> |
d = exp( avsum / (double)navsum ); |
257 |
> |
setcolor(acol, d, d, d); /* neutral color */ |
258 |
> |
} |
259 |
|
} |
260 |
|
|
261 |
|
|
389 |
|
register AMBVAL *ap; |
390 |
|
FVECT pv, nv; |
391 |
|
{ |
392 |
< |
FVECT v1, v2; |
392 |
> |
FVECT v1; |
393 |
|
register int i; |
394 |
|
double d; |
395 |
|
|
398 |
|
for (i = 0; i < 3; i++) |
399 |
|
d += ap->gpos[i]*(pv[i]-ap->pos[i]); |
400 |
|
/* gradient due to rotation */ |
401 |
< |
VCOPY(v1, ap->dir); |
402 |
< |
fcross(v2, v1, nv); |
381 |
< |
d += DOT(ap->gdir, v2); |
401 |
> |
VCROSS(v1, ap->dir, nv); |
402 |
> |
d += DOT(ap->gdir, v1); |
403 |
|
if (d <= 0.0) { |
404 |
|
setcolor(cr, 0.0, 0.0, 0.0); |
405 |
|
return; |
463 |
|
register AMBVAL *aval; |
464 |
|
{ |
465 |
|
register AMBVAL *av; |
466 |
+ |
double d; |
467 |
|
|
468 |
|
if ((av = newambval()) == NULL) |
469 |
|
error(SYSTEM, "out of memory in avstore"); |
470 |
|
copystruct(av, aval); |
471 |
|
av->latick = ambclock; |
472 |
|
av->next = NULL; |
451 |
– |
addcolor(avsum, av->val); /* add to sum for averaging */ |
473 |
|
nambvals++; |
474 |
+ |
d = bright(av->val); |
475 |
+ |
if (d > FTINY) { /* add to log sum for averaging */ |
476 |
+ |
avsum += log(d); |
477 |
+ |
navsum++; |
478 |
+ |
} |
479 |
|
return(av); |
480 |
|
} |
481 |
|
|