1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id$"; |
3 |
#endif |
4 |
/* |
5 |
* Read and write portable ambient values |
6 |
* |
7 |
* Declarations of external symbols in ambient.h |
8 |
*/ |
9 |
|
10 |
#include "copyright.h" |
11 |
|
12 |
#include "ray.h" |
13 |
|
14 |
#include "ambient.h" |
15 |
|
16 |
|
17 |
#define putvec(v,fp) putflt((v)[0],fp);putflt((v)[1],fp);putflt((v)[2],fp) |
18 |
|
19 |
#define getvec(v,fp) (v)[0]=getflt(fp);(v)[1]=getflt(fp);(v)[2]=getflt(fp) |
20 |
|
21 |
#define badflt(x) ((x) < -FHUGE || (x) > FHUGE) |
22 |
|
23 |
#define badvec(v) (badflt((v)[0]) || badflt((v)[1]) || badflt((v)[2])) |
24 |
|
25 |
|
26 |
|
27 |
void |
28 |
putambmagic(fp) /* write out ambient value magic number */ |
29 |
FILE *fp; |
30 |
{ |
31 |
putint((long)AMBMAGIC, 2, fp); |
32 |
} |
33 |
|
34 |
|
35 |
int |
36 |
hasambmagic(fp) /* read in and check validity of magic # */ |
37 |
FILE *fp; |
38 |
{ |
39 |
register int magic; |
40 |
|
41 |
magic = getint(2, fp); |
42 |
if (feof(fp)) |
43 |
return(0); |
44 |
return(magic == AMBMAGIC); |
45 |
} |
46 |
|
47 |
|
48 |
int |
49 |
writambval(av, fp) /* write ambient value to stream */ |
50 |
register AMBVAL *av; |
51 |
FILE *fp; |
52 |
{ |
53 |
COLR col; |
54 |
|
55 |
putint((long)av->lvl, 1, fp); |
56 |
putflt(av->weight, fp); |
57 |
putvec(av->pos, fp); |
58 |
putvec(av->dir, fp); |
59 |
setcolr(col, colval(av->val,RED), |
60 |
colval(av->val,GRN), colval(av->val,BLU)); |
61 |
fwrite((char *)col, sizeof(col), 1, fp); |
62 |
putflt(av->rad, fp); |
63 |
putvec(av->gpos, fp); |
64 |
putvec(av->gdir, fp); |
65 |
return(ferror(fp) ? -1 : 0); |
66 |
} |
67 |
|
68 |
|
69 |
int |
70 |
ambvalOK(av) /* check consistency of ambient value */ |
71 |
register AMBVAL *av; |
72 |
{ |
73 |
double d; |
74 |
|
75 |
if (badvec(av->pos)) return(0); |
76 |
if (badvec(av->dir)) return(0); |
77 |
d = DOT(av->dir,av->dir); |
78 |
if (d < 0.9999 || d > 1.0001) return(0); |
79 |
if (av->lvl < 0 || av->lvl > 100) return(0); |
80 |
if (av->weight <= 0. || av->weight > 1.) return(0); |
81 |
if (av->rad <= 0. || av->rad >= FHUGE) return(0); |
82 |
if (colval(av->val,RED) < 0. || |
83 |
colval(av->val,RED) > FHUGE || |
84 |
colval(av->val,GRN) < 0. || |
85 |
colval(av->val,GRN) > FHUGE || |
86 |
colval(av->val,BLU) < 0. || |
87 |
colval(av->val,BLU) > FHUGE) return(0); |
88 |
if (badvec(av->gpos)) return(0); |
89 |
if (badvec(av->gdir)) return(0); |
90 |
return(1); |
91 |
} |
92 |
|
93 |
|
94 |
int |
95 |
readambval(av, fp) /* read ambient value from stream */ |
96 |
register AMBVAL *av; |
97 |
FILE *fp; |
98 |
{ |
99 |
COLR col; |
100 |
|
101 |
av->lvl = getint(1, fp); |
102 |
if (feof(fp)) |
103 |
return(0); |
104 |
av->weight = getflt(fp); |
105 |
getvec(av->pos, fp); |
106 |
getvec(av->dir, fp); |
107 |
if (fread((char *)col, sizeof(col), 1, fp) != 1) |
108 |
return(0); |
109 |
colr_color(av->val, col); |
110 |
av->rad = getflt(fp); |
111 |
getvec(av->gpos, fp); |
112 |
getvec(av->gdir, fp); |
113 |
return(feof(fp) ? 0 : ambvalOK(av)); |
114 |
} |