From ea036f403c4dc6c2fdb1ba983a33bbf468408e12 Mon Sep 17 00:00:00 2001 From: EmaMaker Date: Wed, 19 Jul 2023 12:41:44 +0200 Subject: [PATCH] shader.hpp: add geometry shader support --- include/shader.hpp | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/include/shader.hpp b/include/shader.hpp index 1fcb277..09f8ae7 100644 --- a/include/shader.hpp +++ b/include/shader.hpp @@ -19,31 +19,38 @@ public: unsigned int ID; // constructor generates the shader on the fly // ------------------------------------------------------------------------ - Shader(const char *vertexPath, const char *fragmentPath) + Shader(const char* geometryPath, const char *vertexPath, const char *fragmentPath) { // 1. retrieve the vertex/fragment source code from filePath std::string vertexCode; std::string fragmentCode; + std::string geometryCode; std::ifstream vShaderFile; std::ifstream fShaderFile; + std::ifstream gShaderFile; // ensure ifstream objects can throw exceptions: vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit); fShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit); + gShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit); try { // open files vShaderFile.open(vertexPath); fShaderFile.open(fragmentPath); - std::stringstream vShaderStream, fShaderStream; + 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) { @@ -51,8 +58,9 @@ public: } const char *vShaderCode = vertexCode.c_str(); const char *fShaderCode = fragmentCode.c_str(); + const char *gShaderCode = geometryCode.c_str(); // 2. compile shaders - unsigned int vertex, fragment; + unsigned int vertex, fragment, geometry; // vertex shader vertex = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertex, 1, &vShaderCode, NULL); @@ -63,15 +71,23 @@ public: 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); 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); } // activate the shader // ------------------------------------------------------------------------