From 9a75cd09406abe15b0a93faa062be919b91529c3 Mon Sep 17 00:00:00 2001 From: _ <_> Date: Tue, 17 Apr 2018 18:32:23 -0700 Subject: [PATCH] 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; } +