From 86468a9ce444bfe2b590247494046e32bd26d162 Mon Sep 17 00:00:00 2001 From: Robert Date: Wed, 2 Sep 2020 15:07:21 +0200 Subject: [PATCH] Created shader class --- src/CMakeLists.txt | 2 +- src/PlotWindow.cpp | 83 +++----------------------------------- src/PlotWindow.hpp | 8 +--- src/Shader.cpp | 99 ++++++++++++++++++++++++++++++++++++++++++++++ src/Shader.hpp | 21 ++++++++++ 5 files changed, 128 insertions(+), 85 deletions(-) create mode 100644 src/Shader.cpp create mode 100644 src/Shader.hpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ae94d27..aadd196 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,7 +1,7 @@ add_executable(ComplexPlotting "main.cpp" - "PlotWindow.hpp" "PlotWindow.cpp" "PlotManager.hpp" "PlotManager.cpp") + "PlotWindow.hpp" "PlotWindow.cpp" "PlotManager.hpp" "PlotManager.cpp" "Shader.hpp" "Shader.cpp") file(GLOB vendor_SRC ${CMAKE_SOURCE_DIR}/vendor/src/*.c diff --git a/src/PlotWindow.cpp b/src/PlotWindow.cpp index c4395c0..6296f36 100644 --- a/src/PlotWindow.cpp +++ b/src/PlotWindow.cpp @@ -18,7 +18,7 @@ PlotWindow::PlotWindow(int w, int h, int id, std::string title) : 0, 1, 3, 1, 2, 3 }, - vert(0), frag(0), shader(0), VAO(0), VBO(0), EBO(0) + shader(nullptr), VAO(0), VBO(0), EBO(0) { window = glfwCreateWindow(w, h, ("Plot " + std::to_string(id) + " | " + title).c_str(), NULL, NULL); if (window == nullptr) @@ -42,61 +42,7 @@ PlotWindow::PlotWindow(int w, int h, int id, std::string title) : glViewport(0, 0, w, h); - int success; - char errorBuf[512]; - - // Load and compile Vertex Shader - vert = glCreateShader(GL_VERTEX_SHADER); - char* shaderSource = nullptr; - LoadShaderSourceCode("shaders/basic.vert", &shaderSource); - if (shaderSource == nullptr) - shaderSource = ""; - - glShaderSource(vert, 1, &shaderSource, NULL); - glCompileShader(vert); - glGetShaderiv(vert, GL_COMPILE_STATUS, &success); - if (!success) - { - glGetShaderInfoLog(vert, 512, NULL, errorBuf); - std::cerr << "Vertex shader compilation error" << std::endl << errorBuf << std::endl; - glfwSetWindowShouldClose(window, 1); - return; - } - - // load and compile Fragment Shader - frag = glCreateShader(GL_FRAGMENT_SHADER); - shaderSource = nullptr; - LoadShaderSourceCode("shaders/basic.frag", &shaderSource); - if (shaderSource == nullptr) - shaderSource = ""; - - glShaderSource(frag, 1, &shaderSource, NULL); - glCompileShader(frag); - glGetShaderiv(frag, GL_COMPILE_STATUS, &success); - if (!success) - { - glGetShaderInfoLog(frag, 512, NULL, errorBuf); - std::cerr << "Fragment shader compilation error" << std::endl << errorBuf << std::endl; - glfwSetWindowShouldClose(window, 1); - return; - } - - // Link shaders into shader program - shader = glCreateProgram(); - glAttachShader(shader, vert); - glAttachShader(shader, frag); - glLinkProgram(shader); - glGetProgramiv(shader, GL_LINK_STATUS, &success); - if (!success) - { - glGetProgramInfoLog(shader, 512, NULL, errorBuf); - std::cerr << "Shader program linking error" << std::endl << errorBuf << std::endl; - glfwSetWindowShouldClose(window, 1); - return; - } - - glDeleteShader(frag); - glDeleteShader(vert); + shader = new Shader("shaders/basic.vert", "shaders/basic.frag"); // Generate buffers glGenVertexArrays(1, &VAO); @@ -130,7 +76,8 @@ void PlotWindow::Destroy() glDeleteBuffers(1, &VBO); glDeleteVertexArrays(1, &VAO); - glDeleteProgram(shader); + delete shader; + shader = nullptr; glfwDestroyWindow(window); } @@ -148,7 +95,7 @@ void PlotWindow::Clear() void PlotWindow::Display() { - glUseProgram(shader); + shader->Use(); glBindVertexArray(VAO); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); @@ -161,23 +108,3 @@ void PlotWindow::FramebufferSizeCallback(GLFWwindow* window, int w, int h) glfwMakeContextCurrent(window); glViewport(0, 0, w, h); } - -void PlotWindow::LoadShaderSourceCode(const char* path, char** buffer) -{ - std::ifstream file(path); - if (!file.good()) - { - std::cerr << "File not found: " << path << std::endl; - *buffer = nullptr; - return; - } - - std::string code((std::istreambuf_iterator(file)), std::istreambuf_iterator()); - size_t len = code.size(); - *buffer = (char*)malloc(len + sizeof(char)); - if (*buffer == nullptr) - return; - - memcpy_s(*buffer, len, code.c_str(), len); - (*buffer)[len] = '\0'; -} diff --git a/src/PlotWindow.hpp b/src/PlotWindow.hpp index 3eff205..d087ad0 100644 --- a/src/PlotWindow.hpp +++ b/src/PlotWindow.hpp @@ -5,11 +5,9 @@ #include #include -#pragma once +#include "Shader.hpp" typedef unsigned int BufferObject; -typedef unsigned int Shader; -typedef unsigned int ShaderProgram; class PlotWindow { @@ -28,10 +26,8 @@ private: GLFWwindow* window; static void FramebufferSizeCallback(GLFWwindow* window, int w, int h); - static void LoadShaderSourceCode(const char* path, char** buffer); - Shader vert, frag; - ShaderProgram shader; + Shader* shader; BufferObject VAO, VBO, EBO; float vertices[4 * 3 * 4]; diff --git a/src/Shader.cpp b/src/Shader.cpp new file mode 100644 index 0000000..4c92c09 --- /dev/null +++ b/src/Shader.cpp @@ -0,0 +1,99 @@ +#include "Shader.hpp" + +#include +#include + +Shader::Shader(const char* vertShaderPath, const char* fragShaderPath) : + program(0) +{ + int success; + char errorBuffer[512]; + + // Create Vertex shader + unsigned int vert; + vert = glCreateShader(GL_VERTEX_SHADER); + char* sourceBuffer; + LoadShaderSource(vertShaderPath, &sourceBuffer); + if (sourceBuffer == nullptr) + sourceBuffer = ""; + + glShaderSource(vert, 1, &sourceBuffer, NULL); + glCompileShader(vert); + glGetShaderiv(vert, GL_COMPILE_STATUS, &success); + if (!success) + { + glGetShaderInfoLog(vert, 512, NULL, errorBuffer); + std::cerr << vertShaderPath << std::endl << errorBuffer << std::endl; + return; + } + + // Create Fragment shader + unsigned int frag; + frag = glCreateShader(GL_FRAGMENT_SHADER); + LoadShaderSource(fragShaderPath, &sourceBuffer); + if (sourceBuffer == nullptr) + sourceBuffer = ""; + + glShaderSource(frag, 1, &sourceBuffer, NULL); + glCompileShader(frag); + glGetShaderiv(frag, GL_COMPILE_STATUS, &success); + if (!success) + { + glGetShaderInfoLog(frag, 512, NULL, errorBuffer); + std::cerr << fragShaderPath << std::endl << errorBuffer << std::endl; + return; + } + + // Link shaders + program = glCreateProgram(); + glAttachShader(program, vert); + glAttachShader(program, frag); + glLinkProgram(program); + glGetProgramiv(program, GL_LINK_STATUS, &success); + if (!success) + { + glGetProgramInfoLog(program, 512, NULL, errorBuffer); + std::cerr << errorBuffer << std::endl; + return; + } + + glDeleteShader(frag); + glDeleteShader(vert); +} + +Shader::~Shader() +{ + glDeleteProgram(program); +} + +void Shader::Use() +{ + glUseProgram(program); +} + +void Shader::LoadShaderSource(const char* filepath, char** buffer) +{ + std::ifstream file(filepath); + if (!file.good()) + { + std::cerr << "Failed to open file: " << filepath << std::endl; + *buffer = nullptr; + return; + } + + std::string source( + (std::istreambuf_iterator(file)), + std::istreambuf_iterator() + ); + size_t len = source.size(); + + *buffer = (char*)malloc(len + sizeof(char)); + if (*buffer == nullptr) + { + std::cerr << "Failed to allocate memory for shader source code" << std::endl; + return; + } + + memcpy_s(*buffer, len, source.c_str(), len); + (*buffer)[len] = '\0'; +} diff --git a/src/Shader.hpp b/src/Shader.hpp new file mode 100644 index 0000000..3437c96 --- /dev/null +++ b/src/Shader.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include + +typedef unsigned int ProgramID; + +class Shader +{ +public: + Shader(const char* vertShaderPath, const char* fragShaderPath); + ~Shader(); + + void Use(); + +private: + + static void LoadShaderSource(const char* filepath, char** buffer); + +private: + ProgramID program; +}; \ No newline at end of file