lib3ddevil1/demo/extractmesh.c

75 lines
2.8 KiB
C
Raw Normal View History

2018-04-18 12:17:08 +05:30
#include "common.h"
#include "devil1geo.h"
2018-04-18 12:17:08 +05:30
void writemesh(const struct MeshHeader *mh,
const struct Mesh *m,
const char *filename,
unsigned int i) {
2018-04-18 14:42:06 +05:30
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;
2018-04-18 14:42:06 +05:30
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));
2018-04-18 14:42:06 +05:30
append(fn, (char*)(m -> b -> vd.positions), sizeof(struct Coordinate) * vertices);
DEVIL1GEO.printcoordinate(m -> b -> vd.positions, 3);
2018-04-18 14:42:06 +05:30
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);
}
2018-04-18 12:17:08 +05:30
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);
2018-04-18 14:42:06 +05:30
writemesh(mh, &m, filename, i);
2018-04-18 12:17:08 +05:30
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;
}