1 |
– |
#ifndef lint |
1 |
|
static const char RCSid[] = "$Id$"; |
3 |
– |
#endif |
2 |
|
/* |
3 |
|
* ambient.c - routines dealing with ambient (inter-reflected) component. |
4 |
|
* |
7 |
|
|
8 |
|
#include "copyright.h" |
9 |
|
|
10 |
+ |
#include <string.h> |
11 |
+ |
|
12 |
|
#include "platform.h" |
13 |
|
#include "ray.h" |
14 |
|
#include "otypes.h" |
15 |
+ |
#include "resolu.h" |
16 |
|
#include "ambient.h" |
17 |
|
#include "random.h" |
18 |
+ |
#include "pmapamb.h" |
19 |
|
|
20 |
|
#ifndef OCTSCALE |
21 |
|
#define OCTSCALE 1.0 /* ceil((valid rad.)/(cube size)) */ |
23 |
|
|
24 |
|
extern char *shm_boundary; /* memory sharing boundary */ |
25 |
|
|
26 |
< |
#define MAXASET 511 /* maximum number of elements in ambient set */ |
26 |
> |
#ifndef MAXASET |
27 |
> |
#define MAXASET 4095 /* maximum number of elements in ambient set */ |
28 |
> |
#endif |
29 |
|
OBJECT ambset[MAXASET+1]={0}; /* ambient include/exclude set */ |
30 |
|
|
31 |
|
double maxarad; /* maximum ambient radius */ |
38 |
|
|
39 |
|
#ifndef SORT_THRESH |
40 |
|
#ifdef SMLMEM |
41 |
< |
#define SORT_THRESH ((3L<<20)/sizeof(AMBVAL)) |
41 |
> |
#define SORT_THRESH ((16L<<20)/sizeof(AMBVAL)) |
42 |
|
#else |
43 |
< |
#define SORT_THRESH ((9L<<20)/sizeof(AMBVAL)) |
43 |
> |
#define SORT_THRESH ((64L<<20)/sizeof(AMBVAL)) |
44 |
|
#endif |
45 |
|
#endif |
46 |
|
#ifndef SORT_INTVL |
50 |
|
#define MAX_SORT_INTVL (SORT_INTVL<<6) |
51 |
|
#endif |
52 |
|
|
53 |
+ |
|
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 */ |
78 |
|
#define newambval() (AMBVAL *)malloc(sizeof(AMBVAL)) |
79 |
|
#define freeav(av) free((void *)av); |
80 |
|
|
81 |
< |
static void initambfile(), avsave(), avinsert(), sortambvals(), unloadatree(); |
82 |
< |
static int avlmemi(); |
83 |
< |
static AMBVAL *avstore(); |
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(AMBVAL *); |
88 |
> |
static unloadtf_t avinsert; |
89 |
> |
static unloadtf_t av2list; |
90 |
> |
static unloadtf_t avfree; |
91 |
> |
static void unloadatree(AMBTREE *at, unloadtf_t *f); |
92 |
> |
|
93 |
> |
static int aposcmp(const void *avp1, const void *avp2); |
94 |
> |
static int avlmemi(AMBVAL *avaddr); |
95 |
> |
static void sortambvals(int always); |
96 |
> |
|
97 |
|
#ifdef F_SETLKW |
98 |
< |
static void aflock(); |
98 |
> |
static void aflock(int typ); |
99 |
|
#endif |
100 |
|
|
101 |
|
|
102 |
|
void |
103 |
< |
setambres(ar) /* set ambient resolution */ |
104 |
< |
int ar; |
103 |
> |
setambres( /* set ambient resolution */ |
104 |
> |
int ar |
105 |
> |
) |
106 |
|
{ |
107 |
|
ambres = ar < 0 ? 0 : ar; /* may be done already */ |
108 |
|
/* set min & max radii */ |
109 |
|
if (ar <= 0) { |
110 |
|
minarad = 0; |
111 |
< |
maxarad = thescene.cusize / 2.0; |
111 |
> |
maxarad = thescene.cusize*0.2; |
112 |
|
} else { |
113 |
|
minarad = thescene.cusize / ar; |
114 |
< |
maxarad = 64 * minarad; /* heuristic */ |
115 |
< |
if (maxarad > thescene.cusize / 2.0) |
116 |
< |
maxarad = thescene.cusize / 2.0; |
114 |
> |
maxarad = 64.0 * minarad; /* heuristic */ |
115 |
> |
if (maxarad > thescene.cusize*0.2) |
116 |
> |
maxarad = thescene.cusize*0.2; |
117 |
|
} |
118 |
|
if (minarad <= FTINY) |
119 |
< |
minarad = 10*FTINY; |
119 |
> |
minarad = 10.0*FTINY; |
120 |
|
if (maxarad <= minarad) |
121 |
< |
maxarad = 64 * minarad; |
121 |
> |
maxarad = 64.0 * minarad; |
122 |
|
} |
123 |
|
|
124 |
|
|
125 |
|
void |
126 |
< |
setambacc(newa) /* set ambient accuracy */ |
127 |
< |
double newa; |
126 |
> |
setambacc( /* set ambient accuracy */ |
127 |
> |
double newa |
128 |
> |
) |
129 |
|
{ |
130 |
< |
double ambdiff; |
131 |
< |
|
132 |
< |
if (newa < 0.0) |
133 |
< |
newa = 0.0; |
134 |
< |
ambdiff = fabs(newa - ambacc); |
135 |
< |
if (ambdiff >= .01 && (ambacc = newa) > FTINY && nambvals > 0) |
136 |
< |
sortambvals(1); /* rebuild tree */ |
130 |
> |
static double olda; /* remember previous setting here */ |
131 |
> |
|
132 |
> |
newa *= (newa > 0); |
133 |
> |
if (fabs(newa - olda) >= .05*(newa + olda)) { |
134 |
> |
ambacc = newa; |
135 |
> |
if (nambvals > 0) |
136 |
> |
sortambvals(1); /* rebuild tree */ |
137 |
> |
} |
138 |
|
} |
139 |
|
|
140 |
|
|
141 |
|
void |
142 |
< |
setambient() /* initialize calculation */ |
142 |
> |
setambient(void) /* initialize calculation */ |
143 |
|
{ |
144 |
|
int readonly = 0; |
145 |
< |
long pos, flen; |
145 |
> |
long flen; |
146 |
|
AMBVAL amb; |
147 |
|
/* make sure we're fresh */ |
148 |
|
ambdone(); |
162 |
|
readonly = (ambfp = fopen(ambfile, "r")) != NULL; |
163 |
|
if (ambfp != NULL) { |
164 |
|
initambfile(0); /* file exists */ |
165 |
< |
pos = ftell(ambfp); |
165 |
> |
lastpos = ftell(ambfp); |
166 |
|
while (readambval(&amb, ambfp)) |
167 |
< |
avinsert(avstore(&amb)); |
167 |
> |
avstore(&amb); |
168 |
|
nambshare = nambvals; /* share loaded values */ |
169 |
|
if (readonly) { |
170 |
|
sprintf(errmsg, |
176 |
|
return; /* avoid ambsync() */ |
177 |
|
} |
178 |
|
/* align file pointer */ |
179 |
< |
pos += (long)nambvals*AMBVALSIZ; |
180 |
< |
flen = lseek(fileno(ambfp), (off_t)0L, 2); |
181 |
< |
if (flen != pos) { |
179 |
> |
lastpos += (long)nambvals*AMBVALSIZ; |
180 |
> |
flen = lseek(fileno(ambfp), (off_t)0, SEEK_END); |
181 |
> |
if (flen != lastpos) { |
182 |
|
sprintf(errmsg, |
183 |
|
"ignoring last %ld values in ambient file (corrupted)", |
184 |
< |
(flen - pos)/AMBVALSIZ); |
184 |
> |
(flen - lastpos)/AMBVALSIZ); |
185 |
|
error(WARNING, errmsg); |
186 |
< |
fseek(ambfp, pos, 0); |
187 |
< |
ftruncate(fileno(ambfp), (off_t)pos); |
186 |
> |
fseek(ambfp, lastpos, SEEK_SET); |
187 |
> |
#ifndef _WIN32 /* XXX we need a replacement for that one */ |
188 |
> |
ftruncate(fileno(ambfp), (off_t)lastpos); |
189 |
> |
#endif |
190 |
|
} |
191 |
|
} else if ((ambfp = fopen(ambfile, "w+")) != NULL) { |
192 |
|
initambfile(1); /* else create new file */ |
193 |
+ |
fflush(ambfp); |
194 |
+ |
lastpos = ftell(ambfp); |
195 |
|
} else { |
196 |
|
sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile); |
197 |
|
error(SYSTEM, errmsg); |
198 |
|
} |
199 |
< |
nunflshed++; /* lie */ |
200 |
< |
ambsync(); |
199 |
> |
#ifdef F_SETLKW |
200 |
> |
aflock(F_UNLCK); /* release file */ |
201 |
> |
#endif |
202 |
|
} |
203 |
|
|
204 |
|
|
205 |
|
void |
206 |
< |
ambdone() /* close ambient file and free memory */ |
206 |
> |
ambdone(void) /* close ambient file and free memory */ |
207 |
|
{ |
208 |
|
if (ambfp != NULL) { /* close ambient file */ |
209 |
|
ambsync(); |
216 |
|
lastpos = -1; |
217 |
|
} |
218 |
|
/* free ambient tree */ |
219 |
< |
unloadatree(&atrunk, free); |
219 |
> |
unloadatree(&atrunk, avfree); |
220 |
|
/* reset state variables */ |
221 |
|
avsum = 0.; |
222 |
|
navsum = 0; |
229 |
|
|
230 |
|
|
231 |
|
void |
232 |
< |
ambnotify(obj) /* record new modifier */ |
233 |
< |
OBJECT obj; |
232 |
> |
ambnotify( /* record new modifier */ |
233 |
> |
OBJECT obj |
234 |
> |
) |
235 |
|
{ |
236 |
|
static int hitlimit = 0; |
237 |
< |
register OBJREC *o; |
238 |
< |
register char **amblp; |
237 |
> |
OBJREC *o; |
238 |
> |
char **amblp; |
239 |
|
|
240 |
|
if (obj == OVOID) { /* starting over */ |
241 |
|
ambset[0] = 0; |
257 |
|
} |
258 |
|
} |
259 |
|
|
260 |
+ |
/************ THE FOLLOWING ROUTINES DIFFER BETWEEN NEW & OLD ***************/ |
261 |
|
|
262 |
+ |
#ifndef OLDAMB |
263 |
+ |
|
264 |
+ |
#define tfunc(lwr, x, upr) (((x)-(lwr))/((upr)-(lwr))) |
265 |
+ |
|
266 |
+ |
static int plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang); |
267 |
+ |
static double sumambient(COLOR acol, RAY *r, FVECT rn, int al, |
268 |
+ |
AMBTREE *at, FVECT c0, double s); |
269 |
+ |
static int makeambient(COLOR acol, RAY *r, FVECT rn, int al); |
270 |
+ |
static int extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv, |
271 |
+ |
FVECT uvw[3]); |
272 |
+ |
|
273 |
|
void |
274 |
< |
ambient(acol, r, nrm) /* compute ambient component for ray */ |
275 |
< |
COLOR acol; |
276 |
< |
register RAY *r; |
277 |
< |
FVECT nrm; |
274 |
> |
multambient( /* compute ambient component & multiply by coef. */ |
275 |
> |
COLOR aval, |
276 |
> |
RAY *r, |
277 |
> |
FVECT nrm |
278 |
> |
) |
279 |
|
{ |
280 |
|
static int rdepth = 0; /* ambient recursion */ |
281 |
+ |
COLOR acol, caustic; |
282 |
+ |
int ok; |
283 |
|
double d, l; |
284 |
|
|
285 |
+ |
/* PMAP: Factor in ambient from photon map, if enabled and ray is |
286 |
+ |
* ambient. Return as all ambient components accounted for, else |
287 |
+ |
* continue. */ |
288 |
+ |
if (ambPmap(aval, r, rdepth)) |
289 |
+ |
return; |
290 |
+ |
|
291 |
+ |
/* PMAP: Factor in specular-diffuse ambient (caustics) from photon |
292 |
+ |
* map, if enabled and ray is primary, else caustic is zero. Continue |
293 |
+ |
* with RADIANCE ambient calculation */ |
294 |
+ |
copycolor(caustic, aval); |
295 |
+ |
ambPmapCaustic(caustic, r, rdepth); |
296 |
+ |
|
297 |
|
if (ambdiv <= 0) /* no ambient calculation */ |
298 |
|
goto dumbamb; |
299 |
|
/* check number of bounces */ |
305 |
|
goto dumbamb; |
306 |
|
|
307 |
|
if (ambacc <= FTINY) { /* no ambient storage */ |
308 |
+ |
copycolor(acol, aval); |
309 |
|
rdepth++; |
310 |
+ |
ok = doambient(acol, r, r->rweight, |
311 |
+ |
NULL, NULL, NULL, NULL, NULL); |
312 |
+ |
rdepth--; |
313 |
+ |
if (!ok) |
314 |
+ |
goto dumbamb; |
315 |
+ |
copycolor(aval, acol); |
316 |
+ |
|
317 |
+ |
/* PMAP: add in caustic */ |
318 |
+ |
addcolor(aval, caustic); |
319 |
+ |
return; |
320 |
+ |
} |
321 |
+ |
|
322 |
+ |
if (tracktime) /* sort to minimize thrashing */ |
323 |
+ |
sortambvals(0); |
324 |
+ |
/* interpolate ambient value */ |
325 |
+ |
setcolor(acol, 0.0, 0.0, 0.0); |
326 |
+ |
d = sumambient(acol, r, nrm, rdepth, |
327 |
+ |
&atrunk, thescene.cuorg, thescene.cusize); |
328 |
+ |
|
329 |
+ |
if (d > FTINY) { |
330 |
+ |
d = 1.0/d; |
331 |
+ |
scalecolor(acol, d); |
332 |
+ |
multcolor(aval, acol); |
333 |
+ |
|
334 |
+ |
/* PMAP: add in caustic */ |
335 |
+ |
addcolor(aval, caustic); |
336 |
+ |
return; |
337 |
+ |
} |
338 |
+ |
|
339 |
+ |
rdepth++; /* need to cache new value */ |
340 |
+ |
ok = makeambient(acol, r, nrm, rdepth-1); |
341 |
+ |
rdepth--; |
342 |
+ |
|
343 |
+ |
if (ok) { |
344 |
+ |
multcolor(aval, acol); /* computed new value */ |
345 |
+ |
|
346 |
+ |
/* PMAP: add in caustic */ |
347 |
+ |
addcolor(aval, caustic); |
348 |
+ |
return; |
349 |
+ |
} |
350 |
+ |
|
351 |
+ |
dumbamb: /* return global value */ |
352 |
+ |
if ((ambvwt <= 0) | (navsum == 0)) { |
353 |
+ |
multcolor(aval, ambval); |
354 |
+ |
|
355 |
+ |
/* PMAP: add in caustic */ |
356 |
+ |
addcolor(aval, caustic); |
357 |
+ |
return; |
358 |
+ |
} |
359 |
+ |
|
360 |
+ |
l = bright(ambval); /* average in computations */ |
361 |
+ |
if (l > FTINY) { |
362 |
+ |
d = (log(l)*(double)ambvwt + avsum) / |
363 |
+ |
(double)(ambvwt + navsum); |
364 |
+ |
d = exp(d) / l; |
365 |
+ |
scalecolor(aval, d); |
366 |
+ |
multcolor(aval, ambval); /* apply color of ambval */ |
367 |
+ |
} else { |
368 |
+ |
d = exp( avsum / (double)navsum ); |
369 |
+ |
scalecolor(aval, d); /* neutral color */ |
370 |
+ |
} |
371 |
+ |
} |
372 |
+ |
|
373 |
+ |
|
374 |
+ |
/* Plug a potential leak where ambient cache value is occluded */ |
375 |
+ |
static int |
376 |
+ |
plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang) |
377 |
+ |
{ |
378 |
+ |
const double cost70sq = 0.1169778; /* cos(70deg)^2 */ |
379 |
+ |
RAY rtst; |
380 |
+ |
FVECT vdif; |
381 |
+ |
double normdot, ndotd, nadotd; |
382 |
+ |
double a, b, c, t[2]; |
383 |
+ |
|
384 |
+ |
ang += 2.*PI*(ang < 0); /* check direction flags */ |
385 |
+ |
if ( !(ap->corral>>(int)(ang*(16./PI)) & 1) ) |
386 |
+ |
return(0); |
387 |
+ |
/* |
388 |
+ |
* Generate test ray, targeting 20 degrees above sample point plane |
389 |
+ |
* along surface normal from cache position. This should be high |
390 |
+ |
* enough to miss local geometry we don't really care about. |
391 |
+ |
*/ |
392 |
+ |
VSUB(vdif, ap->pos, r->rop); |
393 |
+ |
normdot = DOT(anorm, r->ron); |
394 |
+ |
ndotd = DOT(vdif, r->ron); |
395 |
+ |
nadotd = DOT(vdif, anorm); |
396 |
+ |
a = normdot*normdot - cost70sq; |
397 |
+ |
b = 2.0*(normdot*ndotd - nadotd*cost70sq); |
398 |
+ |
c = ndotd*ndotd - DOT(vdif,vdif)*cost70sq; |
399 |
+ |
if (quadratic(t, a, b, c) != 2) |
400 |
+ |
return(1); /* should rarely happen */ |
401 |
+ |
if (t[1] <= FTINY) |
402 |
+ |
return(0); /* should fail behind test */ |
403 |
+ |
rayorigin(&rtst, SHADOW, r, NULL); |
404 |
+ |
VSUM(rtst.rdir, vdif, anorm, t[1]); /* further dist. > plane */ |
405 |
+ |
rtst.rmax = normalize(rtst.rdir); /* short ray test */ |
406 |
+ |
while (localhit(&rtst, &thescene)) { /* check for occluder */ |
407 |
+ |
if (rtst.ro->omod != OVOID && |
408 |
+ |
(rtst.clipset == NULL || |
409 |
+ |
!inset(rtst.clipset, rtst.ro->omod))) |
410 |
+ |
return(1); /* plug light leak */ |
411 |
+ |
VCOPY(rtst.rorg, rtst.rop); /* skip invisible surface */ |
412 |
+ |
rtst.rmax -= rtst.rot; |
413 |
+ |
rayclear(&rtst); |
414 |
+ |
} |
415 |
+ |
return(0); /* seems we're OK */ |
416 |
+ |
} |
417 |
+ |
|
418 |
+ |
|
419 |
+ |
static double |
420 |
+ |
sumambient( /* get interpolated ambient value */ |
421 |
+ |
COLOR acol, |
422 |
+ |
RAY *r, |
423 |
+ |
FVECT rn, |
424 |
+ |
int al, |
425 |
+ |
AMBTREE *at, |
426 |
+ |
FVECT c0, |
427 |
+ |
double s |
428 |
+ |
) |
429 |
+ |
{ /* initial limit is 10 degrees plus ambacc radians */ |
430 |
+ |
const double minangle = 10.0 * PI/180.; |
431 |
+ |
double maxangle = minangle + ambacc; |
432 |
+ |
double wsum = 0.0; |
433 |
+ |
FVECT ck0; |
434 |
+ |
int i, j; |
435 |
+ |
AMBVAL *av; |
436 |
+ |
|
437 |
+ |
if (at->kid != NULL) { /* sum children first */ |
438 |
+ |
s *= 0.5; |
439 |
+ |
for (i = 0; i < 8; i++) { |
440 |
+ |
for (j = 0; j < 3; j++) { |
441 |
+ |
ck0[j] = c0[j]; |
442 |
+ |
if (1<<j & i) |
443 |
+ |
ck0[j] += s; |
444 |
+ |
if (r->rop[j] < ck0[j] - OCTSCALE*s) |
445 |
+ |
break; |
446 |
+ |
if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s) |
447 |
+ |
break; |
448 |
+ |
} |
449 |
+ |
if (j == 3) |
450 |
+ |
wsum += sumambient(acol, r, rn, al, |
451 |
+ |
at->kid+i, ck0, s); |
452 |
+ |
} |
453 |
+ |
/* good enough? */ |
454 |
+ |
if (wsum >= 0.05 && s > minarad*10.0) |
455 |
+ |
return(wsum); |
456 |
+ |
} |
457 |
+ |
/* adjust maximum angle */ |
458 |
+ |
if (at->alist != NULL && (at->alist->lvl <= al) & (r->rweight < 0.6)) |
459 |
+ |
maxangle = (maxangle - PI/2.)*pow(r->rweight,0.13) + PI/2.; |
460 |
+ |
/* sum this node */ |
461 |
+ |
for (av = at->alist; av != NULL; av = av->next) { |
462 |
+ |
double u, v, d, delta_r2, delta_t2; |
463 |
+ |
COLOR ct; |
464 |
+ |
FVECT uvw[3]; |
465 |
+ |
/* record access */ |
466 |
+ |
if (tracktime) |
467 |
+ |
av->latick = ambclock; |
468 |
+ |
/* |
469 |
+ |
* Ambient level test |
470 |
+ |
*/ |
471 |
+ |
if (av->lvl > al || /* list sorted, so this works */ |
472 |
+ |
(av->lvl == al) & (av->weight < 0.9*r->rweight)) |
473 |
+ |
break; |
474 |
+ |
/* |
475 |
+ |
* Direction test using unperturbed normal |
476 |
+ |
*/ |
477 |
+ |
decodedir(uvw[2], av->ndir); |
478 |
+ |
d = DOT(uvw[2], r->ron); |
479 |
+ |
if (d <= 0.0) /* >= 90 degrees */ |
480 |
+ |
continue; |
481 |
+ |
delta_r2 = 2.0 - 2.0*d; /* approx. radians^2 */ |
482 |
+ |
if (delta_r2 >= maxangle*maxangle) |
483 |
+ |
continue; |
484 |
+ |
/* |
485 |
+ |
* Modified ray behind test |
486 |
+ |
*/ |
487 |
+ |
VSUB(ck0, r->rop, av->pos); |
488 |
+ |
d = DOT(ck0, uvw[2]); |
489 |
+ |
if (d < -minarad*ambacc-.001) |
490 |
+ |
continue; |
491 |
+ |
d /= av->rad[0]; |
492 |
+ |
delta_t2 = d*d; |
493 |
+ |
if (delta_t2 >= ambacc*ambacc) |
494 |
+ |
continue; |
495 |
+ |
/* |
496 |
+ |
* Elliptical radii test based on Hessian |
497 |
+ |
*/ |
498 |
+ |
decodedir(uvw[0], av->udir); |
499 |
+ |
VCROSS(uvw[1], uvw[2], uvw[0]); |
500 |
+ |
d = (u = DOT(ck0, uvw[0])) / av->rad[0]; |
501 |
+ |
delta_t2 += d*d; |
502 |
+ |
d = (v = DOT(ck0, uvw[1])) / av->rad[1]; |
503 |
+ |
delta_t2 += d*d; |
504 |
+ |
if (delta_t2 >= ambacc*ambacc) |
505 |
+ |
continue; |
506 |
+ |
/* |
507 |
+ |
* Test for potential light leak |
508 |
+ |
*/ |
509 |
+ |
if (av->corral && plugaleak(r, av, uvw[2], atan2a(v,u))) |
510 |
+ |
continue; |
511 |
+ |
/* |
512 |
+ |
* Extrapolate value and compute final weight (hat function) |
513 |
+ |
*/ |
514 |
+ |
if (!extambient(ct, av, r->rop, rn, uvw)) |
515 |
+ |
continue; |
516 |
+ |
d = tfunc(maxangle, sqrt(delta_r2), 0.0) * |
517 |
+ |
tfunc(ambacc, sqrt(delta_t2), 0.0); |
518 |
+ |
scalecolor(ct, d); |
519 |
+ |
addcolor(acol, ct); |
520 |
+ |
wsum += d; |
521 |
+ |
} |
522 |
+ |
return(wsum); |
523 |
+ |
} |
524 |
+ |
|
525 |
+ |
|
526 |
+ |
static int |
527 |
+ |
makeambient( /* make a new ambient value for storage */ |
528 |
+ |
COLOR acol, |
529 |
+ |
RAY *r, |
530 |
+ |
FVECT rn, |
531 |
+ |
int al |
532 |
+ |
) |
533 |
+ |
{ |
534 |
+ |
AMBVAL amb; |
535 |
+ |
FVECT uvw[3]; |
536 |
+ |
int i; |
537 |
+ |
|
538 |
+ |
amb.weight = 1.0; /* compute weight */ |
539 |
+ |
for (i = al; i-- > 0; ) |
540 |
+ |
amb.weight *= AVGREFL; |
541 |
+ |
if (r->rweight < 0.1*amb.weight) /* heuristic override */ |
542 |
+ |
amb.weight = 1.25*r->rweight; |
543 |
+ |
setcolor(acol, AVGREFL, AVGREFL, AVGREFL); |
544 |
+ |
/* compute ambient */ |
545 |
+ |
i = doambient(acol, r, amb.weight, |
546 |
+ |
uvw, amb.rad, amb.gpos, amb.gdir, &amb.corral); |
547 |
+ |
scalecolor(acol, 1./AVGREFL); /* undo assumed reflectance */ |
548 |
+ |
if (i <= 0 || amb.rad[0] <= FTINY) /* no Hessian or zero radius */ |
549 |
+ |
return(i); |
550 |
+ |
/* store value */ |
551 |
+ |
VCOPY(amb.pos, r->rop); |
552 |
+ |
amb.ndir = encodedir(r->ron); |
553 |
+ |
amb.udir = encodedir(uvw[0]); |
554 |
+ |
amb.lvl = al; |
555 |
+ |
copycolor(amb.val, acol); |
556 |
+ |
/* insert into tree */ |
557 |
+ |
avsave(&amb); /* and save to file */ |
558 |
+ |
if (rn != r->ron) { /* texture */ |
559 |
+ |
VCOPY(uvw[2], r->ron); |
560 |
+ |
extambient(acol, &amb, r->rop, rn, uvw); |
561 |
+ |
} |
562 |
+ |
return(1); |
563 |
+ |
} |
564 |
+ |
|
565 |
+ |
|
566 |
+ |
static int |
567 |
+ |
extambient( /* extrapolate value at pv, nv */ |
568 |
+ |
COLOR cr, |
569 |
+ |
AMBVAL *ap, |
570 |
+ |
FVECT pv, |
571 |
+ |
FVECT nv, |
572 |
+ |
FVECT uvw[3] |
573 |
+ |
) |
574 |
+ |
{ |
575 |
+ |
const double min_d = 0.05; |
576 |
+ |
static FVECT my_uvw[3]; |
577 |
+ |
FVECT v1; |
578 |
+ |
int i; |
579 |
+ |
double d = 1.0; /* zeroeth order */ |
580 |
+ |
|
581 |
+ |
if (uvw == NULL) { /* need local coordinates? */ |
582 |
+ |
decodedir(my_uvw[2], ap->ndir); |
583 |
+ |
decodedir(my_uvw[0], ap->udir); |
584 |
+ |
VCROSS(my_uvw[1], my_uvw[2], my_uvw[0]); |
585 |
+ |
uvw = my_uvw; |
586 |
+ |
} |
587 |
+ |
for (i = 3; i--; ) /* gradient due to translation */ |
588 |
+ |
d += (pv[i] - ap->pos[i]) * |
589 |
+ |
(ap->gpos[0]*uvw[0][i] + ap->gpos[1]*uvw[1][i]); |
590 |
+ |
|
591 |
+ |
VCROSS(v1, uvw[2], nv); /* gradient due to rotation */ |
592 |
+ |
for (i = 3; i--; ) |
593 |
+ |
d += v1[i] * (ap->gdir[0]*uvw[0][i] + ap->gdir[1]*uvw[1][i]); |
594 |
+ |
|
595 |
+ |
if (d < min_d) /* should not use if we can avoid it */ |
596 |
+ |
d = min_d; |
597 |
+ |
copycolor(cr, ap->val); |
598 |
+ |
scalecolor(cr, d); |
599 |
+ |
return(d > min_d); |
600 |
+ |
} |
601 |
+ |
|
602 |
+ |
|
603 |
+ |
static void |
604 |
+ |
avinsert( /* insert ambient value in our tree */ |
605 |
+ |
AMBVAL *av |
606 |
+ |
) |
607 |
+ |
{ |
608 |
+ |
AMBTREE *at; |
609 |
+ |
AMBVAL *ap; |
610 |
+ |
AMBVAL avh; |
611 |
+ |
FVECT ck0; |
612 |
+ |
double s; |
613 |
+ |
int branch; |
614 |
+ |
int i; |
615 |
+ |
|
616 |
+ |
if (av->rad[0] <= FTINY) |
617 |
+ |
error(CONSISTENCY, "zero ambient radius in avinsert"); |
618 |
+ |
at = &atrunk; |
619 |
+ |
VCOPY(ck0, thescene.cuorg); |
620 |
+ |
s = thescene.cusize; |
621 |
+ |
while (s*(OCTSCALE/2) > av->rad[1]*ambacc) { |
622 |
+ |
if (at->kid == NULL) |
623 |
+ |
if ((at->kid = newambtree()) == NULL) |
624 |
+ |
error(SYSTEM, "out of memory in avinsert"); |
625 |
+ |
s *= 0.5; |
626 |
+ |
branch = 0; |
627 |
+ |
for (i = 0; i < 3; i++) |
628 |
+ |
if (av->pos[i] > ck0[i] + s) { |
629 |
+ |
ck0[i] += s; |
630 |
+ |
branch |= 1 << i; |
631 |
+ |
} |
632 |
+ |
at = at->kid + branch; |
633 |
+ |
} |
634 |
+ |
avh.next = at->alist; /* order by increasing level */ |
635 |
+ |
for (ap = &avh; ap->next != NULL; ap = ap->next) |
636 |
+ |
if ( ap->next->lvl > av->lvl || |
637 |
+ |
(ap->next->lvl == av->lvl) & |
638 |
+ |
(ap->next->weight <= av->weight) ) |
639 |
+ |
break; |
640 |
+ |
av->next = ap->next; |
641 |
+ |
ap->next = (AMBVAL*)av; |
642 |
+ |
at->alist = avh.next; |
643 |
+ |
} |
644 |
+ |
|
645 |
+ |
|
646 |
+ |
#else /* ! NEWAMB */ |
647 |
+ |
|
648 |
+ |
static double sumambient(COLOR acol, RAY *r, FVECT rn, int al, |
649 |
+ |
AMBTREE *at, FVECT c0, double s); |
650 |
+ |
static double makeambient(COLOR acol, RAY *r, FVECT rn, int al); |
651 |
+ |
static void extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv); |
652 |
+ |
|
653 |
+ |
|
654 |
+ |
void |
655 |
+ |
multambient( /* compute ambient component & multiply by coef. */ |
656 |
+ |
COLOR aval, |
657 |
+ |
RAY *r, |
658 |
+ |
FVECT nrm |
659 |
+ |
) |
660 |
+ |
{ |
661 |
+ |
static int rdepth = 0; /* ambient recursion */ |
662 |
+ |
COLOR acol, caustic; |
663 |
+ |
double d, l; |
664 |
+ |
|
665 |
+ |
/* PMAP: Factor in ambient from global photon map (if enabled) and return |
666 |
+ |
* as all ambient components accounted for */ |
667 |
+ |
if (ambPmap(aval, r, rdepth)) |
668 |
+ |
return; |
669 |
+ |
|
670 |
+ |
/* PMAP: Otherwise factor in ambient from caustic photon map |
671 |
+ |
* (ambPmapCaustic() returns zero if caustic photons disabled) and |
672 |
+ |
* continue with RADIANCE ambient calculation */ |
673 |
+ |
copycolor(caustic, aval); |
674 |
+ |
ambPmapCaustic(caustic, r, rdepth); |
675 |
+ |
|
676 |
+ |
if (ambdiv <= 0) /* no ambient calculation */ |
677 |
+ |
goto dumbamb; |
678 |
+ |
/* check number of bounces */ |
679 |
+ |
if (rdepth >= ambounce) |
680 |
+ |
goto dumbamb; |
681 |
+ |
/* check ambient list */ |
682 |
+ |
if (ambincl != -1 && r->ro != NULL && |
683 |
+ |
ambincl != inset(ambset, r->ro->omod)) |
684 |
+ |
goto dumbamb; |
685 |
+ |
|
686 |
+ |
if (ambacc <= FTINY) { /* no ambient storage */ |
687 |
+ |
copycolor(acol, aval); |
688 |
+ |
rdepth++; |
689 |
|
d = doambient(acol, r, r->rweight, NULL, NULL); |
690 |
|
rdepth--; |
691 |
|
if (d <= FTINY) |
692 |
|
goto dumbamb; |
693 |
+ |
copycolor(aval, acol); |
694 |
+ |
|
695 |
+ |
/* PMAP: add in caustic */ |
696 |
+ |
addcolor(aval, caustic); |
697 |
|
return; |
698 |
|
} |
699 |
|
|
700 |
|
if (tracktime) /* sort to minimize thrashing */ |
701 |
|
sortambvals(0); |
702 |
< |
/* get ambient value */ |
702 |
> |
/* interpolate ambient value */ |
703 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
704 |
|
d = sumambient(acol, r, nrm, rdepth, |
705 |
|
&atrunk, thescene.cuorg, thescene.cusize); |
706 |
+ |
|
707 |
|
if (d > FTINY) { |
708 |
< |
scalecolor(acol, 1.0/d); |
708 |
> |
d = 1.0/d; |
709 |
> |
scalecolor(acol, d); |
710 |
> |
multcolor(aval, acol); |
711 |
> |
|
712 |
> |
/* PMAP: add in caustic */ |
713 |
> |
addcolor(aval, caustic); |
714 |
|
return; |
715 |
|
} |
716 |
+ |
|
717 |
|
rdepth++; /* need to cache new value */ |
718 |
|
d = makeambient(acol, r, nrm, rdepth-1); |
719 |
|
rdepth--; |
720 |
< |
if (d > FTINY) |
720 |
> |
|
721 |
> |
if (d > FTINY) { |
722 |
> |
multcolor(aval, acol); /* got new value */ |
723 |
> |
|
724 |
> |
/* PMAP: add in caustic */ |
725 |
> |
addcolor(aval, caustic); |
726 |
|
return; |
727 |
+ |
} |
728 |
+ |
|
729 |
|
dumbamb: /* return global value */ |
730 |
< |
copycolor(acol, ambval); |
731 |
< |
if (ambvwt <= 0 | navsum == 0) |
730 |
> |
if ((ambvwt <= 0) | (navsum == 0)) { |
731 |
> |
multcolor(aval, ambval); |
732 |
> |
|
733 |
> |
/* PMAP: add in caustic */ |
734 |
> |
addcolor(aval, caustic); |
735 |
|
return; |
736 |
+ |
} |
737 |
+ |
|
738 |
|
l = bright(ambval); /* average in computations */ |
739 |
|
if (l > FTINY) { |
740 |
|
d = (log(l)*(double)ambvwt + avsum) / |
741 |
|
(double)(ambvwt + navsum); |
742 |
|
d = exp(d) / l; |
743 |
< |
scalecolor(acol, d); /* apply color of ambval */ |
743 |
> |
scalecolor(aval, d); |
744 |
> |
multcolor(aval, ambval); /* apply color of ambval */ |
745 |
|
} else { |
746 |
|
d = exp( avsum / (double)navsum ); |
747 |
< |
setcolor(acol, d, d, d); /* neutral color */ |
747 |
> |
scalecolor(aval, d); /* neutral color */ |
748 |
|
} |
749 |
|
} |
750 |
|
|
751 |
|
|
752 |
< |
double |
753 |
< |
sumambient(acol, r, rn, al, at, c0, s) /* get interpolated ambient value */ |
754 |
< |
COLOR acol; |
755 |
< |
register RAY *r; |
756 |
< |
FVECT rn; |
757 |
< |
int al; |
758 |
< |
AMBTREE *at; |
759 |
< |
FVECT c0; |
760 |
< |
double s; |
752 |
> |
static double |
753 |
> |
sumambient( /* get interpolated ambient value */ |
754 |
> |
COLOR acol, |
755 |
> |
RAY *r, |
756 |
> |
FVECT rn, |
757 |
> |
int al, |
758 |
> |
AMBTREE *at, |
759 |
> |
FVECT c0, |
760 |
> |
double s |
761 |
> |
) |
762 |
|
{ |
763 |
|
double d, e1, e2, wt, wsum; |
764 |
|
COLOR ct; |
765 |
|
FVECT ck0; |
766 |
|
int i; |
767 |
< |
register int j; |
768 |
< |
register AMBVAL *av; |
767 |
> |
int j; |
768 |
> |
AMBVAL *av; |
769 |
|
|
770 |
|
wsum = 0.0; |
771 |
|
/* do this node */ |
776 |
|
/* |
777 |
|
* Ambient level test. |
778 |
|
*/ |
779 |
< |
if (av->lvl > al) /* list sorted, so this works */ |
779 |
> |
if (av->lvl > al || /* list sorted, so this works */ |
780 |
> |
(av->lvl == al) & (av->weight < 0.9*r->rweight)) |
781 |
|
break; |
322 |
– |
if (av->weight < r->rweight-FTINY) |
323 |
– |
continue; |
782 |
|
/* |
783 |
|
* Ambient radius test. |
784 |
|
*/ |
785 |
< |
d = av->pos[0] - r->rop[0]; |
786 |
< |
e1 = d * d; |
329 |
< |
d = av->pos[1] - r->rop[1]; |
330 |
< |
e1 += d * d; |
331 |
< |
d = av->pos[2] - r->rop[2]; |
332 |
< |
e1 += d * d; |
333 |
< |
e1 /= av->rad * av->rad; |
785 |
> |
VSUB(ck0, av->pos, r->rop); |
786 |
> |
e1 = DOT(ck0, ck0) / (av->rad * av->rad); |
787 |
|
if (e1 > ambacc*ambacc*1.21) |
788 |
|
continue; |
789 |
|
/* |
800 |
|
} |
801 |
|
} |
802 |
|
e2 = (1.0 - d) * r->rweight; |
803 |
< |
if (e2 < 0.0) e2 = 0.0; |
804 |
< |
if (e1 + e2 > ambacc*ambacc*1.21) |
803 |
> |
if (e2 < 0.0) |
804 |
> |
e2 = 0.0; |
805 |
> |
else if (e1 + e2 > ambacc*ambacc*1.21) |
806 |
|
continue; |
807 |
|
/* |
808 |
|
* Ray behind test. |
852 |
|
break; |
853 |
|
} |
854 |
|
if (j == 3) |
855 |
< |
wsum += sumambient(acol, r, rn, al, at->kid+i, ck0, s); |
855 |
> |
wsum += sumambient(acol, r, rn, al, |
856 |
> |
at->kid+i, ck0, s); |
857 |
|
} |
858 |
|
return(wsum); |
859 |
|
} |
860 |
|
|
861 |
|
|
862 |
< |
double |
863 |
< |
makeambient(acol, r, rn, al) /* make a new ambient value */ |
864 |
< |
COLOR acol; |
865 |
< |
register RAY *r; |
866 |
< |
FVECT rn; |
867 |
< |
int al; |
862 |
> |
static double |
863 |
> |
makeambient( /* make a new ambient value for storage */ |
864 |
> |
COLOR acol, |
865 |
> |
RAY *r, |
866 |
> |
FVECT rn, |
867 |
> |
int al |
868 |
> |
) |
869 |
|
{ |
870 |
|
AMBVAL amb; |
871 |
|
FVECT gp, gd; |
872 |
< |
/* compute weight */ |
873 |
< |
amb.weight = pow(AVGREFL, (double)al); |
874 |
< |
if (r->rweight < 0.1*amb.weight) /* heuristic */ |
875 |
< |
amb.weight = r->rweight; |
872 |
> |
int i; |
873 |
> |
|
874 |
> |
amb.weight = 1.0; /* compute weight */ |
875 |
> |
for (i = al; i-- > 0; ) |
876 |
> |
amb.weight *= AVGREFL; |
877 |
> |
if (r->rweight < 0.1*amb.weight) /* heuristic override */ |
878 |
> |
amb.weight = 1.25*r->rweight; |
879 |
> |
setcolor(acol, AVGREFL, AVGREFL, AVGREFL); |
880 |
|
/* compute ambient */ |
881 |
|
amb.rad = doambient(acol, r, amb.weight, gp, gd); |
882 |
< |
if (amb.rad <= FTINY) |
882 |
> |
if (amb.rad <= FTINY) { |
883 |
> |
setcolor(acol, 0.0, 0.0, 0.0); |
884 |
|
return(0.0); |
885 |
< |
/* store it */ |
885 |
> |
} |
886 |
> |
scalecolor(acol, 1./AVGREFL); /* undo assumed reflectance */ |
887 |
> |
/* store value */ |
888 |
|
VCOPY(amb.pos, r->rop); |
889 |
|
VCOPY(amb.dir, r->ron); |
890 |
|
amb.lvl = al; |
899 |
|
} |
900 |
|
|
901 |
|
|
902 |
< |
void |
903 |
< |
extambient(cr, ap, pv, nv) /* extrapolate value at pv, nv */ |
904 |
< |
COLOR cr; |
905 |
< |
register AMBVAL *ap; |
906 |
< |
FVECT pv, nv; |
902 |
> |
static void |
903 |
> |
extambient( /* extrapolate value at pv, nv */ |
904 |
> |
COLOR cr, |
905 |
> |
AMBVAL *ap, |
906 |
> |
FVECT pv, |
907 |
> |
FVECT nv |
908 |
> |
) |
909 |
|
{ |
910 |
|
FVECT v1; |
911 |
< |
register int i; |
911 |
> |
int i; |
912 |
|
double d; |
913 |
|
|
914 |
|
d = 1.0; /* zeroeth order */ |
928 |
|
|
929 |
|
|
930 |
|
static void |
931 |
< |
initambfile(creat) /* initialize ambient file */ |
932 |
< |
int creat; |
931 |
> |
avinsert( /* insert ambient value in our tree */ |
932 |
> |
AMBVAL *av |
933 |
> |
) |
934 |
|
{ |
935 |
+ |
AMBTREE *at; |
936 |
+ |
AMBVAL *ap; |
937 |
+ |
AMBVAL avh; |
938 |
+ |
FVECT ck0; |
939 |
+ |
double s; |
940 |
+ |
int branch; |
941 |
+ |
int i; |
942 |
+ |
|
943 |
+ |
if (av->rad <= FTINY) |
944 |
+ |
error(CONSISTENCY, "zero ambient radius in avinsert"); |
945 |
+ |
at = &atrunk; |
946 |
+ |
VCOPY(ck0, thescene.cuorg); |
947 |
+ |
s = thescene.cusize; |
948 |
+ |
while (s*(OCTSCALE/2) > av->rad*ambacc) { |
949 |
+ |
if (at->kid == NULL) |
950 |
+ |
if ((at->kid = newambtree()) == NULL) |
951 |
+ |
error(SYSTEM, "out of memory in avinsert"); |
952 |
+ |
s *= 0.5; |
953 |
+ |
branch = 0; |
954 |
+ |
for (i = 0; i < 3; i++) |
955 |
+ |
if (av->pos[i] > ck0[i] + s) { |
956 |
+ |
ck0[i] += s; |
957 |
+ |
branch |= 1 << i; |
958 |
+ |
} |
959 |
+ |
at = at->kid + branch; |
960 |
+ |
} |
961 |
+ |
avh.next = at->alist; /* order by increasing level */ |
962 |
+ |
for (ap = &avh; ap->next != NULL; ap = ap->next) |
963 |
+ |
if ( ap->next->lvl > av->lvl || |
964 |
+ |
(ap->next->lvl == av->lvl) & |
965 |
+ |
(ap->next->weight <= av->weight) ) |
966 |
+ |
break; |
967 |
+ |
av->next = ap->next; |
968 |
+ |
ap->next = (AMBVAL*)av; |
969 |
+ |
at->alist = avh.next; |
970 |
+ |
} |
971 |
+ |
|
972 |
+ |
#endif /* ! NEWAMB */ |
973 |
+ |
|
974 |
+ |
/************* FOLLOWING ROUTINES SAME FOR NEW & OLD METHODS ***************/ |
975 |
+ |
|
976 |
+ |
static void |
977 |
+ |
initambfile( /* initialize ambient file */ |
978 |
+ |
int cre8 |
979 |
+ |
) |
980 |
+ |
{ |
981 |
|
extern char *progname, *octname; |
982 |
|
static char *mybuf = NULL; |
983 |
|
|
984 |
|
#ifdef F_SETLKW |
985 |
< |
aflock(creat ? F_WRLCK : F_RDLCK); |
985 |
> |
aflock(cre8 ? F_WRLCK : F_RDLCK); |
986 |
|
#endif |
987 |
|
SET_FILE_BINARY(ambfp); |
988 |
|
if (mybuf == NULL) |
989 |
|
mybuf = (char *)bmalloc(BUFSIZ+8); |
990 |
|
setbuf(ambfp, mybuf); |
991 |
< |
if (creat) { /* new file */ |
991 |
> |
if (cre8) { /* new file */ |
992 |
|
newheader("RADIANCE", ambfp); |
993 |
|
fprintf(ambfp, "%s -av %g %g %g -aw %d -ab %d -aa %g ", |
994 |
|
progname, colval(ambval,RED), |
997 |
|
fprintf(ambfp, "-ad %d -as %d -ar %d ", |
998 |
|
ambdiv, ambssamp, ambres); |
999 |
|
if (octname != NULL) |
1000 |
< |
printargs(1, &octname, ambfp); |
1001 |
< |
else |
490 |
< |
fputc('\n', ambfp); |
1000 |
> |
fputs(octname, ambfp); |
1001 |
> |
fputc('\n', ambfp); |
1002 |
|
fprintf(ambfp, "SOFTWARE= %s\n", VersionID); |
1003 |
|
fputnow(ambfp); |
1004 |
|
fputformat(AMBFMT, ambfp); |
1005 |
< |
putc('\n', ambfp); |
1005 |
> |
fputc('\n', ambfp); |
1006 |
|
putambmagic(ambfp); |
1007 |
|
} else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp)) |
1008 |
|
error(USER, "bad ambient file"); |
1010 |
|
|
1011 |
|
|
1012 |
|
static void |
1013 |
< |
avsave(av) /* insert and save an ambient value */ |
1014 |
< |
AMBVAL *av; |
1013 |
> |
avsave( /* insert and save an ambient value */ |
1014 |
> |
AMBVAL *av |
1015 |
> |
) |
1016 |
|
{ |
1017 |
< |
avinsert(avstore(av)); |
1017 |
> |
avstore(av); |
1018 |
|
if (ambfp == NULL) |
1019 |
|
return; |
1020 |
|
if (writambval(av, ambfp) < 0) |
1029 |
|
|
1030 |
|
|
1031 |
|
static AMBVAL * |
1032 |
< |
avstore(aval) /* allocate memory and store aval */ |
1033 |
< |
register AMBVAL *aval; |
1032 |
> |
avstore( /* allocate memory and save aval */ |
1033 |
> |
AMBVAL *aval |
1034 |
> |
) |
1035 |
|
{ |
1036 |
< |
register AMBVAL *av; |
1036 |
> |
AMBVAL *av; |
1037 |
|
double d; |
1038 |
|
|
1039 |
|
if ((av = newambval()) == NULL) |
1040 |
|
error(SYSTEM, "out of memory in avstore"); |
1041 |
< |
copystruct(av, aval); |
1041 |
> |
*av = *aval; |
1042 |
|
av->latick = ambclock; |
1043 |
|
av->next = NULL; |
1044 |
|
nambvals++; |
1047 |
|
avsum += log(d); |
1048 |
|
navsum++; |
1049 |
|
} |
1050 |
+ |
avinsert(av); /* insert in our cache tree */ |
1051 |
|
return(av); |
1052 |
|
} |
1053 |
|
|
1058 |
|
|
1059 |
|
|
1060 |
|
static AMBTREE * |
1061 |
< |
newambtree() /* allocate 8 ambient tree structs */ |
1061 |
> |
newambtree(void) /* allocate 8 ambient tree structs */ |
1062 |
|
{ |
1063 |
< |
register AMBTREE *atp, *upperlim; |
1063 |
> |
AMBTREE *atp, *upperlim; |
1064 |
|
|
1065 |
|
if (atfreelist == NULL) { /* get more nodes */ |
1066 |
|
atfreelist = (AMBTREE *)malloc(ATALLOCSZ*8*sizeof(AMBTREE)); |
1074 |
|
} |
1075 |
|
atp = atfreelist; |
1076 |
|
atfreelist = atp->kid; |
1077 |
< |
bzero((char *)atp, 8*sizeof(AMBTREE)); |
1077 |
> |
memset((char *)atp, '\0', 8*sizeof(AMBTREE)); |
1078 |
|
return(atp); |
1079 |
|
} |
1080 |
|
|
1081 |
|
|
1082 |
|
static void |
1083 |
< |
freeambtree(atp) /* free 8 ambient tree structs */ |
1084 |
< |
AMBTREE *atp; |
1083 |
> |
freeambtree( /* free 8 ambient tree structs */ |
1084 |
> |
AMBTREE *atp |
1085 |
> |
) |
1086 |
|
{ |
1087 |
|
atp->kid = atfreelist; |
1088 |
|
atfreelist = atp; |
1090 |
|
|
1091 |
|
|
1092 |
|
static void |
1093 |
< |
avinsert(av) /* insert ambient value in our tree */ |
1094 |
< |
register AMBVAL *av; |
1093 |
> |
unloadatree( /* unload an ambient value tree */ |
1094 |
> |
AMBTREE *at, |
1095 |
> |
unloadtf_t *f |
1096 |
> |
) |
1097 |
|
{ |
1098 |
< |
register AMBTREE *at; |
1099 |
< |
register AMBVAL *ap; |
583 |
< |
AMBVAL avh; |
584 |
< |
FVECT ck0; |
585 |
< |
double s; |
586 |
< |
int branch; |
587 |
< |
register int i; |
588 |
< |
|
589 |
< |
if (av->rad <= FTINY) |
590 |
< |
error(CONSISTENCY, "zero ambient radius in avinsert"); |
591 |
< |
at = &atrunk; |
592 |
< |
VCOPY(ck0, thescene.cuorg); |
593 |
< |
s = thescene.cusize; |
594 |
< |
while (s*(OCTSCALE/2) > av->rad*ambacc) { |
595 |
< |
if (at->kid == NULL) |
596 |
< |
if ((at->kid = newambtree()) == NULL) |
597 |
< |
error(SYSTEM, "out of memory in avinsert"); |
598 |
< |
s *= 0.5; |
599 |
< |
branch = 0; |
600 |
< |
for (i = 0; i < 3; i++) |
601 |
< |
if (av->pos[i] > ck0[i] + s) { |
602 |
< |
ck0[i] += s; |
603 |
< |
branch |= 1 << i; |
604 |
< |
} |
605 |
< |
at = at->kid + branch; |
606 |
< |
} |
607 |
< |
avh.next = at->alist; /* order by increasing level */ |
608 |
< |
for (ap = &avh; ap->next != NULL; ap = ap->next) |
609 |
< |
if (ap->next->lvl >= av->lvl) |
610 |
< |
break; |
611 |
< |
av->next = ap->next; |
612 |
< |
ap->next = av; |
613 |
< |
at->alist = avh.next; |
614 |
< |
} |
615 |
< |
|
616 |
< |
|
617 |
< |
static void |
618 |
< |
unloadatree(at, f) /* unload an ambient value tree */ |
619 |
< |
register AMBTREE *at; |
620 |
< |
void (*f)(); |
621 |
< |
{ |
622 |
< |
register AMBVAL *av; |
623 |
< |
register int i; |
1098 |
> |
AMBVAL *av; |
1099 |
> |
int i; |
1100 |
|
/* transfer values at this node */ |
1101 |
|
for (av = at->alist; av != NULL; av = at->alist) { |
1102 |
|
at->alist = av->next; |
1118 |
|
static AMBVAL **avlist2; /* memory positions for sorting */ |
1119 |
|
static int i_avlist; /* index for lists */ |
1120 |
|
|
1121 |
+ |
static int alatcmp(const void *av1, const void *av2); |
1122 |
|
|
1123 |
< |
static int |
1124 |
< |
av2list(av) |
648 |
< |
register AMBVAL *av; |
1123 |
> |
static void |
1124 |
> |
avfree(AMBVAL *av) |
1125 |
|
{ |
1126 |
+ |
free(av); |
1127 |
+ |
} |
1128 |
+ |
|
1129 |
+ |
static void |
1130 |
+ |
av2list( |
1131 |
+ |
AMBVAL *av |
1132 |
+ |
) |
1133 |
+ |
{ |
1134 |
|
#ifdef DEBUG |
1135 |
|
if (i_avlist >= nambvals) |
1136 |
|
error(CONSISTENCY, "too many ambient values in av2list1"); |
1137 |
|
#endif |
1138 |
< |
avlist1[i_avlist].p = avlist2[i_avlist] = av; |
1138 |
> |
avlist1[i_avlist].p = avlist2[i_avlist] = (AMBVAL*)av; |
1139 |
|
avlist1[i_avlist++].t = av->latick; |
1140 |
|
} |
1141 |
|
|
1142 |
|
|
1143 |
|
static int |
1144 |
< |
alatcmp(av1, av2) /* compare ambient values for MRA */ |
1145 |
< |
struct avl *av1, *av2; |
1144 |
> |
alatcmp( /* compare ambient values for MRA */ |
1145 |
> |
const void *av1, |
1146 |
> |
const void *av2 |
1147 |
> |
) |
1148 |
|
{ |
1149 |
< |
register long lc = av2->t - av1->t; |
1149 |
> |
long lc = ((struct avl *)av2)->t - ((struct avl *)av1)->t; |
1150 |
|
return(lc<0 ? -1 : lc>0 ? 1 : 0); |
1151 |
|
} |
1152 |
|
|
1158 |
|
* assumes pointers differ by exact struct size increments. |
1159 |
|
*/ |
1160 |
|
static int |
1161 |
< |
aposcmp(avp1, avp2) /* compare ambient value positions */ |
1162 |
< |
const void *avp1, *avp2; |
1161 |
> |
aposcmp( /* compare ambient value positions */ |
1162 |
> |
const void *avp1, |
1163 |
> |
const void *avp2 |
1164 |
> |
) |
1165 |
|
{ |
1166 |
< |
register long diff = *(char * const *)avp1 - *(char * const *)avp2; |
1166 |
> |
long diff = *(char * const *)avp1 - *(char * const *)avp2; |
1167 |
|
if (diff < 0) |
1168 |
|
return(-1); |
1169 |
|
return(diff > 0); |
1170 |
|
} |
1171 |
|
|
1172 |
< |
#if 1 |
1172 |
> |
|
1173 |
|
static int |
1174 |
< |
avlmemi(avaddr) /* find list position from address */ |
1175 |
< |
AMBVAL *avaddr; |
1174 |
> |
avlmemi( /* find list position from address */ |
1175 |
> |
AMBVAL *avaddr |
1176 |
> |
) |
1177 |
|
{ |
1178 |
< |
register AMBVAL **avlpp; |
1178 |
> |
AMBVAL **avlpp; |
1179 |
|
|
1180 |
|
avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2, |
1181 |
< |
nambvals, sizeof(AMBVAL *), aposcmp); |
1181 |
> |
nambvals, sizeof(AMBVAL *), &aposcmp); |
1182 |
|
if (avlpp == NULL) |
1183 |
|
error(CONSISTENCY, "address not found in avlmemi"); |
1184 |
|
return(avlpp - avlist2); |
1185 |
|
} |
697 |
– |
#else |
698 |
– |
#define avlmemi(avaddr) ((AMBVAL **)bsearch((char *)&avaddr,(char *)avlist2, \ |
699 |
– |
nambvals,sizeof(AMBVAL *),aposcmp) - avlist2) |
700 |
– |
#endif |
1186 |
|
|
1187 |
|
|
1188 |
|
static void |
1189 |
< |
sortambvals(always) /* resort ambient values */ |
1190 |
< |
int always; |
1189 |
> |
sortambvals( /* resort ambient values */ |
1190 |
> |
int always |
1191 |
> |
) |
1192 |
|
{ |
1193 |
|
AMBTREE oldatrunk; |
1194 |
|
AMBVAL tav, *tap, *pnext; |
1195 |
< |
register int i, j; |
1195 |
> |
int i, j; |
1196 |
|
/* see if it's time yet */ |
1197 |
|
if (!always && (ambclock++ < lastsort+sortintvl || |
1198 |
|
nambvals < SORT_THRESH)) |
1223 |
|
if (avlist2 != NULL) |
1224 |
|
free((void *)avlist2); |
1225 |
|
if (always) { /* rebuild without sorting */ |
1226 |
< |
copystruct(&oldatrunk, &atrunk); |
1226 |
> |
oldatrunk = atrunk; |
1227 |
|
atrunk.alist = NULL; |
1228 |
|
atrunk.kid = NULL; |
1229 |
|
unloadatree(&oldatrunk, avinsert); |
1254 |
|
if (avlist1[i].p == NULL) |
1255 |
|
continue; |
1256 |
|
tap = avlist2[i]; |
1257 |
< |
copystruct(&tav, tap); |
1257 |
> |
tav = *tap; |
1258 |
|
for (j = i; (pnext = avlist1[j].p) != tap; |
1259 |
|
j = avlmemi(pnext)) { |
1260 |
< |
copystruct(avlist2[j], pnext); |
1260 |
> |
*(avlist2[j]) = *pnext; |
1261 |
|
avinsert(avlist2[j]); |
1262 |
|
avlist1[j].p = NULL; |
1263 |
|
} |
1264 |
< |
copystruct(avlist2[j], &tav); |
1264 |
> |
*(avlist2[j]) = tav; |
1265 |
|
avinsert(avlist2[j]); |
1266 |
|
avlist1[j].p = NULL; |
1267 |
|
} |
1286 |
|
#ifdef F_SETLKW |
1287 |
|
|
1288 |
|
static void |
1289 |
< |
aflock(typ) /* lock/unlock ambient file */ |
1290 |
< |
int typ; |
1289 |
> |
aflock( /* lock/unlock ambient file */ |
1290 |
> |
int typ |
1291 |
> |
) |
1292 |
|
{ |
1293 |
|
static struct flock fls; /* static so initialized to zeroes */ |
1294 |
|
|
1295 |
+ |
if (typ == fls.l_type) /* already called? */ |
1296 |
+ |
return; |
1297 |
|
fls.l_type = typ; |
1298 |
|
if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0) |
1299 |
|
error(SYSTEM, "cannot (un)lock ambient file"); |
1301 |
|
|
1302 |
|
|
1303 |
|
int |
1304 |
< |
ambsync() /* synchronize ambient file */ |
1304 |
> |
ambsync(void) /* synchronize ambient file */ |
1305 |
|
{ |
1306 |
|
long flen; |
1307 |
|
AMBVAL avs; |
1308 |
< |
register int n; |
1308 |
> |
int n; |
1309 |
|
|
1310 |
< |
if (nunflshed == 0) |
1310 |
> |
if (ambfp == NULL) /* no ambient file? */ |
1311 |
|
return(0); |
1312 |
< |
if (lastpos < 0) /* initializing (locked in initambfile) */ |
1313 |
< |
goto syncend; |
825 |
< |
/* gain exclusive access */ |
826 |
< |
aflock(F_WRLCK); |
1312 |
> |
/* gain appropriate access */ |
1313 |
> |
aflock(nunflshed ? F_WRLCK : F_RDLCK); |
1314 |
|
/* see if file has grown */ |
1315 |
< |
if ((flen = lseek(fileno(ambfp), (off_t)0L, 2)) < 0) |
1315 |
> |
if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0) |
1316 |
|
goto seekerr; |
1317 |
< |
if (n = flen - lastpos) { /* file has grown */ |
1317 |
> |
if ((n = flen - lastpos) > 0) { /* file has grown */ |
1318 |
|
if (ambinp == NULL) { /* use duplicate filedes */ |
1319 |
|
ambinp = fdopen(dup(fileno(ambfp)), "r"); |
1320 |
|
if (ambinp == NULL) |
1321 |
|
error(SYSTEM, "fdopen failed in ambsync"); |
1322 |
|
} |
1323 |
< |
if (fseek(ambinp, lastpos, 0) < 0) |
1323 |
> |
if (fseek(ambinp, lastpos, SEEK_SET) < 0) |
1324 |
|
goto seekerr; |
1325 |
|
while (n >= AMBVALSIZ) { /* load contributed values */ |
1326 |
|
if (!readambval(&avs, ambinp)) { |
1330 |
|
error(WARNING, errmsg); |
1331 |
|
break; |
1332 |
|
} |
1333 |
< |
avinsert(avstore(&avs)); |
1333 |
> |
avstore(&avs); |
1334 |
|
n -= AMBVALSIZ; |
1335 |
|
} |
1336 |
+ |
lastpos = flen - n; |
1337 |
|
/*** seek always as safety measure |
1338 |
|
if (n) ***/ /* alignment */ |
1339 |
< |
if (lseek(fileno(ambfp), (off_t)(flen-n), 0) < 0) |
1339 |
> |
if (lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0) |
1340 |
|
goto seekerr; |
1341 |
|
} |
854 |
– |
#ifdef DEBUG |
855 |
– |
if (ambfp->_ptr - ambfp->_base != nunflshed*AMBVALSIZ) { |
856 |
– |
sprintf(errmsg, "ambient file buffer at %d rather than %d", |
857 |
– |
ambfp->_ptr - ambfp->_base, |
858 |
– |
nunflshed*AMBVALSIZ); |
859 |
– |
error(CONSISTENCY, errmsg); |
860 |
– |
} |
861 |
– |
#endif |
862 |
– |
syncend: |
1342 |
|
n = fflush(ambfp); /* calls write() at last */ |
1343 |
< |
if ((lastpos = lseek(fileno(ambfp), (off_t)0L, 1)) < 0) |
1343 |
> |
if (n != EOF) |
1344 |
> |
lastpos += (long)nunflshed*AMBVALSIZ; |
1345 |
> |
else if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0) |
1346 |
|
goto seekerr; |
1347 |
+ |
|
1348 |
|
aflock(F_UNLCK); /* release file */ |
1349 |
|
nunflshed = 0; |
1350 |
|
return(n); |
1351 |
|
seekerr: |
1352 |
|
error(SYSTEM, "seek failed in ambsync"); |
1353 |
+ |
return -1; /* pro forma return */ |
1354 |
|
} |
1355 |
|
|
1356 |
< |
#else |
1356 |
> |
#else /* ! F_SETLKW */ |
1357 |
|
|
1358 |
|
int |
1359 |
< |
ambsync() /* flush ambient file */ |
1359 |
> |
ambsync(void) /* flush ambient file */ |
1360 |
|
{ |
1361 |
< |
if (nunflshed == 0) |
1361 |
> |
if (ambfp == NULL) |
1362 |
|
return(0); |
1363 |
|
nunflshed = 0; |
1364 |
|
return(fflush(ambfp)); |
1365 |
|
} |
1366 |
|
|
1367 |
< |
#endif |
1367 |
> |
#endif /* ! F_SETLKW */ |