diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-01-24 13:16:05 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-01-24 13:16:05 +0000 |
commit | 12d38a0114657cc468e93f7eb0f97b2b1ac8f924 (patch) | |
tree | 7aec12c6469db3a46811ef6c49800b33a029292e /gfx/gl | |
parent | Allow physical objects to share meshes and textures (diff) | |
download | ilt-12d38a0114657cc468e93f7eb0f97b2b1ac8f924.tar.bz2 ilt-12d38a0114657cc468e93f7eb0f97b2b1ac8f924.tar.xz ilt-12d38a0114657cc468e93f7eb0f97b2b1ac8f924.zip |
Big tidy up of shader wrapper
Diffstat (limited to 'gfx/gl')
-rw-r--r-- | gfx/gl/shader.cpp | 59 | ||||
-rw-r--r-- | gfx/gl/shader.h | 32 |
2 files changed, 38 insertions, 53 deletions
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 <array>
#include <gfx/gl/shaders/fs-basicShader.h>
#include <gfx/gl/shaders/vs-basicShader.h>
#include <glm/glm.hpp>
#include <stdexcept>
+#include <string>
-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<GLchar, 1024> 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<unsigned char> 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 <GL/glew.h>
-#include <array>
-#include <special_members.hpp>
-#include <string>
+#include <glRef.hpp>
+#include <string_view>
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<GLuint, __glewCreateShader, __glewDeleteShader>;
+
+ Source(const std::basic_string_view<unsigned char> 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, __glewCreateProgram, __glewDeleteProgram>;
- GLuint m_program;
- std::array<GLuint, NUM_SHADERS> m_shaders;
- std::array<GLint, NUM_UNIFORMS> m_uniforms;
+ ProgramRef m_program;
+ GLint mvp_uniform, normal_uniform, lightDir_uniform;
};
#endif
|