From e5490b9451db11641c1d897023f1ca288aa92a3a Mon Sep 17 00:00:00 2001 From: Anakin Date: Tue, 8 Nov 2016 16:42:05 +0100 Subject: [PATCH] collect all texture data and give it to the shader later, solid color works, texture crash --- MshViewer/Header/OpenGLController.h | 9 ++ MshViewer/Source/OpenGlController.cpp | 120 +++++++++++++++----------- 2 files changed, 80 insertions(+), 49 deletions(-) diff --git a/MshViewer/Header/OpenGLController.h b/MshViewer/Header/OpenGLController.h index 0c91487..da09ec3 100644 --- a/MshViewer/Header/OpenGLController.h +++ b/MshViewer/Header/OpenGLController.h @@ -5,6 +5,7 @@ #include #include #include "Object.h" +#include "Texture.h" #define VERTEX_INDEX_XYZ 0 #define VERTEX_INDEX_UV 1 @@ -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 { //////////////////////////////////////////////////////////////////////////////////////////// @@ -65,6 +73,7 @@ private: // data std::vector vModels; + std::vector vTextures; // transformation ========================= //values diff --git a/MshViewer/Source/OpenGlController.cpp b/MshViewer/Source/OpenGlController.cpp index d4e89a8..04e2218 100644 --- a/MshViewer/Source/OpenGlController.cpp +++ b/MshViewer/Source/OpenGlController.cpp @@ -41,6 +41,13 @@ OpenGLController::~OpenGLController() delete cursor->vertex; delete cursor; } + + while (!vTextures.empty()) + { + textureData* cursor = vTextures.back(); + vTextures.pop_back(); + delete cursor; + } } @@ -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() @@ -266,7 +278,7 @@ void OpenGLController::updateScene() void OpenGLController::loadMsh(const char * path) { - // get data + // get all models try { Object obj(path); @@ -278,55 +290,9 @@ 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 tempBufferData; - + for (auto& it : vModels) { for (unsigned int i = 0; i < it->meshSize; i++) @@ -351,6 +317,7 @@ void OpenGLController::loadMsh(const char * path) } } + // fill the buffer glBindBuffer(GL_ARRAY_BUFFER, gluiVertexBufferID); glBufferData( GL_ARRAY_BUFFER, @@ -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); + + + }