1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: readmesh.c,v 2.2 2003/03/14 21:27:46 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* Routines for reading a compiled mesh from a file |
6 |
*/ |
7 |
|
8 |
#include "standard.h" |
9 |
#include "octree.h" |
10 |
#include "object.h" |
11 |
#include "mesh.h" |
12 |
|
13 |
static char *meshfn; /* input file name */ |
14 |
static FILE *meshfp; /* mesh file pointer */ |
15 |
static int objsize; /* sizeof(OBJECT) from writer */ |
16 |
|
17 |
|
18 |
static void |
19 |
mesherror(etyp, msg) /* mesh read error */ |
20 |
int etyp; |
21 |
char *msg; |
22 |
{ |
23 |
char msgbuf[128]; |
24 |
|
25 |
sprintf(msgbuf, "(%s): %s", meshfn, msg); |
26 |
error(etyp, msgbuf); |
27 |
} |
28 |
|
29 |
|
30 |
static long |
31 |
mgetint(siz) /* get a siz-byte integer */ |
32 |
int siz; |
33 |
{ |
34 |
register long r; |
35 |
|
36 |
r = getint(siz, meshfp); |
37 |
if (feof(meshfp)) |
38 |
mesherror(USER, "truncated mesh file"); |
39 |
return(r); |
40 |
} |
41 |
|
42 |
|
43 |
static double |
44 |
mgetflt() /* get a floating point number */ |
45 |
{ |
46 |
double r; |
47 |
|
48 |
r = getflt(meshfp); |
49 |
if (feof(meshfp)) |
50 |
mesherror(USER, "truncated mesh file"); |
51 |
return(r); |
52 |
} |
53 |
|
54 |
|
55 |
static OCTREE |
56 |
getfullnode() /* get a set, return fullnode */ |
57 |
{ |
58 |
OBJECT set[MAXSET+1]; |
59 |
register int i; |
60 |
|
61 |
if ((set[0] = mgetint(objsize)) > MAXSET) |
62 |
mesherror(USER, "bad set in getfullnode"); |
63 |
for (i = 1; i <= set[0]; i++) |
64 |
set[i] = mgetint(objsize); |
65 |
return(fullnode(set)); |
66 |
} |
67 |
|
68 |
|
69 |
static OCTREE |
70 |
gettree() /* get a pre-ordered octree */ |
71 |
{ |
72 |
register OCTREE ot; |
73 |
register int i; |
74 |
|
75 |
switch (getc(meshfp)) { |
76 |
case OT_EMPTY: |
77 |
return(EMPTY); |
78 |
case OT_FULL: |
79 |
return(getfullnode()); |
80 |
case OT_TREE: |
81 |
if ((ot = octalloc()) == EMPTY) |
82 |
mesherror(SYSTEM, "out of tree space in readmesh"); |
83 |
for (i = 0; i < 8; i++) |
84 |
octkid(ot, i) = gettree(); |
85 |
return(ot); |
86 |
case EOF: |
87 |
mesherror(USER, "truncated mesh octree"); |
88 |
default: |
89 |
mesherror(USER, "damaged mesh octree"); |
90 |
} |
91 |
} |
92 |
|
93 |
|
94 |
static void |
95 |
skiptree() /* skip octree on input */ |
96 |
{ |
97 |
register int i; |
98 |
|
99 |
switch (getc(meshfp)) { |
100 |
case OT_EMPTY: |
101 |
return; |
102 |
case OT_FULL: |
103 |
for (i = mgetint(objsize)*objsize; i-- > 0; ) |
104 |
if (getc(meshfp) == EOF) |
105 |
mesherror(USER, "truncated mesh octree"); |
106 |
return; |
107 |
case OT_TREE: |
108 |
for (i = 0; i < 8; i++) |
109 |
skiptree(); |
110 |
return; |
111 |
case EOF: |
112 |
mesherror(USER, "truncated mesh octree"); |
113 |
default: |
114 |
mesherror(USER, "damaged mesh octree"); |
115 |
} |
116 |
} |
117 |
|
118 |
|
119 |
static void |
120 |
getpatch(pp) /* load a mesh patch */ |
121 |
register MESHPATCH *pp; |
122 |
{ |
123 |
int flags; |
124 |
int i, j; |
125 |
/* vertex flags */ |
126 |
flags = mgetint(1); |
127 |
if (!(flags & MT_V) || flags & ~(MT_V|MT_N|MT_UV)) |
128 |
mesherror(USER, "bad patch flags"); |
129 |
/* allocate vertices */ |
130 |
pp->nverts = mgetint(2); |
131 |
if (pp->nverts <= 0 || pp->nverts > 256) |
132 |
mesherror(USER, "bad number of patch vertices"); |
133 |
pp->xyz = (uint4 (*)[3])malloc(pp->nverts*3*sizeof(uint4)); |
134 |
if (pp->xyz == NULL) |
135 |
goto nomem; |
136 |
if (flags & MT_N) { |
137 |
pp->norm = (int4 *)calloc(pp->nverts, sizeof(int4)); |
138 |
if (pp->norm == NULL) |
139 |
goto nomem; |
140 |
} else |
141 |
pp->norm = NULL; |
142 |
if (flags & MT_UV) { |
143 |
pp->uv = (uint4 (*)[2])calloc(pp->nverts, 2*sizeof(uint4)); |
144 |
if (pp->uv == NULL) |
145 |
goto nomem; |
146 |
} else |
147 |
pp->uv = NULL; |
148 |
/* vertex xyz locations */ |
149 |
for (i = 0; i < pp->nverts; i++) |
150 |
for (j = 0; j < 3; j++) |
151 |
pp->xyz[i][j] = mgetint(4); |
152 |
/* vertex normals */ |
153 |
if (flags & MT_N) |
154 |
for (i = 0; i < pp->nverts; i++) |
155 |
pp->norm[i] = mgetint(4); |
156 |
/* uv coordinates */ |
157 |
if (flags & MT_UV) |
158 |
for (i = 0; i < pp->nverts; i++) |
159 |
for (j = 0; j < 2; j++) |
160 |
pp->uv[i][j] = mgetint(4); |
161 |
/* local triangles */ |
162 |
pp->ntris = mgetint(2); |
163 |
if (pp->ntris < 0 || pp->ntris > 512) |
164 |
mesherror(USER, "bad number of local triangles"); |
165 |
if (pp->ntris) { |
166 |
pp->tri = (struct PTri *)malloc(pp->ntris * |
167 |
sizeof(struct PTri)); |
168 |
if (pp->tri == NULL) |
169 |
goto nomem; |
170 |
for (i = 0; i < pp->ntris; i++) { |
171 |
pp->tri[i].v1 = mgetint(1); |
172 |
pp->tri[i].v2 = mgetint(1); |
173 |
pp->tri[i].v3 = mgetint(1); |
174 |
} |
175 |
} else |
176 |
pp->tri = NULL; |
177 |
/* local triangle material(s) */ |
178 |
if (mgetint(2) > 1) { |
179 |
pp->trimat = (int2 *)malloc(pp->ntris*sizeof(int2)); |
180 |
if (pp->trimat == NULL) |
181 |
goto nomem; |
182 |
for (i = 0; i < pp->ntris; i++) |
183 |
pp->trimat[i] = mgetint(2); |
184 |
} else { |
185 |
pp->solemat = mgetint(2); |
186 |
pp->trimat = NULL; |
187 |
} |
188 |
/* joiner triangles */ |
189 |
pp->nj1tris = mgetint(2); |
190 |
if (pp->nj1tris < 0 || pp->nj1tris > 512) |
191 |
mesherror(USER, "bad number of joiner triangles"); |
192 |
if (pp->nj1tris) { |
193 |
pp->j1tri = (struct PJoin1 *)malloc(pp->nj1tris * |
194 |
sizeof(struct PJoin1)); |
195 |
if (pp->j1tri == NULL) |
196 |
goto nomem; |
197 |
for (i = 0; i < pp->nj1tris; i++) { |
198 |
pp->j1tri[i].v1j = mgetint(4); |
199 |
pp->j1tri[i].v2 = mgetint(1); |
200 |
pp->j1tri[i].v3 = mgetint(1); |
201 |
pp->j1tri[i].mat = mgetint(2); |
202 |
} |
203 |
} else |
204 |
pp->j1tri = NULL; |
205 |
/* double joiner triangles */ |
206 |
pp->nj2tris = mgetint(2); |
207 |
if (pp->nj2tris < 0 || pp->nj2tris > 256) |
208 |
mesherror(USER, "bad number of double joiner triangles"); |
209 |
if (pp->nj2tris) { |
210 |
pp->j2tri = (struct PJoin2 *)malloc(pp->nj2tris * |
211 |
sizeof(struct PJoin2)); |
212 |
if (pp->j2tri == NULL) |
213 |
goto nomem; |
214 |
for (i = 0; i < pp->nj2tris; i++) { |
215 |
pp->j2tri[i].v1j = mgetint(4); |
216 |
pp->j2tri[i].v2j = mgetint(4); |
217 |
pp->j2tri[i].v3 = mgetint(1); |
218 |
pp->j2tri[i].mat = mgetint(2); |
219 |
} |
220 |
} else |
221 |
pp->j2tri = NULL; |
222 |
return; |
223 |
nomem: |
224 |
error(SYSTEM, "out of mesh memory in getpatch"); |
225 |
} |
226 |
|
227 |
|
228 |
void |
229 |
readmesh(mp, path, flags) /* read in mesh structures */ |
230 |
MESH *mp; |
231 |
char *path; |
232 |
int flags; |
233 |
{ |
234 |
char *err; |
235 |
char sbuf[64]; |
236 |
int i; |
237 |
/* check what's loaded */ |
238 |
flags &= (IO_INFO|IO_BOUNDS|IO_TREE|IO_SCENE) & ~mp->ldflags; |
239 |
/* open input file */ |
240 |
if (path == NULL) { |
241 |
meshfn = "standard input"; |
242 |
meshfp = stdin; |
243 |
} else if ((meshfp = fopen(meshfn=path, "r")) == NULL) { |
244 |
sprintf(errmsg, "cannot open mesh file \"%s\"", path); |
245 |
error(SYSTEM, errmsg); |
246 |
} |
247 |
SET_FILE_BINARY(meshfp); |
248 |
/* read header */ |
249 |
checkheader(meshfp, MESHFMT, flags&IO_INFO ? stdout : (FILE *)NULL); |
250 |
/* read format number */ |
251 |
objsize = getint(2, meshfp) - MESHMAGIC; |
252 |
if (objsize <= 0 || objsize > MAXOBJSIZ || objsize > sizeof(long)) |
253 |
mesherror(USER, "incompatible mesh format"); |
254 |
/* read boundaries */ |
255 |
if (flags & IO_BOUNDS) { |
256 |
for (i = 0; i < 3; i++) |
257 |
mp->mcube.cuorg[i] = atof(getstr(sbuf, meshfp)); |
258 |
mp->mcube.cusize = atof(getstr(sbuf, meshfp)); |
259 |
for (i = 0; i < 2; i++) { |
260 |
mp->uvlim[0][i] = mgetflt(); |
261 |
mp->uvlim[1][i] = mgetflt(); |
262 |
} |
263 |
} else { |
264 |
for (i = 0; i < 4; i++) |
265 |
getstr(sbuf, meshfp); |
266 |
for (i = 0; i < 4; i++) |
267 |
mgetflt(); |
268 |
} |
269 |
/* read the octree */ |
270 |
if (flags & IO_TREE) |
271 |
mp->mcube.cutree = gettree(); |
272 |
else if (flags & IO_SCENE) |
273 |
skiptree(); |
274 |
/* read materials and patches */ |
275 |
if (flags & IO_SCENE) { |
276 |
mp->mat0 = nobjects; |
277 |
readscene(meshfp, objsize); |
278 |
mp->nmats = nobjects - mp->mat0; |
279 |
mp->npatches = mgetint(4); |
280 |
mp->patch = (MESHPATCH *)calloc(mp->npatches, |
281 |
sizeof(MESHPATCH)); |
282 |
if (mp->patch == NULL) |
283 |
mesherror(SYSTEM, "out of patch memory"); |
284 |
for (i = 0; i < mp->npatches; i++) |
285 |
getpatch(&mp->patch[i]); |
286 |
} |
287 |
/* clean up */ |
288 |
fclose(meshfp); |
289 |
mp->ldflags |= flags; |
290 |
/* verify data */ |
291 |
if ((err = checkmesh(mp)) != NULL) |
292 |
mesherror(USER, err); |
293 |
} |