From 12d38a0114657cc468e93f7eb0f97b2b1ac8f924 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 24 Jan 2021 13:16:05 +0000 Subject: Big tidy up of shader wrapper --- gfx/gl/shader.cpp | 59 ++++++++++++++++++++----------------------------------- gfx/gl/shader.h | 32 ++++++++++++++++-------------- 2 files changed, 38 insertions(+), 53 deletions(-) (limited to 'gfx/gl') diff --git a/gfx/gl/shader.cpp b/gfx/gl/shader.cpp index 33fdf61..e59d431 100644 --- a/gfx/gl/shader.cpp +++ b/gfx/gl/shader.cpp @@ -1,20 +1,16 @@ #include "shader.h" #include "transform.h" +#include #include #include #include #include +#include -Shader::Shader(const std::string &) : - m_program {glCreateProgram()}, m_shaders {CreateShader( - (GLchar *)(basicShader_vs), basicShader_vs_len, GL_VERTEX_SHADER), - CreateShader( - (GLchar *)basicShader_fs, basicShader_fs_len, GL_FRAGMENT_SHADER)}, - m_uniforms {} +Shader::Shader() : mvp_uniform {}, normal_uniform {}, lightDir_uniform {} { - for (auto m_shader : m_shaders) { - glAttachShader(m_program, m_shader); - } + glAttachShader(m_program, Source {{basicShader_vs, basicShader_vs_len}, GL_VERTEX_SHADER}.id); + glAttachShader(m_program, Source {{basicShader_fs, basicShader_fs_len}, GL_FRAGMENT_SHADER}.id); glBindAttribLocation(m_program, 0, "position"); glBindAttribLocation(m_program, 1, "texCoord"); @@ -26,18 +22,9 @@ Shader::Shader(const std::string &) : glValidateProgram(m_program); CheckShaderError(m_program, GL_VALIDATE_STATUS, true, "Invalid shader program"); - m_uniforms = {glGetUniformLocation(m_program, "MVP"), glGetUniformLocation(m_program, "Normal"), - glGetUniformLocation(m_program, "lightDirection")}; -} - -Shader::~Shader() -{ - for (auto m_shader : m_shaders) { - glDetachShader(m_program, m_shader); - glDeleteShader(m_shader); - } - - glDeleteProgram(m_program); + mvp_uniform = glGetUniformLocation(m_program, "MVP"); + normal_uniform = glGetUniformLocation(m_program, "Normal"); + lightDir_uniform = glGetUniformLocation(m_program, "lightDirection"); } void @@ -52,13 +39,13 @@ Shader::Update(const Transform & transform, const Camera & camera) const glm::mat4 MVP = transform.GetMVP(camera); glm::mat4 Normal = transform.GetModel(); - glUniformMatrix4fv(m_uniforms[0], 1, GL_FALSE, &MVP[0][0]); - glUniformMatrix4fv(m_uniforms[1], 1, GL_FALSE, &Normal[0][0]); - glUniform3f(m_uniforms[2], 0.0F, 0.0F, 1.0F); + glUniformMatrix4fv(mvp_uniform, 1, GL_FALSE, &MVP[0][0]); + glUniformMatrix4fv(normal_uniform, 1, GL_FALSE, &Normal[0][0]); + glUniform3f(lightDir_uniform, 0.0F, 0.0F, 1.0F); } void -Shader::CheckShaderError(GLuint shader, GLuint flag, bool isProgram, const std::string & errorMessage) +Shader::CheckShaderError(GLuint shader, GLuint flag, bool isProgram, std::string_view errorMessage) { GLint success = 0; std::array error {}; @@ -78,23 +65,19 @@ Shader::CheckShaderError(GLuint shader, GLuint flag, bool isProgram, const std:: glGetShaderInfoLog(shader, error.size(), nullptr, error.data()); } - throw std::runtime_error {errorMessage + ": '" + std::string {error.data(), error.size()} + "'"}; + throw std::runtime_error {std::string {errorMessage} + ": '" + std::string {error.data(), error.size()} + "'"}; } } -GLuint -Shader::CreateShader(const GLchar * text, GLint len, unsigned int type) +Shader::Source::Source(const std::basic_string_view text, GLuint type) : + Source {(const GLchar *)(text.data()), (GLint)(text.length()), type} { - GLuint shader = glCreateShader(type); - - if (shader == 0) { - throw std::runtime_error {"Error compiling shader type " + std::to_string(type)}; - } - - glShaderSource(shader, 1, &text, &len); - glCompileShader(shader); +} - CheckShaderError(shader, GL_COMPILE_STATUS, false, "Error compiling shader!"); +Shader::Source::Source(const GLchar * text, GLint len, unsigned int type) : id {type} +{ + glShaderSource(id, 1, &text, &len); + glCompileShader(id); - return shader; + CheckShaderError(id, GL_COMPILE_STATUS, false, "Error compiling shader!"); } diff --git a/gfx/gl/shader.h b/gfx/gl/shader.h index 2072199..e253f50 100644 --- a/gfx/gl/shader.h +++ b/gfx/gl/shader.h @@ -2,34 +2,36 @@ #define SHADER_INCLUDED_H #include -#include -#include -#include +#include +#include class Camera; class Transform; class Shader { public: - explicit Shader(const std::string & fileName); - virtual ~Shader(); - - NO_COPY(Shader); - NO_MOVE(Shader); + Shader(); void Bind() const; void Update(const Transform & transform, const Camera & camera) const; private: - static constexpr unsigned int NUM_SHADERS = 2; - static constexpr unsigned int NUM_UNIFORMS = 3; + class Source { + public: + using ShaderRef = glRef; + + Source(const std::basic_string_view text, GLuint type); + Source(const GLchar * text, GLint len, GLuint type); + + ShaderRef id; + }; + + static void CheckShaderError(GLuint shader, GLuint flag, bool isProgram, std::string_view errorMessage); - static void CheckShaderError(GLuint shader, GLuint flag, bool isProgram, const std::string & errorMessage); - static GLuint CreateShader(const GLchar * text, GLint len, unsigned int type); + using ProgramRef = glRef; - GLuint m_program; - std::array m_shaders; - std::array m_uniforms; + ProgramRef m_program; + GLint mvp_uniform, normal_uniform, lightDir_uniform; }; #endif -- cgit v1.2.3