diff options
Diffstat (limited to 'gfx/gl/shader.cpp')
-rw-r--r-- | gfx/gl/shader.cpp | 59 |
1 files changed, 21 insertions, 38 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!");
}
|