From c0a84c75131e39f5b3fb1de46ef627251c5c4fef Mon Sep 17 00:00:00 2001 From: Anakin Date: Sat, 22 Oct 2016 13:49:45 +0200 Subject: [PATCH] get all materials, save the texture name for every modl, program crashes at glfwPollEvents(); fix??? --- MshViewer/Header/Object.h | 3 +- MshViewer/Source/Object.cpp | 80 +++++++++++++++++++++++---- MshViewer/Source/OpenGlController.cpp | 3 +- 3 files changed, 73 insertions(+), 13 deletions(-) diff --git a/MshViewer/Header/Object.h b/MshViewer/Header/Object.h index a584791..3ef357d 100644 --- a/MshViewer/Header/Object.h +++ b/MshViewer/Header/Object.h @@ -54,13 +54,14 @@ private: std::list lModls; std::fstream fsMesh; - std::list lTextures; + std::vector vTextures; private: void setModlDefault(Modl* model); void loadChunks(std::list &destination, std::streampos start, const std::uint32_t end); void analyseMsh2Chunks(std::list &chunkList); + void analyseMatdChunks(Modl* dataDestination, std::list &chunkList); void analyseModlChunks(Modl* dataDestination, std::list &chunkList); void analyseGeomChunks(Modl* dataDestination, std::list &chunkList); void analyseSegmChunks(Modl* dataDestination, std::list &chunkList); diff --git a/MshViewer/Source/Object.cpp b/MshViewer/Source/Object.cpp index bf0c5b8..9aaaf20 100644 --- a/MshViewer/Source/Object.cpp +++ b/MshViewer/Source/Object.cpp @@ -2,7 +2,6 @@ #include - ///////////////////////////////////////////////////////////////////////// // public constructor/destructor @@ -24,7 +23,7 @@ Object::Object(const char* path) fsMesh.read(reinterpret_cast(&tempFileSize), sizeof(tempFileSize)); loadChunks(tempMainChunks, fsMesh.tellg(), tempFileSize); - // evaluate sub chunks (= find MSH2) + // evaluate HEDR subchunks (= find MSH2) for (std::list::iterator it = tempMainChunks.begin(); it != tempMainChunks.end(); it++) { if (!strcmp("MSH2", (*it)->name)) @@ -65,7 +64,6 @@ Object::~Object() } - ///////////////////////////////////////////////////////////////////////// // private functions @@ -135,17 +133,51 @@ void Object::analyseMsh2Chunks(std::list& chunkList) { for (std::list::iterator it = chunkList.begin(); it != chunkList.end(); it++) { + Modl* tempModl = new Modl; + setModlDefault(tempModl); + if (!strcmp("MATL", (*it)->name)) { + // get all MATD from MATL list + std::list tempMatlChunks; + loadChunks(tempMatlChunks, (*it)->position, (*it)->size); + + // evaluate MATL subchunks + for (std::list::iterator it = tempMatlChunks.begin(); it != tempMatlChunks.end(); it++) + { + // This shouldn't be anything else than MATD + if (!strcmp("MATD", (*it)->name)) + { + // get all subchunks from MATD + std::list tempMatdChunks; + loadChunks(tempMatdChunks, (*it)->position, (*it)->size); + + // analyse MATD subchunks + analyseMatdChunks(tempModl, tempMatdChunks); + + // clean up + while (!tempMatdChunks.empty()) + { + ChunkHeader* tempCursor = tempMatdChunks.front(); + tempMatdChunks.pop_front(); + delete tempCursor; + } + } + } + + // clean up + while (!tempMatlChunks.empty()) + { + ChunkHeader* tempCursor = tempMatlChunks.front(); + tempMatlChunks.pop_front(); + delete tempCursor; + } continue; } if (!strcmp("MODL", (*it)->name)) { - Modl* tempModl = new Modl; - setModlDefault(tempModl); - // get all subchunks std::list tempChunks; loadChunks(tempChunks, (*it)->position, (*it)->size); @@ -161,9 +193,27 @@ void Object::analyseMsh2Chunks(std::list& chunkList) delete tempCursor; } - // save Model data - lModls.push_back(tempModl); + continue; + } + // save Model data + lModls.push_back(tempModl); + } +} + +void Object::analyseMatdChunks(Modl * dataDestination, std::list& chunkList) +{ + for (std::list::iterator it = chunkList.begin(); it != chunkList.end(); it++) + { + //TX1D, TX2D, TX3D + if (!strcmp("TX0D", (*it)->name)) + { + fsMesh.seekg((*it)->position); + char* buffer = new char[(*it)->size + 1]; + *buffer = { 0 }; + fsMesh.read(buffer, (*it)->size); + vTextures.push_back(buffer); + delete buffer; continue; } } @@ -186,6 +236,7 @@ void Object::analyseModlChunks(Modl* dataDestination, std::list& c { fsMesh.seekg((*it)->position); char* buffer = new char[(*it)->size]; + *buffer = { 0 }; fsMesh.read(buffer, (*it)->size); dataDestination->parent = buffer; delete buffer; @@ -196,6 +247,7 @@ void Object::analyseModlChunks(Modl* dataDestination, std::list& c { fsMesh.seekg((*it)->position); char* buffer = new char[(*it)->size]; + *buffer = { 0 }; fsMesh.read(buffer, (*it)->size); dataDestination->name = buffer; delete buffer; @@ -328,8 +380,15 @@ void Object::analyseSegmChunks(Modl * dataDestination, std::list& if (!strcmp("MATI", (*it)->name)) { fsMesh.seekg((*it)->position); - // material index index into MATL - // long int - 4 - material index + std::uint32_t tempIndex; + fsMesh.read(reinterpret_cast(&tempIndex), sizeof(tempIndex)); + if (vTextures.size() < tempIndex - 1) + { + std::cout << "warning texture index <" << tempIndex << "> unknown" << std::endl; + dataDestination->texture = ""; + continue; + } + dataDestination->texture = vTextures[tempIndex - 1]; continue; } @@ -410,6 +469,7 @@ void Object::analyseClthChunks(Modl * dataDestination, std::list& { fsMesh.seekg((*it)->position); char* buffer = new char[(*it)->size]; + *buffer = { 0 }; fsMesh.read(buffer, (*it)->size); dataDestination->texture = buffer; delete buffer; diff --git a/MshViewer/Source/OpenGlController.cpp b/MshViewer/Source/OpenGlController.cpp index ce141d8..2515ba3 100644 --- a/MshViewer/Source/OpenGlController.cpp +++ b/MshViewer/Source/OpenGlController.cpp @@ -309,11 +309,10 @@ void OpenGLController::loadMsh(const char * path) } catch (std::invalid_argument e) { - GLubyte solidColor[4] = { 255,0,0,255 }; + GLubyte solidColor[4] = { 255, 0, 0, 255}; glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, (const GLvoid*)solidColor); } - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);