1 |
– |
/* Copyright (c) 1993 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 |
+ |
* Declarations of external symbols in ambient.h |
8 |
|
*/ |
9 |
|
|
10 |
+ |
/* ==================================================================== |
11 |
+ |
* The Radiance Software License, Version 1.0 |
12 |
+ |
* |
13 |
+ |
* Copyright (c) 1990 - 2002 The Regents of the University of California, |
14 |
+ |
* through Lawrence Berkeley National Laboratory. All rights reserved. |
15 |
+ |
* |
16 |
+ |
* Redistribution and use in source and binary forms, with or without |
17 |
+ |
* modification, are permitted provided that the following conditions |
18 |
+ |
* are met: |
19 |
+ |
* |
20 |
+ |
* 1. Redistributions of source code must retain the above copyright |
21 |
+ |
* notice, this list of conditions and the following disclaimer. |
22 |
+ |
* |
23 |
+ |
* 2. Redistributions in binary form must reproduce the above copyright |
24 |
+ |
* notice, this list of conditions and the following disclaimer in |
25 |
+ |
* the documentation and/or other materials provided with the |
26 |
+ |
* distribution. |
27 |
+ |
* |
28 |
+ |
* 3. The end-user documentation included with the redistribution, |
29 |
+ |
* if any, must include the following acknowledgment: |
30 |
+ |
* "This product includes Radiance software |
31 |
+ |
* (http://radsite.lbl.gov/) |
32 |
+ |
* developed by the Lawrence Berkeley National Laboratory |
33 |
+ |
* (http://www.lbl.gov/)." |
34 |
+ |
* Alternately, this acknowledgment may appear in the software itself, |
35 |
+ |
* if and wherever such third-party acknowledgments normally appear. |
36 |
+ |
* |
37 |
+ |
* 4. The names "Radiance," "Lawrence Berkeley National Laboratory" |
38 |
+ |
* and "The Regents of the University of California" must |
39 |
+ |
* not be used to endorse or promote products derived from this |
40 |
+ |
* software without prior written permission. For written |
41 |
+ |
* permission, please contact [email protected]. |
42 |
+ |
* |
43 |
+ |
* 5. Products derived from this software may not be called "Radiance", |
44 |
+ |
* nor may "Radiance" appear in their name, without prior written |
45 |
+ |
* permission of Lawrence Berkeley National Laboratory. |
46 |
+ |
* |
47 |
+ |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
48 |
+ |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
49 |
+ |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
50 |
+ |
* DISCLAIMED. IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR |
51 |
+ |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
52 |
+ |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
53 |
+ |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
54 |
+ |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
55 |
+ |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
56 |
+ |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
57 |
+ |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
58 |
+ |
* SUCH DAMAGE. |
59 |
+ |
* ==================================================================== |
60 |
+ |
* |
61 |
+ |
* This software consists of voluntary contributions made by many |
62 |
+ |
* individuals on behalf of Lawrence Berkeley National Laboratory. For more |
63 |
+ |
* information on Lawrence Berkeley National Laboratory, please see |
64 |
+ |
* <http://www.lbl.gov/>. |
65 |
+ |
*/ |
66 |
+ |
|
67 |
|
#include "ray.h" |
68 |
|
|
13 |
– |
#include "octree.h" |
14 |
– |
|
69 |
|
#include "otypes.h" |
70 |
|
|
71 |
|
#include "ambient.h" |
72 |
|
|
73 |
|
#include "random.h" |
74 |
|
|
75 |
< |
#define OCTSCALE 0.5 /* ceil((valid rad.)/(cube size)) */ |
75 |
> |
#ifndef OCTSCALE |
76 |
> |
#define OCTSCALE 1.0 /* ceil((valid rad.)/(cube size)) */ |
77 |
> |
#endif |
78 |
|
|
79 |
< |
typedef struct ambtree { |
24 |
< |
AMBVAL *alist; /* ambient value list */ |
25 |
< |
struct ambtree *kid; /* 8 child nodes */ |
26 |
< |
} AMBTREE; /* ambient octree */ |
79 |
> |
extern char *shm_boundary; /* memory sharing boundary */ |
80 |
|
|
28 |
– |
extern CUBE thescene; /* contains space boundaries */ |
29 |
– |
|
30 |
– |
extern char *shm_boundary; /* shared memory boundary */ |
31 |
– |
|
81 |
|
#define MAXASET 511 /* maximum number of elements in ambient set */ |
82 |
|
OBJECT ambset[MAXASET+1]={0}; /* ambient include/exclude set */ |
83 |
|
|
91 |
|
|
92 |
|
#ifndef SORT_THRESH |
93 |
|
#ifdef BIGMEM |
94 |
< |
#define SORT_THRESH (6*(1L<<20)/sizeof(AMBVAL)) |
94 |
> |
#define SORT_THRESH ((9L<<20)/sizeof(AMBVAL)) |
95 |
|
#else |
96 |
< |
#define SORT_THRESH (2*(1L<<20)/sizeof(AMBVAL)) |
96 |
> |
#define SORT_THRESH ((3L<<20)/sizeof(AMBVAL)) |
97 |
|
#endif |
98 |
|
#endif |
99 |
|
#ifndef SORT_INTVL |
100 |
< |
#define SORT_INTVL (SORT_THRESH*2) |
100 |
> |
#define SORT_INTVL (SORT_THRESH<<1) |
101 |
|
#endif |
102 |
+ |
#ifndef MAX_SORT_INTVL |
103 |
+ |
#define MAX_SORT_INTVL (SORT_INTVL<<6) |
104 |
+ |
#endif |
105 |
|
|
106 |
< |
static long ambclock = 0; /* ambient access clock */ |
107 |
< |
static int nambvals = 0; /* number of stored ambient values */ |
108 |
< |
static long lastsort = 0; /* time of last value sort */ |
106 |
> |
static double avsum = 0.; /* computed ambient value sum (log) */ |
107 |
> |
static unsigned int navsum = 0; /* number of values in avsum */ |
108 |
> |
static unsigned int nambvals = 0; /* total number of indirect values */ |
109 |
> |
static unsigned int nambshare = 0; /* number of values from file */ |
110 |
> |
static unsigned long ambclock = 0; /* ambient access clock */ |
111 |
> |
static unsigned long lastsort = 0; /* time of last value sort */ |
112 |
|
static long sortintvl = SORT_INTVL; /* time until next sort */ |
113 |
+ |
static FILE *ambinp = NULL; /* auxiliary file for input */ |
114 |
+ |
static long lastpos = -1; /* last flush position */ |
115 |
|
|
116 |
|
#define MAXACLOCK (1L<<30) /* clock turnover value */ |
117 |
< |
#define tracktime (shm_boundary == NULL || ambfp == NULL) |
118 |
< |
#define need2sort (ambclock > lastsort+sortintvl && \ |
119 |
< |
nambvals > SORT_THRESH) |
117 |
> |
/* |
118 |
> |
* Track access times unless we are sharing ambient values |
119 |
> |
* through memory on a multiprocessor, when we want to avoid |
120 |
> |
* claiming our own memory (copy on write). Go ahead anyway |
121 |
> |
* if more than two thirds of our values are unshared. |
122 |
> |
* Compile with -Dtracktime=0 to turn this code off. |
123 |
> |
*/ |
124 |
> |
#ifndef tracktime |
125 |
> |
#define tracktime (shm_boundary == NULL || nambvals > 3*nambshare) |
126 |
> |
#endif |
127 |
|
|
128 |
|
#define AMBFLUSH (BUFSIZ/AMBVALSIZ) |
129 |
|
|
130 |
< |
#define newambval() (AMBVAL *)bmalloc(sizeof(AMBVAL)) |
130 |
> |
#define newambval() (AMBVAL *)malloc(sizeof(AMBVAL)) |
131 |
> |
#define freeav(av) free((void *)av); |
132 |
|
|
133 |
< |
extern long ftell(), lseek(); |
134 |
< |
static int initambfile(), avsave(), avinsert(), sortambvals(); |
133 |
> |
static void initambfile(), avsave(), avinsert(), sortambvals(), unloadatree(); |
134 |
> |
static int avlmemi(); |
135 |
|
static AMBVAL *avstore(); |
136 |
|
#ifdef F_SETLKW |
137 |
< |
static aflock(); |
137 |
> |
static void aflock(); |
138 |
|
#endif |
139 |
|
|
140 |
|
|
141 |
+ |
void |
142 |
|
setambres(ar) /* set ambient resolution */ |
143 |
|
int ar; |
144 |
|
{ |
149 |
|
maxarad = thescene.cusize / 2.0; |
150 |
|
} else { |
151 |
|
minarad = thescene.cusize / ar; |
152 |
< |
maxarad = 16 * minarad; /* heuristic */ |
152 |
> |
maxarad = 64 * minarad; /* heuristic */ |
153 |
|
if (maxarad > thescene.cusize / 2.0) |
154 |
|
maxarad = thescene.cusize / 2.0; |
155 |
|
} |
160 |
|
} |
161 |
|
|
162 |
|
|
163 |
+ |
void |
164 |
|
setambacc(newa) /* set ambient accuracy */ |
165 |
|
double newa; |
166 |
|
{ |
167 |
< |
static double oldambacc = -1.0; |
167 |
> |
double ambdiff; |
168 |
|
|
169 |
< |
ambacc = newa < 0.0 ? 0.0 : newa; /* may be done already */ |
170 |
< |
if (oldambacc < -FTINY) |
171 |
< |
oldambacc = ambacc; /* do nothing first call */ |
172 |
< |
if (fabs(newa - oldambacc) < 0.01) |
173 |
< |
return; /* insignificant -- don't bother */ |
107 |
< |
if (ambacc <= FTINY) |
108 |
< |
return; /* cannot build new tree */ |
109 |
< |
/* else need to rebuild tree */ |
110 |
< |
sortambvals(1); |
111 |
< |
oldambacc = ambacc; /* remeber setting for next call */ |
169 |
> |
if (newa < 0.0) |
170 |
> |
newa = 0.0; |
171 |
> |
ambdiff = fabs(newa - ambacc); |
172 |
> |
if (ambdiff >= .01 && (ambacc = newa) > FTINY && nambvals > 0) |
173 |
> |
sortambvals(1); /* rebuild tree */ |
174 |
|
} |
175 |
|
|
176 |
|
|
177 |
< |
setambient(afile) /* initialize calculation */ |
178 |
< |
char *afile; |
177 |
> |
void |
178 |
> |
setambient() /* initialize calculation */ |
179 |
|
{ |
180 |
< |
long headlen; |
180 |
> |
int readonly = 0; |
181 |
> |
long pos, flen; |
182 |
|
AMBVAL amb; |
183 |
+ |
/* make sure we're fresh */ |
184 |
+ |
ambdone(); |
185 |
|
/* init ambient limits */ |
186 |
|
setambres(ambres); |
187 |
|
setambacc(ambacc); |
188 |
< |
if (afile == NULL) |
188 |
> |
if (ambfile == NULL || !ambfile[0]) |
189 |
|
return; |
190 |
|
if (ambacc <= FTINY) { |
191 |
|
sprintf(errmsg, "zero ambient accuracy so \"%s\" not opened", |
192 |
< |
afile); |
192 |
> |
ambfile); |
193 |
|
error(WARNING, errmsg); |
194 |
|
return; |
195 |
|
} |
196 |
|
/* open ambient file */ |
197 |
< |
if ((ambfp = fopen(afile, "r+")) != NULL) { |
198 |
< |
initambfile(0); |
199 |
< |
headlen = ftell(ambfp); |
197 |
> |
if ((ambfp = fopen(ambfile, "r+")) == NULL) |
198 |
> |
readonly = (ambfp = fopen(ambfile, "r")) != NULL; |
199 |
> |
if (ambfp != NULL) { |
200 |
> |
initambfile(0); /* file exists */ |
201 |
> |
pos = ftell(ambfp); |
202 |
|
while (readambval(&amb, ambfp)) |
203 |
|
avinsert(avstore(&amb)); |
204 |
< |
/* align */ |
205 |
< |
fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1); |
206 |
< |
} else if ((ambfp = fopen(afile, "w+")) != NULL) |
207 |
< |
initambfile(1); |
208 |
< |
else { |
209 |
< |
sprintf(errmsg, "cannot open ambient file \"%s\"", afile); |
204 |
> |
nambshare = nambvals; /* share loaded values */ |
205 |
> |
if (readonly) { |
206 |
> |
sprintf(errmsg, |
207 |
> |
"loaded %u values from read-only ambient file", |
208 |
> |
nambvals); |
209 |
> |
error(WARNING, errmsg); |
210 |
> |
fclose(ambfp); /* close file so no writes */ |
211 |
> |
ambfp = NULL; |
212 |
> |
return; /* avoid ambsync() */ |
213 |
> |
} |
214 |
> |
/* align file pointer */ |
215 |
> |
pos += (long)nambvals*AMBVALSIZ; |
216 |
> |
flen = lseek(fileno(ambfp), (off_t)0L, 2); |
217 |
> |
if (flen != pos) { |
218 |
> |
sprintf(errmsg, |
219 |
> |
"ignoring last %ld values in ambient file (corrupted)", |
220 |
> |
(flen - pos)/AMBVALSIZ); |
221 |
> |
error(WARNING, errmsg); |
222 |
> |
fseek(ambfp, pos, 0); |
223 |
> |
ftruncate(fileno(ambfp), (off_t)pos); |
224 |
> |
} |
225 |
> |
} else if ((ambfp = fopen(ambfile, "w+")) != NULL) { |
226 |
> |
initambfile(1); /* else create new file */ |
227 |
> |
} else { |
228 |
> |
sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile); |
229 |
|
error(SYSTEM, errmsg); |
230 |
|
} |
231 |
|
nunflshed++; /* lie */ |
233 |
|
} |
234 |
|
|
235 |
|
|
236 |
+ |
void |
237 |
+ |
ambdone() /* close ambient file and free memory */ |
238 |
+ |
{ |
239 |
+ |
if (ambfp != NULL) { /* close ambient file */ |
240 |
+ |
ambsync(); |
241 |
+ |
fclose(ambfp); |
242 |
+ |
ambfp = NULL; |
243 |
+ |
if (ambinp != NULL) { |
244 |
+ |
fclose(ambinp); |
245 |
+ |
ambinp = NULL; |
246 |
+ |
} |
247 |
+ |
lastpos = -1; |
248 |
+ |
} |
249 |
+ |
/* free ambient tree */ |
250 |
+ |
unloadatree(&atrunk, free); |
251 |
+ |
/* reset state variables */ |
252 |
+ |
avsum = 0.; |
253 |
+ |
navsum = 0; |
254 |
+ |
nambvals = 0; |
255 |
+ |
nambshare = 0; |
256 |
+ |
ambclock = 0; |
257 |
+ |
lastsort = 0; |
258 |
+ |
sortintvl = SORT_INTVL; |
259 |
+ |
} |
260 |
+ |
|
261 |
+ |
|
262 |
+ |
void |
263 |
|
ambnotify(obj) /* record new modifier */ |
264 |
|
OBJECT obj; |
265 |
|
{ |
266 |
|
static int hitlimit = 0; |
267 |
< |
register OBJREC *o = objptr(obj); |
267 |
> |
register OBJREC *o; |
268 |
|
register char **amblp; |
269 |
|
|
270 |
+ |
if (obj == OVOID) { /* starting over */ |
271 |
+ |
ambset[0] = 0; |
272 |
+ |
hitlimit = 0; |
273 |
+ |
return; |
274 |
+ |
} |
275 |
+ |
o = objptr(obj); |
276 |
|
if (hitlimit || !ismodifier(o->otype)) |
277 |
|
return; |
278 |
|
for (amblp = amblist; *amblp != NULL; amblp++) |
288 |
|
} |
289 |
|
|
290 |
|
|
291 |
< |
ambient(acol, r) /* compute ambient component for ray */ |
291 |
> |
void |
292 |
> |
ambient(acol, r, nrm) /* compute ambient component for ray */ |
293 |
|
COLOR acol; |
294 |
|
register RAY *r; |
295 |
+ |
FVECT nrm; |
296 |
|
{ |
297 |
|
static int rdepth = 0; /* ambient recursion */ |
298 |
< |
double d; |
298 |
> |
double d, l; |
299 |
|
|
300 |
|
if (ambdiv <= 0) /* no ambient calculation */ |
301 |
|
goto dumbamb; |
311 |
|
rdepth++; |
312 |
|
d = doambient(acol, r, r->rweight, NULL, NULL); |
313 |
|
rdepth--; |
314 |
< |
if (d == 0.0) |
314 |
> |
if (d <= FTINY) |
315 |
|
goto dumbamb; |
316 |
|
return; |
317 |
|
} |
318 |
< |
/* get ambient value */ |
319 |
< |
if (need2sort) |
318 |
> |
|
319 |
> |
if (tracktime) /* sort to minimize thrashing */ |
320 |
|
sortambvals(0); |
321 |
+ |
/* get ambient value */ |
322 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
323 |
< |
d = sumambient(acol, r, rdepth, |
323 |
> |
d = sumambient(acol, r, nrm, rdepth, |
324 |
|
&atrunk, thescene.cuorg, thescene.cusize); |
325 |
< |
if (d > FTINY) |
325 |
> |
if (d > FTINY) { |
326 |
|
scalecolor(acol, 1.0/d); |
327 |
< |
else { |
206 |
< |
d = makeambient(acol, r, rdepth++); |
207 |
< |
rdepth--; |
327 |
> |
return; |
328 |
|
} |
329 |
+ |
rdepth++; /* need to cache new value */ |
330 |
+ |
d = makeambient(acol, r, nrm, rdepth-1); |
331 |
+ |
rdepth--; |
332 |
|
if (d > FTINY) |
333 |
|
return; |
334 |
|
dumbamb: /* return global value */ |
335 |
|
copycolor(acol, ambval); |
336 |
+ |
if (ambvwt <= 0 | navsum == 0) |
337 |
+ |
return; |
338 |
+ |
l = bright(ambval); /* average in computations */ |
339 |
+ |
if (l > FTINY) { |
340 |
+ |
d = (log(l)*(double)ambvwt + avsum) / |
341 |
+ |
(double)(ambvwt + navsum); |
342 |
+ |
d = exp(d) / l; |
343 |
+ |
scalecolor(acol, d); /* apply color of ambval */ |
344 |
+ |
} else { |
345 |
+ |
d = exp( avsum / (double)navsum ); |
346 |
+ |
setcolor(acol, d, d, d); /* neutral color */ |
347 |
+ |
} |
348 |
|
} |
349 |
|
|
350 |
|
|
351 |
|
double |
352 |
< |
sumambient(acol, r, al, at, c0, s) /* get interpolated ambient value */ |
352 |
> |
sumambient(acol, r, rn, al, at, c0, s) /* get interpolated ambient value */ |
353 |
|
COLOR acol; |
354 |
|
register RAY *r; |
355 |
+ |
FVECT rn; |
356 |
|
int al; |
357 |
|
AMBTREE *at; |
358 |
|
FVECT c0; |
364 |
|
int i; |
365 |
|
register int j; |
366 |
|
register AMBVAL *av; |
367 |
< |
/* do this node */ |
367 |
> |
|
368 |
|
wsum = 0.0; |
369 |
+ |
/* do this node */ |
370 |
|
for (av = at->alist; av != NULL; av = av->next) { |
371 |
+ |
double rn_dot = -2.0; |
372 |
|
if (tracktime) |
373 |
< |
av->latick = ambclock++; |
373 |
> |
av->latick = ambclock; |
374 |
|
/* |
375 |
|
* Ambient level test. |
376 |
|
*/ |
381 |
|
/* |
382 |
|
* Ambient radius test. |
383 |
|
*/ |
384 |
< |
e1 = 0.0; |
385 |
< |
for (j = 0; j < 3; j++) { |
386 |
< |
d = av->pos[j] - r->rop[j]; |
387 |
< |
e1 += d * d; |
388 |
< |
} |
384 |
> |
d = av->pos[0] - r->rop[0]; |
385 |
> |
e1 = d * d; |
386 |
> |
d = av->pos[1] - r->rop[1]; |
387 |
> |
e1 += d * d; |
388 |
> |
d = av->pos[2] - r->rop[2]; |
389 |
> |
e1 += d * d; |
390 |
|
e1 /= av->rad * av->rad; |
391 |
|
if (e1 > ambacc*ambacc*1.21) |
392 |
|
continue; |
393 |
|
/* |
394 |
< |
* Normal direction test. |
394 |
> |
* Direction test using closest normal. |
395 |
|
*/ |
396 |
< |
e2 = (1.0 - DOT(av->dir, r->ron)) * r->rweight; |
396 |
> |
d = DOT(av->dir, r->ron); |
397 |
> |
if (rn != r->ron) { |
398 |
> |
rn_dot = DOT(av->dir, rn); |
399 |
> |
if (rn_dot > 1.0-FTINY) |
400 |
> |
rn_dot = 1.0-FTINY; |
401 |
> |
if (rn_dot >= d-FTINY) { |
402 |
> |
d = rn_dot; |
403 |
> |
rn_dot = -2.0; |
404 |
> |
} |
405 |
> |
} |
406 |
> |
e2 = (1.0 - d) * r->rweight; |
407 |
|
if (e2 < 0.0) e2 = 0.0; |
408 |
|
if (e1 + e2 > ambacc*ambacc*1.21) |
409 |
|
continue; |
419 |
|
/* |
420 |
|
* Jittering final test reduces image artifacts. |
421 |
|
*/ |
422 |
< |
wt = sqrt(e1) + sqrt(e2); |
423 |
< |
wt *= .9 + .2*urand(9015+samplendx); |
424 |
< |
if (wt > ambacc) |
422 |
> |
e1 = sqrt(e1); |
423 |
> |
e2 = sqrt(e2); |
424 |
> |
wt = e1 + e2; |
425 |
> |
if (wt > ambacc*(.9+.2*urand(9015+samplendx))) |
426 |
|
continue; |
427 |
+ |
/* |
428 |
+ |
* Recompute directional error using perturbed normal |
429 |
+ |
*/ |
430 |
+ |
if (rn_dot > 0.0) { |
431 |
+ |
e2 = sqrt((1.0 - rn_dot)*r->rweight); |
432 |
+ |
wt = e1 + e2; |
433 |
+ |
} |
434 |
|
if (wt <= 1e-3) |
435 |
|
wt = 1e3; |
436 |
|
else |
437 |
|
wt = 1.0 / wt; |
438 |
|
wsum += wt; |
439 |
< |
extambient(ct, av, r->rop, r->ron); |
439 |
> |
extambient(ct, av, r->rop, rn); |
440 |
|
scalecolor(ct, wt); |
441 |
|
addcolor(acol, ct); |
442 |
|
} |
455 |
|
break; |
456 |
|
} |
457 |
|
if (j == 3) |
458 |
< |
wsum += sumambient(acol, r, al, at->kid+i, ck0, s); |
458 |
> |
wsum += sumambient(acol, r, rn, al, at->kid+i, ck0, s); |
459 |
|
} |
460 |
|
return(wsum); |
461 |
|
} |
462 |
|
|
463 |
|
|
464 |
|
double |
465 |
< |
makeambient(acol, r, al) /* make a new ambient value */ |
465 |
> |
makeambient(acol, r, rn, al) /* make a new ambient value */ |
466 |
|
COLOR acol; |
467 |
|
register RAY *r; |
468 |
+ |
FVECT rn; |
469 |
|
int al; |
470 |
|
{ |
471 |
|
AMBVAL amb; |
472 |
|
FVECT gp, gd; |
473 |
|
/* compute weight */ |
474 |
|
amb.weight = pow(AVGREFL, (double)al); |
475 |
< |
if (r->rweight < 0.2*amb.weight) /* heuristic */ |
475 |
> |
if (r->rweight < 0.1*amb.weight) /* heuristic */ |
476 |
|
amb.weight = r->rweight; |
477 |
|
/* compute ambient */ |
478 |
|
amb.rad = doambient(acol, r, amb.weight, gp, gd); |
479 |
< |
if (amb.rad == 0.0) |
479 |
> |
if (amb.rad <= FTINY) |
480 |
|
return(0.0); |
481 |
|
/* store it */ |
482 |
|
VCOPY(amb.pos, r->rop); |
487 |
|
VCOPY(amb.gdir, gd); |
488 |
|
/* insert into tree */ |
489 |
|
avsave(&amb); /* and save to file */ |
490 |
+ |
if (rn != r->ron) |
491 |
+ |
extambient(acol, &amb, r->rop, rn); /* texture */ |
492 |
|
return(amb.rad); |
493 |
|
} |
494 |
|
|
495 |
|
|
496 |
+ |
void |
497 |
|
extambient(cr, ap, pv, nv) /* extrapolate value at pv, nv */ |
498 |
|
COLOR cr; |
499 |
|
register AMBVAL *ap; |
500 |
|
FVECT pv, nv; |
501 |
|
{ |
502 |
< |
FVECT v1, v2; |
502 |
> |
FVECT v1; |
503 |
|
register int i; |
504 |
|
double d; |
505 |
|
|
508 |
|
for (i = 0; i < 3; i++) |
509 |
|
d += ap->gpos[i]*(pv[i]-ap->pos[i]); |
510 |
|
/* gradient due to rotation */ |
511 |
< |
VCOPY(v1, ap->dir); |
512 |
< |
fcross(v2, v1, nv); |
352 |
< |
d += DOT(ap->gdir, v2); |
511 |
> |
VCROSS(v1, ap->dir, nv); |
512 |
> |
d += DOT(ap->gdir, v1); |
513 |
|
if (d <= 0.0) { |
514 |
|
setcolor(cr, 0.0, 0.0, 0.0); |
515 |
|
return; |
519 |
|
} |
520 |
|
|
521 |
|
|
522 |
< |
static |
522 |
> |
static void |
523 |
|
initambfile(creat) /* initialize ambient file */ |
524 |
|
int creat; |
525 |
|
{ |
526 |
< |
extern char *progname, *octname, VersionID[]; |
526 |
> |
extern char *progname, *octname; |
527 |
> |
static char *mybuf = NULL; |
528 |
|
|
529 |
|
#ifdef F_SETLKW |
530 |
|
aflock(creat ? F_WRLCK : F_RDLCK); |
532 |
|
#ifdef MSDOS |
533 |
|
setmode(fileno(ambfp), O_BINARY); |
534 |
|
#endif |
535 |
< |
setbuf(ambfp, bmalloc(BUFSIZ+8)); |
535 |
> |
if (mybuf == NULL) |
536 |
> |
mybuf = (char *)bmalloc(BUFSIZ+8); |
537 |
> |
setbuf(ambfp, mybuf); |
538 |
|
if (creat) { /* new file */ |
539 |
|
newheader("RADIANCE", ambfp); |
540 |
< |
fprintf(ambfp, "%s -av %g %g %g -ab %d -aa %g ", |
540 |
> |
fprintf(ambfp, "%s -av %g %g %g -aw %d -ab %d -aa %g ", |
541 |
|
progname, colval(ambval,RED), |
542 |
|
colval(ambval,GRN), colval(ambval,BLU), |
543 |
< |
ambounce, ambacc); |
544 |
< |
fprintf(ambfp, "-ad %d -as %d -ar %d %s\n", |
545 |
< |
ambdiv, ambssamp, ambres, |
546 |
< |
octname==NULL ? "" : octname); |
543 |
> |
ambvwt, ambounce, ambacc); |
544 |
> |
fprintf(ambfp, "-ad %d -as %d -ar %d ", |
545 |
> |
ambdiv, ambssamp, ambres); |
546 |
> |
if (octname != NULL) |
547 |
> |
printargs(1, &octname, ambfp); |
548 |
> |
else |
549 |
> |
fputc('\n', ambfp); |
550 |
|
fprintf(ambfp, "SOFTWARE= %s\n", VersionID); |
551 |
+ |
fputnow(ambfp); |
552 |
|
fputformat(AMBFMT, ambfp); |
553 |
|
putc('\n', ambfp); |
554 |
|
putambmagic(ambfp); |
557 |
|
} |
558 |
|
|
559 |
|
|
560 |
< |
static |
560 |
> |
static void |
561 |
|
avsave(av) /* insert and save an ambient value */ |
562 |
|
AMBVAL *av; |
563 |
|
{ |
571 |
|
goto writerr; |
572 |
|
return; |
573 |
|
writerr: |
574 |
< |
error(SYSTEM, "error writing ambient file"); |
574 |
> |
error(SYSTEM, "error writing to ambient file"); |
575 |
|
} |
576 |
|
|
577 |
|
|
580 |
|
register AMBVAL *aval; |
581 |
|
{ |
582 |
|
register AMBVAL *av; |
583 |
+ |
double d; |
584 |
|
|
585 |
|
if ((av = newambval()) == NULL) |
586 |
|
error(SYSTEM, "out of memory in avstore"); |
588 |
|
av->latick = ambclock; |
589 |
|
av->next = NULL; |
590 |
|
nambvals++; |
591 |
+ |
d = bright(av->val); |
592 |
+ |
if (d > FTINY) { /* add to log sum for averaging */ |
593 |
+ |
avsum += log(d); |
594 |
+ |
navsum++; |
595 |
+ |
} |
596 |
|
return(av); |
597 |
|
} |
598 |
|
|
602 |
|
static AMBTREE *atfreelist = NULL; /* free ambient tree structures */ |
603 |
|
|
604 |
|
|
605 |
< |
static |
433 |
< |
AMBTREE * |
605 |
> |
static AMBTREE * |
606 |
|
newambtree() /* allocate 8 ambient tree structs */ |
607 |
|
{ |
608 |
|
register AMBTREE *atp, *upperlim; |
609 |
|
|
610 |
|
if (atfreelist == NULL) { /* get more nodes */ |
611 |
< |
atfreelist = (AMBTREE *)bmalloc(ATALLOCSZ*8*sizeof(AMBTREE)); |
611 |
> |
atfreelist = (AMBTREE *)malloc(ATALLOCSZ*8*sizeof(AMBTREE)); |
612 |
|
if (atfreelist == NULL) |
613 |
|
return(NULL); |
614 |
|
/* link new free list */ |
624 |
|
} |
625 |
|
|
626 |
|
|
627 |
< |
static |
627 |
> |
static void |
628 |
|
freeambtree(atp) /* free 8 ambient tree structs */ |
629 |
|
AMBTREE *atp; |
630 |
|
{ |
633 |
|
} |
634 |
|
|
635 |
|
|
636 |
< |
static |
636 |
> |
static void |
637 |
|
avinsert(av) /* insert ambient value in our tree */ |
638 |
|
register AMBVAL *av; |
639 |
|
{ |
673 |
|
} |
674 |
|
|
675 |
|
|
676 |
< |
static |
676 |
> |
static void |
677 |
|
unloadatree(at, f) /* unload an ambient value tree */ |
678 |
|
register AMBTREE *at; |
679 |
< |
int (*f)(); |
679 |
> |
void (*f)(); |
680 |
|
{ |
681 |
|
register AMBVAL *av; |
682 |
|
register int i; |
694 |
|
} |
695 |
|
|
696 |
|
|
697 |
< |
static AMBVAL **avlist1, **avlist2; /* ambient value lists for sorting */ |
697 |
> |
static struct avl { |
698 |
> |
AMBVAL *p; |
699 |
> |
unsigned long t; |
700 |
> |
} *avlist1; /* ambient value list with ticks */ |
701 |
> |
static AMBVAL **avlist2; /* memory positions for sorting */ |
702 |
|
static int i_avlist; /* index for lists */ |
703 |
|
|
704 |
|
|
705 |
< |
static |
705 |
> |
static int |
706 |
|
av2list(av) |
707 |
< |
AMBVAL *av; |
707 |
> |
register AMBVAL *av; |
708 |
|
{ |
709 |
+ |
#ifdef DEBUG |
710 |
|
if (i_avlist >= nambvals) |
711 |
|
error(CONSISTENCY, "too many ambient values in av2list1"); |
712 |
< |
avlist1[i_avlist] = avlist2[i_avlist] = av; |
713 |
< |
i_avlist++; |
712 |
> |
#endif |
713 |
> |
avlist1[i_avlist].p = avlist2[i_avlist] = av; |
714 |
> |
avlist1[i_avlist++].t = av->latick; |
715 |
|
} |
716 |
|
|
717 |
|
|
718 |
|
static int |
719 |
< |
alatcmp(avp1, avp2) /* compare ambient values for MRA */ |
720 |
< |
AMBVAL **avp1, **avp2; |
719 |
> |
alatcmp(av1, av2) /* compare ambient values for MRA */ |
720 |
> |
struct avl *av1, *av2; |
721 |
|
{ |
722 |
< |
return((**avp2).latick - (**avp1).latick); |
722 |
> |
register long lc = av2->t - av1->t; |
723 |
> |
return(lc<0 ? -1 : lc>0 ? 1 : 0); |
724 |
|
} |
725 |
|
|
726 |
|
|
727 |
+ |
/* GW NOTE 2002/10/3: |
728 |
+ |
* I used to compare AMBVAL pointers, but found that this was the |
729 |
+ |
* cause of a serious consistency error with gcc, since the optimizer |
730 |
+ |
* uses some dangerous trick in pointer subtraction that |
731 |
+ |
* assumes pointers differ by exact struct size increments. |
732 |
+ |
*/ |
733 |
|
static int |
734 |
|
aposcmp(avp1, avp2) /* compare ambient value positions */ |
735 |
< |
AMBVAL **avp1, **avp2; |
735 |
> |
const void *avp1, *avp2; |
736 |
|
{ |
737 |
< |
return(*avp1 - *avp2); |
737 |
> |
register long diff = *(char * const *)avp1 - *(char * const *)avp2; |
738 |
> |
if (diff < 0) |
739 |
> |
return(-1); |
740 |
> |
return(diff > 0); |
741 |
|
} |
742 |
|
|
743 |
+ |
#if 1 |
744 |
+ |
static int |
745 |
+ |
avlmemi(avaddr) /* find list position from address */ |
746 |
+ |
AMBVAL *avaddr; |
747 |
+ |
{ |
748 |
+ |
register AMBVAL **avlpp; |
749 |
|
|
750 |
< |
static |
750 |
> |
avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2, |
751 |
> |
nambvals, sizeof(AMBVAL *), aposcmp); |
752 |
> |
if (avlpp == NULL) |
753 |
> |
error(CONSISTENCY, "address not found in avlmemi"); |
754 |
> |
return(avlpp - avlist2); |
755 |
> |
} |
756 |
> |
#else |
757 |
> |
#define avlmemi(avaddr) ((AMBVAL **)bsearch((char *)&avaddr,(char *)avlist2, \ |
758 |
> |
nambvals,sizeof(AMBVAL *),aposcmp) - avlist2) |
759 |
> |
#endif |
760 |
> |
|
761 |
> |
|
762 |
> |
static void |
763 |
|
sortambvals(always) /* resort ambient values */ |
764 |
|
int always; |
765 |
|
{ |
766 |
|
AMBTREE oldatrunk; |
767 |
|
AMBVAL tav, *tap, *pnext; |
768 |
|
register int i, j; |
769 |
+ |
/* see if it's time yet */ |
770 |
+ |
if (!always && (ambclock++ < lastsort+sortintvl || |
771 |
+ |
nambvals < SORT_THRESH)) |
772 |
+ |
return; |
773 |
|
/* |
774 |
|
* The idea here is to minimize memory thrashing |
775 |
|
* in VM systems by improving reference locality. |
785 |
|
* the "always" parameter set to 1 so that the ambient |
786 |
|
* tree will be rebuilt with the new accuracy parameter. |
787 |
|
*/ |
788 |
< |
if (tracktime) { |
579 |
< |
avlist1 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *)); |
788 |
> |
if (tracktime) { /* allocate pointer arrays to sort */ |
789 |
|
avlist2 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *)); |
790 |
< |
} else |
791 |
< |
avlist1 = avlist2 = NULL; |
792 |
< |
if (avlist2 == NULL) { /* rebuild tree? */ |
793 |
< |
if (avlist1 != NULL) |
794 |
< |
free((char *)avlist1); |
795 |
< |
if (!always) |
796 |
< |
return; |
797 |
< |
copystruct(&oldatrunk, &atrunk); |
798 |
< |
atrunk.alist = NULL; |
799 |
< |
atrunk.kid = NULL; |
800 |
< |
unloadatree(&oldatrunk, avinsert); |
790 |
> |
avlist1 = (struct avl *)malloc(nambvals*sizeof(struct avl)); |
791 |
> |
} else { |
792 |
> |
avlist2 = NULL; |
793 |
> |
avlist1 = NULL; |
794 |
> |
} |
795 |
> |
if (avlist1 == NULL) { /* no time tracking -- rebuild tree? */ |
796 |
> |
if (avlist2 != NULL) |
797 |
> |
free((void *)avlist2); |
798 |
> |
if (always) { /* rebuild without sorting */ |
799 |
> |
copystruct(&oldatrunk, &atrunk); |
800 |
> |
atrunk.alist = NULL; |
801 |
> |
atrunk.kid = NULL; |
802 |
> |
unloadatree(&oldatrunk, avinsert); |
803 |
> |
} |
804 |
|
} else { /* sort memory by last access time */ |
593 |
– |
i_avlist = 0; |
594 |
– |
unloadatree(&atrunk, av2list); /* empty current tree */ |
805 |
|
/* |
806 |
|
* Sorting memory is tricky because it isn't contiguous. |
807 |
|
* We have to sort an array of pointers by MRA and also |
808 |
|
* by memory position. We then copy values in "loops" |
809 |
|
* to minimize memory hits. Nevertheless, we will visit |
810 |
< |
* everyone at least once, and this is an expensive process |
810 |
> |
* everyone at least twice, and this is an expensive process |
811 |
|
* when we're thrashing, which is when we need to do it. |
812 |
|
*/ |
813 |
< |
qsort((char *)avlist1, nambvals, sizeof(AMBVAL *), alatcmp); |
813 |
> |
#ifdef DEBUG |
814 |
> |
sprintf(errmsg, "sorting %u ambient values at ambclock=%lu...", |
815 |
> |
nambvals, ambclock); |
816 |
> |
eputs(errmsg); |
817 |
> |
#endif |
818 |
> |
i_avlist = 0; |
819 |
> |
unloadatree(&atrunk, av2list); /* empty current tree */ |
820 |
> |
#ifdef DEBUG |
821 |
> |
if (i_avlist < nambvals) |
822 |
> |
error(CONSISTENCY, "missing ambient values in sortambvals"); |
823 |
> |
#endif |
824 |
> |
qsort((char *)avlist1, nambvals, sizeof(struct avl), alatcmp); |
825 |
|
qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp); |
826 |
|
for (i = 0; i < nambvals; i++) { |
827 |
< |
if (avlist1[i] == NULL || avlist1[i] == avlist2[i]) |
827 |
> |
if (avlist1[i].p == NULL) |
828 |
|
continue; |
829 |
|
tap = avlist2[i]; |
830 |
|
copystruct(&tav, tap); |
831 |
< |
for (j = i; (pnext = avlist1[j]) != tap; |
832 |
< |
j = (AMBVAL **)bsearch((char *)&pnext, |
612 |
< |
(char *)(avlist2+i),nambvals-i, |
613 |
< |
sizeof(AMBVAL *),aposcmp) - |
614 |
< |
avlist2) { |
831 |
> |
for (j = i; (pnext = avlist1[j].p) != tap; |
832 |
> |
j = avlmemi(pnext)) { |
833 |
|
copystruct(avlist2[j], pnext); |
834 |
|
avinsert(avlist2[j]); |
835 |
< |
avlist1[j] = NULL; |
835 |
> |
avlist1[j].p = NULL; |
836 |
|
} |
837 |
|
copystruct(avlist2[j], &tav); |
838 |
|
avinsert(avlist2[j]); |
839 |
< |
avlist1[j] = NULL; |
839 |
> |
avlist1[j].p = NULL; |
840 |
|
} |
841 |
< |
free((char *)avlist1); |
842 |
< |
free((char *)avlist2); |
843 |
< |
if (sortintvl < MAXACLOCK/4) |
841 |
> |
free((void *)avlist1); |
842 |
> |
free((void *)avlist2); |
843 |
> |
/* compute new sort interval */ |
844 |
> |
sortintvl = ambclock - lastsort; |
845 |
> |
if (sortintvl >= MAX_SORT_INTVL/2) |
846 |
> |
sortintvl = MAX_SORT_INTVL; |
847 |
> |
else |
848 |
|
sortintvl <<= 1; /* wait twice as long next */ |
849 |
+ |
#ifdef DEBUG |
850 |
+ |
eputs("done\n"); |
851 |
+ |
#endif |
852 |
|
} |
853 |
|
if (ambclock >= MAXACLOCK) |
854 |
|
ambclock = MAXACLOCK/2; |
858 |
|
|
859 |
|
#ifdef F_SETLKW |
860 |
|
|
861 |
< |
static |
861 |
> |
static void |
862 |
|
aflock(typ) /* lock/unlock ambient file */ |
863 |
|
int typ; |
864 |
|
{ |
873 |
|
int |
874 |
|
ambsync() /* synchronize ambient file */ |
875 |
|
{ |
651 |
– |
static FILE *ambinp = NULL; |
652 |
– |
static long lastpos = -1; |
876 |
|
long flen; |
877 |
|
AMBVAL avs; |
878 |
|
register int n; |
884 |
|
/* gain exclusive access */ |
885 |
|
aflock(F_WRLCK); |
886 |
|
/* see if file has grown */ |
887 |
< |
if ((flen = lseek(fileno(ambfp), 0L, 2)) < 0) |
887 |
> |
if ((flen = lseek(fileno(ambfp), (off_t)0L, 2)) < 0) |
888 |
|
goto seekerr; |
889 |
|
if (n = flen - lastpos) { /* file has grown */ |
890 |
|
if (ambinp == NULL) { /* use duplicate filedes */ |
895 |
|
if (fseek(ambinp, lastpos, 0) < 0) |
896 |
|
goto seekerr; |
897 |
|
while (n >= AMBVALSIZ) { /* load contributed values */ |
898 |
< |
readambval(&avs, ambinp); |
898 |
> |
if (!readambval(&avs, ambinp)) { |
899 |
> |
sprintf(errmsg, |
900 |
> |
"ambient file \"%s\" corrupted near character %ld", |
901 |
> |
ambfile, flen - n); |
902 |
> |
error(WARNING, errmsg); |
903 |
> |
break; |
904 |
> |
} |
905 |
|
avinsert(avstore(&avs)); |
906 |
|
n -= AMBVALSIZ; |
907 |
|
} |
908 |
|
/*** seek always as safety measure |
909 |
|
if (n) ***/ /* alignment */ |
910 |
< |
if (lseek(fileno(ambfp), flen-n, 0) < 0) |
910 |
> |
if (lseek(fileno(ambfp), (off_t)(flen-n), 0) < 0) |
911 |
|
goto seekerr; |
912 |
|
} |
913 |
|
#ifdef DEBUG |
920 |
|
#endif |
921 |
|
syncend: |
922 |
|
n = fflush(ambfp); /* calls write() at last */ |
923 |
< |
if ((lastpos = lseek(fileno(ambfp), 0L, 1)) < 0) |
923 |
> |
if ((lastpos = lseek(fileno(ambfp), (off_t)0L, 1)) < 0) |
924 |
|
goto seekerr; |
925 |
|
aflock(F_UNLCK); /* release file */ |
926 |
|
nunflshed = 0; |