1 |
< |
/* Copyright (c) 1995 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1996 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
21 |
|
#ifndef OCTSCALE |
22 |
|
#define OCTSCALE 1.0 /* ceil((valid rad.)/(cube size)) */ |
23 |
|
#endif |
24 |
– |
#ifndef AMBVWT |
25 |
– |
#define AMBVWT 250 /* relative ambient value weight (# calcs) */ |
26 |
– |
#endif |
24 |
|
|
25 |
|
typedef struct ambtree { |
26 |
|
AMBVAL *alist; /* ambient value list */ |
50 |
|
#endif |
51 |
|
#endif |
52 |
|
#ifndef SORT_INTVL |
53 |
< |
#define SORT_INTVL (SORT_THRESH*256) |
53 |
> |
#define SORT_INTVL (SORT_THRESH<<4) |
54 |
|
#endif |
55 |
|
#ifndef MAX_SORT_INTVL |
56 |
< |
#define MAX_SORT_INTVL (SORT_INTVL<<4) |
56 |
> |
#define MAX_SORT_INTVL (SORT_INTVL<<8) |
57 |
|
#endif |
58 |
|
|
59 |
|
static COLOR avsum = BLKCOLOR; /* computed ambient value sum */ |
60 |
< |
static unsigned int nambvals = 0; /* number of computed ambient values */ |
60 |
> |
static unsigned int nambvals = 0; /* total number of indirect values */ |
61 |
> |
static unsigned int nambshare = 0; /* number of values from file */ |
62 |
|
static unsigned long ambclock = 0; /* ambient access clock */ |
63 |
|
static unsigned long lastsort = 0; /* time of last value sort */ |
64 |
|
static long sortintvl = SORT_INTVL; /* time until next sort */ |
67 |
|
/* |
68 |
|
* Track access times unless we are sharing ambient values |
69 |
|
* through memory on a multiprocessor, when we want to avoid |
70 |
< |
* claiming our own memory (copy on write). |
70 |
> |
* claiming our own memory (copy on write). Go ahead anyway |
71 |
> |
* if more than two thirds of our values are unshared. |
72 |
|
*/ |
73 |
< |
#define tracktime (shm_boundary == NULL || ambfp == NULL) |
73 |
> |
#define tracktime (shm_boundary == NULL || nambvals > 3*nambshare) |
74 |
|
|
75 |
|
#define AMBFLUSH (BUFSIZ/AMBVALSIZ) |
76 |
|
|
77 |
|
#define newambval() (AMBVAL *)bmalloc(sizeof(AMBVAL)) |
78 |
|
|
79 |
|
extern long ftell(), lseek(); |
80 |
< |
static int initambfile(), avsave(), avinsert(), sortambvals(); |
80 |
> |
static int initambfile(), avsave(), avinsert(), sortambvals(), avlmemi(); |
81 |
|
static AMBVAL *avstore(); |
82 |
|
#ifdef F_SETLKW |
83 |
|
static aflock(); |
108 |
|
setambacc(newa) /* set ambient accuracy */ |
109 |
|
double newa; |
110 |
|
{ |
111 |
< |
static double oldambacc = -1.0; |
111 |
> |
double ambdiff; |
112 |
|
|
113 |
< |
ambacc = newa < 0.0 ? 0.0 : newa; /* may be done already */ |
114 |
< |
if (oldambacc < -FTINY) |
115 |
< |
oldambacc = ambacc; /* do nothing first call */ |
116 |
< |
if (fabs(newa - oldambacc) < 0.01) |
117 |
< |
return; /* insignificant -- don't bother */ |
119 |
< |
if (ambacc <= FTINY) |
120 |
< |
return; /* cannot build new tree */ |
121 |
< |
/* else need to rebuild tree */ |
122 |
< |
sortambvals(1); |
123 |
< |
oldambacc = ambacc; /* remeber setting for next call */ |
113 |
> |
if (newa < 0.0) |
114 |
> |
newa = 0.0; |
115 |
> |
ambdiff = fabs(newa - ambacc); |
116 |
> |
if (ambdiff >= .01 && (ambacc = newa) > FTINY && nambvals > 0) |
117 |
> |
sortambvals(1); /* rebuild tree */ |
118 |
|
} |
119 |
|
|
120 |
|
|
121 |
|
setambient(afile) /* initialize calculation */ |
122 |
|
char *afile; |
123 |
|
{ |
124 |
< |
long headlen; |
124 |
> |
long pos, flen; |
125 |
|
AMBVAL amb; |
126 |
|
/* init ambient limits */ |
127 |
|
setambres(ambres); |
137 |
|
/* open ambient file */ |
138 |
|
if ((ambfp = fopen(afile, "r+")) != NULL) { |
139 |
|
initambfile(0); |
140 |
< |
headlen = ftell(ambfp); |
140 |
> |
pos = ftell(ambfp); |
141 |
|
while (readambval(&amb, ambfp)) |
142 |
|
avinsert(avstore(&amb)); |
143 |
|
/* align */ |
144 |
< |
fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1); |
144 |
> |
pos += (long)nambvals*AMBVALSIZ; |
145 |
> |
flen = lseek(fileno(ambfp), 0L, 2); |
146 |
> |
if (flen != pos) { |
147 |
> |
error(WARNING, |
148 |
> |
"ignoring last %ld values in ambient file (corrupted)", |
149 |
> |
(flen - pos)/AMBVALSIZ); |
150 |
> |
fseek(ambfp, pos, 0); |
151 |
> |
ftruncate(fileno(ambfp), pos); |
152 |
> |
} |
153 |
> |
nambshare = nambvals; |
154 |
|
} else if ((ambfp = fopen(afile, "w+")) != NULL) |
155 |
|
initambfile(1); |
156 |
|
else { |
184 |
|
} |
185 |
|
|
186 |
|
|
187 |
< |
ambient(acol, r) /* compute ambient component for ray */ |
187 |
> |
ambient(acol, r, nrm) /* compute ambient component for ray */ |
188 |
|
COLOR acol; |
189 |
|
register RAY *r; |
190 |
+ |
FVECT nrm; |
191 |
|
{ |
192 |
|
static int rdepth = 0; /* ambient recursion */ |
193 |
|
double d; |
210 |
|
goto dumbamb; |
211 |
|
return; |
212 |
|
} |
213 |
< |
/* resort memory? */ |
214 |
< |
sortambvals(0); |
213 |
> |
|
214 |
> |
if (tracktime) /* sort to minimize thrashing */ |
215 |
> |
sortambvals(0); |
216 |
|
/* get ambient value */ |
217 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
218 |
< |
d = sumambient(acol, r, rdepth, |
218 |
> |
d = sumambient(acol, r, nrm, rdepth, |
219 |
|
&atrunk, thescene.cuorg, thescene.cusize); |
220 |
|
if (d > FTINY) { |
221 |
|
scalecolor(acol, 1.0/d); |
222 |
|
return; |
223 |
|
} |
224 |
< |
rdepth++; |
225 |
< |
d = makeambient(acol, r, rdepth-1); |
224 |
> |
rdepth++; /* need to cache new value */ |
225 |
> |
d = makeambient(acol, r, nrm, rdepth-1); |
226 |
|
rdepth--; |
227 |
|
if (d > FTINY) |
228 |
|
return; |
229 |
|
dumbamb: /* return global value */ |
230 |
|
copycolor(acol, ambval); |
231 |
< |
#if AMBVWT |
227 |
< |
if (nambvals == 0) |
231 |
> |
if (ambvwt <= 0 | nambvals == 0) |
232 |
|
return; |
233 |
< |
scalecolor(acol, (double)AMBVWT); |
233 |
> |
scalecolor(acol, (double)ambvwt); |
234 |
|
addcolor(acol, avsum); /* average in computations */ |
235 |
< |
d = 1.0/(AMBVWT+nambvals); |
235 |
> |
d = 1.0/(ambvwt+nambvals); |
236 |
|
scalecolor(acol, d); |
233 |
– |
#endif |
237 |
|
} |
238 |
|
|
239 |
|
|
240 |
|
double |
241 |
< |
sumambient(acol, r, al, at, c0, s) /* get interpolated ambient value */ |
241 |
> |
sumambient(acol, r, rn, al, at, c0, s) /* get interpolated ambient value */ |
242 |
|
COLOR acol; |
243 |
|
register RAY *r; |
244 |
+ |
FVECT rn; |
245 |
|
int al; |
246 |
|
AMBTREE *at; |
247 |
|
FVECT c0; |
258 |
|
/* do this node */ |
259 |
|
for (av = at->alist; av != NULL; av = av->next) { |
260 |
|
if (tracktime) |
261 |
< |
av->latick = ambclock++; |
261 |
> |
av->latick = ambclock; |
262 |
|
/* |
263 |
|
* Ambient level test. |
264 |
|
*/ |
305 |
|
else |
306 |
|
wt = 1.0 / wt; |
307 |
|
wsum += wt; |
308 |
< |
extambient(ct, av, r->rop, r->ron); |
308 |
> |
extambient(ct, av, r->rop, rn); |
309 |
|
scalecolor(ct, wt); |
310 |
|
addcolor(acol, ct); |
311 |
|
} |
324 |
|
break; |
325 |
|
} |
326 |
|
if (j == 3) |
327 |
< |
wsum += sumambient(acol, r, al, at->kid+i, ck0, s); |
327 |
> |
wsum += sumambient(acol, r, rn, al, at->kid+i, ck0, s); |
328 |
|
} |
329 |
|
return(wsum); |
330 |
|
} |
331 |
|
|
332 |
|
|
333 |
|
double |
334 |
< |
makeambient(acol, r, al) /* make a new ambient value */ |
334 |
> |
makeambient(acol, r, rn, al) /* make a new ambient value */ |
335 |
|
COLOR acol; |
336 |
|
register RAY *r; |
337 |
+ |
FVECT rn; |
338 |
|
int al; |
339 |
|
{ |
340 |
|
AMBVAL amb; |
341 |
|
FVECT gp, gd; |
342 |
|
/* compute weight */ |
343 |
|
amb.weight = pow(AVGREFL, (double)al); |
344 |
< |
if (r->rweight < 0.2*amb.weight) /* heuristic */ |
344 |
> |
if (r->rweight < 0.1*amb.weight) /* heuristic */ |
345 |
|
amb.weight = r->rweight; |
346 |
|
/* compute ambient */ |
347 |
|
amb.rad = doambient(acol, r, amb.weight, gp, gd); |
348 |
< |
if (amb.rad == 0.0) |
348 |
> |
if (amb.rad <= FTINY) |
349 |
|
return(0.0); |
350 |
|
/* store it */ |
351 |
|
VCOPY(amb.pos, r->rop); |
356 |
|
VCOPY(amb.gdir, gd); |
357 |
|
/* insert into tree */ |
358 |
|
avsave(&amb); /* and save to file */ |
359 |
+ |
if (rn != r->ron) |
360 |
+ |
extambient(acol, &amb, r->rop, rn); /* texture */ |
361 |
|
return(amb.rad); |
362 |
|
} |
363 |
|
|
552 |
|
} |
553 |
|
|
554 |
|
|
555 |
< |
static AMBVAL **avlist1, **avlist2; /* ambient value lists for sorting */ |
555 |
> |
static struct avl { |
556 |
> |
AMBVAL *p; |
557 |
> |
unsigned long t; |
558 |
> |
} *avlist1; /* ambient value list with ticks */ |
559 |
> |
static AMBVAL **avlist2; /* memory positions for sorting */ |
560 |
|
static int i_avlist; /* index for lists */ |
561 |
|
|
562 |
|
|
563 |
|
static |
564 |
|
av2list(av) |
565 |
< |
AMBVAL *av; |
565 |
> |
register AMBVAL *av; |
566 |
|
{ |
567 |
|
#ifdef DEBUG |
568 |
|
if (i_avlist >= nambvals) |
569 |
|
error(CONSISTENCY, "too many ambient values in av2list1"); |
570 |
|
#endif |
571 |
< |
avlist1[i_avlist] = avlist2[i_avlist] = av; |
572 |
< |
i_avlist++; |
571 |
> |
avlist1[i_avlist].p = avlist2[i_avlist] = av; |
572 |
> |
avlist1[i_avlist++].t = av->latick; |
573 |
|
} |
574 |
|
|
575 |
|
|
576 |
|
static int |
577 |
< |
alatcmp(avp1, avp2) /* compare ambient values for MRA */ |
578 |
< |
AMBVAL **avp1, **avp2; |
577 |
> |
alatcmp(av1, av2) /* compare ambient values for MRA */ |
578 |
> |
struct avl *av1, *av2; |
579 |
|
{ |
580 |
< |
return((**avp2).latick - (**avp1).latick); |
580 |
> |
register long lc = av2->t - av1->t; |
581 |
> |
return(lc<0 ? -1 : lc>0 ? 1 : 0); |
582 |
|
} |
583 |
|
|
584 |
|
|
590 |
|
} |
591 |
|
|
592 |
|
|
593 |
< |
#ifdef DEBUG |
593 |
> |
#if 1 |
594 |
|
static int |
595 |
|
avlmemi(avaddr) /* find list position from address */ |
596 |
|
AMBVAL *avaddr; |
617 |
|
AMBVAL tav, *tap, *pnext; |
618 |
|
register int i, j; |
619 |
|
/* see if it's time yet */ |
620 |
< |
if (!always && (ambclock < lastsort+sortintvl || |
620 |
> |
if (!always && (ambclock++ < lastsort+sortintvl || |
621 |
|
nambvals < SORT_THRESH)) |
622 |
|
return; |
623 |
|
/* |
636 |
|
* tree will be rebuilt with the new accuracy parameter. |
637 |
|
*/ |
638 |
|
if (tracktime) { /* allocate pointer arrays to sort */ |
627 |
– |
avlist1 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *)); |
639 |
|
avlist2 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *)); |
640 |
< |
} else |
641 |
< |
avlist1 = avlist2 = NULL; |
642 |
< |
if (avlist2 == NULL) { /* no time tracking -- rebuild tree? */ |
643 |
< |
if (avlist1 != NULL) |
644 |
< |
free((char *)avlist1); |
640 |
> |
avlist1 = (struct avl *)malloc(nambvals*sizeof(struct avl)); |
641 |
> |
} else { |
642 |
> |
avlist2 = NULL; |
643 |
> |
avlist1 = NULL; |
644 |
> |
} |
645 |
> |
if (avlist1 == NULL) { /* no time tracking -- rebuild tree? */ |
646 |
> |
if (avlist2 != NULL) |
647 |
> |
free((char *)avlist2); |
648 |
|
if (always) { /* rebuild without sorting */ |
649 |
|
copystruct(&oldatrunk, &atrunk); |
650 |
|
atrunk.alist = NULL; |
671 |
|
if (i_avlist < nambvals) |
672 |
|
error(CONSISTENCY, "missing ambient values in sortambvals"); |
673 |
|
#endif |
674 |
< |
qsort((char *)avlist1, nambvals, sizeof(AMBVAL *), alatcmp); |
674 |
> |
qsort((char *)avlist1, nambvals, sizeof(struct avl), alatcmp); |
675 |
|
qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp); |
676 |
|
for (i = 0; i < nambvals; i++) { |
677 |
< |
if (avlist1[i] == NULL) |
677 |
> |
if (avlist1[i].p == NULL) |
678 |
|
continue; |
679 |
|
tap = avlist2[i]; |
680 |
|
copystruct(&tav, tap); |
681 |
< |
for (j = i; (pnext = avlist1[j]) != tap; |
681 |
> |
for (j = i; (pnext = avlist1[j].p) != tap; |
682 |
|
j = avlmemi(pnext)) { |
683 |
|
copystruct(avlist2[j], pnext); |
684 |
|
avinsert(avlist2[j]); |
685 |
< |
avlist1[j] = NULL; |
685 |
> |
avlist1[j].p = NULL; |
686 |
|
} |
687 |
|
copystruct(avlist2[j], &tav); |
688 |
|
avinsert(avlist2[j]); |
689 |
< |
avlist1[j] = NULL; |
689 |
> |
avlist1[j].p = NULL; |
690 |
|
} |
691 |
|
free((char *)avlist1); |
692 |
|
free((char *)avlist2); |
747 |
|
if (fseek(ambinp, lastpos, 0) < 0) |
748 |
|
goto seekerr; |
749 |
|
while (n >= AMBVALSIZ) { /* load contributed values */ |
750 |
< |
readambval(&avs, ambinp); |
750 |
> |
if (!readambval(&avs, ambinp)) { |
751 |
> |
sprintf(errmsg, |
752 |
> |
"ambient file corrupted near character %ld", |
753 |
> |
flen - n); |
754 |
> |
error(WARNING, errmsg); |
755 |
> |
break; |
756 |
> |
} |
757 |
|
avinsert(avstore(&avs)); |
758 |
|
n -= AMBVALSIZ; |
759 |
|
} |