From 6670f3b41c348ce726cc1c9e025439c6ab6b785d Mon Sep 17 00:00:00 2001 From: EmaMaker Date: Sun, 17 Sep 2023 10:40:21 +0200 Subject: [PATCH] shader: make geometry shader not mandatory --- include/shader.hpp | 50 +++++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/include/shader.hpp b/include/shader.hpp index 09f8ae7..0aa6acf 100644 --- a/include/shader.hpp +++ b/include/shader.hpp @@ -34,60 +34,70 @@ public: gShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit); try { + std::stringstream vShaderStream, fShaderStream, gShaderStream; + if(geometryPath){ + gShaderFile.open(geometryPath); + gShaderStream << gShaderFile.rdbuf(); + gShaderFile.close(); + geometryCode = gShaderStream.str(); + } + // open files vShaderFile.open(vertexPath); fShaderFile.open(fragmentPath); - gShaderFile.open(geometryPath); - std::stringstream vShaderStream, fShaderStream, gShaderStream; // read file's buffer contents into streams vShaderStream << vShaderFile.rdbuf(); fShaderStream << fShaderFile.rdbuf(); - gShaderStream << gShaderFile.rdbuf(); // close file handlers vShaderFile.close(); fShaderFile.close(); - gShaderFile.close(); // convert stream into string vertexCode = vShaderStream.str(); fragmentCode = fShaderStream.str(); - geometryCode = gShaderStream.str(); } catch (std::ifstream::failure &e) { std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ: " << e.what() << std::endl; } - const char *vShaderCode = vertexCode.c_str(); - const char *fShaderCode = fragmentCode.c_str(); - const char *gShaderCode = geometryCode.c_str(); - // 2. compile shaders + // shader Program + ID = glCreateProgram(); unsigned int vertex, fragment, geometry; + + // geometry shader + if(geometryPath){ + const char *gShaderCode = geometryCode.c_str(); + geometry = glCreateShader(GL_GEOMETRY_SHADER); + glShaderSource(geometry, 1, &gShaderCode, NULL); + glCompileShader(geometry); + checkCompileErrors(geometry, "GEOMETRY"); + + glAttachShader(ID, geometry); + } + // vertex shader + const char *vShaderCode = vertexCode.c_str(); vertex = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertex, 1, &vShaderCode, NULL); glCompileShader(vertex); checkCompileErrors(vertex, "VERTEX"); + glAttachShader(ID, vertex); + // fragment Shader + const char *fShaderCode = fragmentCode.c_str(); fragment = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragment, 1, &fShaderCode, NULL); glCompileShader(fragment); checkCompileErrors(fragment, "FRAGMENT"); - // geometry shader - geometry = glCreateShader(GL_GEOMETRY_SHADER); - glShaderSource(geometry, 1, &gShaderCode, NULL); - glCompileShader(geometry); - checkCompileErrors(geometry, "GEOMETRY"); - - // shader Program - ID = glCreateProgram(); - glAttachShader(ID, vertex); glAttachShader(ID, fragment); - glAttachShader(ID, geometry); + + // Constructu the program glLinkProgram(ID); checkCompileErrors(ID, "PROGRAM"); + // delete the shaders as they're linked into our program now and no longer necessary glDeleteShader(vertex); glDeleteShader(fragment); - glDeleteShader(geometry); + if(geometryPath) glDeleteShader(geometry); } // activate the shader // ------------------------------------------------------------------------