From e509b1e11c20c137797503a0a231a95dfeb5ed42 Mon Sep 17 00:00:00 2001 From: Anakin Date: Sun, 30 Oct 2016 14:22:08 +0100 Subject: [PATCH] display all models from a mesh, problem, apply different model matrices for each model, solution give a list of all models to ogl controller, where they are handled, next steps: find solution for wrong rotation + draw ALL models instead of only the first --- MshViewer/Header/Object.h | 7 +- MshViewer/Header/OpenGLController.h | 2 + MshViewer/Source/Object.cpp | 95 ++++++++++++++++++++++----- MshViewer/Source/OpenGlController.cpp | 84 ++++++++++++++++++----- MshViewer/main.cpp | 2 +- 5 files changed, 153 insertions(+), 37 deletions(-) diff --git a/MshViewer/Header/Object.h b/MshViewer/Header/Object.h index a5e60b1..b84a6d3 100644 --- a/MshViewer/Header/Object.h +++ b/MshViewer/Header/Object.h @@ -4,6 +4,7 @@ #include #include #include +#include enum Mtyp { null, @@ -25,6 +26,7 @@ struct Modl { std::string parent; Mtyp type; std::uint32_t renderFlags; + glm::mat4 m4x4Translation; struct { float scale[3]; float rotation[4]; @@ -52,7 +54,7 @@ public: private: - std::list lModls; + std::vector vModls; std::fstream fsMesh; std::vector vTextures; @@ -73,7 +75,8 @@ private: public: std::vector getVertex() const; std::vector getUV() const; - std::list getSize() const; + std::uint32_t getSize() const; std::list getTexture() const; + std::vector getModels() const; }; diff --git a/MshViewer/Header/OpenGLController.h b/MshViewer/Header/OpenGLController.h index 537fd03..abd804b 100644 --- a/MshViewer/Header/OpenGLController.h +++ b/MshViewer/Header/OpenGLController.h @@ -4,6 +4,7 @@ #include #include #include +#include "Object.h" class OpenGLController { @@ -50,6 +51,7 @@ private: std::vector vfVertices; std::vector vfUV; std::uint32_t ui32MeshSize; + std::vector vModels; // transformation =============== //values diff --git a/MshViewer/Source/Object.cpp b/MshViewer/Source/Object.cpp index 512f986..8bcb685 100644 --- a/MshViewer/Source/Object.cpp +++ b/MshViewer/Source/Object.cpp @@ -73,6 +73,7 @@ void Object::setModlDefault(Modl * model) model->parent = ""; model->type = null; model->renderFlags = -1; + model->m4x4Translation = glm::mat4(1.0f); model->tran.scale[0] = 0; model->tran.scale[1] = 0; model->tran.scale[2] = 0; @@ -196,7 +197,7 @@ void Object::analyseMsh2Chunks(std::list& chunkList) } // save Model data - lModls.push_back(tempModl); + vModls.push_back(tempModl); continue; } @@ -265,17 +266,54 @@ void Object::analyseModlChunks(Modl* dataDestination, std::list& c if (!strcmp("TRAN", (*it)->name)) { + float tempScale[3]; + float tempRotation[4]; + float tempTrans[3]; + fsMesh.seekg((*it)->position); - fsMesh.read(reinterpret_cast(&dataDestination->tran.scale[0]), sizeof(float)); - fsMesh.read(reinterpret_cast(&dataDestination->tran.scale[1]), sizeof(float)); - fsMesh.read(reinterpret_cast(&dataDestination->tran.scale[2]), sizeof(float)); - fsMesh.read(reinterpret_cast(&dataDestination->tran.rotation[0]), sizeof(float)); - fsMesh.read(reinterpret_cast(&dataDestination->tran.rotation[1]), sizeof(float)); - fsMesh.read(reinterpret_cast(&dataDestination->tran.rotation[2]), sizeof(float)); - fsMesh.read(reinterpret_cast(&dataDestination->tran.rotation[3]), sizeof(float)); - fsMesh.read(reinterpret_cast(&dataDestination->tran.translation[0]), sizeof(float)); - fsMesh.read(reinterpret_cast(&dataDestination->tran.translation[1]), sizeof(float)); - fsMesh.read(reinterpret_cast(&dataDestination->tran.translation[2]), sizeof(float)); + + fsMesh.read(reinterpret_cast(&tempScale[0]), sizeof(float)); + fsMesh.read(reinterpret_cast(&tempScale[1]), sizeof(float)); + fsMesh.read(reinterpret_cast(&tempScale[2]), sizeof(float)); + + //TODO: rotation value is wrong + fsMesh.read(reinterpret_cast(&tempRotation[0]), sizeof(float)); + fsMesh.read(reinterpret_cast(&tempRotation[1]), sizeof(float)); + fsMesh.read(reinterpret_cast(&tempRotation[2]), sizeof(float)); + fsMesh.read(reinterpret_cast(&tempRotation[3]), sizeof(float)); + + fsMesh.read(reinterpret_cast(&tempTrans[0]), sizeof(float)); + fsMesh.read(reinterpret_cast(&tempTrans[1]), sizeof(float)); + fsMesh.read(reinterpret_cast(&tempTrans[2]), sizeof(float)); + + dataDestination->m4x4Translation = glm::scale( + dataDestination->m4x4Translation, + glm::vec3(tempScale[0], tempScale[1], tempScale[2]) + ); + + dataDestination->m4x4Translation = glm::translate( + dataDestination->m4x4Translation, + glm::vec3(tempTrans[0], tempTrans[1], tempTrans[2]) + ); + + dataDestination->m4x4Translation = glm::rotate( + dataDestination->m4x4Translation, + tempRotation[0], + glm::vec3(1, 0, 0) + ); + + dataDestination->m4x4Translation = glm::rotate( + dataDestination->m4x4Translation, + tempRotation[1], + glm::vec3(0, 1, 0) + ); + + dataDestination->m4x4Translation = glm::rotate( + dataDestination->m4x4Translation, + tempRotation[2], + glm::vec3(0, 0, 1) + ); + continue; } @@ -528,8 +566,11 @@ std::vector Object::getVertex() const { std::vector tempData; - for (std::list::const_iterator it = lModls.begin(); it != lModls.end(); it++) + for (std::vector::const_iterator it = vModls.begin(); it != vModls.end(); it++) { + if ((*it)->renderFlags == 1) + continue; + for (unsigned int i = 0; i < (*it)->meshSize; i++) { tempData.push_back((GLfloat)(*it)->vertex[(*it)->mesh[i] * 3]); @@ -545,8 +586,11 @@ std::vector Object::getUV() const { std::vector tempData; - for (std::list::const_iterator it = lModls.begin(); it != lModls.end(); it++) + for (std::vector::const_iterator it = vModls.begin(); it != vModls.end(); it++) { + if ((*it)->renderFlags == 1) + continue; + if ((*it)->uv == NULL) { for (unsigned int i = 0; i < (*it)->meshSize; i++) @@ -563,12 +607,17 @@ std::vector Object::getUV() const return tempData; } -std::list Object::getSize() const +std::uint32_t Object::getSize() const { - std::list tempData; + std::uint32_t tempData(0); - for (std::list::const_iterator it = lModls.begin(); it != lModls.end(); it++) - tempData.push_back((*it)->meshSize); + for (std::vector::const_iterator it = vModls.begin(); it != vModls.end(); it++) + { + if ((*it)->renderFlags == 1) + continue; + + tempData += (*it)->meshSize; + } return tempData; } @@ -577,12 +626,22 @@ std::list Object::getTexture() const { std::list tempData; - for (std::list::const_iterator it = lModls.begin(); it != lModls.end(); it++) + for (std::vector::const_iterator it = vModls.begin(); it != vModls.end(); it++) + { + if ((*it)->renderFlags == 1) + continue; + tempData.push_back((*it)->texture); + } return tempData; } +std::vector Object::getModels() const +{ + return vModls; +} + ///////////////////////////////////////////////////////////////////////// // public functions diff --git a/MshViewer/Source/OpenGlController.cpp b/MshViewer/Source/OpenGlController.cpp index ea7e8f1..b7edda7 100644 --- a/MshViewer/Source/OpenGlController.cpp +++ b/MshViewer/Source/OpenGlController.cpp @@ -8,7 +8,6 @@ #include "shader.hpp" #include "import.h" #include "Texture.h" -#include "Object.h" #define VERTEX_SHADER "Shader/VertexTextureShader.mv2shdr" #define FRAGMENT_SHADER "Shader/FragmentTextureShader.mv2shdr" @@ -167,7 +166,10 @@ glm::mat4 OpenGLController::getMVPMatrix() glm::vec3(dTranslationX, dTranslationY, dTranslationZ - 1), glm::vec3(0, 1, 0) ); - m4x4Model = glm::mat4(1.0f); + //m4x4Model = glm::mat4(1.0f); + + //TODO for all + m4x4Model = vModels.front()->m4x4Translation; m4x4Model = glm::rotate(m4x4Model, fRotationX, glm::vec3(1, 0, 0)); m4x4Model = glm::rotate(m4x4Model, fRotationY, glm::vec3(0, 1, 0)); m4x4Model = glm::rotate(m4x4Model, fRotationZ, glm::vec3(0, 0, 1)); @@ -247,7 +249,8 @@ void OpenGLController::updateScene() glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0); //draw objects - glDrawArrays(GL_TRIANGLES, 0, ui32MeshSize); + //// TODO: for all + glDrawArrays(GL_TRIANGLES, 0, vModels.front()->meshSize); //close attributes glDisableVertexAttribArray(0); @@ -285,10 +288,13 @@ void OpenGLController::loadMsh(const char * path) try { Object obj(path); - vfVertices = obj.getVertex(); - vfUV = obj.getUV(); - listTextures = obj.getTexture(); - ui32MeshSize = obj.getSize().front(); + + vModels = obj.getModels(); + + //vfVertices = obj.getVertex(); + //vfUV = obj.getUV(); + //listTextures = obj.getTexture(); + //ui32MeshSize = obj.getSize(); } catch (std::invalid_argument e) { @@ -301,21 +307,39 @@ void OpenGLController::loadMsh(const char * path) try { - if (listTextures.empty()) - throw std::invalid_argument("no texture names"); + ////TODO: for all + if (vModels.front()->texture == "") + throw std::invalid_argument("no texture name"); std::string tempPath = path; while (tempPath.back() != '/' && tempPath.back() != '\\') tempPath.pop_back(); - TextureTGA tempTex(std::string(tempPath + listTextures.front()).c_str()); - glTexImage2D(GL_TEXTURE_2D, 0, tempTex.hasAlpha() ? GL_RGBA : GL_RGB, tempTex.getWidth(), tempTex.getHeight(), 0, tempTex.hasAlpha() ? GL_BGRA : GL_BGR, GL_UNSIGNED_BYTE, tempTex.getData().data()); + TextureTGA tempTex(std::string(tempPath + vModels.front()->texture).c_str()); + glTexImage2D(GL_TEXTURE_2D, + 0, + tempTex.hasAlpha() ? GL_RGBA : GL_RGB, + tempTex.getWidth(), + tempTex.getHeight(), + 0, tempTex.hasAlpha() ? GL_BGRA : GL_BGR, + GL_UNSIGNED_BYTE, + tempTex.getData().data() + ); } catch (std::invalid_argument e) { 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); + 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); @@ -325,18 +349,46 @@ void OpenGLController::loadMsh(const char * path) glGenerateMipmap(GL_TEXTURE_2D); glBindBuffer(GL_ARRAY_BUFFER, gluiVertexBufferID); + + ////TODO: for all + std::vector tempVertex; + + for (unsigned int i = 0; i < vModels.front()->meshSize; i++) + { + tempVertex.push_back((GLfloat)vModels.front()->vertex[vModels.front()->mesh[i] * 3]); + tempVertex.push_back((GLfloat)vModels.front()->vertex[vModels.front()->mesh[i] * 3 + 1]); + tempVertex.push_back((GLfloat)vModels.front()->vertex[vModels.front()->mesh[i] * 3 + 2]); + } + glBufferData( GL_ARRAY_BUFFER, - sizeof(vfVertices) * vfVertices.size(), - vfVertices.data(), + sizeof(tempVertex) * tempVertex.size(), + tempVertex.data(), GL_STATIC_DRAW ); + ////TODO: for all + std::vector tempUV; + + if (vModels.front()->uv == NULL) + { + for (unsigned int i = 0; i < vModels.front()->meshSize; i++) + tempUV.push_back(1.0); + } + else + { + for (unsigned int i = 0; i < vModels.front()->meshSize; i++) + { + tempUV.push_back((GLfloat)vModels.front()->uv[vModels.front()->mesh[i] * 2]); + tempUV.push_back((GLfloat)vModels.front()->uv[vModels.front()->mesh[i] * 2 + 1]); + } + } + glBindBuffer(GL_ARRAY_BUFFER, gluiUVBufferID); glBufferData( GL_ARRAY_BUFFER, - sizeof(vfUV) * vfUV.size(), - vfUV.data(), + sizeof(tempUV) * tempUV.size(), + tempUV.data(), GL_STATIC_DRAW ); diff --git a/MshViewer/main.cpp b/MshViewer/main.cpp index 0b9541c..c6984d7 100644 --- a/MshViewer/main.cpp +++ b/MshViewer/main.cpp @@ -28,7 +28,7 @@ openGL: OpenGLController *scene = OpenGLController::getInstance(); - scene->loadMsh("..\\Release\\Msh\\cubeTex.msh"); + scene->loadMsh("..\\Release\\Msh\\cubeTrans.msh"); do { scene->updateScene();