From 4df1024a55f42d633bbcc8488dd085d0853883d2 Mon Sep 17 00:00:00 2001 From: _ <_> Date: Fri, 13 Apr 2018 14:56:14 -0700 Subject: [PATCH] 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 }