#include "common.h" #include "devil1geo.h" void writemesh(const struct MeshHeader *mh, const struct Mesh *m, const char *filename, unsigned int i) { char *fn = (char*)malloc(strlen(filename) + 3 + 4); sprintf(fn, "%s_%d.msh", filename, i); struct Header h = { 1, 0, 0, 0, 0xCCCCCCCC, 0 }; // mesh header offset batches need to be changed for compability with parsing. struct MeshHeader newmh = *(mh); newmh.offsetBatches = sizeof(struct Header) + sizeof(struct MeshHeader); // batch data the same treatment. struct BatchData newbatch = *(m -> b) -> bd; unsigned int vertices = m -> b -> bd -> numVertex; newbatch.offsetPositions = sizeof(struct MeshHeader) + sizeof(struct BatchData); newbatch.offsetNormals = newbatch.offsetPositions + (sizeof(struct Coordinate) * vertices); newbatch.offsetUVs = newbatch.offsetNormals + (sizeof(struct Coordinate) * vertices); newbatch.offsetBoneIndexes = newbatch.offsetUVs + (sizeof(struct UVs) * vertices); newbatch.offsetBoneWeights = newbatch.offsetBoneIndexes + (sizeof(struct BoneIndexes) * vertices); // write to file. write(fn, (char*)(&h), sizeof(struct Header)); append(fn, (char*)(&newmh), sizeof(struct MeshHeader)); append(fn, (char*)(&newbatch), sizeof(struct BatchData)); append(fn, (char*)(m -> b -> vd.positions), sizeof(struct Coordinate) * vertices); DEVIL1GEO.printcoordinate(m -> b -> vd.positions, 3); 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); 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); extractmeshes(buffer, bufsize, f); free(buffer); return 0; }