diff --git a/demo/extractmesh.c b/demo/extractmesh.c index 299ddeb..8468a42 100644 --- a/demo/extractmesh.c +++ b/demo/extractmesh.c @@ -18,24 +18,42 @@ void writemesh(const struct MeshHeader *mh, // 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); + + + + // batch data the same treatment. + struct BatchData * newBatches = malloc(newmh.numBatch * sizeof(struct BatchData)); + uint64_t previousBatch = 0; + for (int i = 0; i < newmh.numBatch; i++) { + newBatches[i] = (m -> b) -> bd[i]; + unsigned int nVertices = m -> b -> bd[i].numVertex; + if (previousBatch == 0) { + newBatches[i].offsetPositions = newmh.offsetBatches + ( newmh.numBatch * sizeof(struct BatchData)); + } + else { + newBatches[i].offsetPositions = previousBatch; + } + newBatches[i].offsetNormals = newBatches[i].offsetPositions + (sizeof(struct Coordinate) * nVertices); + newBatches[i].offsetUVs = newBatches[i].offsetNormals + (sizeof(struct Coordinate) * nVertices); + newBatches[i].offsetBoneIndexes = newBatches[i].offsetUVs + (sizeof(struct UVs) * nVertices); + newBatches[i].offsetBoneWeights = newBatches[i].offsetBoneIndexes + (sizeof(struct BoneIndexes) * nVertices); + append(fn, (char*)(&newBatches[i]), sizeof(struct BatchData)); + previousBatch = newBatches[i].offsetBoneWeights + (sizeof(struct BoneWeights) * nVertices);; + } + + for (int i = 0; i < newmh.numBatch; i++) { + unsigned int nVertices = m -> b -> bd[i].numVertex; + append(fn, (char*)(m -> b[i].vd.positions), sizeof(struct Coordinate) * nVertices); + DEVIL1GEO.printcoordinate(m -> b -> vd.positions, 3); + append(fn, (char*)(m -> b[i].vd.normals), sizeof(struct Coordinate) * nVertices); + append(fn, (char*)(m -> b[i].vd.u), sizeof(struct UVs) * nVertices); + append(fn, (char*)(m -> b[i].vd.bi), sizeof(struct BoneIndexes) * nVertices); + append(fn, (char*)(m -> b[i].vd.bw), sizeof(struct BoneWeights) * nVertices); + } + + free(newBatches); free(fn); }