diff --git a/MshViewer/Header/Object.h b/MshViewer/Header/Object.h index a5e60b1f7becf30ecd8f4f2ba4daeac7aabc13cc..b84a6d3a7861dbad7b460895dd0259240e131534 100644 --- a/MshViewer/Header/Object.h +++ b/MshViewer/Header/Object.h @@ -4,6 +4,7 @@ #include <fstream> #include <string> #include <gl\glew.h> +#include <glm\gtc\matrix_transform.hpp> 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<Modl*> lModls; + std::vector<Modl*> vModls; std::fstream fsMesh; std::vector<std::string> vTextures; @@ -73,7 +75,8 @@ private: public: std::vector<GLfloat> getVertex() const; std::vector<GLfloat> getUV() const; - std::list<std::uint32_t> getSize() const; + std::uint32_t getSize() const; std::list<std::string> getTexture() const; + std::vector<Modl*> getModels() const; }; diff --git a/MshViewer/Header/OpenGLController.h b/MshViewer/Header/OpenGLController.h index 537fd039499a435bdf461e0bb41ecdb773f44091..abd804bc9ffa66a05d50fcaf5473d56bf398583a 100644 --- a/MshViewer/Header/OpenGLController.h +++ b/MshViewer/Header/OpenGLController.h @@ -4,6 +4,7 @@ #include <gl\glfw3.h> #include <glm\glm.hpp> #include <vector> +#include "Object.h" class OpenGLController { @@ -50,6 +51,7 @@ private: std::vector<GLfloat> vfVertices; std::vector<GLfloat> vfUV; std::uint32_t ui32MeshSize; + std::vector<Modl*> vModels; // transformation =============== //values diff --git a/MshViewer/Source/Object.cpp b/MshViewer/Source/Object.cpp index 512f98681163b851d87e48505cc9a0bcf95270c7..8bcb685608064a7c79f37a777159ea5c90030563 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<ChunkHeader*>& chunkList) } // save Model data - lModls.push_back(tempModl); + vModls.push_back(tempModl); continue; } @@ -265,17 +266,54 @@ void Object::analyseModlChunks(Modl* dataDestination, std::list<ChunkHeader*>& c if (!strcmp("TRAN", (*it)->name)) { + float tempScale[3]; + float tempRotation[4]; + float tempTrans[3]; + fsMesh.seekg((*it)->position); - fsMesh.read(reinterpret_cast<char*>(&dataDestination->tran.scale[0]), sizeof(float)); - fsMesh.read(reinterpret_cast<char*>(&dataDestination->tran.scale[1]), sizeof(float)); - fsMesh.read(reinterpret_cast<char*>(&dataDestination->tran.scale[2]), sizeof(float)); - fsMesh.read(reinterpret_cast<char*>(&dataDestination->tran.rotation[0]), sizeof(float)); - fsMesh.read(reinterpret_cast<char*>(&dataDestination->tran.rotation[1]), sizeof(float)); - fsMesh.read(reinterpret_cast<char*>(&dataDestination->tran.rotation[2]), sizeof(float)); - fsMesh.read(reinterpret_cast<char*>(&dataDestination->tran.rotation[3]), sizeof(float)); - fsMesh.read(reinterpret_cast<char*>(&dataDestination->tran.translation[0]), sizeof(float)); - fsMesh.read(reinterpret_cast<char*>(&dataDestination->tran.translation[1]), sizeof(float)); - fsMesh.read(reinterpret_cast<char*>(&dataDestination->tran.translation[2]), sizeof(float)); + + fsMesh.read(reinterpret_cast<char*>(&tempScale[0]), sizeof(float)); + fsMesh.read(reinterpret_cast<char*>(&tempScale[1]), sizeof(float)); + fsMesh.read(reinterpret_cast<char*>(&tempScale[2]), sizeof(float)); + + //TODO: rotation value is wrong + fsMesh.read(reinterpret_cast<char*>(&tempRotation[0]), sizeof(float)); + fsMesh.read(reinterpret_cast<char*>(&tempRotation[1]), sizeof(float)); + fsMesh.read(reinterpret_cast<char*>(&tempRotation[2]), sizeof(float)); + fsMesh.read(reinterpret_cast<char*>(&tempRotation[3]), sizeof(float)); + + fsMesh.read(reinterpret_cast<char*>(&tempTrans[0]), sizeof(float)); + fsMesh.read(reinterpret_cast<char*>(&tempTrans[1]), sizeof(float)); + fsMesh.read(reinterpret_cast<char*>(&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<GLfloat> Object::getVertex() const { std::vector<GLfloat> tempData; - for (std::list<Modl*>::const_iterator it = lModls.begin(); it != lModls.end(); it++) + for (std::vector<Modl*>::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<GLfloat> Object::getUV() const { std::vector<GLfloat> tempData; - for (std::list<Modl*>::const_iterator it = lModls.begin(); it != lModls.end(); it++) + for (std::vector<Modl*>::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<GLfloat> Object::getUV() const return tempData; } -std::list<std::uint32_t> Object::getSize() const +std::uint32_t Object::getSize() const { - std::list<std::uint32_t> tempData; + std::uint32_t tempData(0); + + for (std::vector<Modl*>::const_iterator it = vModls.begin(); it != vModls.end(); it++) + { + if ((*it)->renderFlags == 1) + continue; - for (std::list<Modl*>::const_iterator it = lModls.begin(); it != lModls.end(); it++) - tempData.push_back((*it)->meshSize); + tempData += (*it)->meshSize; + } return tempData; } @@ -577,12 +626,22 @@ std::list<std::string> Object::getTexture() const { std::list<std::string> tempData; - for (std::list<Modl*>::const_iterator it = lModls.begin(); it != lModls.end(); it++) + for (std::vector<Modl*>::const_iterator it = vModls.begin(); it != vModls.end(); it++) + { + if ((*it)->renderFlags == 1) + continue; + tempData.push_back((*it)->texture); + } return tempData; } +std::vector<Modl*> Object::getModels() const +{ + return vModls; +} + ///////////////////////////////////////////////////////////////////////// // public functions diff --git a/MshViewer/Source/OpenGlController.cpp b/MshViewer/Source/OpenGlController.cpp index ea7e8f1dbcc1594776c6842d23b5c4e0c5a2280e..b7edda7b8288c04a6411399f08f6429595e282bb 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<GLfloat> 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<GLfloat> 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 0b9541c44204e2e635c77367f8d5d27993c400e7..c6984d789da57d9cd0b13eb25646a393b8656906 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();