lib3ddevil1/demo/extractmesh.c
2018-04-18 02:12:06 -07:00

54 lines
1.8 KiB
C

#include "common.h"
#include "devil1geo.h"
void writemesh(struct MeshHeader *mh, struct Mesh *m, const char *filename, unsigned int i) {
char *fn = (char*)malloc(strlen(filename) + 3 + 4);
sprintf(fn, "%s_%d.msh", filename, i);
unsigned int vertices = m -> b -> bd -> numVertex;
write(fn, (char*)mh, sizeof(struct MeshHeader));
append(fn, (char*)(m -> b -> bd), sizeof(struct BatchData));
append(fn, (char*)(m -> b -> vd.positions), sizeof(struct Coordinate) * vertices);
append(fn, (char*)(m -> b -> vd.normals), sizeof(struct Coordinate) * vertices);
append(fn, (char*)(m -> b -> vd.u), sizeof(struct UVs) * vertices);
append(fn, (char*)(m -> b -> vd.bi), sizeof(struct BoneIndexes) * vertices);
append(fn, (char*)(m -> b -> vd.bw), sizeof(struct BoneWeights) * vertices);
free(fn);
}
void extractmeshes(const char *filedata,
unsigned int filesize,
const char *filename) {
if (filedata == NULL || filesize <= 0) {
return;
}
struct Header *h = (struct Header*)filedata;
struct MeshHeader *mh = NULL;
struct Mesh m;
m.b = NULL;
unsigned int i;
for (i = 0; i < h -> numMesh; i++) {
DEVIL1GEO.getmeshheader(&mh, i, filedata);
m.b = (struct Batch*)malloc(sizeof(struct Batch) * (mh -> numBatch));
if (m.b != NULL) {
DEVIL1GEO.getmesh(&m, i, filedata);
// do something with mesh e.g write to file.
writemesh(mh, &m, filename, i);
free(m.b);
}
} // end for
}
int main(int argc, char ** argv) {
char *f = argv[1];
unsigned int bufsize = 0;
char *buffer = loadfile(f, &bufsize);
// unpackpld(buffer, bufsize, f);
// exporttextures(buffer, bufsize, f);
extractmeshes(buffer, bufsize, f);
free(buffer);
return 0;
}