1 |
greg |
2.1 |
#ifndef lint |
2 |
greg |
2.3 |
static const char RCSid[] = "$Id: sceneio.c,v 2.2 2003/06/07 12:50:20 schorsch Exp $"; |
3 |
greg |
2.1 |
#endif |
4 |
|
|
/* |
5 |
|
|
* Portable, binary Radiance i/o routines. |
6 |
|
|
* |
7 |
|
|
* Called from octree and mesh i/o routines. |
8 |
|
|
*/ |
9 |
|
|
|
10 |
|
|
#include "copyright.h" |
11 |
|
|
#include "standard.h" |
12 |
|
|
#include "octree.h" |
13 |
|
|
#include "object.h" |
14 |
|
|
#include "otypes.h" |
15 |
|
|
|
16 |
|
|
static OBJECT object0; /* zeroeth object */ |
17 |
|
|
static short otypmap[NUMOTYPE+32]; /* object type map */ |
18 |
|
|
|
19 |
|
|
|
20 |
|
|
static int |
21 |
|
|
getobj(fp, objsiz) /* get next object */ |
22 |
|
|
FILE *fp; |
23 |
|
|
int objsiz; |
24 |
|
|
{ |
25 |
|
|
char sbuf[MAXSTR]; |
26 |
|
|
int obj; |
27 |
|
|
register int i; |
28 |
|
|
register long m; |
29 |
|
|
register OBJREC *objp; |
30 |
|
|
|
31 |
|
|
i = getint(1, fp); |
32 |
|
|
if (i == -1) |
33 |
|
|
return(OVOID); /* terminator */ |
34 |
|
|
if ((obj = newobject()) == OVOID) |
35 |
|
|
error(SYSTEM, "out of object space"); |
36 |
|
|
objp = objptr(obj); |
37 |
|
|
if ((objp->otype = otypmap[i]) < 0) |
38 |
|
|
error(USER, "reference to unknown type"); |
39 |
|
|
if ((m = getint(objsiz, fp)) != OVOID) { |
40 |
|
|
m += object0; |
41 |
|
|
if ((OBJECT)m != m) |
42 |
|
|
error(INTERNAL, "too many objects in getobj"); |
43 |
|
|
} |
44 |
|
|
objp->omod = m; |
45 |
|
|
objp->oname = savqstr(getstr(sbuf, fp)); |
46 |
|
|
if ((objp->oargs.nsargs = getint(2, fp)) > 0) { |
47 |
|
|
objp->oargs.sarg = (char **)malloc |
48 |
|
|
(objp->oargs.nsargs*sizeof(char *)); |
49 |
|
|
if (objp->oargs.sarg == NULL) |
50 |
|
|
goto memerr; |
51 |
|
|
for (i = 0; i < objp->oargs.nsargs; i++) |
52 |
|
|
objp->oargs.sarg[i] = savestr(getstr(sbuf, fp)); |
53 |
|
|
} else |
54 |
|
|
objp->oargs.sarg = NULL; |
55 |
|
|
#ifdef IARGS |
56 |
|
|
if ((objp->oargs.niargs = getint(2, fp)) > 0) { |
57 |
|
|
objp->oargs.iarg = (long *)malloc |
58 |
|
|
(objp->oargs.niargs*sizeof(long)); |
59 |
|
|
if (objp->oargs.iarg == NULL) |
60 |
|
|
goto memerr; |
61 |
|
|
for (i = 0; i < objp->oargs.niargs; i++) |
62 |
|
|
objp->oargs.iarg[i] = getint(4, fp); |
63 |
|
|
} else |
64 |
|
|
objp->oargs.iarg = NULL; |
65 |
|
|
#endif |
66 |
|
|
if ((objp->oargs.nfargs = getint(2, fp)) > 0) { |
67 |
|
|
objp->oargs.farg = (FLOAT *)malloc |
68 |
|
|
(objp->oargs.nfargs*sizeof(FLOAT)); |
69 |
|
|
if (objp->oargs.farg == NULL) |
70 |
|
|
goto memerr; |
71 |
|
|
for (i = 0; i < objp->oargs.nfargs; i++) |
72 |
|
|
objp->oargs.farg[i] = getflt(fp); |
73 |
|
|
} else |
74 |
|
|
objp->oargs.farg = NULL; |
75 |
|
|
if (feof(fp)) |
76 |
|
|
error(SYSTEM, "unexpected EOF in getobj"); |
77 |
|
|
/* initialize */ |
78 |
|
|
objp->os = NULL; |
79 |
|
|
/* insert */ |
80 |
|
|
insertobject(obj); |
81 |
|
|
return(obj); |
82 |
|
|
memerr: |
83 |
|
|
error(SYSTEM, "out of memory in getobj"); |
84 |
schorsch |
2.2 |
return 0; /* pro forma return */ |
85 |
greg |
2.1 |
} |
86 |
|
|
|
87 |
|
|
|
88 |
|
|
void |
89 |
|
|
readscene(fp, objsiz) /* read binary scene description */ |
90 |
|
|
FILE *fp; |
91 |
|
|
int objsiz; |
92 |
|
|
{ |
93 |
|
|
char sbuf[32]; |
94 |
|
|
int i; |
95 |
|
|
/* record starting object */ |
96 |
|
|
object0 = nobjects; |
97 |
|
|
/* read type map */ |
98 |
|
|
for (i = 0; getstr(sbuf, fp) != NULL && sbuf[0]; i++) |
99 |
|
|
if ((otypmap[i] = otype(sbuf)) < 0) { |
100 |
|
|
sprintf(errmsg, "unknown object type \"%s\"", |
101 |
|
|
sbuf); |
102 |
|
|
error(WARNING, errmsg); |
103 |
|
|
} |
104 |
|
|
/* read objects */ |
105 |
|
|
while (getobj(fp, objsiz) != OVOID) |
106 |
|
|
; |
107 |
|
|
} |
108 |
|
|
|
109 |
|
|
|
110 |
|
|
static void |
111 |
|
|
putobj(o, fp) /* write out object */ |
112 |
|
|
FILE *fp; |
113 |
|
|
register OBJREC *o; |
114 |
|
|
{ |
115 |
|
|
register int i; |
116 |
|
|
|
117 |
|
|
if (o == NULL) { /* terminator */ |
118 |
|
|
putint(-1L, 1, fp); |
119 |
|
|
return; |
120 |
|
|
} |
121 |
|
|
putint((long)o->otype, 1, fp); |
122 |
|
|
putint((long)o->omod, sizeof(OBJECT), fp); |
123 |
|
|
putstr(o->oname, fp); |
124 |
|
|
putint((long)o->oargs.nsargs, 2, fp); |
125 |
|
|
for (i = 0; i < o->oargs.nsargs; i++) |
126 |
|
|
putstr(o->oargs.sarg[i], fp); |
127 |
|
|
#ifdef IARGS |
128 |
|
|
putint((long)o->oargs.niargs, 2, fp); |
129 |
|
|
for (i = 0; i < o->oargs.niargs; i++) |
130 |
|
|
putint((long)o->oargs.iarg[i], 4, fp); |
131 |
|
|
#endif |
132 |
|
|
putint((long)o->oargs.nfargs, 2, fp); |
133 |
|
|
for (i = 0; i < o->oargs.nfargs; i++) |
134 |
|
|
putflt(o->oargs.farg[i], fp); |
135 |
|
|
} |
136 |
|
|
|
137 |
|
|
|
138 |
|
|
void |
139 |
|
|
writescene(firstobj, nobjs, fp) /* write binary scene description */ |
140 |
greg |
2.3 |
int firstobj, nobjs; |
141 |
greg |
2.1 |
FILE *fp; |
142 |
|
|
{ |
143 |
|
|
int i; |
144 |
|
|
/* write out type list */ |
145 |
|
|
for (i = 0; i < NUMOTYPE; i++) |
146 |
|
|
putstr(ofun[i].funame, fp); |
147 |
|
|
putstr("", fp); |
148 |
|
|
/* write objects */ |
149 |
|
|
for (i = firstobj; i < firstobj+nobjs; i++) |
150 |
|
|
putobj(objptr(i), fp); |
151 |
|
|
putobj(NULL, fp); /* terminator */ |
152 |
|
|
if (ferror(fp)) |
153 |
|
|
error(SYSTEM, "write error in writescene"); |
154 |
|
|
} |