From 0e5c96b83b833babce63d3a102c588fe62092591 Mon Sep 17 00:00:00 2001 From: _ <_> Date: Sat, 7 Apr 2018 03:24:07 -0700 Subject: [PATCH 1/8] Added function to get mesh headers --- Makefile | 5 ++++- include/devil1geo.h | 12 ++++++++++-- test/main.c | 24 +++++++++++++++++++++++- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 23ac41f..f945ff5 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ CFLAGS= -I"include" all: main -main: devil1pld.o devil1tex.o +main: devil1pld.o devil1tex.o src/devil1geo.o $(CC) $^ test/main.c $(CFLAGS) -o $(EX) devil1pld.o: src/devil1pld.c @@ -13,5 +13,8 @@ devil1pld.o: src/devil1pld.c devil1tex.o: src/devil1tex.c $(CC) -c $^ $(CFLAGS) +devil1geo.o: src/devil1geo.c + $(CC) -c $^ $(CFLAGS) + clean: rm *.o $(EX) diff --git a/include/devil1geo.h b/include/devil1geo.h index 8d07d1c..9f62e84 100644 --- a/include/devil1geo.h +++ b/include/devil1geo.h @@ -2,7 +2,10 @@ #ifndef DEVIL1GEO_H #define DEVIL1GEO_H #include -#pragma pack(1) +#include + + +#pragma pack(push, 1) struct Header { unsigned char numMesh; unsigned char unknownNumberB; @@ -12,7 +15,7 @@ struct Header { uint64_t unknownOffset; }; -struct MeshHeaders { +struct MeshHeader { int16_t numBatch; int16_t numVertex; uint32_t u; // @@ -39,6 +42,7 @@ struct BoneIndexes { struct BoneWeights { uint16_t weights; // }; +#pragma pack(pop) // This is where most of the parsing will be. // this struct is in-order of what the file format will have. @@ -65,5 +69,9 @@ struct Mesh { struct Batch b; }; +void printgheader(struct Header*); + +bool getmeshheaders(struct MeshHeader **, const char *, unsigned int); + #endif diff --git a/test/main.c b/test/main.c index 62ca3ac..6a89535 100644 --- a/test/main.c +++ b/test/main.c @@ -5,6 +5,7 @@ #include "devil1pld.h" #include "devil1tex.h" +#include "devil1geo.h" char *loadfile(const char *fname, unsigned int *s) { FILE *f = fopen(fname, "rb"); @@ -104,12 +105,33 @@ void exporttextures(const char *filedata, return; } +void extractmeshes(const char* filedata, + unsigned int filesize, + const char *filename) { + if (filedata == NULL || filesize <= 0) { + return; + } + struct Header *h = NULL; + h = (struct Header*)filedata; + printf("pointer %x\n", h); + printgheader(h); + struct MeshHeader **mh = NULL; + mh = (struct MeshHeader**) + malloc(sizeof(struct MeshHeader*) * h -> numMesh); + getmeshheaders(mh, filedata, filesize); + unsigned int i; + for (i = 0; i < h -> numMesh; i++) { + printf("returning %x \n", mh[i]); + } +} + int main(int argc, char ** argv) { char *f = argv[1]; unsigned int bufsize = 0; char *buffer = loadfile(f, &bufsize); - unpackpld(buffer, bufsize, f); +// unpackpld(buffer, bufsize, f); // exporttextures(buffer, bufsize, f); + extractmeshes(buffer, bufsize, f); free(buffer); return 0; } From 8016deae86509f854e4f689faea830d5ad4bf5d8 Mon Sep 17 00:00:00 2001 From: <> Date: Sat, 7 Apr 2018 04:39:16 -0700 Subject: [PATCH 2/8] Moved out for loop & fixed getting mesh headers --- include/devil1geo.h | 4 +++- src/devil1geo.c | 42 ++++++++++++++++++++++++++++++++++++++++++ test/main.c | 16 +++++++--------- 3 files changed, 52 insertions(+), 10 deletions(-) create mode 100644 src/devil1geo.c diff --git a/include/devil1geo.h b/include/devil1geo.h index 9f62e84..87bf242 100644 --- a/include/devil1geo.h +++ b/include/devil1geo.h @@ -71,7 +71,9 @@ struct Mesh { void printgheader(struct Header*); -bool getmeshheaders(struct MeshHeader **, const char *, unsigned int); +void printmeshheader(struct MeshHeader*); + +bool getmeshheader(struct MeshHeader**, unsigned int i, const char * const); #endif diff --git a/src/devil1geo.c b/src/devil1geo.c new file mode 100644 index 0000000..0cc7add --- /dev/null +++ b/src/devil1geo.c @@ -0,0 +1,42 @@ +#include "devil1geo.h" +#include + +void printgheader(struct Header *gh) { + if (gh != NULL) { + printf("pointer %x\n", gh); + printf("number of meshes %x\n", gh -> numMesh); + printf("unknown number B %x\n", gh -> unknownNumberB); + printf("unknown number C %x\n", gh -> unknownNumberC); + printf("unknown number D %x\n", gh -> unknownNumberD); + printf("padding %x\n", gh -> padding); + printf("unknown offset %x\n", gh -> unknownOffset); + } +} + +void printmeshheader(struct MeshHeader *mh) { + if (mh == NULL) { + return; + } + printf("number of batches %x\n", mh -> numBatch); + printf("number of vertices %x\n", mh -> numVertex); + printf("unknown %x\n", mh -> u); + printf("batch offset %x\n", mh -> offsetBatches); + printf("flags %x\n\n", mh -> flags); +} + +bool getmeshheader(struct MeshHeader **hs, + unsigned int i, + const char * const filedata) { + bool done = false; + if (hs == NULL || filedata == NULL) { + return done; + } + struct Header *h = (struct Header*)filedata; + if (h -> numMesh < i) { + return done; + } + unsigned int pos = sizeof(struct MeshHeader) * i + sizeof(struct Header); + (*hs) = (struct MeshHeader*)(filedata + pos); + done = true; + return done; +} diff --git a/test/main.c b/test/main.c index 6a89535..e008c14 100644 --- a/test/main.c +++ b/test/main.c @@ -111,17 +111,15 @@ void extractmeshes(const char* filedata, if (filedata == NULL || filesize <= 0) { return; } - struct Header *h = NULL; - h = (struct Header*)filedata; - printf("pointer %x\n", h); - printgheader(h); - struct MeshHeader **mh = NULL; - mh = (struct MeshHeader**) - malloc(sizeof(struct MeshHeader*) * h -> numMesh); - getmeshheaders(mh, filedata, filesize); + struct Header *h = (struct Header*)filedata; + struct MeshHeader *mh = NULL; unsigned int i; + bool ok; for (i = 0; i < h -> numMesh; i++) { - printf("returning %x \n", mh[i]); + ok = getmeshheader(&mh, i, filedata); + if (ok) { + printmeshheader(mh); + } } } From fb962d2ffefe8365d0fd4f0631b2c7017e77d544 Mon Sep 17 00:00:00 2001 From: _ <_> Date: Sat, 7 Apr 2018 19:37:43 -0700 Subject: [PATCH 3/8] Implemented correct initialization of batch and positions array --- include/devil1geo.h | 11 ++++++++++- src/devil1geo.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ test/main.c | 9 ++++++--- 3 files changed, 61 insertions(+), 4 deletions(-) diff --git a/include/devil1geo.h b/include/devil1geo.h index 87bf242..f40f967 100644 --- a/include/devil1geo.h +++ b/include/devil1geo.h @@ -56,7 +56,6 @@ struct Batch { uint64_t offsetBoneIndexes; // uint64_t offsetBoneWeights; // uint64_t offsets[1]; // - int64_t pos; // set while parsing batch from ftell(); // following structs should in an array of size numVertex struct Positions *p; struct Normals *n; @@ -73,7 +72,17 @@ void printgheader(struct Header*); void printmeshheader(struct MeshHeader*); +void printmeshbatch(struct Batch*); + +void printpositions(struct Positions*, unsigned int); + +// ** = 'pass by reference' of a pointer to struct bool getmeshheader(struct MeshHeader**, unsigned int i, const char * const); +// ** = 'pass by reference' of a pointer to struct +bool getmeshbatch(struct Batch**, + struct MeshHeader*, + const char * const); + #endif diff --git a/src/devil1geo.c b/src/devil1geo.c index 0cc7add..0ad4286 100644 --- a/src/devil1geo.c +++ b/src/devil1geo.c @@ -24,6 +24,31 @@ void printmeshheader(struct MeshHeader *mh) { printf("flags %x\n\n", mh -> flags); } +void printmeshbatch(struct Batch *b) { + if (b == NULL) { + return; + } + printf("number of vertices %x\n", b -> numVertex); + printf("unknown byte %x\n", b -> uB); + printf("padding %x\n", b -> padding); + printf("offsetPositions %x\n", b -> offsetPositions); + printf("offsetNormals %x\n", b -> offsetNormals); + printf("offsetUVs %x\n", b -> offsetUVs); + printf("offsetBoneIndexes %x\n", b -> offsetBoneIndexes); + printf("offsetBoneWeights %x\n", b -> offsetBoneWeights); + printf("offsets %x\n\n", b -> offsets[0]); +} + +void printpositions(struct Positions *p, unsigned int count) { + if (p == NULL) { + return; + } + unsigned int i; + for (i = 0; i < count; i++) { + printf("(%f, %f, %f)\n", (p[i]).x, (p[i]).y, (p[i]).z); + } +} + bool getmeshheader(struct MeshHeader **hs, unsigned int i, const char * const filedata) { @@ -40,3 +65,23 @@ bool getmeshheader(struct MeshHeader **hs, done = true; return done; } + +bool getmeshbatch(struct Batch **b, + struct MeshHeader *h, + const char * const filedata) { + bool done = false; + if (b == NULL || h == NULL || filedata == NULL) { + return done; + } + unsigned int offset = h -> offsetBatches; + *b = (struct Batch*)(filedata + offset); + (*b) -> p = (struct Positions*)(filedata + ((*b) -> offsetPositions)); + printf("p = %x\n", (*b) -> offsetPositions); + printpositions((*b) -> p, 3); + (*b) -> n = NULL; + (*b) -> u = NULL; + (*b) -> bi = NULL; + (*b) -> bw = NULL; + done = true; + return done; +} diff --git a/test/main.c b/test/main.c index e008c14..0e80adf 100644 --- a/test/main.c +++ b/test/main.c @@ -105,7 +105,7 @@ void exporttextures(const char *filedata, return; } -void extractmeshes(const char* filedata, +void extractmeshes(const char *filedata, unsigned int filesize, const char *filename) { if (filedata == NULL || filesize <= 0) { @@ -113,12 +113,15 @@ void extractmeshes(const char* filedata, } struct Header *h = (struct Header*)filedata; struct MeshHeader *mh = NULL; + struct Batch *b = NULL; unsigned int i; bool ok; - for (i = 0; i < h -> numMesh; i++) { + //h -> numMesh + for (i = 0; i < 3; i++) { ok = getmeshheader(&mh, i, filedata); if (ok) { - printmeshheader(mh); + getmeshbatch(&b, mh, filedata); + printmeshbatch(b); } } } From ad357afc36077668aa55488a84753e6424c825cd Mon Sep 17 00:00:00 2001 From: _ <_> Date: Sat, 7 Apr 2018 22:09:29 -0700 Subject: [PATCH 4/8] Initialized pointers for arrays in struct Batch --- include/devil1geo.h | 2 ++ src/devil1geo.c | 22 +++++++++++++++------- test/main.c | 2 +- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/include/devil1geo.h b/include/devil1geo.h index f40f967..8bfb750 100644 --- a/include/devil1geo.h +++ b/include/devil1geo.h @@ -76,6 +76,8 @@ void printmeshbatch(struct Batch*); void printpositions(struct Positions*, unsigned int); +void printnormals(struct Normals*, unsigned int); + // ** = 'pass by reference' of a pointer to struct bool getmeshheader(struct MeshHeader**, unsigned int i, const char * const); diff --git a/src/devil1geo.c b/src/devil1geo.c index 0ad4286..bfa9ce3 100644 --- a/src/devil1geo.c +++ b/src/devil1geo.c @@ -49,6 +49,16 @@ void printpositions(struct Positions *p, unsigned int count) { } } +void printnormals(struct Normals *n, unsigned int count) { + if (n == NULL) { + return; + } + unsigned int i; + for (i = 0; i < count; i++) { + printf("(%f, %f, %f)\n", (n[i]).x, (n[i]).y, (n[i]).z); + } +} + bool getmeshheader(struct MeshHeader **hs, unsigned int i, const char * const filedata) { @@ -75,13 +85,11 @@ bool getmeshbatch(struct Batch **b, } unsigned int offset = h -> offsetBatches; *b = (struct Batch*)(filedata + offset); - (*b) -> p = (struct Positions*)(filedata + ((*b) -> offsetPositions)); - printf("p = %x\n", (*b) -> offsetPositions); - printpositions((*b) -> p, 3); - (*b) -> n = NULL; - (*b) -> u = NULL; - (*b) -> bi = NULL; - (*b) -> bw = NULL; + (*b) -> p = (struct Positions*)(filedata + ((*b) -> offsetPositions)); + (*b) -> n = (struct Normals*)(filedata + ((*b) -> offsetNormals)); + (*b) -> u = (struct UVs*)(filedata + ((*b) -> offsetUVs)); + (*b) -> bi = (struct BoneIndexes*)(filedata + ((*b) -> offsetBoneIndexes)); + (*b) -> bw = (struct BoneWeights*)(filedata + ((*b) -> offsetBoneWeights)); done = true; return done; } diff --git a/test/main.c b/test/main.c index 0e80adf..9b28057 100644 --- a/test/main.c +++ b/test/main.c @@ -117,7 +117,7 @@ void extractmeshes(const char *filedata, unsigned int i; bool ok; //h -> numMesh - for (i = 0; i < 3; i++) { + for (i = 1; i < 2; i++) { ok = getmeshheader(&mh, i, filedata); if (ok) { getmeshbatch(&b, mh, filedata); From d38926b86ab4ba6dbf64803f41f2c286c23bfbaf Mon Sep 17 00:00:00 2001 From: _ <_> Date: Mon, 9 Apr 2018 20:51:37 -0700 Subject: [PATCH 5/8] Added for loop to accomodate cases with multiple batches --- include/devil1geo.h | 4 +++- test/main.c | 13 ++++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/include/devil1geo.h b/include/devil1geo.h index 8bfb750..ad5def3 100644 --- a/include/devil1geo.h +++ b/include/devil1geo.h @@ -42,7 +42,7 @@ struct BoneIndexes { struct BoneWeights { uint16_t weights; // }; -#pragma pack(pop) + // This is where most of the parsing will be. // this struct is in-order of what the file format will have. @@ -68,6 +68,8 @@ struct Mesh { struct Batch b; }; +#pragma pack(pop) + void printgheader(struct Header*); void printmeshheader(struct MeshHeader*); diff --git a/test/main.c b/test/main.c index 9b28057..d2a5fcf 100644 --- a/test/main.c +++ b/test/main.c @@ -115,15 +115,18 @@ void extractmeshes(const char *filedata, struct MeshHeader *mh = NULL; struct Batch *b = NULL; unsigned int i; + unsigned int j; bool ok; //h -> numMesh - for (i = 1; i < 2; i++) { + for (i = 0; i < 2; i++) { ok = getmeshheader(&mh, i, filedata); if (ok) { - getmeshbatch(&b, mh, filedata); - printmeshbatch(b); - } - } + for (j = 0; j < mh -> numBatch; j++) { + getmeshbatch(&b, mh, filedata); + printmeshbatch(b); + } // end for + } // end if + } // end for } int main(int argc, char ** argv) { From 4df1024a55f42d633bbcc8488dd085d0853883d2 Mon Sep 17 00:00:00 2001 From: _ <_> Date: Fri, 13 Apr 2018 14:56:14 -0700 Subject: [PATCH 6/8] Fixed iterating bug with initializing mesh batches --- include/devil1geo.h | 34 +++++++++++++++--------------- src/devil1geo.c | 50 +++++++++++++++++++++------------------------ test/main.c | 10 ++++----- 3 files changed, 46 insertions(+), 48 deletions(-) diff --git a/include/devil1geo.h b/include/devil1geo.h index ad5def3..e68466f 100644 --- a/include/devil1geo.h +++ b/include/devil1geo.h @@ -23,11 +23,7 @@ struct MeshHeader { uint64_t flags; }; // put these in an array of size: [header.numMesh] -struct Positions { - float x, y, z; -}; - -struct Normals { +struct Coordinate { float x, y, z; }; @@ -43,10 +39,7 @@ struct BoneWeights { uint16_t weights; // }; - -// This is where most of the parsing will be. -// this struct is in-order of what the file format will have. -struct Batch { +struct BatchData { int16_t numVertex; int16_t uB; uint32_t padding; // @@ -56,16 +49,27 @@ struct Batch { uint64_t offsetBoneIndexes; // uint64_t offsetBoneWeights; // uint64_t offsets[1]; // +}; + +struct VertexData { // following structs should in an array of size numVertex - struct Positions *p; - struct Normals *n; + struct Coordinate *positions; + struct Coordinate *normals; struct UVs *u; struct BoneIndexes *bi; struct BoneWeights *bw; }; +// This is where most of the parsing will be. +// this struct is in-order of what the file format will have. +struct Batch { + struct BatchData *bd; + struct VertexData vd; +}; + struct Mesh { - struct Batch b; + // may contain multiple batches + struct Batch *b; }; #pragma pack(pop) @@ -76,15 +80,13 @@ void printmeshheader(struct MeshHeader*); void printmeshbatch(struct Batch*); -void printpositions(struct Positions*, unsigned int); - -void printnormals(struct Normals*, unsigned int); +void printcoordinate(struct Coordinate*, unsigned int); // ** = 'pass by reference' of a pointer to struct bool getmeshheader(struct MeshHeader**, unsigned int i, const char * const); // ** = 'pass by reference' of a pointer to struct -bool getmeshbatch(struct Batch**, +bool getmeshbatch(struct Batch*, struct MeshHeader*, const char * const); diff --git a/src/devil1geo.c b/src/devil1geo.c index bfa9ce3..3edad51 100644 --- a/src/devil1geo.c +++ b/src/devil1geo.c @@ -28,18 +28,20 @@ void printmeshbatch(struct Batch *b) { if (b == NULL) { return; } - printf("number of vertices %x\n", b -> numVertex); - printf("unknown byte %x\n", b -> uB); - printf("padding %x\n", b -> padding); - printf("offsetPositions %x\n", b -> offsetPositions); - printf("offsetNormals %x\n", b -> offsetNormals); - printf("offsetUVs %x\n", b -> offsetUVs); - printf("offsetBoneIndexes %x\n", b -> offsetBoneIndexes); - printf("offsetBoneWeights %x\n", b -> offsetBoneWeights); - printf("offsets %x\n\n", b -> offsets[0]); + struct BatchData *bd = b -> bd; + printf("number of vertices %x\n", bd -> numVertex); + printf("unknown byte %x\n", bd -> uB); + printf("padding %x\n", bd -> padding); + printf("offsetPositions %x\n", bd -> offsetPositions); + printf("offsetNormals %x\n", bd -> offsetNormals); + printf("offsetUVs %x\n", bd -> offsetUVs); + printf("offsetBoneIndexes %x\n", bd -> offsetBoneIndexes); + printf("offsetBoneWeights %x\n", bd -> offsetBoneWeights); + printf("offsets %x\n\n", bd -> offsets[0]); + printcoordinate(b -> vd.positions, 3); } -void printpositions(struct Positions *p, unsigned int count) { +void printcoordinate(struct Coordinate *p, unsigned int count) { if (p == NULL) { return; } @@ -49,16 +51,6 @@ void printpositions(struct Positions *p, unsigned int count) { } } -void printnormals(struct Normals *n, unsigned int count) { - if (n == NULL) { - return; - } - unsigned int i; - for (i = 0; i < count; i++) { - printf("(%f, %f, %f)\n", (n[i]).x, (n[i]).y, (n[i]).z); - } -} - bool getmeshheader(struct MeshHeader **hs, unsigned int i, const char * const filedata) { @@ -76,7 +68,7 @@ bool getmeshheader(struct MeshHeader **hs, return done; } -bool getmeshbatch(struct Batch **b, +bool getmeshbatch(struct Batch *b, struct MeshHeader *h, const char * const filedata) { bool done = false; @@ -84,12 +76,16 @@ bool getmeshbatch(struct Batch **b, return done; } unsigned int offset = h -> offsetBatches; - *b = (struct Batch*)(filedata + offset); - (*b) -> p = (struct Positions*)(filedata + ((*b) -> offsetPositions)); - (*b) -> n = (struct Normals*)(filedata + ((*b) -> offsetNormals)); - (*b) -> u = (struct UVs*)(filedata + ((*b) -> offsetUVs)); - (*b) -> bi = (struct BoneIndexes*)(filedata + ((*b) -> offsetBoneIndexes)); - (*b) -> bw = (struct BoneWeights*)(filedata + ((*b) -> offsetBoneWeights)); + struct BatchData *d1 = NULL; + struct VertexData d2; + d1 = (struct BatchData*) (filedata + offset); + d2.positions = (struct Coordinate*) (filedata + (d1 -> offsetPositions)); + d2.normals = (struct Coordinate*) (filedata + (d1 -> offsetNormals)); + d2.u = (struct UVs*) (filedata + (d1 -> offsetUVs)); + d2.bi = (struct BoneIndexes*)(filedata + (d1 -> offsetBoneIndexes)); + d2.bw = (struct BoneWeights*)(filedata + (d1 -> offsetBoneWeights)); + b -> bd = d1; + b -> vd = d2; done = true; return done; } diff --git a/test/main.c b/test/main.c index d2a5fcf..ca53b4c 100644 --- a/test/main.c +++ b/test/main.c @@ -113,18 +113,18 @@ void extractmeshes(const char *filedata, } struct Header *h = (struct Header*)filedata; struct MeshHeader *mh = NULL; - struct Batch *b = NULL; + struct Batch b; unsigned int i; unsigned int j; bool ok; //h -> numMesh - for (i = 0; i < 2; i++) { + for (i = 0; i < h -> numMesh; i++) { ok = getmeshheader(&mh, i, filedata); if (ok) { - for (j = 0; j < mh -> numBatch; j++) { + // for (j = 0; j < mh -> numBatch; j++) { getmeshbatch(&b, mh, filedata); - printmeshbatch(b); - } // end for + printmeshbatch(&b); + // } // end for } // end if } // end for } From ad32d3d10a4a659507748745a42360d34562637f Mon Sep 17 00:00:00 2001 From: surkeh Date: Fri, 13 Apr 2018 17:22:07 -0700 Subject: [PATCH 7/8] Meshes with multiple batches handled --- include/devil1geo.h | 2 +- src/devil1geo.c | 5 ++--- test/main.c | 9 +++++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/include/devil1geo.h b/include/devil1geo.h index e68466f..fbe4db4 100644 --- a/include/devil1geo.h +++ b/include/devil1geo.h @@ -87,7 +87,7 @@ bool getmeshheader(struct MeshHeader**, unsigned int i, const char * const); // ** = 'pass by reference' of a pointer to struct bool getmeshbatch(struct Batch*, - struct MeshHeader*, + unsigned int offset, const char * const); #endif diff --git a/src/devil1geo.c b/src/devil1geo.c index 3edad51..7aa5d7b 100644 --- a/src/devil1geo.c +++ b/src/devil1geo.c @@ -69,13 +69,12 @@ bool getmeshheader(struct MeshHeader **hs, } bool getmeshbatch(struct Batch *b, - struct MeshHeader *h, + unsigned int offset, const char * const filedata) { bool done = false; - if (b == NULL || h == NULL || filedata == NULL) { + if (b == NULL || filedata == NULL) { return done; } - unsigned int offset = h -> offsetBatches; struct BatchData *d1 = NULL; struct VertexData d2; d1 = (struct BatchData*) (filedata + offset); diff --git a/test/main.c b/test/main.c index ca53b4c..94bc145 100644 --- a/test/main.c +++ b/test/main.c @@ -117,14 +117,15 @@ void extractmeshes(const char *filedata, unsigned int i; unsigned int j; bool ok; - //h -> numMesh + //for (i = 0; i < 5; i++) { for (i = 0; i < h -> numMesh; i++) { ok = getmeshheader(&mh, i, filedata); if (ok) { - // for (j = 0; j < mh -> numBatch; j++) { - getmeshbatch(&b, mh, filedata); + for (j = 0; j < mh -> numBatch; j++) { + unsigned int offset = mh->offsetBatches + j * sizeof(struct BatchData); + getmeshbatch(&b, offset, filedata); printmeshbatch(&b); - // } // end for + } // end for } // end if } // end for } From 9a75cd09406abe15b0a93faa062be919b91529c3 Mon Sep 17 00:00:00 2001 From: _ <_> Date: Tue, 17 Apr 2018 18:32:23 -0700 Subject: [PATCH 8/8] Added getmesh function --- include/devil1geo.h | 17 +++++++---------- src/devil1geo.c | 25 +++++++++++++++++++++++++ test/main.c | 22 ++++++++++------------ 3 files changed, 42 insertions(+), 22 deletions(-) diff --git a/include/devil1geo.h b/include/devil1geo.h index fbe4db4..048b5e1 100644 --- a/include/devil1geo.h +++ b/include/devil1geo.h @@ -52,7 +52,7 @@ struct BatchData { }; struct VertexData { - // following structs should in an array of size numVertex + // following structs should in an array of size 'numVertex' struct Coordinate *positions; struct Coordinate *normals; struct UVs *u; @@ -60,15 +60,13 @@ struct VertexData { struct BoneWeights *bw; }; -// This is where most of the parsing will be. -// this struct is in-order of what the file format will have. struct Batch { - struct BatchData *bd; - struct VertexData vd; + struct BatchData *bd; // pointer to region in file data + struct VertexData vd; // collection of pointers to regions in file data }; struct Mesh { - // may contain multiple batches + // array of batches struct Batch *b; }; @@ -85,10 +83,9 @@ void printcoordinate(struct Coordinate*, unsigned int); // ** = 'pass by reference' of a pointer to struct bool getmeshheader(struct MeshHeader**, unsigned int i, const char * const); -// ** = 'pass by reference' of a pointer to struct -bool getmeshbatch(struct Batch*, - unsigned int offset, - const char * const); +bool getmeshbatch(struct Batch*, unsigned int offset, const char * const); + +bool getmesh(struct Mesh*, unsigned int i, const char* filename); #endif diff --git a/src/devil1geo.c b/src/devil1geo.c index 7aa5d7b..401eafe 100644 --- a/src/devil1geo.c +++ b/src/devil1geo.c @@ -88,3 +88,28 @@ bool getmeshbatch(struct Batch *b, done = true; return done; } + +// assume client has allocated memory for mesh +bool getmesh(struct Mesh *m, + unsigned int i, + const char * const filedata) { + bool done = false; + if (m == NULL || filedata == NULL || m -> b == NULL) { + return done; + } + struct MeshHeader *mh = NULL; + bool ok = getmeshheader(&mh, i, filedata); + if (ok) { + unsigned int j; + struct Batch b; + for (j = 0; j < mh -> numBatch; j++) { + unsigned int offset = mh->offsetBatches + j * sizeof(struct BatchData); + getmeshbatch(&b, offset, filedata); + printmeshbatch(&b); + m -> b[j] = b; + } + done = true; + } + return done; +} + diff --git a/test/main.c b/test/main.c index 94bc145..a6673dd 100644 --- a/test/main.c +++ b/test/main.c @@ -113,20 +113,17 @@ void extractmeshes(const char *filedata, } struct Header *h = (struct Header*)filedata; struct MeshHeader *mh = NULL; - struct Batch b; + struct Mesh m; + m.b = NULL; unsigned int i; - unsigned int j; - bool ok; - //for (i = 0; i < 5; i++) { for (i = 0; i < h -> numMesh; i++) { - ok = getmeshheader(&mh, i, filedata); - if (ok) { - for (j = 0; j < mh -> numBatch; j++) { - unsigned int offset = mh->offsetBatches + j * sizeof(struct BatchData); - getmeshbatch(&b, offset, filedata); - printmeshbatch(&b); - } // end for - } // end if + getmeshheader(&mh, i, filedata); + m.b = (struct Batch*)malloc(sizeof(struct Batch) * (mh -> numBatch)); + if (m.b != NULL) { + getmesh(&m, i, filedata); + // do something with mesh e.g write to file. + free(m.b); + } } // end for } @@ -141,3 +138,4 @@ int main(int argc, char ** argv) { return 0; } +