39 |
|
|
40 |
|
#ifndef SORT_THRESH |
41 |
|
#ifdef SMLMEM |
42 |
< |
#define SORT_THRESH ((3L<<20)/sizeof(AMBVAL)) |
42 |
> |
#define SORT_THRESH ((16L<<20)/sizeof(AMBVAL)) |
43 |
|
#else |
44 |
< |
#define SORT_THRESH ((9L<<20)/sizeof(AMBVAL)) |
44 |
> |
#define SORT_THRESH ((64L<<20)/sizeof(AMBVAL)) |
45 |
|
#endif |
46 |
|
#endif |
47 |
|
#ifndef SORT_INTVL |
84 |
|
static AMBTREE *newambtree(void); |
85 |
|
static void freeambtree(AMBTREE *atp); |
86 |
|
|
87 |
< |
typedef void unloadtf_t(void *); |
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); |
99 |
|
#endif |
100 |
|
|
101 |
|
|
102 |
< |
extern void |
102 |
> |
void |
103 |
|
setambres( /* set ambient resolution */ |
104 |
|
int ar |
105 |
|
) |
122 |
|
} |
123 |
|
|
124 |
|
|
125 |
< |
extern void |
125 |
> |
void |
126 |
|
setambacc( /* set ambient accuracy */ |
127 |
|
double newa |
128 |
|
) |
137 |
|
} |
138 |
|
|
139 |
|
|
140 |
< |
extern void |
140 |
> |
void |
141 |
|
setambient(void) /* initialize calculation */ |
142 |
|
{ |
143 |
|
int readonly = 0; |
195 |
|
sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile); |
196 |
|
error(SYSTEM, errmsg); |
197 |
|
} |
198 |
+ |
#ifdef getc_unlocked |
199 |
+ |
flockfile(ambfp); /* application-level lock */ |
200 |
+ |
#endif |
201 |
|
#ifdef F_SETLKW |
202 |
|
aflock(F_UNLCK); /* release file */ |
203 |
|
#endif |
204 |
|
} |
205 |
|
|
206 |
|
|
207 |
< |
extern void |
207 |
> |
void |
208 |
|
ambdone(void) /* close ambient file and free memory */ |
209 |
|
{ |
210 |
|
if (ambfp != NULL) { /* close ambient file */ |
218 |
|
lastpos = -1; |
219 |
|
} |
220 |
|
/* free ambient tree */ |
221 |
< |
unloadatree(&atrunk, free); |
221 |
> |
unloadatree(&atrunk, &avfree); |
222 |
|
/* reset state variables */ |
223 |
|
avsum = 0.; |
224 |
|
navsum = 0; |
230 |
|
} |
231 |
|
|
232 |
|
|
233 |
< |
extern void |
233 |
> |
void |
234 |
|
ambnotify( /* record new modifier */ |
235 |
|
OBJECT obj |
236 |
|
) |
237 |
|
{ |
238 |
|
static int hitlimit = 0; |
239 |
< |
register OBJREC *o; |
240 |
< |
register char **amblp; |
239 |
> |
OBJREC *o; |
240 |
> |
char **amblp; |
241 |
|
|
242 |
|
if (obj == OVOID) { /* starting over */ |
243 |
|
ambset[0] = 0; |
259 |
|
} |
260 |
|
} |
261 |
|
|
262 |
+ |
/************ THE FOLLOWING ROUTINES DIFFER BETWEEN NEW & OLD ***************/ |
263 |
|
|
264 |
< |
extern void |
264 |
> |
#ifdef NEWAMB |
265 |
> |
|
266 |
> |
#define tfunc(lwr, x, upr) (((x)-(lwr)/((upr)-(lwr))) |
267 |
> |
|
268 |
> |
|
269 |
> |
void |
270 |
|
multambient( /* compute ambient component & multiply by coef. */ |
271 |
|
COLOR aval, |
272 |
< |
register RAY *r, |
272 |
> |
RAY *r, |
273 |
|
FVECT nrm |
274 |
|
) |
275 |
|
{ |
276 |
|
static int rdepth = 0; /* ambient recursion */ |
277 |
|
COLOR acol; |
278 |
+ |
int ok; |
279 |
|
double d, l; |
280 |
|
|
281 |
|
if (ambdiv <= 0) /* no ambient calculation */ |
291 |
|
if (ambacc <= FTINY) { /* no ambient storage */ |
292 |
|
copycolor(acol, aval); |
293 |
|
rdepth++; |
294 |
+ |
ok = doambient(acol, r, r->rweight, NULL, NULL, NULL, NULL); |
295 |
+ |
rdepth--; |
296 |
+ |
if (!ok) |
297 |
+ |
goto dumbamb; |
298 |
+ |
copycolor(aval, acol); |
299 |
+ |
return; |
300 |
+ |
} |
301 |
+ |
|
302 |
+ |
if (tracktime) /* sort to minimize thrashing */ |
303 |
+ |
sortambvals(0); |
304 |
+ |
/* interpolate ambient value */ |
305 |
+ |
setcolor(acol, 0.0, 0.0, 0.0); |
306 |
+ |
d = sumambient(acol, r, nrm, rdepth, |
307 |
+ |
&atrunk, thescene.cuorg, thescene.cusize); |
308 |
+ |
if (d > FTINY) { |
309 |
+ |
d = 1.0/d; |
310 |
+ |
scalecolor(acol, d); |
311 |
+ |
multcolor(aval, acol); |
312 |
+ |
return; |
313 |
+ |
} |
314 |
+ |
rdepth++; /* need to cache new value */ |
315 |
+ |
ok = makeambient(acol, r, nrm, rdepth-1); |
316 |
+ |
rdepth--; |
317 |
+ |
if (ok) { |
318 |
+ |
multcolor(aval, acol); /* got new value */ |
319 |
+ |
return; |
320 |
+ |
} |
321 |
+ |
dumbamb: /* return global value */ |
322 |
+ |
if ((ambvwt <= 0) | (navsum == 0)) { |
323 |
+ |
multcolor(aval, ambval); |
324 |
+ |
return; |
325 |
+ |
} |
326 |
+ |
l = bright(ambval); /* average in computations */ |
327 |
+ |
if (l > FTINY) { |
328 |
+ |
d = (log(l)*(double)ambvwt + avsum) / |
329 |
+ |
(double)(ambvwt + navsum); |
330 |
+ |
d = exp(d) / l; |
331 |
+ |
scalecolor(aval, d); |
332 |
+ |
multcolor(aval, ambval); /* apply color of ambval */ |
333 |
+ |
} else { |
334 |
+ |
d = exp( avsum / (double)navsum ); |
335 |
+ |
scalecolor(aval, d); /* neutral color */ |
336 |
+ |
} |
337 |
+ |
} |
338 |
+ |
|
339 |
+ |
|
340 |
+ |
double |
341 |
+ |
sumambient( /* get interpolated ambient value */ |
342 |
+ |
COLOR acol, |
343 |
+ |
RAY *r, |
344 |
+ |
FVECT rn, |
345 |
+ |
int al, |
346 |
+ |
AMBTREE *at, |
347 |
+ |
FVECT c0, |
348 |
+ |
double s |
349 |
+ |
) |
350 |
+ |
{ /* initial limit is ambacc radians */ |
351 |
+ |
const double maxangle = (ambacc-PI/2.)*pow(r->rweight,0.13) + PI/2.; |
352 |
+ |
double wsum = 0.0; |
353 |
+ |
FVECT ck0; |
354 |
+ |
int i, j; |
355 |
+ |
AMBVAL *av; |
356 |
+ |
/* sum this node */ |
357 |
+ |
for (av = at->alist; av != NULL; av = av->next) { |
358 |
+ |
double d, delta_r2, delta_t2; |
359 |
+ |
COLOR ct; |
360 |
+ |
FVECT uvw[3]; |
361 |
+ |
/* record access */ |
362 |
+ |
if (tracktime) |
363 |
+ |
av->latick = ambclock; |
364 |
+ |
/* |
365 |
+ |
* Ambient level test. |
366 |
+ |
*/ |
367 |
+ |
if (av->lvl > al) /* list sorted, so this works */ |
368 |
+ |
break; |
369 |
+ |
if (av->weight < 0.9*r->rweight) |
370 |
+ |
continue; |
371 |
+ |
/* |
372 |
+ |
* Direction test using unperturbed normal. |
373 |
+ |
*/ |
374 |
+ |
decodedir(uvw[2], av->ndir); |
375 |
+ |
d = DOT(uvw[2], r->ron); |
376 |
+ |
if (d <= 0.0) /* >= 90 degrees */ |
377 |
+ |
continue; |
378 |
+ |
delta_r2 = 2.0 - 2.0*d; /* approx. radians^2 */ |
379 |
+ |
if (delta_r2 >= maxangle*maxangle) |
380 |
+ |
continue; |
381 |
+ |
/* |
382 |
+ |
* Ambient radius test. |
383 |
+ |
*/ |
384 |
+ |
decodedir(uvw[0], av->udir); |
385 |
+ |
VCROSS(uvw[1], uvw[2], uvw[0]); |
386 |
+ |
VSUB(ck0, av->pos, r->rop); |
387 |
+ |
d = DOT(ck0, uvw[0]) / av->rad[0]; |
388 |
+ |
delta_t2 = d*d; |
389 |
+ |
d = DOT(ck0, uvw[1]) / av->rad[1]; |
390 |
+ |
delta_t2 += d*d; |
391 |
+ |
if (delta_t2 >= ambacc*ambacc) |
392 |
+ |
continue; |
393 |
+ |
/* |
394 |
+ |
* Ray behind test. |
395 |
+ |
*/ |
396 |
+ |
d = 0.0; |
397 |
+ |
for (j = 0; j < 3; j++) |
398 |
+ |
d += (r->rop[j] - av->pos[j])*(uvw[2][j] + r->ron[j]); |
399 |
+ |
if (d*0.5 < -minarad*ambacc-.001) |
400 |
+ |
continue; |
401 |
+ |
/* |
402 |
+ |
* Convert to final weight (hat function) |
403 |
+ |
*/ |
404 |
+ |
d = tfunc(maxangle, sqrt(delta_r2), 0.0) * |
405 |
+ |
tfunc(ambacc, sqrt(delta_t2), 0.0); |
406 |
+ |
wsum += d; |
407 |
+ |
extambient(ct, av, uvw, r->rop, rn); |
408 |
+ |
scalecolor(ct, d); |
409 |
+ |
addcolor(acol, ct); |
410 |
+ |
} |
411 |
+ |
if (at->kid == NULL) |
412 |
+ |
return(wsum); |
413 |
+ |
/* sum children */ |
414 |
+ |
s *= 0.5; |
415 |
+ |
for (i = 0; i < 8; i++) { |
416 |
+ |
for (j = 0; j < 3; j++) { |
417 |
+ |
ck0[j] = c0[j]; |
418 |
+ |
if (1<<j & i) |
419 |
+ |
ck0[j] += s; |
420 |
+ |
if (r->rop[j] < ck0[j] - OCTSCALE*s) |
421 |
+ |
break; |
422 |
+ |
if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s) |
423 |
+ |
break; |
424 |
+ |
} |
425 |
+ |
if (j == 3) |
426 |
+ |
wsum += sumambient(acol, r, rn, al, |
427 |
+ |
at->kid+i, ck0, s); |
428 |
+ |
} |
429 |
+ |
return(wsum); |
430 |
+ |
} |
431 |
+ |
|
432 |
+ |
|
433 |
+ |
int |
434 |
+ |
makeambient( /* make a new ambient value for storage */ |
435 |
+ |
COLOR acol, |
436 |
+ |
RAY *r, |
437 |
+ |
FVECT rn, |
438 |
+ |
int al |
439 |
+ |
) |
440 |
+ |
{ |
441 |
+ |
AMBVAL amb; |
442 |
+ |
FVECT uv[2]; |
443 |
+ |
int i; |
444 |
+ |
|
445 |
+ |
amb.weight = 1.0; /* compute weight */ |
446 |
+ |
for (i = al; i-- > 0; ) |
447 |
+ |
amb.weight *= AVGREFL; |
448 |
+ |
if (r->rweight < 0.1*amb.weight) /* heuristic override */ |
449 |
+ |
amb.weight = 1.25*r->rweight; |
450 |
+ |
setcolor(acol, AVGREFL, AVGREFL, AVGREFL); |
451 |
+ |
/* compute ambient */ |
452 |
+ |
if (!doambient(acol, r, amb.weight, uv, amb.rad, amb.gpos, amb.gdir)) { |
453 |
+ |
setcolor(acol, 0.0, 0.0, 0.0); |
454 |
+ |
return(0); |
455 |
+ |
} |
456 |
+ |
scalecolor(acol, 1./AVGREFL); /* undo assumed reflectance */ |
457 |
+ |
/* store value */ |
458 |
+ |
VCOPY(amb.pos, r->rop); |
459 |
+ |
amb.ndir = encodedir(r->ron); |
460 |
+ |
amb.udir = encodedir(uv[0]); |
461 |
+ |
amb.lvl = al; |
462 |
+ |
copycolor(amb.val, acol); |
463 |
+ |
/* insert into tree */ |
464 |
+ |
avsave(&amb); /* and save to file */ |
465 |
+ |
if (rn != r->ron) |
466 |
+ |
extambient(acol, &amb, r->rop, rn); /* texture */ |
467 |
+ |
return(1); |
468 |
+ |
} |
469 |
+ |
|
470 |
+ |
|
471 |
+ |
void |
472 |
+ |
extambient( /* extrapolate value at pv, nv */ |
473 |
+ |
COLOR cr, |
474 |
+ |
AMBVAL *ap, |
475 |
+ |
FVECT uvw[3], |
476 |
+ |
FVECT pv, |
477 |
+ |
FVECT nv |
478 |
+ |
) |
479 |
+ |
{ |
480 |
+ |
FVECT v1; |
481 |
+ |
int i; |
482 |
+ |
double d = 1.0; /* zeroeth order */ |
483 |
+ |
|
484 |
+ |
for (i = 3; i--; ) /* gradient due to translation */ |
485 |
+ |
d += (pv[i] - ap->pos[i]) * |
486 |
+ |
(ap->gpos[0]*uvw[0][i] + ap->gpos[1]*uvw[1][i]); |
487 |
+ |
|
488 |
+ |
VCROSS(v1, uvw[2], nv); /* gradient due to rotation */ |
489 |
+ |
for (i = 3; i--; ) |
490 |
+ |
d += v1[i] * (ap->gdir[0]*uvw[0][i] + ap->gdir[1]*uvw[1][i]); |
491 |
+ |
|
492 |
+ |
if (d <= 0.0) { |
493 |
+ |
setcolor(cr, 0.0, 0.0, 0.0); |
494 |
+ |
return; |
495 |
+ |
} |
496 |
+ |
copycolor(cr, ap->val); |
497 |
+ |
scalecolor(cr, d); |
498 |
+ |
} |
499 |
+ |
|
500 |
+ |
|
501 |
+ |
static void |
502 |
+ |
avinsert( /* insert ambient value in our tree */ |
503 |
+ |
AMBVAL *av |
504 |
+ |
) |
505 |
+ |
{ |
506 |
+ |
AMBTREE *at; |
507 |
+ |
AMBVAL *ap; |
508 |
+ |
AMBVAL avh; |
509 |
+ |
FVECT ck0; |
510 |
+ |
double s; |
511 |
+ |
int branch; |
512 |
+ |
int i; |
513 |
+ |
|
514 |
+ |
if (av->rad[0] <= FTINY) |
515 |
+ |
error(CONSISTENCY, "zero ambient radius in avinsert"); |
516 |
+ |
at = &atrunk; |
517 |
+ |
VCOPY(ck0, thescene.cuorg); |
518 |
+ |
s = thescene.cusize; |
519 |
+ |
while (s*(OCTSCALE/2) > av->rad[1]*ambacc) { |
520 |
+ |
if (at->kid == NULL) |
521 |
+ |
if ((at->kid = newambtree()) == NULL) |
522 |
+ |
error(SYSTEM, "out of memory in avinsert"); |
523 |
+ |
s *= 0.5; |
524 |
+ |
branch = 0; |
525 |
+ |
for (i = 0; i < 3; i++) |
526 |
+ |
if (av->pos[i] > ck0[i] + s) { |
527 |
+ |
ck0[i] += s; |
528 |
+ |
branch |= 1 << i; |
529 |
+ |
} |
530 |
+ |
at = at->kid + branch; |
531 |
+ |
} |
532 |
+ |
avh.next = at->alist; /* order by increasing level */ |
533 |
+ |
for (ap = &avh; ap->next != NULL; ap = ap->next) |
534 |
+ |
if (ap->next->lvl >= av->lvl) |
535 |
+ |
break; |
536 |
+ |
av->next = ap->next; |
537 |
+ |
ap->next = (AMBVAL*)av; |
538 |
+ |
at->alist = avh.next; |
539 |
+ |
} |
540 |
+ |
|
541 |
+ |
|
542 |
+ |
#else /* ! NEWAMB */ |
543 |
+ |
|
544 |
+ |
|
545 |
+ |
void |
546 |
+ |
multambient( /* compute ambient component & multiply by coef. */ |
547 |
+ |
COLOR aval, |
548 |
+ |
RAY *r, |
549 |
+ |
FVECT nrm |
550 |
+ |
) |
551 |
+ |
{ |
552 |
+ |
static int rdepth = 0; /* ambient recursion */ |
553 |
+ |
COLOR acol; |
554 |
+ |
double d, l; |
555 |
+ |
|
556 |
+ |
if (ambdiv <= 0) /* no ambient calculation */ |
557 |
+ |
goto dumbamb; |
558 |
+ |
/* check number of bounces */ |
559 |
+ |
if (rdepth >= ambounce) |
560 |
+ |
goto dumbamb; |
561 |
+ |
/* check ambient list */ |
562 |
+ |
if (ambincl != -1 && r->ro != NULL && |
563 |
+ |
ambincl != inset(ambset, r->ro->omod)) |
564 |
+ |
goto dumbamb; |
565 |
+ |
|
566 |
+ |
if (ambacc <= FTINY) { /* no ambient storage */ |
567 |
+ |
copycolor(acol, aval); |
568 |
+ |
rdepth++; |
569 |
|
d = doambient(acol, r, r->rweight, NULL, NULL); |
570 |
|
rdepth--; |
571 |
|
if (d <= FTINY) |
612 |
|
} |
613 |
|
|
614 |
|
|
615 |
< |
extern double |
615 |
> |
double |
616 |
|
sumambient( /* get interpolated ambient value */ |
617 |
|
COLOR acol, |
618 |
< |
register RAY *r, |
618 |
> |
RAY *r, |
619 |
|
FVECT rn, |
620 |
|
int al, |
621 |
|
AMBTREE *at, |
627 |
|
COLOR ct; |
628 |
|
FVECT ck0; |
629 |
|
int i; |
630 |
< |
register int j; |
631 |
< |
register AMBVAL *av; |
630 |
> |
int j; |
631 |
> |
AMBVAL *av; |
632 |
|
|
633 |
|
wsum = 0.0; |
634 |
|
/* do this node */ |
646 |
|
/* |
647 |
|
* Ambient radius test. |
648 |
|
*/ |
649 |
< |
d = av->pos[0] - r->rop[0]; |
650 |
< |
e1 = d * d; |
365 |
< |
d = av->pos[1] - r->rop[1]; |
366 |
< |
e1 += d * d; |
367 |
< |
d = av->pos[2] - r->rop[2]; |
368 |
< |
e1 += d * d; |
369 |
< |
e1 /= av->rad * av->rad; |
649 |
> |
VSUB(ck0, av->pos, r->rop); |
650 |
> |
e1 = DOT(ck0, ck0) / (av->rad * av->rad); |
651 |
|
if (e1 > ambacc*ambacc*1.21) |
652 |
|
continue; |
653 |
|
/* |
664 |
|
} |
665 |
|
} |
666 |
|
e2 = (1.0 - d) * r->rweight; |
667 |
< |
if (e2 < 0.0) e2 = 0.0; |
668 |
< |
if (e1 + e2 > ambacc*ambacc*1.21) |
667 |
> |
if (e2 < 0.0) |
668 |
> |
e2 = 0.0; |
669 |
> |
else if (e1 + e2 > ambacc*ambacc*1.21) |
670 |
|
continue; |
671 |
|
/* |
672 |
|
* Ray behind test. |
723 |
|
} |
724 |
|
|
725 |
|
|
726 |
< |
extern double |
726 |
> |
double |
727 |
|
makeambient( /* make a new ambient value for storage */ |
728 |
|
COLOR acol, |
729 |
|
RAY *r, |
763 |
|
} |
764 |
|
|
765 |
|
|
766 |
< |
extern void |
766 |
> |
void |
767 |
|
extambient( /* extrapolate value at pv, nv */ |
768 |
|
COLOR cr, |
769 |
< |
register AMBVAL *ap, |
769 |
> |
AMBVAL *ap, |
770 |
|
FVECT pv, |
771 |
|
FVECT nv |
772 |
|
) |
773 |
|
{ |
774 |
|
FVECT v1; |
775 |
< |
register int i; |
775 |
> |
int i; |
776 |
|
double d; |
777 |
|
|
778 |
|
d = 1.0; /* zeroeth order */ |
792 |
|
|
793 |
|
|
794 |
|
static void |
795 |
+ |
avinsert( /* insert ambient value in our tree */ |
796 |
+ |
AMBVAL *av |
797 |
+ |
) |
798 |
+ |
{ |
799 |
+ |
AMBTREE *at; |
800 |
+ |
AMBVAL *ap; |
801 |
+ |
AMBVAL avh; |
802 |
+ |
FVECT ck0; |
803 |
+ |
double s; |
804 |
+ |
int branch; |
805 |
+ |
int i; |
806 |
+ |
|
807 |
+ |
if (av->rad <= FTINY) |
808 |
+ |
error(CONSISTENCY, "zero ambient radius in avinsert"); |
809 |
+ |
at = &atrunk; |
810 |
+ |
VCOPY(ck0, thescene.cuorg); |
811 |
+ |
s = thescene.cusize; |
812 |
+ |
while (s*(OCTSCALE/2) > av->rad*ambacc) { |
813 |
+ |
if (at->kid == NULL) |
814 |
+ |
if ((at->kid = newambtree()) == NULL) |
815 |
+ |
error(SYSTEM, "out of memory in avinsert"); |
816 |
+ |
s *= 0.5; |
817 |
+ |
branch = 0; |
818 |
+ |
for (i = 0; i < 3; i++) |
819 |
+ |
if (av->pos[i] > ck0[i] + s) { |
820 |
+ |
ck0[i] += s; |
821 |
+ |
branch |= 1 << i; |
822 |
+ |
} |
823 |
+ |
at = at->kid + branch; |
824 |
+ |
} |
825 |
+ |
avh.next = at->alist; /* order by increasing level */ |
826 |
+ |
for (ap = &avh; ap->next != NULL; ap = ap->next) |
827 |
+ |
if (ap->next->lvl >= av->lvl) |
828 |
+ |
break; |
829 |
+ |
av->next = ap->next; |
830 |
+ |
ap->next = (AMBVAL*)av; |
831 |
+ |
at->alist = avh.next; |
832 |
+ |
} |
833 |
+ |
|
834 |
+ |
#endif /* ! NEWAMB */ |
835 |
+ |
|
836 |
+ |
/************* FOLLOWING ROUTINES SAME FOR NEW & OLD METHODS ***************/ |
837 |
+ |
|
838 |
+ |
static void |
839 |
|
initambfile( /* initialize ambient file */ |
840 |
|
int cre8 |
841 |
|
) |
844 |
|
static char *mybuf = NULL; |
845 |
|
|
846 |
|
#ifdef F_SETLKW |
847 |
< |
aflock(creat ? F_WRLCK : F_RDLCK); |
847 |
> |
aflock(cre8 ? F_WRLCK : F_RDLCK); |
848 |
|
#endif |
849 |
|
SET_FILE_BINARY(ambfp); |
850 |
|
if (mybuf == NULL) |
892 |
|
|
893 |
|
static AMBVAL * |
894 |
|
avstore( /* allocate memory and store aval */ |
895 |
< |
register AMBVAL *aval |
895 |
> |
AMBVAL *aval |
896 |
|
) |
897 |
|
{ |
898 |
< |
register AMBVAL *av; |
898 |
> |
AMBVAL *av; |
899 |
|
double d; |
900 |
|
|
901 |
|
if ((av = newambval()) == NULL) |
921 |
|
static AMBTREE * |
922 |
|
newambtree(void) /* allocate 8 ambient tree structs */ |
923 |
|
{ |
924 |
< |
register AMBTREE *atp, *upperlim; |
924 |
> |
AMBTREE *atp, *upperlim; |
925 |
|
|
926 |
|
if (atfreelist == NULL) { /* get more nodes */ |
927 |
|
atfreelist = (AMBTREE *)malloc(ATALLOCSZ*8*sizeof(AMBTREE)); |
951 |
|
|
952 |
|
|
953 |
|
static void |
628 |
– |
avinsert( /* insert ambient value in our tree */ |
629 |
– |
void *av |
630 |
– |
) |
631 |
– |
{ |
632 |
– |
register AMBTREE *at; |
633 |
– |
register AMBVAL *ap; |
634 |
– |
AMBVAL avh; |
635 |
– |
FVECT ck0; |
636 |
– |
double s; |
637 |
– |
int branch; |
638 |
– |
register int i; |
639 |
– |
|
640 |
– |
if (((AMBVAL*)av)->rad <= FTINY) |
641 |
– |
error(CONSISTENCY, "zero ambient radius in avinsert"); |
642 |
– |
at = &atrunk; |
643 |
– |
VCOPY(ck0, thescene.cuorg); |
644 |
– |
s = thescene.cusize; |
645 |
– |
while (s*(OCTSCALE/2) > ((AMBVAL*)av)->rad*ambacc) { |
646 |
– |
if (at->kid == NULL) |
647 |
– |
if ((at->kid = newambtree()) == NULL) |
648 |
– |
error(SYSTEM, "out of memory in avinsert"); |
649 |
– |
s *= 0.5; |
650 |
– |
branch = 0; |
651 |
– |
for (i = 0; i < 3; i++) |
652 |
– |
if (((AMBVAL*)av)->pos[i] > ck0[i] + s) { |
653 |
– |
ck0[i] += s; |
654 |
– |
branch |= 1 << i; |
655 |
– |
} |
656 |
– |
at = at->kid + branch; |
657 |
– |
} |
658 |
– |
avh.next = at->alist; /* order by increasing level */ |
659 |
– |
for (ap = &avh; ap->next != NULL; ap = ap->next) |
660 |
– |
if (ap->next->lvl >= ((AMBVAL*)av)->lvl) |
661 |
– |
break; |
662 |
– |
((AMBVAL*)av)->next = ap->next; |
663 |
– |
ap->next = (AMBVAL*)av; |
664 |
– |
at->alist = avh.next; |
665 |
– |
} |
666 |
– |
|
667 |
– |
|
668 |
– |
static void |
954 |
|
unloadatree( /* unload an ambient value tree */ |
955 |
< |
register AMBTREE *at, |
955 |
> |
AMBTREE *at, |
956 |
|
unloadtf_t *f |
957 |
|
) |
958 |
|
{ |
959 |
< |
register AMBVAL *av; |
960 |
< |
register int i; |
959 |
> |
AMBVAL *av; |
960 |
> |
int i; |
961 |
|
/* transfer values at this node */ |
962 |
|
for (av = at->alist; av != NULL; av = at->alist) { |
963 |
|
at->alist = av->next; |
982 |
|
static int alatcmp(const void *av1, const void *av2); |
983 |
|
|
984 |
|
static void |
985 |
+ |
avfree(AMBVAL *av) |
986 |
+ |
{ |
987 |
+ |
free(av); |
988 |
+ |
} |
989 |
+ |
|
990 |
+ |
static void |
991 |
|
av2list( |
992 |
< |
void *av |
992 |
> |
AMBVAL *av |
993 |
|
) |
994 |
|
{ |
995 |
|
#ifdef DEBUG |
997 |
|
error(CONSISTENCY, "too many ambient values in av2list1"); |
998 |
|
#endif |
999 |
|
avlist1[i_avlist].p = avlist2[i_avlist] = (AMBVAL*)av; |
1000 |
< |
avlist1[i_avlist++].t = ((AMBVAL*)av)->latick; |
1000 |
> |
avlist1[i_avlist++].t = av->latick; |
1001 |
|
} |
1002 |
|
|
1003 |
|
|
1007 |
|
const void *av2 |
1008 |
|
) |
1009 |
|
{ |
1010 |
< |
register long lc = ((struct avl *)av2)->t - ((struct avl *)av1)->t; |
1010 |
> |
long lc = ((struct avl *)av2)->t - ((struct avl *)av1)->t; |
1011 |
|
return(lc<0 ? -1 : lc>0 ? 1 : 0); |
1012 |
|
} |
1013 |
|
|
1024 |
|
const void *avp2 |
1025 |
|
) |
1026 |
|
{ |
1027 |
< |
register long diff = *(char * const *)avp1 - *(char * const *)avp2; |
1027 |
> |
long diff = *(char * const *)avp1 - *(char * const *)avp2; |
1028 |
|
if (diff < 0) |
1029 |
|
return(-1); |
1030 |
|
return(diff > 0); |
1031 |
|
} |
1032 |
|
|
1033 |
< |
#if 1 |
1033 |
> |
|
1034 |
|
static int |
1035 |
|
avlmemi( /* find list position from address */ |
1036 |
|
AMBVAL *avaddr |
1037 |
|
) |
1038 |
|
{ |
1039 |
< |
register AMBVAL **avlpp; |
1039 |
> |
AMBVAL **avlpp; |
1040 |
|
|
1041 |
|
avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2, |
1042 |
< |
nambvals, sizeof(AMBVAL *), aposcmp); |
1042 |
> |
nambvals, sizeof(AMBVAL *), &aposcmp); |
1043 |
|
if (avlpp == NULL) |
1044 |
|
error(CONSISTENCY, "address not found in avlmemi"); |
1045 |
|
return(avlpp - avlist2); |
1046 |
|
} |
756 |
– |
#else |
757 |
– |
#define avlmemi(avaddr) ((AMBVAL **)bsearch((char *)&avaddr,(char *)avlist2, \ |
758 |
– |
nambvals,sizeof(AMBVAL *),aposcmp) - avlist2) |
759 |
– |
#endif |
1047 |
|
|
1048 |
|
|
1049 |
|
static void |
1053 |
|
{ |
1054 |
|
AMBTREE oldatrunk; |
1055 |
|
AMBVAL tav, *tap, *pnext; |
1056 |
< |
register int i, j; |
1056 |
> |
int i, j; |
1057 |
|
/* see if it's time yet */ |
1058 |
|
if (!always && (ambclock++ < lastsort+sortintvl || |
1059 |
|
nambvals < SORT_THRESH)) |
1087 |
|
oldatrunk = atrunk; |
1088 |
|
atrunk.alist = NULL; |
1089 |
|
atrunk.kid = NULL; |
1090 |
< |
unloadatree(&oldatrunk, avinsert); |
1090 |
> |
unloadatree(&oldatrunk, &avinsert); |
1091 |
|
} |
1092 |
|
} else { /* sort memory by last access time */ |
1093 |
|
/* |
1104 |
|
eputs(errmsg); |
1105 |
|
#endif |
1106 |
|
i_avlist = 0; |
1107 |
< |
unloadatree(&atrunk, av2list); /* empty current tree */ |
1107 |
> |
unloadatree(&atrunk, &av2list); /* empty current tree */ |
1108 |
|
#ifdef DEBUG |
1109 |
|
if (i_avlist < nambvals) |
1110 |
|
error(CONSISTENCY, "missing ambient values in sortambvals"); |
1111 |
|
#endif |
1112 |
< |
qsort((char *)avlist1, nambvals, sizeof(struct avl), alatcmp); |
1113 |
< |
qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp); |
1112 |
> |
qsort((char *)avlist1, nambvals, sizeof(struct avl), &alatcmp); |
1113 |
> |
qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), &aposcmp); |
1114 |
|
for (i = 0; i < nambvals; i++) { |
1115 |
|
if (avlist1[i].p == NULL) |
1116 |
|
continue; |
1161 |
|
} |
1162 |
|
|
1163 |
|
|
1164 |
< |
extern int |
1164 |
> |
int |
1165 |
|
ambsync(void) /* synchronize ambient file */ |
1166 |
|
{ |
1167 |
|
long flen; |
1168 |
|
AMBVAL avs; |
1169 |
< |
register int n; |
1169 |
> |
int n; |
1170 |
|
|
1171 |
|
if (ambfp == NULL) /* no ambient file? */ |
1172 |
|
return(0); |
1214 |
|
return -1; /* pro forma return */ |
1215 |
|
} |
1216 |
|
|
1217 |
< |
#else |
1217 |
> |
#else /* ! F_SETLKW */ |
1218 |
|
|
1219 |
< |
extern int |
1219 |
> |
int |
1220 |
|
ambsync(void) /* flush ambient file */ |
1221 |
|
{ |
1222 |
|
if (ambfp == NULL) |
1225 |
|
return(fflush(ambfp)); |
1226 |
|
} |
1227 |
|
|
1228 |
< |
#endif |
1228 |
> |
#endif /* ! F_SETLKW */ |