From 240864050eedd0c55ab4c93e543638a2e8857304 Mon Sep 17 00:00:00 2001 From: _ <_> Date: Wed, 18 Apr 2018 02:12:06 -0700 Subject: [PATCH 1/5] Wrote 'dump' of batch data --- demo/common.h | 18 ++++++++++++++++-- demo/extractmesh.c | 15 +++++++++++++++ src/devil1geo.c | 1 + 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/demo/common.h b/demo/common.h index 9d3d888..37779d4 100644 --- a/demo/common.h +++ b/demo/common.h @@ -44,10 +44,24 @@ void write(const char *filename, written = fwrite(t, sizeof(unsigned char), size, out); fclose(out); if (written == 0) { - perror("texture write error"); + perror("write error"); } } } - +void append(const char *filename, const char *t, unsigned size) { + if (filename == NULL) { + return; + } + unsigned int written = 0; + FILE *out = fopen(filename, "ab"); + if (out != NULL) { + written = fwrite(t, sizeof(unsigned char), size, out); + fclose(out); + if (written == 0) { + perror("write error"); + } + printf("wrote %d\n", written); + } +} diff --git a/demo/extractmesh.c b/demo/extractmesh.c index 2ce3c6e..6b6a983 100644 --- a/demo/extractmesh.c +++ b/demo/extractmesh.c @@ -1,6 +1,20 @@ #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) { @@ -18,6 +32,7 @@ void extractmeshes(const char *filedata, 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 diff --git a/src/devil1geo.c b/src/devil1geo.c index bf3cfb2..2882bd2 100644 --- a/src/devil1geo.c +++ b/src/devil1geo.c @@ -121,3 +121,4 @@ static bool getmesh(struct Mesh *m, return done; } + From ceb6409abdc803c7a5a2983d1e2eda242a78a4af Mon Sep 17 00:00:00 2001 From: _ <_> Date: Wed, 18 Apr 2018 03:06:48 -0700 Subject: [PATCH 2/5] Updated offset related attributes before file write. --- demo/common.h | 2 +- demo/extractmesh.c | 33 +++++++++++++++++++++++++++------ src/devil1geo.c | 2 +- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/demo/common.h b/demo/common.h index 37779d4..44f2f10 100644 --- a/demo/common.h +++ b/demo/common.h @@ -62,6 +62,6 @@ void append(const char *filename, const char *t, unsigned size) { if (written == 0) { perror("write error"); } - printf("wrote %d\n", written); + printf("wrote %x\n", written); } } diff --git a/demo/extractmesh.c b/demo/extractmesh.c index 6b6a983..299ddeb 100644 --- a/demo/extractmesh.c +++ b/demo/extractmesh.c @@ -1,13 +1,37 @@ #include "common.h" #include "devil1geo.h" -void writemesh(struct MeshHeader *mh, struct Mesh *m, const char *filename, unsigned int i) { +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; - write(fn, (char*)mh, sizeof(struct MeshHeader)); - append(fn, (char*)(m -> b -> bd), sizeof(struct BatchData)); + 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); @@ -31,7 +55,6 @@ void extractmeshes(const char *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); } @@ -43,8 +66,6 @@ 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; diff --git a/src/devil1geo.c b/src/devil1geo.c index 2882bd2..7bd72c7 100644 --- a/src/devil1geo.c +++ b/src/devil1geo.c @@ -113,7 +113,7 @@ static bool getmesh(struct Mesh *m, for (j = 0; j < mh -> numBatch; j++) { unsigned int offset = mh->offsetBatches + j * sizeof(struct BatchData); getmeshbatch(&b, offset, filedata); - printmeshbatch(&b); +// printmeshbatch(&b); m -> b[j] = b; } done = true; From 2257f7f034af4203b28b90bc306c9d9adecc80a9 Mon Sep 17 00:00:00 2001 From: surkeh Date: Thu, 19 Apr 2018 04:24:17 -0700 Subject: [PATCH 3/5] Fixed geometry offset and handled multiple batch meshes --- demo/extractmesh.c | 47 ++++++++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/demo/extractmesh.c b/demo/extractmesh.c index 299ddeb..d34a8a6 100644 --- a/demo/extractmesh.c +++ b/demo/extractmesh.c @@ -18,24 +18,39 @@ 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); + + struct BatchData * newBatches = malloc(newmh.numBatch * sizeof(struct BatchData)); + // batch data the same treatment. + 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(fn); } From 3d20691b8156007a5f5fc94736edabb64ec31ada Mon Sep 17 00:00:00 2001 From: surkeh Date: Thu, 19 Apr 2018 04:28:20 -0700 Subject: [PATCH 4/5] Added missing free for newBatches malloc --- demo/extractmesh.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/demo/extractmesh.c b/demo/extractmesh.c index d34a8a6..8468a42 100644 --- a/demo/extractmesh.c +++ b/demo/extractmesh.c @@ -21,8 +21,10 @@ void writemesh(const struct MeshHeader *mh, write(fn, (char*)(&h), sizeof(struct Header)); append(fn, (char*)(&newmh), sizeof(struct MeshHeader)); - struct BatchData * newBatches = malloc(newmh.numBatch * sizeof(struct BatchData)); + + // 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]; @@ -51,6 +53,7 @@ void writemesh(const struct MeshHeader *mh, append(fn, (char*)(m -> b[i].vd.bw), sizeof(struct BoneWeights) * nVertices); } + free(newBatches); free(fn); } From e1d2dff25e03a651d4c6fb4539ab20a41ea767dd Mon Sep 17 00:00:00 2001 From: _ <_> Date: Thu, 19 Apr 2018 04:37:59 -0700 Subject: [PATCH 5/5] Removed printouts --- demo/common.h | 1 - demo/extractmesh.c | 1 - 2 files changed, 2 deletions(-) diff --git a/demo/common.h b/demo/common.h index 44f2f10..0a2f2d8 100644 --- a/demo/common.h +++ b/demo/common.h @@ -62,6 +62,5 @@ void append(const char *filename, const char *t, unsigned size) { if (written == 0) { perror("write error"); } - printf("wrote %x\n", written); } } diff --git a/demo/extractmesh.c b/demo/extractmesh.c index 8468a42..62b89de 100644 --- a/demo/extractmesh.c +++ b/demo/extractmesh.c @@ -46,7 +46,6 @@ void writemesh(const struct MeshHeader *mh, 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);