5 |
|
* Routines for reading a compiled mesh from a file |
6 |
|
*/ |
7 |
|
|
8 |
+ |
#include <time.h> |
9 |
+ |
|
10 |
+ |
#include "platform.h" |
11 |
|
#include "standard.h" |
12 |
|
#include "octree.h" |
13 |
|
#include "object.h" |
14 |
|
#include "mesh.h" |
15 |
+ |
#include "resolu.h" |
16 |
|
|
17 |
+ |
#ifdef getc_unlocked /* avoid horrendous overhead of flockfile */ |
18 |
+ |
#undef getc |
19 |
+ |
#define getc getc_unlocked |
20 |
+ |
#endif |
21 |
+ |
|
22 |
|
static char *meshfn; /* input file name */ |
23 |
|
static FILE *meshfp; /* mesh file pointer */ |
24 |
|
static int objsize; /* sizeof(OBJECT) from writer */ |
82 |
|
register int i; |
83 |
|
|
84 |
|
switch (getc(meshfp)) { |
85 |
< |
case OT_EMPTY: |
86 |
< |
return(EMPTY); |
87 |
< |
case OT_FULL: |
88 |
< |
return(getfullnode()); |
89 |
< |
case OT_TREE: |
90 |
< |
if ((ot = octalloc()) == EMPTY) |
91 |
< |
mesherror(SYSTEM, "out of tree space in readmesh"); |
92 |
< |
for (i = 0; i < 8; i++) |
93 |
< |
octkid(ot, i) = gettree(); |
94 |
< |
return(ot); |
95 |
< |
case EOF: |
96 |
< |
mesherror(USER, "truncated mesh octree"); |
97 |
< |
default: |
98 |
< |
mesherror(USER, "damaged mesh octree"); |
85 |
> |
case OT_EMPTY: |
86 |
> |
return(EMPTY); |
87 |
> |
case OT_FULL: |
88 |
> |
return(getfullnode()); |
89 |
> |
case OT_TREE: |
90 |
> |
if ((ot = octalloc()) == EMPTY) |
91 |
> |
mesherror(SYSTEM, "out of tree space in readmesh"); |
92 |
> |
for (i = 0; i < 8; i++) |
93 |
> |
octkid(ot, i) = gettree(); |
94 |
> |
return(ot); |
95 |
> |
case EOF: |
96 |
> |
mesherror(USER, "truncated mesh octree"); |
97 |
> |
default: |
98 |
> |
mesherror(USER, "damaged mesh octree"); |
99 |
|
} |
100 |
+ |
return (OCTREE)NULL; /* pro forma return */ |
101 |
|
} |
102 |
|
|
103 |
|
|
140 |
|
pp->nverts = mgetint(2); |
141 |
|
if (pp->nverts <= 0 || pp->nverts > 256) |
142 |
|
mesherror(USER, "bad number of patch vertices"); |
143 |
< |
pp->xyz = (uint4 (*)[3])malloc(pp->nverts*3*sizeof(uint4)); |
143 |
> |
pp->xyz = (uint32 (*)[3])malloc(pp->nverts*3*sizeof(uint32)); |
144 |
|
if (pp->xyz == NULL) |
145 |
|
goto nomem; |
146 |
|
if (flags & MT_N) { |
147 |
< |
pp->norm = (int4 *)calloc(pp->nverts, sizeof(int4)); |
147 |
> |
pp->norm = (int32 *)calloc(pp->nverts, sizeof(int32)); |
148 |
|
if (pp->norm == NULL) |
149 |
|
goto nomem; |
150 |
|
} else |
151 |
|
pp->norm = NULL; |
152 |
|
if (flags & MT_UV) { |
153 |
< |
pp->uv = (uint4 (*)[2])calloc(pp->nverts, 2*sizeof(uint4)); |
153 |
> |
pp->uv = (uint32 (*)[2])calloc(pp->nverts, 2*sizeof(uint32)); |
154 |
|
if (pp->uv == NULL) |
155 |
|
goto nomem; |
156 |
|
} else |
184 |
|
} |
185 |
|
} else |
186 |
|
pp->tri = NULL; |
187 |
+ |
/* local triangle material(s) */ |
188 |
+ |
if (mgetint(2) > 1) { |
189 |
+ |
pp->trimat = (int16 *)malloc(pp->ntris*sizeof(int16)); |
190 |
+ |
if (pp->trimat == NULL) |
191 |
+ |
goto nomem; |
192 |
+ |
for (i = 0; i < pp->ntris; i++) |
193 |
+ |
pp->trimat[i] = mgetint(2); |
194 |
+ |
} else { |
195 |
+ |
pp->solemat = mgetint(2); |
196 |
+ |
pp->trimat = NULL; |
197 |
+ |
} |
198 |
|
/* joiner triangles */ |
199 |
|
pp->nj1tris = mgetint(2); |
200 |
|
if (pp->nj1tris < 0 || pp->nj1tris > 512) |
208 |
|
pp->j1tri[i].v1j = mgetint(4); |
209 |
|
pp->j1tri[i].v2 = mgetint(1); |
210 |
|
pp->j1tri[i].v3 = mgetint(1); |
211 |
+ |
pp->j1tri[i].mat = mgetint(2); |
212 |
|
} |
213 |
|
} else |
214 |
|
pp->j1tri = NULL; |
225 |
|
pp->j2tri[i].v1j = mgetint(4); |
226 |
|
pp->j2tri[i].v2j = mgetint(4); |
227 |
|
pp->j2tri[i].v3 = mgetint(1); |
228 |
+ |
pp->j2tri[i].mat = mgetint(2); |
229 |
|
} |
230 |
|
} else |
231 |
|
pp->j2tri = NULL; |
241 |
|
char *path; |
242 |
|
int flags; |
243 |
|
{ |
244 |
+ |
char *err; |
245 |
|
char sbuf[64]; |
246 |
|
int i; |
247 |
|
/* check what's loaded */ |
254 |
|
sprintf(errmsg, "cannot open mesh file \"%s\"", path); |
255 |
|
error(SYSTEM, errmsg); |
256 |
|
} |
257 |
< |
#ifdef MSDOS |
234 |
< |
setmode(fileno(meshfp), O_BINARY); |
235 |
< |
#endif |
257 |
> |
SET_FILE_BINARY(meshfp); |
258 |
|
/* read header */ |
259 |
|
checkheader(meshfp, MESHFMT, flags&IO_INFO ? stdout : (FILE *)NULL); |
260 |
|
/* read format number */ |
281 |
|
mp->mcube.cutree = gettree(); |
282 |
|
else if (flags & IO_SCENE) |
283 |
|
skiptree(); |
284 |
< |
/* read the patches */ |
284 |
> |
/* read materials and patches */ |
285 |
|
if (flags & IO_SCENE) { |
286 |
+ |
mp->mat0 = nobjects; |
287 |
+ |
readscene(meshfp, objsize); |
288 |
+ |
mp->nmats = nobjects - mp->mat0; |
289 |
|
mp->npatches = mgetint(4); |
290 |
|
mp->patch = (MESHPATCH *)calloc(mp->npatches, |
291 |
|
sizeof(MESHPATCH)); |
297 |
|
/* clean up */ |
298 |
|
fclose(meshfp); |
299 |
|
mp->ldflags |= flags; |
300 |
+ |
/* verify data */ |
301 |
+ |
if ((err = checkmesh(mp)) != NULL) |
302 |
+ |
mesherror(USER, err); |
303 |
|
} |