1 |
– |
#ifndef lint |
1 |
|
static const char RCSid[] = "$Id$"; |
3 |
– |
#endif |
2 |
|
/* |
3 |
|
* ambient.c - routines dealing with ambient (inter-reflected) component. |
4 |
|
* |
12 |
|
#include "platform.h" |
13 |
|
#include "ray.h" |
14 |
|
#include "otypes.h" |
15 |
+ |
#include "otspecial.h" |
16 |
|
#include "resolu.h" |
17 |
|
#include "ambient.h" |
18 |
|
#include "random.h" |
19 |
+ |
#include "pmapamb.h" |
20 |
|
|
21 |
|
#ifndef OCTSCALE |
22 |
|
#define OCTSCALE 1.0 /* ceil((valid rad.)/(cube size)) */ |
77 |
|
#define AMBFLUSH (BUFSIZ/AMBVALSIZ) |
78 |
|
|
79 |
|
#define newambval() (AMBVAL *)malloc(sizeof(AMBVAL)) |
80 |
– |
#define freeav(av) free((void *)av); |
80 |
|
|
81 |
+ |
#define tfunc(lwr, x, upr) (((x)-(lwr))/((upr)-(lwr))) |
82 |
+ |
|
83 |
|
static void initambfile(int creat); |
84 |
|
static void avsave(AMBVAL *av); |
85 |
|
static AMBVAL *avstore(AMBVAL *aval); |
96 |
|
static int avlmemi(AMBVAL *avaddr); |
97 |
|
static void sortambvals(int always); |
98 |
|
|
99 |
+ |
static int plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang); |
100 |
+ |
static double sumambient(COLOR acol, RAY *r, FVECT rn, int al, |
101 |
+ |
AMBTREE *at, FVECT c0, double s); |
102 |
+ |
static int makeambient(COLOR acol, RAY *r, FVECT rn, int al); |
103 |
+ |
static int extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv, |
104 |
+ |
FVECT uvw[3]); |
105 |
+ |
|
106 |
|
#ifdef F_SETLKW |
107 |
|
static void aflock(int typ); |
108 |
|
#endif |
141 |
|
newa *= (newa > 0); |
142 |
|
if (fabs(newa - olda) >= .05*(newa + olda)) { |
143 |
|
ambacc = newa; |
144 |
< |
if (nambvals > 0) |
144 |
> |
if (ambacc > FTINY && nambvals > 0) |
145 |
|
sortambvals(1); /* rebuild tree */ |
146 |
|
} |
147 |
|
} |
193 |
|
(flen - lastpos)/AMBVALSIZ); |
194 |
|
error(WARNING, errmsg); |
195 |
|
fseek(ambfp, lastpos, SEEK_SET); |
188 |
– |
#ifndef _WIN32 /* XXX we need a replacement for that one */ |
196 |
|
ftruncate(fileno(ambfp), (off_t)lastpos); |
190 |
– |
#endif |
197 |
|
} |
198 |
|
} else if ((ambfp = fopen(ambfile, "w+")) != NULL) { |
199 |
|
initambfile(1); /* else create new file */ |
203 |
|
sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile); |
204 |
|
error(SYSTEM, errmsg); |
205 |
|
} |
200 |
– |
#ifdef getc_unlocked |
201 |
– |
flockfile(ambfp); /* application-level lock */ |
202 |
– |
#endif |
206 |
|
#ifdef F_SETLKW |
207 |
|
aflock(F_UNLCK); /* release file */ |
208 |
|
#endif |
223 |
|
lastpos = -1; |
224 |
|
} |
225 |
|
/* free ambient tree */ |
226 |
< |
unloadatree(&atrunk, &avfree); |
226 |
> |
unloadatree(&atrunk, avfree); |
227 |
|
/* reset state variables */ |
228 |
|
avsum = 0.; |
229 |
|
navsum = 0; |
264 |
|
} |
265 |
|
} |
266 |
|
|
264 |
– |
/************ THE FOLLOWING ROUTINES DIFFER BETWEEN NEW & OLD ***************/ |
267 |
|
|
266 |
– |
#ifdef NEWAMB |
267 |
– |
|
268 |
– |
#define tfunc(lwr, x, upr) (((x)-(lwr))/((upr)-(lwr))) |
269 |
– |
|
270 |
– |
static int plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang); |
271 |
– |
static double sumambient(COLOR acol, RAY *r, FVECT rn, int al, |
272 |
– |
AMBTREE *at, FVECT c0, double s); |
273 |
– |
static int makeambient(COLOR acol, RAY *r, FVECT rn, int al); |
274 |
– |
static void extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv, |
275 |
– |
FVECT uvw[3]); |
276 |
– |
|
268 |
|
void |
269 |
|
multambient( /* compute ambient component & multiply by coef. */ |
270 |
|
COLOR aval, |
273 |
|
) |
274 |
|
{ |
275 |
|
static int rdepth = 0; /* ambient recursion */ |
276 |
< |
COLOR acol; |
277 |
< |
int ok; |
276 |
> |
COLOR acol, caustic; |
277 |
> |
int i, ok; |
278 |
|
double d, l; |
279 |
|
|
280 |
+ |
/* PMAP: Factor in ambient from photon map, if enabled and ray is |
281 |
+ |
* ambient. Return as all ambient components accounted for, else |
282 |
+ |
* continue. */ |
283 |
+ |
if (ambPmap(aval, r, rdepth)) |
284 |
+ |
return; |
285 |
+ |
|
286 |
+ |
/* PMAP: Factor in specular-diffuse ambient (caustics) from photon |
287 |
+ |
* map, if enabled and ray is primary, else caustic is zero. Continue |
288 |
+ |
* with RADIANCE ambient calculation */ |
289 |
+ |
copycolor(caustic, aval); |
290 |
+ |
ambPmapCaustic(caustic, r, rdepth); |
291 |
+ |
|
292 |
|
if (ambdiv <= 0) /* no ambient calculation */ |
293 |
|
goto dumbamb; |
294 |
|
/* check number of bounces */ |
300 |
|
goto dumbamb; |
301 |
|
|
302 |
|
if (ambacc <= FTINY) { /* no ambient storage */ |
303 |
+ |
FVECT uvd[2]; |
304 |
+ |
float dgrad[2], *dgp = NULL; |
305 |
+ |
|
306 |
+ |
if (nrm != r->ron && DOT(nrm,r->ron) < 0.9999) |
307 |
+ |
dgp = dgrad; /* compute rotational grad. */ |
308 |
|
copycolor(acol, aval); |
309 |
|
rdepth++; |
310 |
|
ok = doambient(acol, r, r->rweight, |
311 |
< |
NULL, NULL, NULL, NULL, NULL); |
311 |
> |
uvd, NULL, NULL, dgp, NULL); |
312 |
|
rdepth--; |
313 |
|
if (!ok) |
314 |
|
goto dumbamb; |
315 |
+ |
if ((ok > 0) & (dgp != NULL)) { /* apply texture */ |
316 |
+ |
FVECT v1; |
317 |
+ |
VCROSS(v1, r->ron, nrm); |
318 |
+ |
d = 1.0; |
319 |
+ |
for (i = 3; i--; ) |
320 |
+ |
d += v1[i] * (dgp[0]*uvd[0][i] + dgp[1]*uvd[1][i]); |
321 |
+ |
if (d >= 0.05) |
322 |
+ |
scalecolor(acol, d); |
323 |
+ |
} |
324 |
|
copycolor(aval, acol); |
325 |
+ |
|
326 |
+ |
/* PMAP: add in caustic */ |
327 |
+ |
addcolor(aval, caustic); |
328 |
|
return; |
329 |
|
} |
330 |
|
|
334 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
335 |
|
d = sumambient(acol, r, nrm, rdepth, |
336 |
|
&atrunk, thescene.cuorg, thescene.cusize); |
337 |
+ |
|
338 |
|
if (d > FTINY) { |
339 |
|
d = 1.0/d; |
340 |
|
scalecolor(acol, d); |
341 |
|
multcolor(aval, acol); |
342 |
+ |
|
343 |
+ |
/* PMAP: add in caustic */ |
344 |
+ |
addcolor(aval, caustic); |
345 |
|
return; |
346 |
|
} |
347 |
+ |
|
348 |
|
rdepth++; /* need to cache new value */ |
349 |
|
ok = makeambient(acol, r, nrm, rdepth-1); |
350 |
|
rdepth--; |
351 |
+ |
|
352 |
|
if (ok) { |
353 |
|
multcolor(aval, acol); /* computed new value */ |
354 |
+ |
|
355 |
+ |
/* PMAP: add in caustic */ |
356 |
+ |
addcolor(aval, caustic); |
357 |
|
return; |
358 |
|
} |
359 |
+ |
|
360 |
|
dumbamb: /* return global value */ |
361 |
|
if ((ambvwt <= 0) | (navsum == 0)) { |
362 |
|
multcolor(aval, ambval); |
363 |
+ |
|
364 |
+ |
/* PMAP: add in caustic */ |
365 |
+ |
addcolor(aval, caustic); |
366 |
|
return; |
367 |
|
} |
368 |
< |
l = bright(ambval); /* average in computations */ |
368 |
> |
|
369 |
> |
l = bright(ambval); /* average in computations */ |
370 |
|
if (l > FTINY) { |
371 |
|
d = (log(l)*(double)ambvwt + avsum) / |
372 |
|
(double)(ambvwt + navsum); |
413 |
|
VSUM(rtst.rdir, vdif, anorm, t[1]); /* further dist. > plane */ |
414 |
|
rtst.rmax = normalize(rtst.rdir); /* short ray test */ |
415 |
|
while (localhit(&rtst, &thescene)) { /* check for occluder */ |
416 |
< |
if (rtst.ro->omod != OVOID && |
416 |
> |
OBJREC *m = findmaterial(rtst.ro); |
417 |
> |
if (m != NULL && !istransp(m->otype) && !isBSDFproxy(m) && |
418 |
|
(rtst.clipset == NULL || |
419 |
|
!inset(rtst.clipset, rtst.ro->omod))) |
420 |
|
return(1); /* plug light leak */ |
521 |
|
/* |
522 |
|
* Extrapolate value and compute final weight (hat function) |
523 |
|
*/ |
524 |
< |
extambient(ct, av, r->rop, rn, uvw); |
524 |
> |
if (!extambient(ct, av, r->rop, rn, uvw)) |
525 |
> |
continue; |
526 |
|
d = tfunc(maxangle, sqrt(delta_r2), 0.0) * |
527 |
|
tfunc(ambacc, sqrt(delta_t2), 0.0); |
528 |
|
scalecolor(ct, d); |
573 |
|
} |
574 |
|
|
575 |
|
|
576 |
< |
static void |
576 |
> |
static int |
577 |
|
extambient( /* extrapolate value at pv, nv */ |
578 |
|
COLOR cr, |
579 |
|
AMBVAL *ap, |
582 |
|
FVECT uvw[3] |
583 |
|
) |
584 |
|
{ |
585 |
+ |
const double min_d = 0.05; |
586 |
+ |
const double max_d = 20.; |
587 |
|
static FVECT my_uvw[3]; |
588 |
|
FVECT v1; |
589 |
|
int i; |
603 |
|
for (i = 3; i--; ) |
604 |
|
d += v1[i] * (ap->gdir[0]*uvw[0][i] + ap->gdir[1]*uvw[1][i]); |
605 |
|
|
606 |
< |
if (d <= 0.0) { |
607 |
< |
setcolor(cr, 0.0, 0.0, 0.0); |
608 |
< |
return; |
609 |
< |
} |
606 |
> |
if (d < min_d) /* clamp min/max scaling */ |
607 |
> |
d = min_d; |
608 |
> |
else if (d > max_d) |
609 |
> |
d = max_d; |
610 |
|
copycolor(cr, ap->val); |
611 |
|
scalecolor(cr, d); |
612 |
+ |
return(d > min_d); |
613 |
|
} |
614 |
|
|
615 |
|
|
656 |
|
} |
657 |
|
|
658 |
|
|
620 |
– |
#else /* ! NEWAMB */ |
621 |
– |
|
622 |
– |
static double sumambient(COLOR acol, RAY *r, FVECT rn, int al, |
623 |
– |
AMBTREE *at, FVECT c0, double s); |
624 |
– |
static double makeambient(COLOR acol, RAY *r, FVECT rn, int al); |
625 |
– |
static void extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv); |
626 |
– |
|
627 |
– |
|
628 |
– |
void |
629 |
– |
multambient( /* compute ambient component & multiply by coef. */ |
630 |
– |
COLOR aval, |
631 |
– |
RAY *r, |
632 |
– |
FVECT nrm |
633 |
– |
) |
634 |
– |
{ |
635 |
– |
static int rdepth = 0; /* ambient recursion */ |
636 |
– |
COLOR acol; |
637 |
– |
double d, l; |
638 |
– |
|
639 |
– |
if (ambdiv <= 0) /* no ambient calculation */ |
640 |
– |
goto dumbamb; |
641 |
– |
/* check number of bounces */ |
642 |
– |
if (rdepth >= ambounce) |
643 |
– |
goto dumbamb; |
644 |
– |
/* check ambient list */ |
645 |
– |
if (ambincl != -1 && r->ro != NULL && |
646 |
– |
ambincl != inset(ambset, r->ro->omod)) |
647 |
– |
goto dumbamb; |
648 |
– |
|
649 |
– |
if (ambacc <= FTINY) { /* no ambient storage */ |
650 |
– |
copycolor(acol, aval); |
651 |
– |
rdepth++; |
652 |
– |
d = doambient(acol, r, r->rweight, NULL, NULL); |
653 |
– |
rdepth--; |
654 |
– |
if (d <= FTINY) |
655 |
– |
goto dumbamb; |
656 |
– |
copycolor(aval, acol); |
657 |
– |
return; |
658 |
– |
} |
659 |
– |
|
660 |
– |
if (tracktime) /* sort to minimize thrashing */ |
661 |
– |
sortambvals(0); |
662 |
– |
/* interpolate ambient value */ |
663 |
– |
setcolor(acol, 0.0, 0.0, 0.0); |
664 |
– |
d = sumambient(acol, r, nrm, rdepth, |
665 |
– |
&atrunk, thescene.cuorg, thescene.cusize); |
666 |
– |
if (d > FTINY) { |
667 |
– |
d = 1.0/d; |
668 |
– |
scalecolor(acol, d); |
669 |
– |
multcolor(aval, acol); |
670 |
– |
return; |
671 |
– |
} |
672 |
– |
rdepth++; /* need to cache new value */ |
673 |
– |
d = makeambient(acol, r, nrm, rdepth-1); |
674 |
– |
rdepth--; |
675 |
– |
if (d > FTINY) { |
676 |
– |
multcolor(aval, acol); /* got new value */ |
677 |
– |
return; |
678 |
– |
} |
679 |
– |
dumbamb: /* return global value */ |
680 |
– |
if ((ambvwt <= 0) | (navsum == 0)) { |
681 |
– |
multcolor(aval, ambval); |
682 |
– |
return; |
683 |
– |
} |
684 |
– |
l = bright(ambval); /* average in computations */ |
685 |
– |
if (l > FTINY) { |
686 |
– |
d = (log(l)*(double)ambvwt + avsum) / |
687 |
– |
(double)(ambvwt + navsum); |
688 |
– |
d = exp(d) / l; |
689 |
– |
scalecolor(aval, d); |
690 |
– |
multcolor(aval, ambval); /* apply color of ambval */ |
691 |
– |
} else { |
692 |
– |
d = exp( avsum / (double)navsum ); |
693 |
– |
scalecolor(aval, d); /* neutral color */ |
694 |
– |
} |
695 |
– |
} |
696 |
– |
|
697 |
– |
|
698 |
– |
static double |
699 |
– |
sumambient( /* get interpolated ambient value */ |
700 |
– |
COLOR acol, |
701 |
– |
RAY *r, |
702 |
– |
FVECT rn, |
703 |
– |
int al, |
704 |
– |
AMBTREE *at, |
705 |
– |
FVECT c0, |
706 |
– |
double s |
707 |
– |
) |
708 |
– |
{ |
709 |
– |
double d, e1, e2, wt, wsum; |
710 |
– |
COLOR ct; |
711 |
– |
FVECT ck0; |
712 |
– |
int i; |
713 |
– |
int j; |
714 |
– |
AMBVAL *av; |
715 |
– |
|
716 |
– |
wsum = 0.0; |
717 |
– |
/* do this node */ |
718 |
– |
for (av = at->alist; av != NULL; av = av->next) { |
719 |
– |
double rn_dot = -2.0; |
720 |
– |
if (tracktime) |
721 |
– |
av->latick = ambclock; |
722 |
– |
/* |
723 |
– |
* Ambient level test. |
724 |
– |
*/ |
725 |
– |
if (av->lvl > al || /* list sorted, so this works */ |
726 |
– |
(av->lvl == al) & (av->weight < 0.9*r->rweight)) |
727 |
– |
break; |
728 |
– |
/* |
729 |
– |
* Ambient radius test. |
730 |
– |
*/ |
731 |
– |
VSUB(ck0, av->pos, r->rop); |
732 |
– |
e1 = DOT(ck0, ck0) / (av->rad * av->rad); |
733 |
– |
if (e1 > ambacc*ambacc*1.21) |
734 |
– |
continue; |
735 |
– |
/* |
736 |
– |
* Direction test using closest normal. |
737 |
– |
*/ |
738 |
– |
d = DOT(av->dir, r->ron); |
739 |
– |
if (rn != r->ron) { |
740 |
– |
rn_dot = DOT(av->dir, rn); |
741 |
– |
if (rn_dot > 1.0-FTINY) |
742 |
– |
rn_dot = 1.0-FTINY; |
743 |
– |
if (rn_dot >= d-FTINY) { |
744 |
– |
d = rn_dot; |
745 |
– |
rn_dot = -2.0; |
746 |
– |
} |
747 |
– |
} |
748 |
– |
e2 = (1.0 - d) * r->rweight; |
749 |
– |
if (e2 < 0.0) |
750 |
– |
e2 = 0.0; |
751 |
– |
else if (e1 + e2 > ambacc*ambacc*1.21) |
752 |
– |
continue; |
753 |
– |
/* |
754 |
– |
* Ray behind test. |
755 |
– |
*/ |
756 |
– |
d = 0.0; |
757 |
– |
for (j = 0; j < 3; j++) |
758 |
– |
d += (r->rop[j] - av->pos[j]) * |
759 |
– |
(av->dir[j] + r->ron[j]); |
760 |
– |
if (d*0.5 < -minarad*ambacc-.001) |
761 |
– |
continue; |
762 |
– |
/* |
763 |
– |
* Jittering final test reduces image artifacts. |
764 |
– |
*/ |
765 |
– |
e1 = sqrt(e1); |
766 |
– |
e2 = sqrt(e2); |
767 |
– |
wt = e1 + e2; |
768 |
– |
if (wt > ambacc*(.9+.2*urand(9015+samplendx))) |
769 |
– |
continue; |
770 |
– |
/* |
771 |
– |
* Recompute directional error using perturbed normal |
772 |
– |
*/ |
773 |
– |
if (rn_dot > 0.0) { |
774 |
– |
e2 = sqrt((1.0 - rn_dot)*r->rweight); |
775 |
– |
wt = e1 + e2; |
776 |
– |
} |
777 |
– |
if (wt <= 1e-3) |
778 |
– |
wt = 1e3; |
779 |
– |
else |
780 |
– |
wt = 1.0 / wt; |
781 |
– |
wsum += wt; |
782 |
– |
extambient(ct, av, r->rop, rn); |
783 |
– |
scalecolor(ct, wt); |
784 |
– |
addcolor(acol, ct); |
785 |
– |
} |
786 |
– |
if (at->kid == NULL) |
787 |
– |
return(wsum); |
788 |
– |
/* do children */ |
789 |
– |
s *= 0.5; |
790 |
– |
for (i = 0; i < 8; i++) { |
791 |
– |
for (j = 0; j < 3; j++) { |
792 |
– |
ck0[j] = c0[j]; |
793 |
– |
if (1<<j & i) |
794 |
– |
ck0[j] += s; |
795 |
– |
if (r->rop[j] < ck0[j] - OCTSCALE*s) |
796 |
– |
break; |
797 |
– |
if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s) |
798 |
– |
break; |
799 |
– |
} |
800 |
– |
if (j == 3) |
801 |
– |
wsum += sumambient(acol, r, rn, al, |
802 |
– |
at->kid+i, ck0, s); |
803 |
– |
} |
804 |
– |
return(wsum); |
805 |
– |
} |
806 |
– |
|
807 |
– |
|
808 |
– |
static double |
809 |
– |
makeambient( /* make a new ambient value for storage */ |
810 |
– |
COLOR acol, |
811 |
– |
RAY *r, |
812 |
– |
FVECT rn, |
813 |
– |
int al |
814 |
– |
) |
815 |
– |
{ |
816 |
– |
AMBVAL amb; |
817 |
– |
FVECT gp, gd; |
818 |
– |
int i; |
819 |
– |
|
820 |
– |
amb.weight = 1.0; /* compute weight */ |
821 |
– |
for (i = al; i-- > 0; ) |
822 |
– |
amb.weight *= AVGREFL; |
823 |
– |
if (r->rweight < 0.1*amb.weight) /* heuristic override */ |
824 |
– |
amb.weight = 1.25*r->rweight; |
825 |
– |
setcolor(acol, AVGREFL, AVGREFL, AVGREFL); |
826 |
– |
/* compute ambient */ |
827 |
– |
amb.rad = doambient(acol, r, amb.weight, gp, gd); |
828 |
– |
if (amb.rad <= FTINY) { |
829 |
– |
setcolor(acol, 0.0, 0.0, 0.0); |
830 |
– |
return(0.0); |
831 |
– |
} |
832 |
– |
scalecolor(acol, 1./AVGREFL); /* undo assumed reflectance */ |
833 |
– |
/* store value */ |
834 |
– |
VCOPY(amb.pos, r->rop); |
835 |
– |
VCOPY(amb.dir, r->ron); |
836 |
– |
amb.lvl = al; |
837 |
– |
copycolor(amb.val, acol); |
838 |
– |
VCOPY(amb.gpos, gp); |
839 |
– |
VCOPY(amb.gdir, gd); |
840 |
– |
/* insert into tree */ |
841 |
– |
avsave(&amb); /* and save to file */ |
842 |
– |
if (rn != r->ron) |
843 |
– |
extambient(acol, &amb, r->rop, rn); /* texture */ |
844 |
– |
return(amb.rad); |
845 |
– |
} |
846 |
– |
|
847 |
– |
|
659 |
|
static void |
849 |
– |
extambient( /* extrapolate value at pv, nv */ |
850 |
– |
COLOR cr, |
851 |
– |
AMBVAL *ap, |
852 |
– |
FVECT pv, |
853 |
– |
FVECT nv |
854 |
– |
) |
855 |
– |
{ |
856 |
– |
FVECT v1; |
857 |
– |
int i; |
858 |
– |
double d; |
859 |
– |
|
860 |
– |
d = 1.0; /* zeroeth order */ |
861 |
– |
/* gradient due to translation */ |
862 |
– |
for (i = 0; i < 3; i++) |
863 |
– |
d += ap->gpos[i]*(pv[i]-ap->pos[i]); |
864 |
– |
/* gradient due to rotation */ |
865 |
– |
VCROSS(v1, ap->dir, nv); |
866 |
– |
d += DOT(ap->gdir, v1); |
867 |
– |
if (d <= 0.0) { |
868 |
– |
setcolor(cr, 0.0, 0.0, 0.0); |
869 |
– |
return; |
870 |
– |
} |
871 |
– |
copycolor(cr, ap->val); |
872 |
– |
scalecolor(cr, d); |
873 |
– |
} |
874 |
– |
|
875 |
– |
|
876 |
– |
static void |
877 |
– |
avinsert( /* insert ambient value in our tree */ |
878 |
– |
AMBVAL *av |
879 |
– |
) |
880 |
– |
{ |
881 |
– |
AMBTREE *at; |
882 |
– |
AMBVAL *ap; |
883 |
– |
AMBVAL avh; |
884 |
– |
FVECT ck0; |
885 |
– |
double s; |
886 |
– |
int branch; |
887 |
– |
int i; |
888 |
– |
|
889 |
– |
if (av->rad <= FTINY) |
890 |
– |
error(CONSISTENCY, "zero ambient radius in avinsert"); |
891 |
– |
at = &atrunk; |
892 |
– |
VCOPY(ck0, thescene.cuorg); |
893 |
– |
s = thescene.cusize; |
894 |
– |
while (s*(OCTSCALE/2) > av->rad*ambacc) { |
895 |
– |
if (at->kid == NULL) |
896 |
– |
if ((at->kid = newambtree()) == NULL) |
897 |
– |
error(SYSTEM, "out of memory in avinsert"); |
898 |
– |
s *= 0.5; |
899 |
– |
branch = 0; |
900 |
– |
for (i = 0; i < 3; i++) |
901 |
– |
if (av->pos[i] > ck0[i] + s) { |
902 |
– |
ck0[i] += s; |
903 |
– |
branch |= 1 << i; |
904 |
– |
} |
905 |
– |
at = at->kid + branch; |
906 |
– |
} |
907 |
– |
avh.next = at->alist; /* order by increasing level */ |
908 |
– |
for (ap = &avh; ap->next != NULL; ap = ap->next) |
909 |
– |
if ( ap->next->lvl > av->lvl || |
910 |
– |
(ap->next->lvl == av->lvl) & |
911 |
– |
(ap->next->weight <= av->weight) ) |
912 |
– |
break; |
913 |
– |
av->next = ap->next; |
914 |
– |
ap->next = (AMBVAL*)av; |
915 |
– |
at->alist = avh.next; |
916 |
– |
} |
917 |
– |
|
918 |
– |
#endif /* ! NEWAMB */ |
919 |
– |
|
920 |
– |
/************* FOLLOWING ROUTINES SAME FOR NEW & OLD METHODS ***************/ |
921 |
– |
|
922 |
– |
static void |
660 |
|
initambfile( /* initialize ambient file */ |
661 |
|
int cre8 |
662 |
|
) |
757 |
|
} |
758 |
|
atp = atfreelist; |
759 |
|
atfreelist = atp->kid; |
760 |
< |
memset((char *)atp, '\0', 8*sizeof(AMBTREE)); |
760 |
> |
memset(atp, 0, 8*sizeof(AMBTREE)); |
761 |
|
return(atp); |
762 |
|
} |
763 |
|
|
783 |
|
/* transfer values at this node */ |
784 |
|
for (av = at->alist; av != NULL; av = at->alist) { |
785 |
|
at->alist = av->next; |
786 |
+ |
av->next = NULL; |
787 |
|
(*f)(av); |
788 |
|
} |
789 |
|
if (at->kid == NULL) |
861 |
|
{ |
862 |
|
AMBVAL **avlpp; |
863 |
|
|
864 |
< |
avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2, |
865 |
< |
nambvals, sizeof(AMBVAL *), &aposcmp); |
864 |
> |
avlpp = (AMBVAL **)bsearch(&avaddr, avlist2, |
865 |
> |
nambvals, sizeof(AMBVAL *), aposcmp); |
866 |
|
if (avlpp == NULL) |
867 |
|
error(CONSISTENCY, "address not found in avlmemi"); |
868 |
|
return(avlpp - avlist2); |
905 |
|
} |
906 |
|
if (avlist1 == NULL) { /* no time tracking -- rebuild tree? */ |
907 |
|
if (avlist2 != NULL) |
908 |
< |
free((void *)avlist2); |
908 |
> |
free(avlist2); |
909 |
|
if (always) { /* rebuild without sorting */ |
910 |
|
oldatrunk = atrunk; |
911 |
|
atrunk.alist = NULL; |
912 |
|
atrunk.kid = NULL; |
913 |
< |
unloadatree(&oldatrunk, &avinsert); |
913 |
> |
unloadatree(&oldatrunk, avinsert); |
914 |
|
} |
915 |
|
} else { /* sort memory by last access time */ |
916 |
|
/* |
927 |
|
eputs(errmsg); |
928 |
|
#endif |
929 |
|
i_avlist = 0; |
930 |
< |
unloadatree(&atrunk, &av2list); /* empty current tree */ |
930 |
> |
unloadatree(&atrunk, av2list); /* empty current tree */ |
931 |
|
#ifdef DEBUG |
932 |
|
if (i_avlist < nambvals) |
933 |
|
error(CONSISTENCY, "missing ambient values in sortambvals"); |
934 |
|
#endif |
935 |
< |
qsort((char *)avlist1, nambvals, sizeof(struct avl), alatcmp); |
936 |
< |
qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp); |
935 |
> |
qsort(avlist1, nambvals, sizeof(struct avl), alatcmp); |
936 |
> |
qsort(avlist2, nambvals, sizeof(AMBVAL *), aposcmp); |
937 |
|
for (i = 0; i < nambvals; i++) { |
938 |
|
if (avlist1[i].p == NULL) |
939 |
|
continue; |
949 |
|
avinsert(avlist2[j]); |
950 |
|
avlist1[j].p = NULL; |
951 |
|
} |
952 |
< |
free((void *)avlist1); |
953 |
< |
free((void *)avlist2); |
952 |
> |
free(avlist1); |
953 |
> |
free(avlist2); |
954 |
|
/* compute new sort interval */ |
955 |
|
sortintvl = ambclock - lastsort; |
956 |
|
if (sortintvl >= MAX_SORT_INTVL/2) |
978 |
|
|
979 |
|
if (typ == fls.l_type) /* already called? */ |
980 |
|
return; |
981 |
+ |
|
982 |
|
fls.l_type = typ; |
983 |
< |
if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0) |
984 |
< |
error(SYSTEM, "cannot (un)lock ambient file"); |
983 |
> |
do |
984 |
> |
if (fcntl(fileno(ambfp), F_SETLKW, &fls) != -1) |
985 |
> |
return; |
986 |
> |
while (errno == EINTR); |
987 |
> |
|
988 |
> |
error(SYSTEM, "cannot (un)lock ambient file"); |
989 |
|
} |
990 |
|
|
991 |
|
|
1004 |
|
if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0) |
1005 |
|
goto seekerr; |
1006 |
|
if ((n = flen - lastpos) > 0) { /* file has grown */ |
1007 |
< |
if (ambinp == NULL) { /* use duplicate filedes */ |
1008 |
< |
ambinp = fdopen(dup(fileno(ambfp)), "r"); |
1007 |
> |
if (ambinp == NULL) { /* get new file pointer */ |
1008 |
> |
ambinp = fopen(ambfile, "rb"); |
1009 |
|
if (ambinp == NULL) |
1010 |
< |
error(SYSTEM, "fdopen failed in ambsync"); |
1010 |
> |
error(SYSTEM, "fopen failed in ambsync"); |
1011 |
|
} |
1012 |
|
if (fseek(ambinp, lastpos, SEEK_SET) < 0) |
1013 |
|
goto seekerr; |
1022 |
|
avstore(&avs); |
1023 |
|
n -= AMBVALSIZ; |
1024 |
|
} |
1025 |
< |
lastpos = flen - n; |
1026 |
< |
/*** seek always as safety measure |
1027 |
< |
if (n) ***/ /* alignment */ |
1285 |
< |
if (lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0) |
1286 |
< |
goto seekerr; |
1025 |
> |
lastpos = flen - n; /* check alignment */ |
1026 |
> |
if (n && lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0) |
1027 |
> |
goto seekerr; |
1028 |
|
} |
1029 |
|
n = fflush(ambfp); /* calls write() at last */ |
1030 |
< |
if (n != EOF) |
1290 |
< |
lastpos += (long)nunflshed*AMBVALSIZ; |
1291 |
< |
else if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0) |
1292 |
< |
goto seekerr; |
1293 |
< |
|
1030 |
> |
lastpos += (long)nunflshed*AMBVALSIZ; |
1031 |
|
aflock(F_UNLCK); /* release file */ |
1032 |
|
nunflshed = 0; |
1033 |
|
return(n); |
1034 |
|
seekerr: |
1035 |
|
error(SYSTEM, "seek failed in ambsync"); |
1036 |
< |
return -1; /* pro forma return */ |
1036 |
> |
return(EOF); /* pro forma return */ |
1037 |
|
} |
1038 |
|
|
1039 |
|
#else /* ! F_SETLKW */ |