From ef2c341a1a3ecc8b60eca29a91f74907383e82e0 Mon Sep 17 00:00:00 2001 From: Anakin Date: Mon, 28 Nov 2016 14:04:09 +0100 Subject: [PATCH] using c++11 loops, reading bbox for each geom, added some TODO use the bbox information --- MshViewer/Header/Object.h | 8 +- MshViewer/Source/Object.cpp | 142 +++++++++++++------------- MshViewer/Source/OpenGlController.cpp | 1 + MshViewer/main.cpp | 2 +- 4 files changed, 81 insertions(+), 72 deletions(-) diff --git a/MshViewer/Header/Object.h b/MshViewer/Header/Object.h index 7aeca22..dc5fd5a 100644 --- a/MshViewer/Header/Object.h +++ b/MshViewer/Header/Object.h @@ -14,6 +14,12 @@ enum Mtyp { shadowMesh = 6 }; +struct Bbox { + float quaternion[4]; + float center[3]; + float extents[3]; +}; + struct ChunkHeader { char name[5]; std::uint32_t size; @@ -33,6 +39,7 @@ struct Modl { Mtyp type = null; std::int32_t renderFlags = -1; glm::mat4 m4x4Translation = glm::mat4(1.0f); + Bbox boundingBox; std::vector segmLst; }; @@ -49,7 +56,6 @@ private: std::fstream fsMesh; std::vector vTextures; - private: void loadChunks(std::list &destination, std::streampos start, const std::uint32_t end); void analyseMsh2Chunks(std::list &chunkList); diff --git a/MshViewer/Source/Object.cpp b/MshViewer/Source/Object.cpp index c0b7855..eaf3092 100644 --- a/MshViewer/Source/Object.cpp +++ b/MshViewer/Source/Object.cpp @@ -107,28 +107,28 @@ void Object::loadChunks(std::list& destination, std::streampos sta void Object::analyseMsh2Chunks(std::list& chunkList) { - for (std::list::iterator it = chunkList.begin(); it != chunkList.end(); it++) + for (auto& it : chunkList) { - if (!strcmp("MATL", (*it)->name)) + if (!strcmp("MATL", it->name)) { // "useless" information how many MATD follow - fsMesh.seekg((*it)->position); + fsMesh.seekg(it->position); std::uint32_t tempMatdCount; fsMesh.read(reinterpret_cast(&tempMatdCount), sizeof(std::uint32_t)); // get all MATD from MATL list std::list tempMatlChunks; - loadChunks(tempMatlChunks, fsMesh.tellg(), (*it)->size - 4); + loadChunks(tempMatlChunks, fsMesh.tellg(), it->size - 4); // evaluate MATL subchunks - for (std::list::iterator it = tempMatlChunks.begin(); it != tempMatlChunks.end(); it++) + for (auto& it : tempMatlChunks) { // This shouldn't be anything else than MATD - if (!strcmp("MATD", (*it)->name)) + if (!strcmp("MATD", it->name)) { // get all subchunks from MATD std::list tempMatdChunks; - loadChunks(tempMatdChunks, (*it)->position, (*it)->size); + loadChunks(tempMatdChunks, it->position, it->size); vTextures.push_back(""); @@ -156,13 +156,13 @@ void Object::analyseMsh2Chunks(std::list& chunkList) continue; } - if (!strcmp("MODL", (*it)->name)) + if (!strcmp("MODL", it->name)) { Modl* tempModl = new Modl; // get all subchunks std::list tempChunks; - loadChunks(tempChunks, (*it)->position, (*it)->size); + loadChunks(tempChunks, it->position, it->size); // evaluate MODL subchunks analyseModlChunks(tempModl, tempChunks); @@ -185,15 +185,15 @@ void Object::analyseMsh2Chunks(std::list& chunkList) void Object::analyseMatdChunks(std::list& chunkList) { - for (std::list::iterator it = chunkList.begin(); it != chunkList.end(); it++) + for (auto& it : chunkList) { //TX1D, TX2D, TX3D - if (!strcmp("TX0D", (*it)->name)) + if (!strcmp("TX0D", it->name)) { - fsMesh.seekg((*it)->position); - char* buffer = new char[(*it)->size + 1]; + fsMesh.seekg(it->position); + char* buffer = new char[it->size + 1]; *buffer = { 0 }; - fsMesh.read(buffer, (*it)->size); + fsMesh.read(buffer, it->size); vTextures.back() = buffer; delete buffer; continue; @@ -203,53 +203,53 @@ void Object::analyseMatdChunks(std::list& chunkList) void Object::analyseModlChunks(Modl* dataDestination, std::list& chunkList) { - for (std::list::iterator it = chunkList.begin(); it != chunkList.end(); it++) + for (auto& it : chunkList) { - if (!strcmp("MTYP", (*it)->name)) + if (!strcmp("MTYP", it->name)) { - fsMesh.seekg((*it)->position); + fsMesh.seekg(it->position); std::uint32_t tempType; fsMesh.read(reinterpret_cast(&tempType), sizeof(tempType)); dataDestination->type = (Mtyp)tempType; continue; } - if (!strcmp("PRNT", (*it)->name)) + if (!strcmp("PRNT", it->name)) { - fsMesh.seekg((*it)->position); - char* buffer = new char[(*it)->size]; + fsMesh.seekg(it->position); + char* buffer = new char[it->size]; *buffer = { 0 }; - fsMesh.read(buffer, (*it)->size); + fsMesh.read(buffer, it->size); dataDestination->parent = buffer; delete buffer; continue; } - if (!strcmp("NAME", (*it)->name)) + if (!strcmp("NAME", it->name)) { - fsMesh.seekg((*it)->position); - char* buffer = new char[(*it)->size]; + fsMesh.seekg(it->position); + char* buffer = new char[it->size]; *buffer = { 0 }; - fsMesh.read(buffer, (*it)->size); + fsMesh.read(buffer, it->size); dataDestination->name = buffer; delete buffer; continue; } - if (!strcmp("FLGS", (*it)->name)) + if (!strcmp("FLGS", it->name)) { - fsMesh.seekg((*it)->position); + fsMesh.seekg(it->position); fsMesh.read(reinterpret_cast(&dataDestination->renderFlags), sizeof(dataDestination->renderFlags)); continue; } - if (!strcmp("TRAN", (*it)->name)) + if (!strcmp("TRAN", it->name)) { float tempScale[3]; float tempRotation[4]; float tempTrans[3]; - fsMesh.seekg((*it)->position); + fsMesh.seekg(it->position); fsMesh.read(reinterpret_cast(&tempScale[0]), sizeof(float)); fsMesh.read(reinterpret_cast(&tempScale[1]), sizeof(float)); @@ -260,6 +260,7 @@ void Object::analyseModlChunks(Modl* dataDestination, std::list& c fsMesh.read(reinterpret_cast(&tempRotation[2]), sizeof(float)); fsMesh.read(reinterpret_cast(&tempRotation[3]), sizeof(float)); + //TODO: make a function for this //calculate x,y,z rotation tempRotation[0] = atan2(2 * (tempRotation[0] * tempRotation[1] + tempRotation[2] * tempRotation[3]), 1 - 2 * (pow(tempRotation[1], 2) + pow(tempRotation[2], 2))); @@ -302,11 +303,11 @@ void Object::analyseModlChunks(Modl* dataDestination, std::list& c continue; } - if (!strcmp("GEOM", (*it)->name)) + if (!strcmp("GEOM", it->name)) { // get all subchunks std::list tempGeomChunks; - loadChunks(tempGeomChunks, (*it)->position, (*it)->size); + loadChunks(tempGeomChunks, it->position, it->size); // evaluate GEOM subchunks analyseGeomChunks(dataDestination, tempGeomChunks); @@ -326,28 +327,32 @@ void Object::analyseModlChunks(Modl* dataDestination, std::list& c void Object::analyseGeomChunks(Modl * dataDestination, std::list& chunkList) { - for (std::list::iterator it = chunkList.begin(); it != chunkList.end(); it++) + for (auto& it : chunkList) { - if (!strcmp("BBOX", (*it)->name)) + if (!strcmp("BBOX", it->name)) { - fsMesh.seekg((*it)->position); - std::uint32_t tempValue; - fsMesh.read(reinterpret_cast(&tempValue), sizeof(tempValue)); + fsMesh.seekg(it->position); - /* - float[4] 16 Quaternion Rotation in XYZW. - float[3] 12 Center of the BBox. - float[3] 12 Extents of the BBox(width / 2, height / 2, depth / 2). - float 4 Bounding sphere radius.*/ + // read in the quaternion + for (int i = 0; i < 4; i++) + fsMesh.read(reinterpret_cast(&dataDestination->boundingBox.quaternion[i]), sizeof(float)); + + //read in the center + for (int i = 0; i < 3; i++) + fsMesh.read(reinterpret_cast(&dataDestination->boundingBox.center[i]), sizeof(float)); + + //read in the extents + for (int i = 0; i < 3; i++) + fsMesh.read(reinterpret_cast(&dataDestination->boundingBox.extents[i]), sizeof(float)); continue; } - if (!strcmp("SEGM", (*it)->name)) + if (!strcmp("SEGM", it->name)) { // get all subchunks std::list tempSegmChunks; - loadChunks(tempSegmChunks, (*it)->position, (*it)->size); + loadChunks(tempSegmChunks, it->position, it->size); // evaluate SEGM subchunks analyseSegmChunks(dataDestination, tempSegmChunks); @@ -362,11 +367,11 @@ void Object::analyseGeomChunks(Modl * dataDestination, std::list& continue; } - if (!strcmp("CLTH", (*it)->name)) + if (!strcmp("CLTH", it->name)) { // get all subchunks std::list tempClthChunks; - loadChunks(tempClthChunks, (*it)->position, (*it)->size); + loadChunks(tempClthChunks, it->position, it->size); // evaluate CLTH subchunks analyseClthChunks(dataDestination, tempClthChunks); @@ -387,24 +392,24 @@ void Object::analyseSegmChunks(Modl * dataDestination, std::list& { Segment* tempData = new Segment; - for (std::list::iterator it = chunkList.begin(); it != chunkList.end(); it++) + for (auto& it : chunkList) { - if (!strcmp("MATI", (*it)->name)) + if (!strcmp("MATI", it->name)) { - fsMesh.seekg((*it)->position); + fsMesh.seekg(it->position); fsMesh.read(reinterpret_cast(&tempData->textureIndex), sizeof(tempData->textureIndex)); continue; } - if (!strcmp("POSL", (*it)->name)) + if (!strcmp("POSL", it->name)) { - readVertex(tempData, (*it)->position); + readVertex(tempData, it->position); continue; } - /*if (!strcmp("NRML", (*it)->name)) + /*if (!strcmp("NRML", it->name)) { - fsMesh.seekg((*it)->position); + fsMesh.seekg(it->position); std::uint32_t tempSize; fsMesh.read(reinterpret_cast(&tempSize), sizeof(tempSize)); // List of normals @@ -413,13 +418,13 @@ void Object::analyseSegmChunks(Modl * dataDestination, std::list& continue; }*/ - if (!strcmp("UV0L", (*it)->name)) + if (!strcmp("UV0L", it->name)) { - readUV(tempData, (*it)->position); + readUV(tempData, it->position); continue; } - if (!strcmp("STRP", (*it)->name)) + if (!strcmp("STRP", it->name)) { // don't get null, bone, shadowMesh and hidden mesh indices if (dataDestination->type == null || @@ -430,7 +435,7 @@ void Object::analyseSegmChunks(Modl * dataDestination, std::list& // jump to the data section and read the size; std::uint32_t tempSize; - fsMesh.seekg((*it)->position); + fsMesh.seekg(it->position); fsMesh.read(reinterpret_cast(&tempSize), sizeof(tempSize)); int highBitCount(0); @@ -472,9 +477,6 @@ void Object::analyseSegmChunks(Modl * dataDestination, std::list& for (int i = 1; i >= 0; i--) tempPoly.push_back(saveData[i]); - // save the old vector and set the pointer to the new one - //tempData->meshIndices.push_back(tempPoly); - //tempPoly = newPointer; } // if high bit set } // for all values @@ -494,14 +496,14 @@ void Object::analyseClthChunks(Modl * dataDestination, std::list& { Segment* tempData = new Segment; - for (std::list::iterator it = chunkList.begin(); it != chunkList.end(); it++) + for (auto& it : chunkList) { - if (!strcmp("CTEX", (*it)->name)) + if (!strcmp("CTEX", it->name)) { - fsMesh.seekg((*it)->position); - char* buffer = new char[(*it)->size]; + fsMesh.seekg(it->position); + char* buffer = new char[it->size]; *buffer = { 0 }; - fsMesh.read(buffer, (*it)->size); + fsMesh.read(buffer, it->size); bool tempFound(false); @@ -525,23 +527,23 @@ void Object::analyseClthChunks(Modl * dataDestination, std::list& continue; } - if (!strcmp("CPOS", (*it)->name)) + if (!strcmp("CPOS", it->name)) { - readVertex(tempData, (*it)->position); + readVertex(tempData, it->position); continue; } - if (!strcmp("CUV0", (*it)->name)) + if (!strcmp("CUV0", it->name)) { - readUV(tempData, (*it)->position); + readUV(tempData, it->position); continue; } - if (!strcmp("CMSH", (*it)->name)) + if (!strcmp("CMSH", it->name)) { // jump to the data section and read the size; std::uint32_t tempSize; - fsMesh.seekg((*it)->position); + fsMesh.seekg(it->position); fsMesh.read(reinterpret_cast(&tempSize), sizeof(tempSize)); std::vector tempPoly; diff --git a/MshViewer/Source/OpenGlController.cpp b/MshViewer/Source/OpenGlController.cpp index 0fcdba2..bc851c6 100644 --- a/MshViewer/Source/OpenGlController.cpp +++ b/MshViewer/Source/OpenGlController.cpp @@ -109,6 +109,7 @@ void OpenGLController::processInit() void OpenGLController::deleteVectors() { + //TODO: does .clear() work, too?? if (vModels != NULL) { while (!vModels->empty()) diff --git a/MshViewer/main.cpp b/MshViewer/main.cpp index 680742b..bc0673e 100644 --- a/MshViewer/main.cpp +++ b/MshViewer/main.cpp @@ -17,7 +17,7 @@ int main(int argc, char** argv) else scene = OpenGLController::getInstance(); - scene->loadMsh("..\\Release\\Msh\\quadPoly.msh"); + scene->loadMsh("..\\Release\\Msh\\multiModTex.msh"); do { scene->updateScene();