| 14 |
|
#include "platform.h" |
| 15 |
|
#include "ray.h" |
| 16 |
|
#include "otypes.h" |
| 17 |
+ |
#include "resolu.h" |
| 18 |
|
#include "ambient.h" |
| 19 |
|
#include "random.h" |
| 20 |
|
|
| 24 |
|
|
| 25 |
|
extern char *shm_boundary; /* memory sharing boundary */ |
| 26 |
|
|
| 27 |
< |
#define MAXASET 511 /* maximum number of elements in ambient set */ |
| 27 |
> |
#ifndef MAXASET |
| 28 |
> |
#define MAXASET 2047 /* maximum number of elements in ambient set */ |
| 29 |
> |
#endif |
| 30 |
|
OBJECT ambset[MAXASET+1]={0}; /* ambient include/exclude set */ |
| 31 |
|
|
| 32 |
|
double maxarad; /* maximum ambient radius */ |
| 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(void *); |
| 88 |
> |
static unloadtf_t avinsert; |
| 89 |
> |
static unloadtf_t av2list; |
| 90 |
> |
static void unloadatree(AMBTREE *at, unloadtf_t *f); |
| 91 |
> |
|
| 92 |
> |
static int aposcmp(const void *avp1, const void *avp2); |
| 93 |
> |
static int avlmemi(AMBVAL *avaddr); |
| 94 |
> |
static void sortambvals(int always); |
| 95 |
> |
|
| 96 |
|
#ifdef F_SETLKW |
| 97 |
< |
static void aflock(); |
| 97 |
> |
static void aflock(int typ); |
| 98 |
|
#endif |
| 99 |
|
|
| 100 |
|
|
| 101 |
< |
void |
| 102 |
< |
setambres(ar) /* set ambient resolution */ |
| 103 |
< |
int ar; |
| 101 |
> |
extern void |
| 102 |
> |
setambres( /* set ambient resolution */ |
| 103 |
> |
int ar |
| 104 |
> |
) |
| 105 |
|
{ |
| 106 |
|
ambres = ar < 0 ? 0 : ar; /* may be done already */ |
| 107 |
|
/* set min & max radii */ |
| 121 |
|
} |
| 122 |
|
|
| 123 |
|
|
| 124 |
< |
void |
| 125 |
< |
setambacc(newa) /* set ambient accuracy */ |
| 126 |
< |
double newa; |
| 124 |
> |
extern void |
| 125 |
> |
setambacc( /* set ambient accuracy */ |
| 126 |
> |
double newa |
| 127 |
> |
) |
| 128 |
|
{ |
| 129 |
|
double ambdiff; |
| 130 |
|
|
| 136 |
|
} |
| 137 |
|
|
| 138 |
|
|
| 139 |
< |
void |
| 140 |
< |
setambient() /* initialize calculation */ |
| 139 |
> |
extern void |
| 140 |
> |
setambient(void) /* initialize calculation */ |
| 141 |
|
{ |
| 142 |
|
int readonly = 0; |
| 143 |
|
long pos, flen; |
| 197 |
|
} |
| 198 |
|
|
| 199 |
|
|
| 200 |
< |
void |
| 201 |
< |
ambdone() /* close ambient file and free memory */ |
| 200 |
> |
extern void |
| 201 |
> |
ambdone(void) /* close ambient file and free memory */ |
| 202 |
|
{ |
| 203 |
|
if (ambfp != NULL) { /* close ambient file */ |
| 204 |
|
ambsync(); |
| 223 |
|
} |
| 224 |
|
|
| 225 |
|
|
| 226 |
< |
void |
| 227 |
< |
ambnotify(obj) /* record new modifier */ |
| 228 |
< |
OBJECT obj; |
| 226 |
> |
extern void |
| 227 |
> |
ambnotify( /* record new modifier */ |
| 228 |
> |
OBJECT obj |
| 229 |
> |
) |
| 230 |
|
{ |
| 231 |
|
static int hitlimit = 0; |
| 232 |
|
register OBJREC *o; |
| 253 |
|
} |
| 254 |
|
|
| 255 |
|
|
| 256 |
< |
void |
| 257 |
< |
ambient(acol, r, nrm) /* compute ambient component for ray */ |
| 258 |
< |
COLOR acol; |
| 259 |
< |
register RAY *r; |
| 260 |
< |
FVECT nrm; |
| 256 |
> |
extern void |
| 257 |
> |
multambient( /* compute ambient component & multiply by coef. */ |
| 258 |
> |
COLOR aval, |
| 259 |
> |
register RAY *r, |
| 260 |
> |
FVECT nrm |
| 261 |
> |
) |
| 262 |
|
{ |
| 263 |
|
static int rdepth = 0; /* ambient recursion */ |
| 264 |
+ |
COLOR acol; |
| 265 |
|
double d, l; |
| 266 |
|
|
| 267 |
|
if (ambdiv <= 0) /* no ambient calculation */ |
| 275 |
|
goto dumbamb; |
| 276 |
|
|
| 277 |
|
if (ambacc <= FTINY) { /* no ambient storage */ |
| 278 |
+ |
copycolor(acol, aval); |
| 279 |
|
rdepth++; |
| 280 |
|
d = doambient(acol, r, r->rweight, NULL, NULL); |
| 281 |
|
rdepth--; |
| 282 |
|
if (d <= FTINY) |
| 283 |
|
goto dumbamb; |
| 284 |
+ |
copycolor(aval, acol); |
| 285 |
|
return; |
| 286 |
|
} |
| 287 |
|
|
| 288 |
|
if (tracktime) /* sort to minimize thrashing */ |
| 289 |
|
sortambvals(0); |
| 290 |
< |
/* get ambient value */ |
| 290 |
> |
/* interpolate ambient value */ |
| 291 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
| 292 |
|
d = sumambient(acol, r, nrm, rdepth, |
| 293 |
|
&atrunk, thescene.cuorg, thescene.cusize); |
| 294 |
|
if (d > FTINY) { |
| 295 |
< |
scalecolor(acol, 1.0/d); |
| 295 |
> |
d = 1.0/d; |
| 296 |
> |
scalecolor(acol, d); |
| 297 |
> |
multcolor(aval, acol); |
| 298 |
|
return; |
| 299 |
|
} |
| 300 |
|
rdepth++; /* need to cache new value */ |
| 301 |
|
d = makeambient(acol, r, nrm, rdepth-1); |
| 302 |
|
rdepth--; |
| 303 |
< |
if (d > FTINY) |
| 303 |
> |
if (d > FTINY) { |
| 304 |
> |
multcolor(aval, acol); /* got new value */ |
| 305 |
|
return; |
| 306 |
+ |
} |
| 307 |
|
dumbamb: /* return global value */ |
| 308 |
< |
copycolor(acol, ambval); |
| 309 |
< |
if ((ambvwt <= 0) | (navsum == 0)) |
| 308 |
> |
if ((ambvwt <= 0) | (navsum == 0)) { |
| 309 |
> |
multcolor(aval, ambval); |
| 310 |
|
return; |
| 311 |
+ |
} |
| 312 |
|
l = bright(ambval); /* average in computations */ |
| 313 |
|
if (l > FTINY) { |
| 314 |
|
d = (log(l)*(double)ambvwt + avsum) / |
| 315 |
|
(double)(ambvwt + navsum); |
| 316 |
|
d = exp(d) / l; |
| 317 |
< |
scalecolor(acol, d); /* apply color of ambval */ |
| 317 |
> |
scalecolor(aval, d); |
| 318 |
> |
multcolor(aval, ambval); /* apply color of ambval */ |
| 319 |
|
} else { |
| 320 |
|
d = exp( avsum / (double)navsum ); |
| 321 |
< |
setcolor(acol, d, d, d); /* neutral color */ |
| 321 |
> |
scalecolor(aval, d); /* neutral color */ |
| 322 |
|
} |
| 323 |
|
} |
| 324 |
|
|
| 325 |
|
|
| 326 |
< |
double |
| 327 |
< |
sumambient(acol, r, rn, al, at, c0, s) /* get interpolated ambient value */ |
| 328 |
< |
COLOR acol; |
| 329 |
< |
register RAY *r; |
| 330 |
< |
FVECT rn; |
| 331 |
< |
int al; |
| 332 |
< |
AMBTREE *at; |
| 333 |
< |
FVECT c0; |
| 334 |
< |
double s; |
| 326 |
> |
extern double |
| 327 |
> |
sumambient( /* get interpolated ambient value */ |
| 328 |
> |
COLOR acol, |
| 329 |
> |
register RAY *r, |
| 330 |
> |
FVECT rn, |
| 331 |
> |
int al, |
| 332 |
> |
AMBTREE *at, |
| 333 |
> |
FVECT c0, |
| 334 |
> |
double s |
| 335 |
> |
) |
| 336 |
|
{ |
| 337 |
|
double d, e1, e2, wt, wsum; |
| 338 |
|
COLOR ct; |
| 352 |
|
*/ |
| 353 |
|
if (av->lvl > al) /* list sorted, so this works */ |
| 354 |
|
break; |
| 355 |
< |
if (av->weight < r->rweight-FTINY) |
| 355 |
> |
if (av->weight < 0.9*r->rweight) |
| 356 |
|
continue; |
| 357 |
|
/* |
| 358 |
|
* Ambient radius test. |
| 431 |
|
break; |
| 432 |
|
} |
| 433 |
|
if (j == 3) |
| 434 |
< |
wsum += sumambient(acol, r, rn, al, at->kid+i, ck0, s); |
| 434 |
> |
wsum += sumambient(acol, r, rn, al, |
| 435 |
> |
at->kid+i, ck0, s); |
| 436 |
|
} |
| 437 |
|
return(wsum); |
| 438 |
|
} |
| 439 |
|
|
| 440 |
|
|
| 441 |
< |
double |
| 442 |
< |
makeambient(acol, r, rn, al) /* make a new ambient value */ |
| 443 |
< |
COLOR acol; |
| 444 |
< |
register RAY *r; |
| 445 |
< |
FVECT rn; |
| 446 |
< |
int al; |
| 441 |
> |
extern double |
| 442 |
> |
makeambient( /* make a new ambient value for storage */ |
| 443 |
> |
COLOR acol, |
| 444 |
> |
RAY *r, |
| 445 |
> |
FVECT rn, |
| 446 |
> |
int al |
| 447 |
> |
) |
| 448 |
|
{ |
| 449 |
|
AMBVAL amb; |
| 450 |
|
FVECT gp, gd; |
| 451 |
< |
/* compute weight */ |
| 452 |
< |
amb.weight = pow(AVGREFL, (double)al); |
| 453 |
< |
if (r->rweight < 0.1*amb.weight) /* heuristic */ |
| 454 |
< |
amb.weight = r->rweight; |
| 451 |
> |
int i; |
| 452 |
> |
|
| 453 |
> |
amb.weight = 1.0; /* compute weight */ |
| 454 |
> |
for (i = al; i-- > 0; ) |
| 455 |
> |
amb.weight *= AVGREFL; |
| 456 |
> |
if (r->rweight < 0.1*amb.weight) /* heuristic override */ |
| 457 |
> |
amb.weight = 1.25*r->rweight; |
| 458 |
> |
setcolor(acol, AVGREFL, AVGREFL, AVGREFL); |
| 459 |
|
/* compute ambient */ |
| 460 |
|
amb.rad = doambient(acol, r, amb.weight, gp, gd); |
| 461 |
< |
if (amb.rad <= FTINY) |
| 461 |
> |
if (amb.rad <= FTINY) { |
| 462 |
> |
setcolor(acol, 0.0, 0.0, 0.0); |
| 463 |
|
return(0.0); |
| 464 |
< |
/* store it */ |
| 464 |
> |
} |
| 465 |
> |
scalecolor(acol, 1./AVGREFL); /* undo assumed reflectance */ |
| 466 |
> |
/* store value */ |
| 467 |
|
VCOPY(amb.pos, r->rop); |
| 468 |
|
VCOPY(amb.dir, r->ron); |
| 469 |
|
amb.lvl = al; |
| 478 |
|
} |
| 479 |
|
|
| 480 |
|
|
| 481 |
< |
void |
| 482 |
< |
extambient(cr, ap, pv, nv) /* extrapolate value at pv, nv */ |
| 483 |
< |
COLOR cr; |
| 484 |
< |
register AMBVAL *ap; |
| 485 |
< |
FVECT pv, nv; |
| 481 |
> |
extern void |
| 482 |
> |
extambient( /* extrapolate value at pv, nv */ |
| 483 |
> |
COLOR cr, |
| 484 |
> |
register AMBVAL *ap, |
| 485 |
> |
FVECT pv, |
| 486 |
> |
FVECT nv |
| 487 |
> |
) |
| 488 |
|
{ |
| 489 |
|
FVECT v1; |
| 490 |
|
register int i; |
| 507 |
|
|
| 508 |
|
|
| 509 |
|
static void |
| 510 |
< |
initambfile(creat) /* initialize ambient file */ |
| 511 |
< |
int creat; |
| 510 |
> |
initambfile( /* initialize ambient file */ |
| 511 |
> |
int creat |
| 512 |
> |
) |
| 513 |
|
{ |
| 514 |
|
extern char *progname, *octname; |
| 515 |
|
static char *mybuf = NULL; |
| 544 |
|
|
| 545 |
|
|
| 546 |
|
static void |
| 547 |
< |
avsave(av) /* insert and save an ambient value */ |
| 548 |
< |
AMBVAL *av; |
| 547 |
> |
avsave( /* insert and save an ambient value */ |
| 548 |
> |
AMBVAL *av |
| 549 |
> |
) |
| 550 |
|
{ |
| 551 |
|
avinsert(avstore(av)); |
| 552 |
|
if (ambfp == NULL) |
| 563 |
|
|
| 564 |
|
|
| 565 |
|
static AMBVAL * |
| 566 |
< |
avstore(aval) /* allocate memory and store aval */ |
| 567 |
< |
register AMBVAL *aval; |
| 566 |
> |
avstore( /* allocate memory and store aval */ |
| 567 |
> |
register AMBVAL *aval |
| 568 |
> |
) |
| 569 |
|
{ |
| 570 |
|
register AMBVAL *av; |
| 571 |
|
double d; |
| 591 |
|
|
| 592 |
|
|
| 593 |
|
static AMBTREE * |
| 594 |
< |
newambtree() /* allocate 8 ambient tree structs */ |
| 594 |
> |
newambtree(void) /* allocate 8 ambient tree structs */ |
| 595 |
|
{ |
| 596 |
|
register AMBTREE *atp, *upperlim; |
| 597 |
|
|
| 613 |
|
|
| 614 |
|
|
| 615 |
|
static void |
| 616 |
< |
freeambtree(atp) /* free 8 ambient tree structs */ |
| 617 |
< |
AMBTREE *atp; |
| 616 |
> |
freeambtree( /* free 8 ambient tree structs */ |
| 617 |
> |
AMBTREE *atp |
| 618 |
> |
) |
| 619 |
|
{ |
| 620 |
|
atp->kid = atfreelist; |
| 621 |
|
atfreelist = atp; |
| 623 |
|
|
| 624 |
|
|
| 625 |
|
static void |
| 626 |
< |
avinsert(av) /* insert ambient value in our tree */ |
| 627 |
< |
register AMBVAL *av; |
| 626 |
> |
avinsert( /* insert ambient value in our tree */ |
| 627 |
> |
void *av |
| 628 |
> |
) |
| 629 |
|
{ |
| 630 |
|
register AMBTREE *at; |
| 631 |
|
register AMBVAL *ap; |
| 635 |
|
int branch; |
| 636 |
|
register int i; |
| 637 |
|
|
| 638 |
< |
if (av->rad <= FTINY) |
| 638 |
> |
if (((AMBVAL*)av)->rad <= FTINY) |
| 639 |
|
error(CONSISTENCY, "zero ambient radius in avinsert"); |
| 640 |
|
at = &atrunk; |
| 641 |
|
VCOPY(ck0, thescene.cuorg); |
| 642 |
|
s = thescene.cusize; |
| 643 |
< |
while (s*(OCTSCALE/2) > av->rad*ambacc) { |
| 643 |
> |
while (s*(OCTSCALE/2) > ((AMBVAL*)av)->rad*ambacc) { |
| 644 |
|
if (at->kid == NULL) |
| 645 |
|
if ((at->kid = newambtree()) == NULL) |
| 646 |
|
error(SYSTEM, "out of memory in avinsert"); |
| 647 |
|
s *= 0.5; |
| 648 |
|
branch = 0; |
| 649 |
|
for (i = 0; i < 3; i++) |
| 650 |
< |
if (av->pos[i] > ck0[i] + s) { |
| 650 |
> |
if (((AMBVAL*)av)->pos[i] > ck0[i] + s) { |
| 651 |
|
ck0[i] += s; |
| 652 |
|
branch |= 1 << i; |
| 653 |
|
} |
| 655 |
|
} |
| 656 |
|
avh.next = at->alist; /* order by increasing level */ |
| 657 |
|
for (ap = &avh; ap->next != NULL; ap = ap->next) |
| 658 |
< |
if (ap->next->lvl >= av->lvl) |
| 658 |
> |
if (ap->next->lvl >= ((AMBVAL*)av)->lvl) |
| 659 |
|
break; |
| 660 |
< |
av->next = ap->next; |
| 661 |
< |
ap->next = av; |
| 660 |
> |
((AMBVAL*)av)->next = ap->next; |
| 661 |
> |
ap->next = (AMBVAL*)av; |
| 662 |
|
at->alist = avh.next; |
| 663 |
|
} |
| 664 |
|
|
| 665 |
|
|
| 666 |
|
static void |
| 667 |
< |
unloadatree(at, f) /* unload an ambient value tree */ |
| 668 |
< |
register AMBTREE *at; |
| 669 |
< |
void (*f)(); |
| 667 |
> |
unloadatree( /* unload an ambient value tree */ |
| 668 |
> |
register AMBTREE *at, |
| 669 |
> |
unloadtf_t *f |
| 670 |
> |
) |
| 671 |
|
{ |
| 672 |
|
register AMBVAL *av; |
| 673 |
|
register int i; |
| 692 |
|
static AMBVAL **avlist2; /* memory positions for sorting */ |
| 693 |
|
static int i_avlist; /* index for lists */ |
| 694 |
|
|
| 695 |
+ |
static int alatcmp(const void *av1, const void *av2); |
| 696 |
|
|
| 697 |
< |
static int |
| 698 |
< |
av2list(av) |
| 699 |
< |
register AMBVAL *av; |
| 697 |
> |
static void |
| 698 |
> |
av2list( |
| 699 |
> |
void *av |
| 700 |
> |
) |
| 701 |
|
{ |
| 702 |
|
#ifdef DEBUG |
| 703 |
|
if (i_avlist >= nambvals) |
| 704 |
|
error(CONSISTENCY, "too many ambient values in av2list1"); |
| 705 |
|
#endif |
| 706 |
< |
avlist1[i_avlist].p = avlist2[i_avlist] = av; |
| 707 |
< |
avlist1[i_avlist++].t = av->latick; |
| 706 |
> |
avlist1[i_avlist].p = avlist2[i_avlist] = (AMBVAL*)av; |
| 707 |
> |
avlist1[i_avlist++].t = ((AMBVAL*)av)->latick; |
| 708 |
|
} |
| 709 |
|
|
| 710 |
|
|
| 711 |
|
static int |
| 712 |
< |
alatcmp(av1, av2) /* compare ambient values for MRA */ |
| 713 |
< |
struct avl *av1, *av2; |
| 712 |
> |
alatcmp( /* compare ambient values for MRA */ |
| 713 |
> |
const void *av1, |
| 714 |
> |
const void *av2 |
| 715 |
> |
) |
| 716 |
|
{ |
| 717 |
< |
register long lc = av2->t - av1->t; |
| 717 |
> |
register long lc = ((struct avl *)av2)->t - ((struct avl *)av1)->t; |
| 718 |
|
return(lc<0 ? -1 : lc>0 ? 1 : 0); |
| 719 |
|
} |
| 720 |
|
|
| 726 |
|
* assumes pointers differ by exact struct size increments. |
| 727 |
|
*/ |
| 728 |
|
static int |
| 729 |
< |
aposcmp(avp1, avp2) /* compare ambient value positions */ |
| 730 |
< |
const void *avp1, *avp2; |
| 729 |
> |
aposcmp( /* compare ambient value positions */ |
| 730 |
> |
const void *avp1, |
| 731 |
> |
const void *avp2 |
| 732 |
> |
) |
| 733 |
|
{ |
| 734 |
|
register long diff = *(char * const *)avp1 - *(char * const *)avp2; |
| 735 |
|
if (diff < 0) |
| 739 |
|
|
| 740 |
|
#if 1 |
| 741 |
|
static int |
| 742 |
< |
avlmemi(avaddr) /* find list position from address */ |
| 743 |
< |
AMBVAL *avaddr; |
| 742 |
> |
avlmemi( /* find list position from address */ |
| 743 |
> |
AMBVAL *avaddr |
| 744 |
> |
) |
| 745 |
|
{ |
| 746 |
|
register AMBVAL **avlpp; |
| 747 |
|
|
| 758 |
|
|
| 759 |
|
|
| 760 |
|
static void |
| 761 |
< |
sortambvals(always) /* resort ambient values */ |
| 762 |
< |
int always; |
| 761 |
> |
sortambvals( /* resort ambient values */ |
| 762 |
> |
int always |
| 763 |
> |
) |
| 764 |
|
{ |
| 765 |
|
AMBTREE oldatrunk; |
| 766 |
|
AMBVAL tav, *tap, *pnext; |
| 858 |
|
#ifdef F_SETLKW |
| 859 |
|
|
| 860 |
|
static void |
| 861 |
< |
aflock(typ) /* lock/unlock ambient file */ |
| 862 |
< |
int typ; |
| 861 |
> |
aflock( /* lock/unlock ambient file */ |
| 862 |
> |
int typ |
| 863 |
> |
) |
| 864 |
|
{ |
| 865 |
|
static struct flock fls; /* static so initialized to zeroes */ |
| 866 |
|
|
| 870 |
|
} |
| 871 |
|
|
| 872 |
|
|
| 873 |
< |
int |
| 874 |
< |
ambsync() /* synchronize ambient file */ |
| 873 |
> |
extern int |
| 874 |
> |
ambsync(void) /* synchronize ambient file */ |
| 875 |
|
{ |
| 876 |
|
long flen; |
| 877 |
|
AMBVAL avs; |
| 927 |
|
return(n); |
| 928 |
|
seekerr: |
| 929 |
|
error(SYSTEM, "seek failed in ambsync"); |
| 930 |
+ |
return -1; /* pro forma return */ |
| 931 |
|
} |
| 932 |
|
|
| 933 |
|
#else |
| 934 |
|
|
| 935 |
< |
int |
| 936 |
< |
ambsync() /* flush ambient file */ |
| 935 |
> |
extern int |
| 936 |
> |
ambsync(void) /* flush ambient file */ |
| 937 |
|
{ |
| 938 |
|
if (nunflshed == 0) |
| 939 |
|
return(0); |