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