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 |
51 |
|
#define MAX_SORT_INTVL (SORT_INTVL<<6) |
52 |
|
#endif |
53 |
|
|
54 |
+ |
|
55 |
|
static double avsum = 0.; /* computed ambient value sum (log) */ |
56 |
|
static unsigned int navsum = 0; /* number of values in avsum */ |
57 |
|
static unsigned int nambvals = 0; /* total number of indirect values */ |
85 |
|
static AMBTREE *newambtree(void); |
86 |
|
static void freeambtree(AMBTREE *atp); |
87 |
|
|
88 |
< |
typedef void unloadtf_t(void *); |
88 |
> |
typedef void unloadtf_t(AMBVAL *); |
89 |
|
static unloadtf_t avinsert; |
90 |
|
static unloadtf_t av2list; |
91 |
+ |
static unloadtf_t avfree; |
92 |
|
static void unloadatree(AMBTREE *at, unloadtf_t *f); |
93 |
|
|
94 |
|
static int aposcmp(const void *avp1, const void *avp2); |
100 |
|
#endif |
101 |
|
|
102 |
|
|
103 |
< |
extern void |
103 |
> |
void |
104 |
|
setambres( /* set ambient resolution */ |
105 |
|
int ar |
106 |
|
) |
109 |
|
/* set min & max radii */ |
110 |
|
if (ar <= 0) { |
111 |
|
minarad = 0; |
112 |
< |
maxarad = thescene.cusize / 2.0; |
112 |
> |
maxarad = thescene.cusize*0.5; |
113 |
|
} else { |
114 |
|
minarad = thescene.cusize / ar; |
115 |
< |
maxarad = 64 * minarad; /* heuristic */ |
116 |
< |
if (maxarad > thescene.cusize / 2.0) |
117 |
< |
maxarad = thescene.cusize / 2.0; |
115 |
> |
maxarad = 64.0 * minarad; /* heuristic */ |
116 |
> |
if (maxarad > thescene.cusize*0.5) |
117 |
> |
maxarad = thescene.cusize*0.5; |
118 |
|
} |
119 |
|
if (minarad <= FTINY) |
120 |
< |
minarad = 10*FTINY; |
120 |
> |
minarad = 10.0*FTINY; |
121 |
|
if (maxarad <= minarad) |
122 |
< |
maxarad = 64 * minarad; |
122 |
> |
maxarad = 64.0 * minarad; |
123 |
|
} |
124 |
|
|
125 |
|
|
126 |
< |
extern void |
126 |
> |
void |
127 |
|
setambacc( /* set ambient accuracy */ |
128 |
|
double newa |
129 |
|
) |
130 |
|
{ |
131 |
< |
double ambdiff; |
132 |
< |
|
133 |
< |
if (newa < 0.0) |
134 |
< |
newa = 0.0; |
135 |
< |
ambdiff = fabs(newa - ambacc); |
136 |
< |
if (ambdiff >= .01 && (ambacc = newa) > FTINY && nambvals > 0) |
137 |
< |
sortambvals(1); /* rebuild tree */ |
131 |
> |
static double olda; /* remember previous setting here */ |
132 |
> |
|
133 |
> |
newa *= (newa > 0); |
134 |
> |
if (fabs(newa - olda) >= .05*(newa + olda)) { |
135 |
> |
ambacc = newa; |
136 |
> |
if (nambvals > 0) |
137 |
> |
sortambvals(1); /* rebuild tree */ |
138 |
> |
} |
139 |
|
} |
140 |
|
|
141 |
|
|
142 |
< |
extern void |
142 |
> |
void |
143 |
|
setambient(void) /* initialize calculation */ |
144 |
|
{ |
145 |
|
int readonly = 0; |
197 |
|
sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile); |
198 |
|
error(SYSTEM, errmsg); |
199 |
|
} |
200 |
+ |
#ifdef getc_unlocked |
201 |
+ |
flockfile(ambfp); /* application-level lock */ |
202 |
+ |
#endif |
203 |
|
#ifdef F_SETLKW |
204 |
|
aflock(F_UNLCK); /* release file */ |
205 |
|
#endif |
206 |
|
} |
207 |
|
|
208 |
|
|
209 |
< |
extern void |
209 |
> |
void |
210 |
|
ambdone(void) /* close ambient file and free memory */ |
211 |
|
{ |
212 |
|
if (ambfp != NULL) { /* close ambient file */ |
220 |
|
lastpos = -1; |
221 |
|
} |
222 |
|
/* free ambient tree */ |
223 |
< |
unloadatree(&atrunk, free); |
223 |
> |
unloadatree(&atrunk, &avfree); |
224 |
|
/* reset state variables */ |
225 |
|
avsum = 0.; |
226 |
|
navsum = 0; |
232 |
|
} |
233 |
|
|
234 |
|
|
235 |
< |
extern void |
235 |
> |
void |
236 |
|
ambnotify( /* record new modifier */ |
237 |
|
OBJECT obj |
238 |
|
) |
261 |
|
} |
262 |
|
} |
263 |
|
|
264 |
+ |
/************ THE FOLLOWING ROUTINES DIFFER BETWEEN NEW & OLD ***************/ |
265 |
|
|
266 |
< |
extern void |
266 |
> |
#ifdef NEWAMB |
267 |
> |
|
268 |
> |
#define tfunc(lwr, x, upr) (((x)-(lwr))/((upr)-(lwr))) |
269 |
> |
|
270 |
> |
static double sumambient(COLOR acol, RAY *r, FVECT rn, int al, |
271 |
> |
AMBTREE *at, FVECT c0, double s); |
272 |
> |
static int makeambient(COLOR acol, RAY *r, FVECT rn, int al); |
273 |
> |
static void extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv, |
274 |
> |
FVECT uvw[3]); |
275 |
> |
|
276 |
> |
void |
277 |
|
multambient( /* compute ambient component & multiply by coef. */ |
278 |
|
COLOR aval, |
279 |
|
RAY *r, |
282 |
|
{ |
283 |
|
static int rdepth = 0; /* ambient recursion */ |
284 |
|
COLOR acol; |
285 |
+ |
int ok; |
286 |
|
double d, l; |
287 |
|
|
288 |
|
if (ambdiv <= 0) /* no ambient calculation */ |
298 |
|
if (ambacc <= FTINY) { /* no ambient storage */ |
299 |
|
copycolor(acol, aval); |
300 |
|
rdepth++; |
301 |
+ |
ok = doambient(acol, r, r->rweight, NULL, NULL, NULL, NULL); |
302 |
+ |
rdepth--; |
303 |
+ |
if (!ok) |
304 |
+ |
goto dumbamb; |
305 |
+ |
copycolor(aval, acol); |
306 |
+ |
return; |
307 |
+ |
} |
308 |
+ |
|
309 |
+ |
if (tracktime) /* sort to minimize thrashing */ |
310 |
+ |
sortambvals(0); |
311 |
+ |
/* interpolate ambient value */ |
312 |
+ |
setcolor(acol, 0.0, 0.0, 0.0); |
313 |
+ |
d = sumambient(acol, r, nrm, rdepth, |
314 |
+ |
&atrunk, thescene.cuorg, thescene.cusize); |
315 |
+ |
if (d > FTINY) { |
316 |
+ |
d = 1.0/d; |
317 |
+ |
scalecolor(acol, d); |
318 |
+ |
multcolor(aval, acol); |
319 |
+ |
return; |
320 |
+ |
} |
321 |
+ |
rdepth++; /* need to cache new value */ |
322 |
+ |
ok = makeambient(acol, r, nrm, rdepth-1); |
323 |
+ |
rdepth--; |
324 |
+ |
if (ok) { |
325 |
+ |
multcolor(aval, acol); /* computed new value */ |
326 |
+ |
return; |
327 |
+ |
} |
328 |
+ |
dumbamb: /* return global value */ |
329 |
+ |
if ((ambvwt <= 0) | (navsum == 0)) { |
330 |
+ |
multcolor(aval, ambval); |
331 |
+ |
return; |
332 |
+ |
} |
333 |
+ |
l = bright(ambval); /* average in computations */ |
334 |
+ |
if (l > FTINY) { |
335 |
+ |
d = (log(l)*(double)ambvwt + avsum) / |
336 |
+ |
(double)(ambvwt + navsum); |
337 |
+ |
d = exp(d) / l; |
338 |
+ |
scalecolor(aval, d); |
339 |
+ |
multcolor(aval, ambval); /* apply color of ambval */ |
340 |
+ |
} else { |
341 |
+ |
d = exp( avsum / (double)navsum ); |
342 |
+ |
scalecolor(aval, d); /* neutral color */ |
343 |
+ |
} |
344 |
+ |
} |
345 |
+ |
|
346 |
+ |
|
347 |
+ |
double |
348 |
+ |
sumambient( /* get interpolated ambient value */ |
349 |
+ |
COLOR acol, |
350 |
+ |
RAY *r, |
351 |
+ |
FVECT rn, |
352 |
+ |
int al, |
353 |
+ |
AMBTREE *at, |
354 |
+ |
FVECT c0, |
355 |
+ |
double s |
356 |
+ |
) |
357 |
+ |
{ /* initial limit is 10 degrees plus ambacc radians */ |
358 |
+ |
const double minangle = 10.0 * PI/180.; |
359 |
+ |
double maxangle = minangle + ambacc; |
360 |
+ |
double wsum = 0.0; |
361 |
+ |
FVECT ck0; |
362 |
+ |
int i, j; |
363 |
+ |
AMBVAL *av; |
364 |
+ |
|
365 |
+ |
if (at->kid != NULL) { /* sum children first */ |
366 |
+ |
s *= 0.5; |
367 |
+ |
for (i = 0; i < 8; i++) { |
368 |
+ |
for (j = 0; j < 3; j++) { |
369 |
+ |
ck0[j] = c0[j]; |
370 |
+ |
if (1<<j & i) |
371 |
+ |
ck0[j] += s; |
372 |
+ |
if (r->rop[j] < ck0[j] - OCTSCALE*s) |
373 |
+ |
break; |
374 |
+ |
if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s) |
375 |
+ |
break; |
376 |
+ |
} |
377 |
+ |
if (j == 3) |
378 |
+ |
wsum += sumambient(acol, r, rn, al, |
379 |
+ |
at->kid+i, ck0, s); |
380 |
+ |
} |
381 |
+ |
/* good enough? */ |
382 |
+ |
if (wsum >= 0.05 && s > minarad*10.0) |
383 |
+ |
return(wsum); |
384 |
+ |
} |
385 |
+ |
/* adjust maximum angle */ |
386 |
+ |
if (at->alist != NULL && (at->alist->lvl <= al) & (r->rweight < 0.6)) |
387 |
+ |
maxangle = (maxangle - PI/2.)*pow(r->rweight,0.13) + PI/2.; |
388 |
+ |
/* sum this node */ |
389 |
+ |
for (av = at->alist; av != NULL; av = av->next) { |
390 |
+ |
double d, delta_r2, delta_t2; |
391 |
+ |
COLOR ct; |
392 |
+ |
FVECT uvw[3]; |
393 |
+ |
/* record access */ |
394 |
+ |
if (tracktime) |
395 |
+ |
av->latick = ambclock; |
396 |
+ |
/* |
397 |
+ |
* Ambient level test |
398 |
+ |
*/ |
399 |
+ |
if (av->lvl > al) /* list sorted, so this works */ |
400 |
+ |
break; |
401 |
+ |
if (av->weight < 0.9*r->rweight) |
402 |
+ |
continue; |
403 |
+ |
/* |
404 |
+ |
* Direction test using unperturbed normal |
405 |
+ |
*/ |
406 |
+ |
decodedir(uvw[2], av->ndir); |
407 |
+ |
d = DOT(uvw[2], r->ron); |
408 |
+ |
if (d <= 0.0) /* >= 90 degrees */ |
409 |
+ |
continue; |
410 |
+ |
delta_r2 = 2.0 - 2.0*d; /* approx. radians^2 */ |
411 |
+ |
if (delta_r2 >= maxangle*maxangle) |
412 |
+ |
continue; |
413 |
+ |
/* |
414 |
+ |
* Modified ray behind test |
415 |
+ |
*/ |
416 |
+ |
VSUB(ck0, av->pos, r->rop); |
417 |
+ |
d = DOT(ck0, uvw[2]); |
418 |
+ |
if (d < -minarad*ambacc-.001) |
419 |
+ |
continue; |
420 |
+ |
d /= av->rad[0]; |
421 |
+ |
delta_t2 = d*d; |
422 |
+ |
if (delta_t2 >= ambacc*ambacc) |
423 |
+ |
continue; |
424 |
+ |
/* |
425 |
+ |
* Elliptical radii test based on Hessian |
426 |
+ |
*/ |
427 |
+ |
decodedir(uvw[0], av->udir); |
428 |
+ |
VCROSS(uvw[1], uvw[2], uvw[0]); |
429 |
+ |
d = DOT(ck0, uvw[0]) / av->rad[0]; |
430 |
+ |
delta_t2 += d*d; |
431 |
+ |
d = DOT(ck0, uvw[1]) / av->rad[1]; |
432 |
+ |
delta_t2 += d*d; |
433 |
+ |
if (delta_t2 >= ambacc*ambacc) |
434 |
+ |
continue; |
435 |
+ |
/* |
436 |
+ |
* Extrapolate value and compute final weight (hat function) |
437 |
+ |
*/ |
438 |
+ |
extambient(ct, av, r->rop, rn, uvw); |
439 |
+ |
d = tfunc(maxangle, sqrt(delta_r2), 0.0) * |
440 |
+ |
tfunc(ambacc, sqrt(delta_t2), 0.0); |
441 |
+ |
scalecolor(ct, d); |
442 |
+ |
addcolor(acol, ct); |
443 |
+ |
wsum += d; |
444 |
+ |
} |
445 |
+ |
return(wsum); |
446 |
+ |
} |
447 |
+ |
|
448 |
+ |
|
449 |
+ |
int |
450 |
+ |
makeambient( /* make a new ambient value for storage */ |
451 |
+ |
COLOR acol, |
452 |
+ |
RAY *r, |
453 |
+ |
FVECT rn, |
454 |
+ |
int al |
455 |
+ |
) |
456 |
+ |
{ |
457 |
+ |
AMBVAL amb; |
458 |
+ |
FVECT uvw[3]; |
459 |
+ |
int i; |
460 |
+ |
|
461 |
+ |
amb.weight = 1.0; /* compute weight */ |
462 |
+ |
for (i = al; i-- > 0; ) |
463 |
+ |
amb.weight *= AVGREFL; |
464 |
+ |
if (r->rweight < 0.1*amb.weight) /* heuristic override */ |
465 |
+ |
amb.weight = 1.25*r->rweight; |
466 |
+ |
setcolor(acol, AVGREFL, AVGREFL, AVGREFL); |
467 |
+ |
/* compute ambient */ |
468 |
+ |
i = doambient(acol, r, amb.weight, uvw, amb.rad, amb.gpos, amb.gdir); |
469 |
+ |
scalecolor(acol, 1./AVGREFL); /* undo assumed reflectance */ |
470 |
+ |
if (i <= 0 || amb.rad[0] <= FTINY) /* no Hessian or zero radius */ |
471 |
+ |
return(i); |
472 |
+ |
/* store value */ |
473 |
+ |
VCOPY(amb.pos, r->rop); |
474 |
+ |
amb.ndir = encodedir(r->ron); |
475 |
+ |
amb.udir = encodedir(uvw[0]); |
476 |
+ |
amb.lvl = al; |
477 |
+ |
copycolor(amb.val, acol); |
478 |
+ |
/* insert into tree */ |
479 |
+ |
avsave(&amb); /* and save to file */ |
480 |
+ |
if (rn != r->ron) { /* texture */ |
481 |
+ |
VCOPY(uvw[2], r->ron); |
482 |
+ |
extambient(acol, &amb, r->rop, rn, uvw); |
483 |
+ |
} |
484 |
+ |
return(1); |
485 |
+ |
} |
486 |
+ |
|
487 |
+ |
|
488 |
+ |
void |
489 |
+ |
extambient( /* extrapolate value at pv, nv */ |
490 |
+ |
COLOR cr, |
491 |
+ |
AMBVAL *ap, |
492 |
+ |
FVECT pv, |
493 |
+ |
FVECT nv, |
494 |
+ |
FVECT uvw[3] |
495 |
+ |
) |
496 |
+ |
{ |
497 |
+ |
static FVECT my_uvw[3]; |
498 |
+ |
FVECT v1; |
499 |
+ |
int i; |
500 |
+ |
double d = 1.0; /* zeroeth order */ |
501 |
+ |
|
502 |
+ |
if (uvw == NULL) { /* need local coordinates? */ |
503 |
+ |
decodedir(my_uvw[2], ap->ndir); |
504 |
+ |
decodedir(my_uvw[0], ap->udir); |
505 |
+ |
VCROSS(my_uvw[1], my_uvw[2], my_uvw[0]); |
506 |
+ |
uvw = my_uvw; |
507 |
+ |
} |
508 |
+ |
for (i = 3; i--; ) /* gradient due to translation */ |
509 |
+ |
d += (pv[i] - ap->pos[i]) * |
510 |
+ |
(ap->gpos[0]*uvw[0][i] + ap->gpos[1]*uvw[1][i]); |
511 |
+ |
|
512 |
+ |
VCROSS(v1, uvw[2], nv); /* gradient due to rotation */ |
513 |
+ |
for (i = 3; i--; ) |
514 |
+ |
d += v1[i] * (ap->gdir[0]*uvw[0][i] + ap->gdir[1]*uvw[1][i]); |
515 |
+ |
|
516 |
+ |
if (d <= 0.0) { |
517 |
+ |
setcolor(cr, 0.0, 0.0, 0.0); |
518 |
+ |
return; |
519 |
+ |
} |
520 |
+ |
copycolor(cr, ap->val); |
521 |
+ |
scalecolor(cr, d); |
522 |
+ |
} |
523 |
+ |
|
524 |
+ |
|
525 |
+ |
static void |
526 |
+ |
avinsert( /* insert ambient value in our tree */ |
527 |
+ |
AMBVAL *av |
528 |
+ |
) |
529 |
+ |
{ |
530 |
+ |
AMBTREE *at; |
531 |
+ |
AMBVAL *ap; |
532 |
+ |
AMBVAL avh; |
533 |
+ |
FVECT ck0; |
534 |
+ |
double s; |
535 |
+ |
int branch; |
536 |
+ |
int i; |
537 |
+ |
|
538 |
+ |
if (av->rad[0] <= FTINY) |
539 |
+ |
error(CONSISTENCY, "zero ambient radius in avinsert"); |
540 |
+ |
at = &atrunk; |
541 |
+ |
VCOPY(ck0, thescene.cuorg); |
542 |
+ |
s = thescene.cusize; |
543 |
+ |
while (s*(OCTSCALE/2) > av->rad[1]*ambacc) { |
544 |
+ |
if (at->kid == NULL) |
545 |
+ |
if ((at->kid = newambtree()) == NULL) |
546 |
+ |
error(SYSTEM, "out of memory in avinsert"); |
547 |
+ |
s *= 0.5; |
548 |
+ |
branch = 0; |
549 |
+ |
for (i = 0; i < 3; i++) |
550 |
+ |
if (av->pos[i] > ck0[i] + s) { |
551 |
+ |
ck0[i] += s; |
552 |
+ |
branch |= 1 << i; |
553 |
+ |
} |
554 |
+ |
at = at->kid + branch; |
555 |
+ |
} |
556 |
+ |
avh.next = at->alist; /* order by increasing level */ |
557 |
+ |
for (ap = &avh; ap->next != NULL; ap = ap->next) |
558 |
+ |
if (ap->next->lvl >= av->lvl) |
559 |
+ |
break; |
560 |
+ |
av->next = ap->next; |
561 |
+ |
ap->next = (AMBVAL*)av; |
562 |
+ |
at->alist = avh.next; |
563 |
+ |
} |
564 |
+ |
|
565 |
+ |
|
566 |
+ |
#else /* ! NEWAMB */ |
567 |
+ |
|
568 |
+ |
static double sumambient(COLOR acol, RAY *r, FVECT rn, int al, |
569 |
+ |
AMBTREE *at, FVECT c0, double s); |
570 |
+ |
static double makeambient(COLOR acol, RAY *r, FVECT rn, int al); |
571 |
+ |
static void extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv); |
572 |
+ |
|
573 |
+ |
|
574 |
+ |
void |
575 |
+ |
multambient( /* compute ambient component & multiply by coef. */ |
576 |
+ |
COLOR aval, |
577 |
+ |
RAY *r, |
578 |
+ |
FVECT nrm |
579 |
+ |
) |
580 |
+ |
{ |
581 |
+ |
static int rdepth = 0; /* ambient recursion */ |
582 |
+ |
COLOR acol; |
583 |
+ |
double d, l; |
584 |
+ |
|
585 |
+ |
if (ambdiv <= 0) /* no ambient calculation */ |
586 |
+ |
goto dumbamb; |
587 |
+ |
/* check number of bounces */ |
588 |
+ |
if (rdepth >= ambounce) |
589 |
+ |
goto dumbamb; |
590 |
+ |
/* check ambient list */ |
591 |
+ |
if (ambincl != -1 && r->ro != NULL && |
592 |
+ |
ambincl != inset(ambset, r->ro->omod)) |
593 |
+ |
goto dumbamb; |
594 |
+ |
|
595 |
+ |
if (ambacc <= FTINY) { /* no ambient storage */ |
596 |
+ |
copycolor(acol, aval); |
597 |
+ |
rdepth++; |
598 |
|
d = doambient(acol, r, r->rweight, NULL, NULL); |
599 |
|
rdepth--; |
600 |
|
if (d <= FTINY) |
641 |
|
} |
642 |
|
|
643 |
|
|
644 |
< |
extern double |
644 |
> |
static double |
645 |
|
sumambient( /* get interpolated ambient value */ |
646 |
|
COLOR acol, |
647 |
|
RAY *r, |
752 |
|
} |
753 |
|
|
754 |
|
|
755 |
< |
extern double |
755 |
> |
static double |
756 |
|
makeambient( /* make a new ambient value for storage */ |
757 |
|
COLOR acol, |
758 |
|
RAY *r, |
792 |
|
} |
793 |
|
|
794 |
|
|
795 |
< |
extern void |
795 |
> |
static void |
796 |
|
extambient( /* extrapolate value at pv, nv */ |
797 |
|
COLOR cr, |
798 |
|
AMBVAL *ap, |
821 |
|
|
822 |
|
|
823 |
|
static void |
824 |
+ |
avinsert( /* insert ambient value in our tree */ |
825 |
+ |
AMBVAL *av |
826 |
+ |
) |
827 |
+ |
{ |
828 |
+ |
AMBTREE *at; |
829 |
+ |
AMBVAL *ap; |
830 |
+ |
AMBVAL avh; |
831 |
+ |
FVECT ck0; |
832 |
+ |
double s; |
833 |
+ |
int branch; |
834 |
+ |
int i; |
835 |
+ |
|
836 |
+ |
if (av->rad <= FTINY) |
837 |
+ |
error(CONSISTENCY, "zero ambient radius in avinsert"); |
838 |
+ |
at = &atrunk; |
839 |
+ |
VCOPY(ck0, thescene.cuorg); |
840 |
+ |
s = thescene.cusize; |
841 |
+ |
while (s*(OCTSCALE/2) > av->rad*ambacc) { |
842 |
+ |
if (at->kid == NULL) |
843 |
+ |
if ((at->kid = newambtree()) == NULL) |
844 |
+ |
error(SYSTEM, "out of memory in avinsert"); |
845 |
+ |
s *= 0.5; |
846 |
+ |
branch = 0; |
847 |
+ |
for (i = 0; i < 3; i++) |
848 |
+ |
if (av->pos[i] > ck0[i] + s) { |
849 |
+ |
ck0[i] += s; |
850 |
+ |
branch |= 1 << i; |
851 |
+ |
} |
852 |
+ |
at = at->kid + branch; |
853 |
+ |
} |
854 |
+ |
avh.next = at->alist; /* order by increasing level */ |
855 |
+ |
for (ap = &avh; ap->next != NULL; ap = ap->next) |
856 |
+ |
if (ap->next->lvl >= av->lvl) |
857 |
+ |
break; |
858 |
+ |
av->next = ap->next; |
859 |
+ |
ap->next = (AMBVAL*)av; |
860 |
+ |
at->alist = avh.next; |
861 |
+ |
} |
862 |
+ |
|
863 |
+ |
#endif /* ! NEWAMB */ |
864 |
+ |
|
865 |
+ |
/************* FOLLOWING ROUTINES SAME FOR NEW & OLD METHODS ***************/ |
866 |
+ |
|
867 |
+ |
static void |
868 |
|
initambfile( /* initialize ambient file */ |
869 |
|
int cre8 |
870 |
|
) |
980 |
|
|
981 |
|
|
982 |
|
static void |
624 |
– |
avinsert( /* insert ambient value in our tree */ |
625 |
– |
void *av |
626 |
– |
) |
627 |
– |
{ |
628 |
– |
AMBTREE *at; |
629 |
– |
AMBVAL *ap; |
630 |
– |
AMBVAL avh; |
631 |
– |
FVECT ck0; |
632 |
– |
double s; |
633 |
– |
int branch; |
634 |
– |
int i; |
635 |
– |
|
636 |
– |
if (((AMBVAL*)av)->rad <= FTINY) |
637 |
– |
error(CONSISTENCY, "zero ambient radius in avinsert"); |
638 |
– |
at = &atrunk; |
639 |
– |
VCOPY(ck0, thescene.cuorg); |
640 |
– |
s = thescene.cusize; |
641 |
– |
while (s*(OCTSCALE/2) > ((AMBVAL*)av)->rad*ambacc) { |
642 |
– |
if (at->kid == NULL) |
643 |
– |
if ((at->kid = newambtree()) == NULL) |
644 |
– |
error(SYSTEM, "out of memory in avinsert"); |
645 |
– |
s *= 0.5; |
646 |
– |
branch = 0; |
647 |
– |
for (i = 0; i < 3; i++) |
648 |
– |
if (((AMBVAL*)av)->pos[i] > ck0[i] + s) { |
649 |
– |
ck0[i] += s; |
650 |
– |
branch |= 1 << i; |
651 |
– |
} |
652 |
– |
at = at->kid + branch; |
653 |
– |
} |
654 |
– |
avh.next = at->alist; /* order by increasing level */ |
655 |
– |
for (ap = &avh; ap->next != NULL; ap = ap->next) |
656 |
– |
if (ap->next->lvl >= ((AMBVAL*)av)->lvl) |
657 |
– |
break; |
658 |
– |
((AMBVAL*)av)->next = ap->next; |
659 |
– |
ap->next = (AMBVAL*)av; |
660 |
– |
at->alist = avh.next; |
661 |
– |
} |
662 |
– |
|
663 |
– |
|
664 |
– |
static void |
983 |
|
unloadatree( /* unload an ambient value tree */ |
984 |
|
AMBTREE *at, |
985 |
|
unloadtf_t *f |
1011 |
|
static int alatcmp(const void *av1, const void *av2); |
1012 |
|
|
1013 |
|
static void |
1014 |
+ |
avfree(AMBVAL *av) |
1015 |
+ |
{ |
1016 |
+ |
free(av); |
1017 |
+ |
} |
1018 |
+ |
|
1019 |
+ |
static void |
1020 |
|
av2list( |
1021 |
< |
void *av |
1021 |
> |
AMBVAL *av |
1022 |
|
) |
1023 |
|
{ |
1024 |
|
#ifdef DEBUG |
1026 |
|
error(CONSISTENCY, "too many ambient values in av2list1"); |
1027 |
|
#endif |
1028 |
|
avlist1[i_avlist].p = avlist2[i_avlist] = (AMBVAL*)av; |
1029 |
< |
avlist1[i_avlist++].t = ((AMBVAL*)av)->latick; |
1029 |
> |
avlist1[i_avlist++].t = av->latick; |
1030 |
|
} |
1031 |
|
|
1032 |
|
|
1059 |
|
return(diff > 0); |
1060 |
|
} |
1061 |
|
|
1062 |
< |
#if 1 |
1062 |
> |
|
1063 |
|
static int |
1064 |
|
avlmemi( /* find list position from address */ |
1065 |
|
AMBVAL *avaddr |
1068 |
|
AMBVAL **avlpp; |
1069 |
|
|
1070 |
|
avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2, |
1071 |
< |
nambvals, sizeof(AMBVAL *), aposcmp); |
1071 |
> |
nambvals, sizeof(AMBVAL *), &aposcmp); |
1072 |
|
if (avlpp == NULL) |
1073 |
|
error(CONSISTENCY, "address not found in avlmemi"); |
1074 |
|
return(avlpp - avlist2); |
1075 |
|
} |
752 |
– |
#else |
753 |
– |
#define avlmemi(avaddr) ((AMBVAL **)bsearch((char *)&avaddr,(char *)avlist2, \ |
754 |
– |
nambvals,sizeof(AMBVAL *),aposcmp) - avlist2) |
755 |
– |
#endif |
1076 |
|
|
1077 |
|
|
1078 |
|
static void |
1116 |
|
oldatrunk = atrunk; |
1117 |
|
atrunk.alist = NULL; |
1118 |
|
atrunk.kid = NULL; |
1119 |
< |
unloadatree(&oldatrunk, avinsert); |
1119 |
> |
unloadatree(&oldatrunk, &avinsert); |
1120 |
|
} |
1121 |
|
} else { /* sort memory by last access time */ |
1122 |
|
/* |
1133 |
|
eputs(errmsg); |
1134 |
|
#endif |
1135 |
|
i_avlist = 0; |
1136 |
< |
unloadatree(&atrunk, av2list); /* empty current tree */ |
1136 |
> |
unloadatree(&atrunk, &av2list); /* empty current tree */ |
1137 |
|
#ifdef DEBUG |
1138 |
|
if (i_avlist < nambvals) |
1139 |
|
error(CONSISTENCY, "missing ambient values in sortambvals"); |
1140 |
|
#endif |
1141 |
< |
qsort((char *)avlist1, nambvals, sizeof(struct avl), alatcmp); |
1142 |
< |
qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp); |
1141 |
> |
qsort((char *)avlist1, nambvals, sizeof(struct avl), &alatcmp); |
1142 |
> |
qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), &aposcmp); |
1143 |
|
for (i = 0; i < nambvals; i++) { |
1144 |
|
if (avlist1[i].p == NULL) |
1145 |
|
continue; |
1190 |
|
} |
1191 |
|
|
1192 |
|
|
1193 |
< |
extern int |
1193 |
> |
int |
1194 |
|
ambsync(void) /* synchronize ambient file */ |
1195 |
|
{ |
1196 |
|
long flen; |
1243 |
|
return -1; /* pro forma return */ |
1244 |
|
} |
1245 |
|
|
1246 |
< |
#else |
1246 |
> |
#else /* ! F_SETLKW */ |
1247 |
|
|
1248 |
< |
extern int |
1248 |
> |
int |
1249 |
|
ambsync(void) /* flush ambient file */ |
1250 |
|
{ |
1251 |
|
if (ambfp == NULL) |
1254 |
|
return(fflush(ambfp)); |
1255 |
|
} |
1256 |
|
|
1257 |
< |
#endif |
1257 |
> |
#endif /* ! F_SETLKW */ |