1 |
– |
/* Copyright (c) 1996 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* ambient.c - routines dealing with ambient (inter-reflected) component. |
6 |
+ |
* |
7 |
+ |
* Declarations of external symbols in ambient.h |
8 |
|
*/ |
9 |
|
|
10 |
+ |
#include "copyright.h" |
11 |
+ |
|
12 |
|
#include "ray.h" |
13 |
|
|
13 |
– |
#include "octree.h" |
14 |
– |
|
14 |
|
#include "otypes.h" |
15 |
|
|
16 |
|
#include "ambient.h" |
21 |
|
#define OCTSCALE 1.0 /* ceil((valid rad.)/(cube size)) */ |
22 |
|
#endif |
23 |
|
|
25 |
– |
typedef struct ambtree { |
26 |
– |
AMBVAL *alist; /* ambient value list */ |
27 |
– |
struct ambtree *kid; /* 8 child nodes */ |
28 |
– |
} AMBTREE; /* ambient octree */ |
29 |
– |
|
30 |
– |
extern CUBE thescene; /* contains space boundaries */ |
31 |
– |
|
24 |
|
extern char *shm_boundary; /* memory sharing boundary */ |
25 |
|
|
26 |
|
#define MAXASET 511 /* maximum number of elements in ambient set */ |
35 |
|
static int nunflshed = 0; /* number of unflushed ambient values */ |
36 |
|
|
37 |
|
#ifndef SORT_THRESH |
38 |
< |
#ifdef BIGMEM |
47 |
< |
#define SORT_THRESH ((9L<<20)/sizeof(AMBVAL)) |
48 |
< |
#else |
38 |
> |
#ifdef SMLMEM |
39 |
|
#define SORT_THRESH ((3L<<20)/sizeof(AMBVAL)) |
40 |
+ |
#else |
41 |
+ |
#define SORT_THRESH ((9L<<20)/sizeof(AMBVAL)) |
42 |
|
#endif |
43 |
|
#endif |
44 |
|
#ifndef SORT_INTVL |
55 |
|
static unsigned long ambclock = 0; /* ambient access clock */ |
56 |
|
static unsigned long lastsort = 0; /* time of last value sort */ |
57 |
|
static long sortintvl = SORT_INTVL; /* time until next sort */ |
58 |
+ |
static FILE *ambinp = NULL; /* auxiliary file for input */ |
59 |
+ |
static long lastpos = -1; /* last flush position */ |
60 |
|
|
61 |
|
#define MAXACLOCK (1L<<30) /* clock turnover value */ |
62 |
|
/* |
72 |
|
|
73 |
|
#define AMBFLUSH (BUFSIZ/AMBVALSIZ) |
74 |
|
|
75 |
< |
#define newambval() (AMBVAL *)bmalloc(sizeof(AMBVAL)) |
75 |
> |
#define newambval() (AMBVAL *)malloc(sizeof(AMBVAL)) |
76 |
> |
#define freeav(av) free((void *)av); |
77 |
|
|
78 |
< |
extern long ftell(), lseek(); |
79 |
< |
static int initambfile(), avsave(), avinsert(), sortambvals(), avlmemi(); |
78 |
> |
static void initambfile(), avsave(), avinsert(), sortambvals(), unloadatree(); |
79 |
> |
static int avlmemi(); |
80 |
|
static AMBVAL *avstore(); |
81 |
|
#ifdef F_SETLKW |
82 |
< |
static aflock(); |
82 |
> |
static void aflock(); |
83 |
|
#endif |
84 |
|
|
85 |
|
|
86 |
+ |
void |
87 |
|
setambres(ar) /* set ambient resolution */ |
88 |
|
int ar; |
89 |
|
{ |
105 |
|
} |
106 |
|
|
107 |
|
|
108 |
+ |
void |
109 |
|
setambacc(newa) /* set ambient accuracy */ |
110 |
|
double newa; |
111 |
|
{ |
119 |
|
} |
120 |
|
|
121 |
|
|
122 |
< |
setambient(afile) /* initialize calculation */ |
123 |
< |
char *afile; |
122 |
> |
void |
123 |
> |
setambient() /* initialize calculation */ |
124 |
|
{ |
125 |
+ |
int readonly = 0; |
126 |
|
long pos, flen; |
127 |
|
AMBVAL amb; |
128 |
+ |
/* make sure we're fresh */ |
129 |
+ |
ambdone(); |
130 |
|
/* init ambient limits */ |
131 |
|
setambres(ambres); |
132 |
|
setambacc(ambacc); |
133 |
< |
if (afile == NULL) |
133 |
> |
if (ambfile == NULL || !ambfile[0]) |
134 |
|
return; |
135 |
|
if (ambacc <= FTINY) { |
136 |
|
sprintf(errmsg, "zero ambient accuracy so \"%s\" not opened", |
137 |
< |
afile); |
137 |
> |
ambfile); |
138 |
|
error(WARNING, errmsg); |
139 |
|
return; |
140 |
|
} |
141 |
|
/* open ambient file */ |
142 |
< |
if ((ambfp = fopen(afile, "r+")) != NULL) { |
143 |
< |
initambfile(0); |
142 |
> |
if ((ambfp = fopen(ambfile, "r+")) == NULL) |
143 |
> |
readonly = (ambfp = fopen(ambfile, "r")) != NULL; |
144 |
> |
if (ambfp != NULL) { |
145 |
> |
initambfile(0); /* file exists */ |
146 |
|
pos = ftell(ambfp); |
147 |
|
while (readambval(&amb, ambfp)) |
148 |
|
avinsert(avstore(&amb)); |
149 |
< |
/* align */ |
149 |
> |
nambshare = nambvals; /* share loaded values */ |
150 |
> |
if (readonly) { |
151 |
> |
sprintf(errmsg, |
152 |
> |
"loaded %u values from read-only ambient file", |
153 |
> |
nambvals); |
154 |
> |
error(WARNING, errmsg); |
155 |
> |
fclose(ambfp); /* close file so no writes */ |
156 |
> |
ambfp = NULL; |
157 |
> |
return; /* avoid ambsync() */ |
158 |
> |
} |
159 |
> |
/* align file pointer */ |
160 |
|
pos += (long)nambvals*AMBVALSIZ; |
161 |
< |
flen = lseek(fileno(ambfp), 0L, 2); |
161 |
> |
flen = lseek(fileno(ambfp), (off_t)0L, 2); |
162 |
|
if (flen != pos) { |
163 |
|
sprintf(errmsg, |
164 |
|
"ignoring last %ld values in ambient file (corrupted)", |
165 |
|
(flen - pos)/AMBVALSIZ); |
166 |
|
error(WARNING, errmsg); |
167 |
|
fseek(ambfp, pos, 0); |
168 |
< |
ftruncate(fileno(ambfp), pos); |
168 |
> |
ftruncate(fileno(ambfp), (off_t)pos); |
169 |
|
} |
170 |
< |
nambshare = nambvals; |
171 |
< |
} else if ((ambfp = fopen(afile, "w+")) != NULL) |
172 |
< |
initambfile(1); |
173 |
< |
else { |
162 |
< |
sprintf(errmsg, "cannot open ambient file \"%s\"", afile); |
170 |
> |
} else if ((ambfp = fopen(ambfile, "w+")) != NULL) { |
171 |
> |
initambfile(1); /* else create new file */ |
172 |
> |
} else { |
173 |
> |
sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile); |
174 |
|
error(SYSTEM, errmsg); |
175 |
|
} |
176 |
|
nunflshed++; /* lie */ |
178 |
|
} |
179 |
|
|
180 |
|
|
181 |
+ |
void |
182 |
+ |
ambdone() /* close ambient file and free memory */ |
183 |
+ |
{ |
184 |
+ |
if (ambfp != NULL) { /* close ambient file */ |
185 |
+ |
ambsync(); |
186 |
+ |
fclose(ambfp); |
187 |
+ |
ambfp = NULL; |
188 |
+ |
if (ambinp != NULL) { |
189 |
+ |
fclose(ambinp); |
190 |
+ |
ambinp = NULL; |
191 |
+ |
} |
192 |
+ |
lastpos = -1; |
193 |
+ |
} |
194 |
+ |
/* free ambient tree */ |
195 |
+ |
unloadatree(&atrunk, free); |
196 |
+ |
/* reset state variables */ |
197 |
+ |
avsum = 0.; |
198 |
+ |
navsum = 0; |
199 |
+ |
nambvals = 0; |
200 |
+ |
nambshare = 0; |
201 |
+ |
ambclock = 0; |
202 |
+ |
lastsort = 0; |
203 |
+ |
sortintvl = SORT_INTVL; |
204 |
+ |
} |
205 |
+ |
|
206 |
+ |
|
207 |
+ |
void |
208 |
|
ambnotify(obj) /* record new modifier */ |
209 |
|
OBJECT obj; |
210 |
|
{ |
211 |
|
static int hitlimit = 0; |
212 |
< |
register OBJREC *o = objptr(obj); |
212 |
> |
register OBJREC *o; |
213 |
|
register char **amblp; |
214 |
|
|
215 |
+ |
if (obj == OVOID) { /* starting over */ |
216 |
+ |
ambset[0] = 0; |
217 |
+ |
hitlimit = 0; |
218 |
+ |
return; |
219 |
+ |
} |
220 |
+ |
o = objptr(obj); |
221 |
|
if (hitlimit || !ismodifier(o->otype)) |
222 |
|
return; |
223 |
|
for (amblp = amblist; *amblp != NULL; amblp++) |
233 |
|
} |
234 |
|
|
235 |
|
|
236 |
+ |
void |
237 |
|
ambient(acol, r, nrm) /* compute ambient component for ray */ |
238 |
|
COLOR acol; |
239 |
|
register RAY *r; |
313 |
|
wsum = 0.0; |
314 |
|
/* do this node */ |
315 |
|
for (av = at->alist; av != NULL; av = av->next) { |
316 |
+ |
double rn_dot = -2.0; |
317 |
|
if (tracktime) |
318 |
|
av->latick = ambclock; |
319 |
|
/* |
336 |
|
if (e1 > ambacc*ambacc*1.21) |
337 |
|
continue; |
338 |
|
/* |
339 |
< |
* Normal direction test. |
339 |
> |
* Direction test using closest normal. |
340 |
|
*/ |
341 |
< |
e2 = (1.0 - DOT(av->dir, r->ron)) * r->rweight; |
341 |
> |
d = DOT(av->dir, r->ron); |
342 |
> |
if (rn != r->ron) { |
343 |
> |
rn_dot = DOT(av->dir, rn); |
344 |
> |
if (rn_dot > 1.0-FTINY) |
345 |
> |
rn_dot = 1.0-FTINY; |
346 |
> |
if (rn_dot >= d-FTINY) { |
347 |
> |
d = rn_dot; |
348 |
> |
rn_dot = -2.0; |
349 |
> |
} |
350 |
> |
} |
351 |
> |
e2 = (1.0 - d) * r->rweight; |
352 |
|
if (e2 < 0.0) e2 = 0.0; |
353 |
|
if (e1 + e2 > ambacc*ambacc*1.21) |
354 |
|
continue; |
364 |
|
/* |
365 |
|
* Jittering final test reduces image artifacts. |
366 |
|
*/ |
367 |
< |
wt = sqrt(e1) + sqrt(e2); |
367 |
> |
e1 = sqrt(e1); |
368 |
> |
e2 = sqrt(e2); |
369 |
> |
wt = e1 + e2; |
370 |
|
if (wt > ambacc*(.9+.2*urand(9015+samplendx))) |
371 |
|
continue; |
372 |
+ |
/* |
373 |
+ |
* Recompute directional error using perturbed normal |
374 |
+ |
*/ |
375 |
+ |
if (rn_dot > 0.0) { |
376 |
+ |
e2 = sqrt((1.0 - rn_dot)*r->rweight); |
377 |
+ |
wt = e1 + e2; |
378 |
+ |
} |
379 |
|
if (wt <= 1e-3) |
380 |
|
wt = 1e3; |
381 |
|
else |
438 |
|
} |
439 |
|
|
440 |
|
|
441 |
+ |
void |
442 |
|
extambient(cr, ap, pv, nv) /* extrapolate value at pv, nv */ |
443 |
|
COLOR cr; |
444 |
|
register AMBVAL *ap; |
445 |
|
FVECT pv, nv; |
446 |
|
{ |
447 |
< |
FVECT v1, v2; |
447 |
> |
FVECT v1; |
448 |
|
register int i; |
449 |
|
double d; |
450 |
|
|
453 |
|
for (i = 0; i < 3; i++) |
454 |
|
d += ap->gpos[i]*(pv[i]-ap->pos[i]); |
455 |
|
/* gradient due to rotation */ |
456 |
< |
VCOPY(v1, ap->dir); |
457 |
< |
fcross(v2, v1, nv); |
392 |
< |
d += DOT(ap->gdir, v2); |
456 |
> |
VCROSS(v1, ap->dir, nv); |
457 |
> |
d += DOT(ap->gdir, v1); |
458 |
|
if (d <= 0.0) { |
459 |
|
setcolor(cr, 0.0, 0.0, 0.0); |
460 |
|
return; |
464 |
|
} |
465 |
|
|
466 |
|
|
467 |
< |
static |
467 |
> |
static void |
468 |
|
initambfile(creat) /* initialize ambient file */ |
469 |
|
int creat; |
470 |
|
{ |
471 |
< |
extern char *progname, *octname, VersionID[]; |
471 |
> |
extern char *progname, *octname; |
472 |
> |
static char *mybuf = NULL; |
473 |
|
|
474 |
|
#ifdef F_SETLKW |
475 |
|
aflock(creat ? F_WRLCK : F_RDLCK); |
477 |
|
#ifdef MSDOS |
478 |
|
setmode(fileno(ambfp), O_BINARY); |
479 |
|
#endif |
480 |
< |
setbuf(ambfp, bmalloc(BUFSIZ+8)); |
480 |
> |
if (mybuf == NULL) |
481 |
> |
mybuf = (char *)bmalloc(BUFSIZ+8); |
482 |
> |
setbuf(ambfp, mybuf); |
483 |
|
if (creat) { /* new file */ |
484 |
|
newheader("RADIANCE", ambfp); |
485 |
|
fprintf(ambfp, "%s -av %g %g %g -aw %d -ab %d -aa %g ", |
486 |
|
progname, colval(ambval,RED), |
487 |
|
colval(ambval,GRN), colval(ambval,BLU), |
488 |
|
ambvwt, ambounce, ambacc); |
489 |
< |
fprintf(ambfp, "-ad %d -as %d -ar %d %s\n", |
490 |
< |
ambdiv, ambssamp, ambres, |
491 |
< |
octname==NULL ? "" : octname); |
489 |
> |
fprintf(ambfp, "-ad %d -as %d -ar %d ", |
490 |
> |
ambdiv, ambssamp, ambres); |
491 |
> |
if (octname != NULL) |
492 |
> |
printargs(1, &octname, ambfp); |
493 |
> |
else |
494 |
> |
fputc('\n', ambfp); |
495 |
|
fprintf(ambfp, "SOFTWARE= %s\n", VersionID); |
496 |
+ |
fputnow(ambfp); |
497 |
|
fputformat(AMBFMT, ambfp); |
498 |
|
putc('\n', ambfp); |
499 |
|
putambmagic(ambfp); |
502 |
|
} |
503 |
|
|
504 |
|
|
505 |
< |
static |
505 |
> |
static void |
506 |
|
avsave(av) /* insert and save an ambient value */ |
507 |
|
AMBVAL *av; |
508 |
|
{ |
516 |
|
goto writerr; |
517 |
|
return; |
518 |
|
writerr: |
519 |
< |
error(SYSTEM, "error writing ambient file"); |
519 |
> |
error(SYSTEM, "error writing to ambient file"); |
520 |
|
} |
521 |
|
|
522 |
|
|
547 |
|
static AMBTREE *atfreelist = NULL; /* free ambient tree structures */ |
548 |
|
|
549 |
|
|
550 |
< |
static |
479 |
< |
AMBTREE * |
550 |
> |
static AMBTREE * |
551 |
|
newambtree() /* allocate 8 ambient tree structs */ |
552 |
|
{ |
553 |
|
register AMBTREE *atp, *upperlim; |
554 |
|
|
555 |
|
if (atfreelist == NULL) { /* get more nodes */ |
556 |
< |
atfreelist = (AMBTREE *)bmalloc(ATALLOCSZ*8*sizeof(AMBTREE)); |
556 |
> |
atfreelist = (AMBTREE *)malloc(ATALLOCSZ*8*sizeof(AMBTREE)); |
557 |
|
if (atfreelist == NULL) |
558 |
|
return(NULL); |
559 |
|
/* link new free list */ |
569 |
|
} |
570 |
|
|
571 |
|
|
572 |
< |
static |
572 |
> |
static void |
573 |
|
freeambtree(atp) /* free 8 ambient tree structs */ |
574 |
|
AMBTREE *atp; |
575 |
|
{ |
578 |
|
} |
579 |
|
|
580 |
|
|
581 |
< |
static |
581 |
> |
static void |
582 |
|
avinsert(av) /* insert ambient value in our tree */ |
583 |
|
register AMBVAL *av; |
584 |
|
{ |
618 |
|
} |
619 |
|
|
620 |
|
|
621 |
< |
static |
621 |
> |
static void |
622 |
|
unloadatree(at, f) /* unload an ambient value tree */ |
623 |
|
register AMBTREE *at; |
624 |
< |
int (*f)(); |
624 |
> |
void (*f)(); |
625 |
|
{ |
626 |
|
register AMBVAL *av; |
627 |
|
register int i; |
647 |
|
static int i_avlist; /* index for lists */ |
648 |
|
|
649 |
|
|
650 |
< |
static |
650 |
> |
static int |
651 |
|
av2list(av) |
652 |
|
register AMBVAL *av; |
653 |
|
{ |
669 |
|
} |
670 |
|
|
671 |
|
|
672 |
+ |
/* GW NOTE 2002/10/3: |
673 |
+ |
* I used to compare AMBVAL pointers, but found that this was the |
674 |
+ |
* cause of a serious consistency error with gcc, since the optimizer |
675 |
+ |
* uses some dangerous trick in pointer subtraction that |
676 |
+ |
* assumes pointers differ by exact struct size increments. |
677 |
+ |
*/ |
678 |
|
static int |
679 |
|
aposcmp(avp1, avp2) /* compare ambient value positions */ |
680 |
< |
AMBVAL **avp1, **avp2; |
680 |
> |
const void *avp1, *avp2; |
681 |
|
{ |
682 |
< |
return(*avp1 - *avp2); |
682 |
> |
register long diff = *(char * const *)avp1 - *(char * const *)avp2; |
683 |
> |
if (diff < 0) |
684 |
> |
return(-1); |
685 |
> |
return(diff > 0); |
686 |
|
} |
687 |
|
|
608 |
– |
|
688 |
|
#if 1 |
689 |
|
static int |
690 |
|
avlmemi(avaddr) /* find list position from address */ |
704 |
|
#endif |
705 |
|
|
706 |
|
|
707 |
< |
static |
707 |
> |
static void |
708 |
|
sortambvals(always) /* resort ambient values */ |
709 |
|
int always; |
710 |
|
{ |
739 |
|
} |
740 |
|
if (avlist1 == NULL) { /* no time tracking -- rebuild tree? */ |
741 |
|
if (avlist2 != NULL) |
742 |
< |
free((char *)avlist2); |
742 |
> |
free((void *)avlist2); |
743 |
|
if (always) { /* rebuild without sorting */ |
744 |
|
copystruct(&oldatrunk, &atrunk); |
745 |
|
atrunk.alist = NULL; |
783 |
|
avinsert(avlist2[j]); |
784 |
|
avlist1[j].p = NULL; |
785 |
|
} |
786 |
< |
free((char *)avlist1); |
787 |
< |
free((char *)avlist2); |
786 |
> |
free((void *)avlist1); |
787 |
> |
free((void *)avlist2); |
788 |
|
/* compute new sort interval */ |
789 |
|
sortintvl = ambclock - lastsort; |
790 |
|
if (sortintvl >= MAX_SORT_INTVL/2) |
803 |
|
|
804 |
|
#ifdef F_SETLKW |
805 |
|
|
806 |
< |
static |
806 |
> |
static void |
807 |
|
aflock(typ) /* lock/unlock ambient file */ |
808 |
|
int typ; |
809 |
|
{ |
818 |
|
int |
819 |
|
ambsync() /* synchronize ambient file */ |
820 |
|
{ |
742 |
– |
static FILE *ambinp = NULL; |
743 |
– |
static long lastpos = -1; |
821 |
|
long flen; |
822 |
|
AMBVAL avs; |
823 |
|
register int n; |
829 |
|
/* gain exclusive access */ |
830 |
|
aflock(F_WRLCK); |
831 |
|
/* see if file has grown */ |
832 |
< |
if ((flen = lseek(fileno(ambfp), 0L, 2)) < 0) |
832 |
> |
if ((flen = lseek(fileno(ambfp), (off_t)0L, 2)) < 0) |
833 |
|
goto seekerr; |
834 |
|
if (n = flen - lastpos) { /* file has grown */ |
835 |
|
if (ambinp == NULL) { /* use duplicate filedes */ |
842 |
|
while (n >= AMBVALSIZ) { /* load contributed values */ |
843 |
|
if (!readambval(&avs, ambinp)) { |
844 |
|
sprintf(errmsg, |
845 |
< |
"ambient file corrupted near character %ld", |
846 |
< |
flen - n); |
845 |
> |
"ambient file \"%s\" corrupted near character %ld", |
846 |
> |
ambfile, flen - n); |
847 |
|
error(WARNING, errmsg); |
848 |
|
break; |
849 |
|
} |
852 |
|
} |
853 |
|
/*** seek always as safety measure |
854 |
|
if (n) ***/ /* alignment */ |
855 |
< |
if (lseek(fileno(ambfp), flen-n, 0) < 0) |
855 |
> |
if (lseek(fileno(ambfp), (off_t)(flen-n), 0) < 0) |
856 |
|
goto seekerr; |
857 |
|
} |
858 |
|
#ifdef DEBUG |
865 |
|
#endif |
866 |
|
syncend: |
867 |
|
n = fflush(ambfp); /* calls write() at last */ |
868 |
< |
if ((lastpos = lseek(fileno(ambfp), 0L, 1)) < 0) |
868 |
> |
if ((lastpos = lseek(fileno(ambfp), (off_t)0L, 1)) < 0) |
869 |
|
goto seekerr; |
870 |
|
aflock(F_UNLCK); /* release file */ |
871 |
|
nunflshed = 0; |