From 8955fc91eafbb44e0fb5ff5e358f67227acfd29c Mon Sep 17 00:00:00 2001 From: Anakin Date: Thu, 8 Sep 2016 17:41:26 +0200 Subject: [PATCH] OpenGLController: moved some code around shader: throw errors --- MshViewer/Source/OpenGlController.cpp | 62 +++++++++++++++------------ MshViewer/Source/shader.cpp | 27 ++++++------ MshViewer/main.cpp | 2 + 3 files changed, 51 insertions(+), 40 deletions(-) diff --git a/MshViewer/Source/OpenGlController.cpp b/MshViewer/Source/OpenGlController.cpp index 06012e0..fda26d4 100644 --- a/MshViewer/Source/OpenGlController.cpp +++ b/MshViewer/Source/OpenGlController.cpp @@ -99,34 +99,6 @@ void OpenGLController::processInit() // draw vertics only from one side glEnable(GL_CULL_FACE); - - // generate stuff - glGenVertexArrays(1, &gluiVertexArrayID); - glBindVertexArray(gluiVertexArrayID); - - glGenBuffers(1, &gluiVertexBufferID); - glGenBuffers(1, &gluiUVBufferID); - - gluiShaderPrgmID = LoadShaders(VERTEX_SHADER, FRAGMENT_SHADER); - - gluiMatrixID = glGetUniformLocation(gluiShaderPrgmID, "MVP"); - gluiSamplerID = glGetUniformLocation(gluiShaderPrgmID, "textureSampler"); - - vfVertices = loadData(); - vfUV = loadUV(); - - glGenTextures(1, &gluiTextureID); - glBindTexture(GL_TEXTURE_2D, gluiTextureID); - TextureTGA tempTex(TEXTURE_NAME); - 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()); - - 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); - - loadMsh(""); } void OpenGLController::startGLFW() @@ -287,6 +259,40 @@ void OpenGLController::updateScene() void OpenGLController::loadMsh(const char * path) { + // generate stuff + glGenVertexArrays(1, &gluiVertexArrayID); + glBindVertexArray(gluiVertexArrayID); + + glGenBuffers(1, &gluiVertexBufferID); + glGenBuffers(1, &gluiUVBufferID); + + try + { + gluiShaderPrgmID = LoadShaders(VERTEX_SHADER, FRAGMENT_SHADER); + } + catch (std::invalid_argument e) + { + MessageBox(NULL, e.what(), "MeshViewer 2.0 Error", MB_OK | MB_ICONERROR); + exit(1); + } + + gluiMatrixID = glGetUniformLocation(gluiShaderPrgmID, "MVP"); + gluiSamplerID = glGetUniformLocation(gluiShaderPrgmID, "textureSampler"); + + // get data + vfVertices = loadData(); + vfUV = loadUV(); + + glGenTextures(1, &gluiTextureID); + glBindTexture(GL_TEXTURE_2D, gluiTextureID); + TextureTGA tempTex(TEXTURE_NAME); + 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()); + + 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); glBindBuffer(GL_ARRAY_BUFFER, gluiVertexBufferID); glBufferData( diff --git a/MshViewer/Source/shader.cpp b/MshViewer/Source/shader.cpp index 02897cf..c6812d5 100644 --- a/MshViewer/Source/shader.cpp +++ b/MshViewer/Source/shader.cpp @@ -1,7 +1,5 @@ -#include #include #include -#include #include #include using namespace std; @@ -27,10 +25,12 @@ GLuint LoadShaders(const char * vertex_file_path,const char * fragment_file_path while(getline(VertexShaderStream, Line)) VertexShaderCode += "\n" + Line; VertexShaderStream.close(); - }else{ - printf("Impossible to open %s. Are you in the right directory ? Don't forget to read the FAQ !\n", vertex_file_path); - getchar(); - return 0; + } + else + { + std::string message("File not found: "); + message += std::string(vertex_file_path); + throw std::invalid_argument(message.c_str()); } // Read the Fragment Shader code from the file @@ -42,13 +42,18 @@ GLuint LoadShaders(const char * vertex_file_path,const char * fragment_file_path FragmentShaderCode += "\n" + Line; FragmentShaderStream.close(); } + else + { + std::string message("File not found: "); + message += std::string(fragment_file_path); + throw std::invalid_argument(message.c_str()); + } GLint Result = GL_FALSE; int InfoLogLength; // Compile Vertex Shader - printf("Compiling shader : %s\n", vertex_file_path); char const * VertexSourcePointer = VertexShaderCode.c_str(); glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL); glCompileShader(VertexShaderID); @@ -59,13 +64,12 @@ GLuint LoadShaders(const char * vertex_file_path,const char * fragment_file_path if ( InfoLogLength > 0 ){ std::vector VertexShaderErrorMessage(InfoLogLength+1); glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]); - printf("%s\n", &VertexShaderErrorMessage[0]); + throw std::invalid_argument(VertexShaderErrorMessage.data()); } // Compile Fragment Shader - printf("Compiling shader : %s\n", fragment_file_path); char const * FragmentSourcePointer = FragmentShaderCode.c_str(); glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , NULL); glCompileShader(FragmentShaderID); @@ -76,13 +80,12 @@ GLuint LoadShaders(const char * vertex_file_path,const char * fragment_file_path if ( InfoLogLength > 0 ){ std::vector FragmentShaderErrorMessage(InfoLogLength+1); glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]); - printf("%s\n", &FragmentShaderErrorMessage[0]); + throw std::invalid_argument(FragmentShaderErrorMessage.data()); } // Link the program - printf("Linking program\n"); GLuint ProgramID = glCreateProgram(); glAttachShader(ProgramID, VertexShaderID); glAttachShader(ProgramID, FragmentShaderID); @@ -94,7 +97,7 @@ GLuint LoadShaders(const char * vertex_file_path,const char * fragment_file_path if ( InfoLogLength > 0 ){ std::vector ProgramErrorMessage(InfoLogLength+1); glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]); - printf("%s\n", &ProgramErrorMessage[0]); + throw std::invalid_argument(ProgramErrorMessage.data()); } diff --git a/MshViewer/main.cpp b/MshViewer/main.cpp index 0d35789..4a90bc0 100644 --- a/MshViewer/main.cpp +++ b/MshViewer/main.cpp @@ -8,6 +8,8 @@ int main(int argc, char** argv) { OpenGLController *scene = OpenGLController::getInstance(); + scene->loadMsh("test.msh"); + do { scene->updateScene(); } while (!glfwWindowShouldClose(scene->getWindow()));