Loading MshViewer/Header/OpenGLController.h +9 −0 Original line number Diff line number Diff line Loading @@ -5,6 +5,7 @@ #include <glm\glm.hpp> #include <vector> #include "Object.h" #include "Texture.h" #define VERTEX_INDEX_XYZ 0 #define VERTEX_INDEX_UV 1 Loading @@ -23,6 +24,13 @@ struct Vertex { GLfloat uv[2]; }; struct textureData { bool alpha; std::uint32_t width; std::uint32_t height; const GLvoid* data; }; class OpenGLController { //////////////////////////////////////////////////////////////////////////////////////////// Loading Loading @@ -65,6 +73,7 @@ private: // data std::vector<Modl*> vModels; std::vector<textureData*> vTextures; // transformation ========================= //values Loading MshViewer/Source/OpenGlController.cpp +71 −49 Original line number Diff line number Diff line Loading @@ -41,6 +41,13 @@ OpenGLController::~OpenGLController() delete cursor->vertex; delete cursor; } while (!vTextures.empty()) { textureData* cursor = vTextures.back(); vTextures.pop_back(); delete cursor; } } Loading Loading @@ -104,8 +111,13 @@ void OpenGLController::processInit() exit(1); } // get some shader IDs gluiMatrixID = glGetUniformLocation(gluiShaderPrgmID, "MVP"); gluiSamplerID = glGetUniformLocation(gluiShaderPrgmID, "textureSampler"); // generate texture glGenTextures(1, &gluiTextureID); glBindTexture(GL_TEXTURE_2D, gluiTextureID); } void OpenGLController::startGLFW() Loading Loading @@ -266,7 +278,7 @@ void OpenGLController::updateScene() void OpenGLController::loadMsh(const char * path) { // get data // get all models try { Object obj(path); Loading @@ -278,53 +290,7 @@ void OpenGLController::loadMsh(const char * path) exit(1); } glGenTextures(1, &gluiTextureID); glBindTexture(GL_TEXTURE_2D, gluiTextureID); try { ////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 + 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 ); } 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); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glGenerateMipmap(GL_TEXTURE_2D); ////TODO: for all // collect vertex data of all models std::vector<Vertex> tempBufferData; for (auto& it : vModels) Loading @@ -351,6 +317,7 @@ void OpenGLController::loadMsh(const char * path) } } // fill the buffer glBindBuffer(GL_ARRAY_BUFFER, gluiVertexBufferID); glBufferData( GL_ARRAY_BUFFER, Loading @@ -360,4 +327,59 @@ void OpenGLController::loadMsh(const char * path) ); glBindBuffer(GL_ARRAY_BUFFER, 0); // get textures for (auto& it : vModels) { textureData* tempData = new textureData; try { if (it->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 + vModels.front()->texture).c_str()); tempData->alpha = tempTex.hasAlpha(); tempData->width = tempTex.getWidth(); tempData->height = tempTex.getHeight(); tempData->data = tempTex.getData().data(); } catch (std::invalid_argument e) { GLubyte solidColor[4] = { 0, 0, 255, 255 }; tempData->alpha = true; tempData->width = 1; tempData->height = 1; tempData->data = (const GLvoid*)solidColor; } vTextures.push_back(tempData); } glTexImage2D(GL_TEXTURE_2D, 0, vTextures.front()->alpha ? GL_RGBA : GL_RGB, vTextures.front()->width, vTextures.front()->height, 0, vTextures.front()->alpha ? GL_BGRA : GL_BGR, GL_UNSIGNED_BYTE, vTextures.front()->data ); // set some texture parameters 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); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glGenerateMipmap(GL_TEXTURE_2D); } Loading
MshViewer/Header/OpenGLController.h +9 −0 Original line number Diff line number Diff line Loading @@ -5,6 +5,7 @@ #include <glm\glm.hpp> #include <vector> #include "Object.h" #include "Texture.h" #define VERTEX_INDEX_XYZ 0 #define VERTEX_INDEX_UV 1 Loading @@ -23,6 +24,13 @@ struct Vertex { GLfloat uv[2]; }; struct textureData { bool alpha; std::uint32_t width; std::uint32_t height; const GLvoid* data; }; class OpenGLController { //////////////////////////////////////////////////////////////////////////////////////////// Loading Loading @@ -65,6 +73,7 @@ private: // data std::vector<Modl*> vModels; std::vector<textureData*> vTextures; // transformation ========================= //values Loading
MshViewer/Source/OpenGlController.cpp +71 −49 Original line number Diff line number Diff line Loading @@ -41,6 +41,13 @@ OpenGLController::~OpenGLController() delete cursor->vertex; delete cursor; } while (!vTextures.empty()) { textureData* cursor = vTextures.back(); vTextures.pop_back(); delete cursor; } } Loading Loading @@ -104,8 +111,13 @@ void OpenGLController::processInit() exit(1); } // get some shader IDs gluiMatrixID = glGetUniformLocation(gluiShaderPrgmID, "MVP"); gluiSamplerID = glGetUniformLocation(gluiShaderPrgmID, "textureSampler"); // generate texture glGenTextures(1, &gluiTextureID); glBindTexture(GL_TEXTURE_2D, gluiTextureID); } void OpenGLController::startGLFW() Loading Loading @@ -266,7 +278,7 @@ void OpenGLController::updateScene() void OpenGLController::loadMsh(const char * path) { // get data // get all models try { Object obj(path); Loading @@ -278,53 +290,7 @@ void OpenGLController::loadMsh(const char * path) exit(1); } glGenTextures(1, &gluiTextureID); glBindTexture(GL_TEXTURE_2D, gluiTextureID); try { ////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 + 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 ); } 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); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glGenerateMipmap(GL_TEXTURE_2D); ////TODO: for all // collect vertex data of all models std::vector<Vertex> tempBufferData; for (auto& it : vModels) Loading @@ -351,6 +317,7 @@ void OpenGLController::loadMsh(const char * path) } } // fill the buffer glBindBuffer(GL_ARRAY_BUFFER, gluiVertexBufferID); glBufferData( GL_ARRAY_BUFFER, Loading @@ -360,4 +327,59 @@ void OpenGLController::loadMsh(const char * path) ); glBindBuffer(GL_ARRAY_BUFFER, 0); // get textures for (auto& it : vModels) { textureData* tempData = new textureData; try { if (it->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 + vModels.front()->texture).c_str()); tempData->alpha = tempTex.hasAlpha(); tempData->width = tempTex.getWidth(); tempData->height = tempTex.getHeight(); tempData->data = tempTex.getData().data(); } catch (std::invalid_argument e) { GLubyte solidColor[4] = { 0, 0, 255, 255 }; tempData->alpha = true; tempData->width = 1; tempData->height = 1; tempData->data = (const GLvoid*)solidColor; } vTextures.push_back(tempData); } glTexImage2D(GL_TEXTURE_2D, 0, vTextures.front()->alpha ? GL_RGBA : GL_RGB, vTextures.front()->width, vTextures.front()->height, 0, vTextures.front()->alpha ? GL_BGRA : GL_BGR, GL_UNSIGNED_BYTE, vTextures.front()->data ); // set some texture parameters 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); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glGenerateMipmap(GL_TEXTURE_2D); }