From 4fe91a12e2e8fc15e8abc748cef7a84698c75ee9 Mon Sep 17 00:00:00 2001 From: Robert Date: Thu, 21 Jan 2021 01:00:42 +0100 Subject: [PATCH] shader class has improved ownership handling stuff garbage thingies --- examples/debug/main.cpp | 4 +- include/shader.hpp | 14 +++- src/shader.cpp | 166 +++++++++++++++++++++------------------- 3 files changed, 101 insertions(+), 83 deletions(-) diff --git a/examples/debug/main.cpp b/examples/debug/main.cpp index 341be0a..118843a 100644 --- a/examples/debug/main.cpp +++ b/examples/debug/main.cpp @@ -55,10 +55,10 @@ int main(int argc, char** argv) { 1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)) } }; - oglu::Shader* shader; + oglu::Shader shader; try { - shader = new oglu::Shader("shaders/vertexShader.vert", "shaders/fragmentShader.frag"); + shader = oglu::MakeShader("shaders/vertexShader.vert", "shaders/fragmentShader.frag"); } catch (const std::exception& e) { diff --git a/include/shader.hpp b/include/shader.hpp index 203d9b9..9894114 100644 --- a/include/shader.hpp +++ b/include/shader.hpp @@ -1,6 +1,7 @@ #ifndef SHADER_HPP #define SHADER_HPP +#include #include #include @@ -8,11 +9,12 @@ namespace oglu { class Color; - class OGLU_API Shader + class OGLU_API AbstractShader { public: - Shader(const char* vertexShaderFile, const char* fragmentShaderFile); - ~Shader(); + friend std::shared_ptr OGLU_API MakeShader(const char* vertexShaderFile, const char* fragmentShaderFile); + AbstractShader(const AbstractShader& other); + ~AbstractShader(); void Use(); @@ -123,11 +125,15 @@ namespace oglu #pragma endregion Uniforms private: + AbstractShader(const char* vertexShaderFile, const char* fragmentShaderFile); + void LoadShaderSource(const char* filename, char** buffer); private: - GLuint vertexShader, fragmentShader, program; + GLuint program; }; + + typedef std::shared_ptr Shader; } #endif \ No newline at end of file diff --git a/src/shader.cpp b/src/shader.cpp index 9b1d041..ad77699 100644 --- a/src/shader.cpp +++ b/src/shader.cpp @@ -9,14 +9,26 @@ namespace oglu { - Shader::Shader(const char* vertexShaderFile, const char* fragmentShaderFile) : - vertexShader(0), fragmentShader(0) + AbstractShader::AbstractShader(const AbstractShader& other) : + program(other.program) + { + + } + + std::shared_ptr MakeShader(const char* vertexShaderFile, const char* fragmentShaderFile) + { + AbstractShader* tmp = new AbstractShader(vertexShaderFile, fragmentShaderFile); + return std::shared_ptr(tmp); + } + + AbstractShader::AbstractShader(const char* vertexShaderFile, const char* fragmentShaderFile) : + program(0) { // Load vertex shader char* source = nullptr; LoadShaderSource(vertexShaderFile, &source); - vertexShader = glCreateShader(GL_VERTEX_SHADER); + GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertexShader, 1, &source, NULL); glCompileShader(vertexShader); @@ -34,7 +46,7 @@ namespace oglu // Load Fragment shader LoadShaderSource(fragmentShaderFile, &source); - fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); + GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragmentShader, 1, &source, NULL); glCompileShader(fragmentShader); @@ -65,148 +77,148 @@ namespace oglu glDeleteShader(vertexShader); } - Shader::~Shader() + AbstractShader::~AbstractShader() { - + glDeleteProgram(program); } - void Shader::Use() + void AbstractShader::Use() { glUseProgram(program); } - GLint Shader::GetUniformLocation(const GLchar* name) + GLint AbstractShader::GetUniformLocation(const GLchar* name) { return glGetUniformLocation(program, name); } #pragma region Uniforms - void Shader::SetUniform(const GLchar* name, GLfloat v0) + void AbstractShader::SetUniform(const GLchar* name, GLfloat v0) { glUniform1f(glGetUniformLocation(program, name), v0); } - void Shader::SetUniform(GLint location, GLfloat v0) + void AbstractShader::SetUniform(GLint location, GLfloat v0) { glUniform1f(location, v0); } - void Shader::SetUniform(const GLchar* name, GLfloat v0, GLfloat v1) + void AbstractShader::SetUniform(const GLchar* name, GLfloat v0, GLfloat v1) { glUniform2f(glGetUniformLocation(program, name), v0, v1); } - void Shader::SetUniform(GLint location, GLfloat v0, GLfloat v1) + void AbstractShader::SetUniform(GLint location, GLfloat v0, GLfloat v1) { glUniform2f(location, v0, v1); } - void Shader::SetUniform(const GLchar* name, GLfloat v0, GLfloat v1, GLfloat v2) + void AbstractShader::SetUniform(const GLchar* name, GLfloat v0, GLfloat v1, GLfloat v2) { glUniform3f(glGetUniformLocation(program, name), v0, v1, v2); } - void Shader::SetUniform(GLint location, GLfloat v0, GLfloat v1, GLfloat v2) + void AbstractShader::SetUniform(GLint location, GLfloat v0, GLfloat v1, GLfloat v2) { glUniform3f(location, v0, v1, v2); } - void Shader::SetUniform(const GLchar* name, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3) + void AbstractShader::SetUniform(const GLchar* name, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3) { glUniform4f(glGetUniformLocation(program, name), v0, v1, v2, v3); } - void Shader::SetUniform(GLint location, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3) + void AbstractShader::SetUniform(GLint location, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3) { glUniform4f(location, v0, v1, v2, v3); } - void Shader::SetUniform(const GLchar* name, GLint v0) + void AbstractShader::SetUniform(const GLchar* name, GLint v0) { glUniform1i(glGetUniformLocation(program, name), v0); } - void Shader::SetUniform(GLint location, GLint v0) + void AbstractShader::SetUniform(GLint location, GLint v0) { glUniform1i(location, v0); } - void Shader::SetUniform(const GLchar* name, GLint v0, GLint v1) + void AbstractShader::SetUniform(const GLchar* name, GLint v0, GLint v1) { glUniform2i(glGetUniformLocation(program, name), v0, v1); } - void Shader::SetUniform(GLint location, GLint v0, GLint v1) + void AbstractShader::SetUniform(GLint location, GLint v0, GLint v1) { glUniform2i(location, v0, v1); } - void Shader::SetUniform(const GLchar* name, GLint v0, GLint v1, GLint v2) + void AbstractShader::SetUniform(const GLchar* name, GLint v0, GLint v1, GLint v2) { glUniform3i(glGetUniformLocation(program, name), v0, v1, v2); } - void Shader::SetUniform(GLint location, GLint v0, GLint v1, GLint v2) + void AbstractShader::SetUniform(GLint location, GLint v0, GLint v1, GLint v2) { glUniform3i(location, v0, v1, v2); } - void Shader::SetUniform(const GLchar* name, GLint v0, GLint v1, GLint v2, GLint v3) + void AbstractShader::SetUniform(const GLchar* name, GLint v0, GLint v1, GLint v2, GLint v3) { glUniform4i(glGetUniformLocation(program, name), v0, v1, v2, v3); } - void Shader::SetUniform(GLint location, GLint v0, GLint v1, GLint v2, GLint v3) + void AbstractShader::SetUniform(GLint location, GLint v0, GLint v1, GLint v2, GLint v3) { glUniform4i(location, v0, v1, v2, v3); } - void Shader::SetUniform(const GLchar* name, GLuint v0) + void AbstractShader::SetUniform(const GLchar* name, GLuint v0) { glUniform1ui(glGetUniformLocation(program, name), v0); } - void Shader::SetUniform(GLint location, GLuint v0) + void AbstractShader::SetUniform(GLint location, GLuint v0) { glUniform1ui(location, v0); } - void Shader::SetUniform(const GLchar* name, GLuint v0, GLuint v1) + void AbstractShader::SetUniform(const GLchar* name, GLuint v0, GLuint v1) { glUniform2ui(glGetUniformLocation(program, name), v0, v1); } - void Shader::SetUniform(GLint location, GLuint v0, GLuint v1) + void AbstractShader::SetUniform(GLint location, GLuint v0, GLuint v1) { glUniform2ui(location, v0, v1); } - void Shader::SetUniform(const GLchar* name, GLuint v0, GLuint v1, GLuint v2) + void AbstractShader::SetUniform(const GLchar* name, GLuint v0, GLuint v1, GLuint v2) { glUniform3ui(glGetUniformLocation(program, name), v0, v1, v2); } - void Shader::SetUniform(GLint location, GLuint v0, GLuint v1, GLuint v2) + void AbstractShader::SetUniform(GLint location, GLuint v0, GLuint v1, GLuint v2) { glUniform3ui(location, v0, v1, v2); } - void Shader::SetUniform(const GLchar* name, GLuint v0, GLuint v1, GLuint v2, GLuint v3) + void AbstractShader::SetUniform(const GLchar* name, GLuint v0, GLuint v1, GLuint v2, GLuint v3) { glUniform4ui(glGetUniformLocation(program, name), v0, v1, v2, v3); } - void Shader::SetUniform(GLint location, GLuint v0, GLuint v1, GLuint v2, GLuint v3) + void AbstractShader::SetUniform(GLint location, GLuint v0, GLuint v1, GLuint v2, GLuint v3) { glUniform4ui(location, v0, v1, v2, v3); } - void Shader::SetUniform(const GLchar* name, const Color* v0, bool ignoreAlpha) + void AbstractShader::SetUniform(const GLchar* name, const Color* v0, bool ignoreAlpha) { SetUniform(glGetUniformLocation(program, name), v0, ignoreAlpha); } - void Shader::SetUniform(GLint location, const Color* v0, bool ignoreAlpha) + void AbstractShader::SetUniform(GLint location, const Color* v0, bool ignoreAlpha) { if (ignoreAlpha) glUniform3f(location, v0->r, v0->g, v0->b); @@ -214,218 +226,218 @@ namespace oglu glUniform4f(location, v0->r, v0->g, v0->b, v0->a); } - void Shader::SetUniform1fv(const GLchar* name, GLsizei count, const GLfloat* value) + void AbstractShader::SetUniform1fv(const GLchar* name, GLsizei count, const GLfloat* value) { glUniform1fv(glGetUniformLocation(program, name), count, value); } - void Shader::SetUniform1fv(GLint location, GLsizei count, const GLfloat* value) + void AbstractShader::SetUniform1fv(GLint location, GLsizei count, const GLfloat* value) { glUniform1fv(location, count, value); } - void Shader::SetUniform2fv(const GLchar* name, GLsizei count, const GLfloat* value) + void AbstractShader::SetUniform2fv(const GLchar* name, GLsizei count, const GLfloat* value) { glUniform2fv(glGetUniformLocation(program, name), count, value); } - void Shader::SetUniform2fv(GLint location, GLsizei count, const GLfloat* value) + void AbstractShader::SetUniform2fv(GLint location, GLsizei count, const GLfloat* value) { glUniform2fv(location, count, value); } - void Shader::SetUniform3fv(const GLchar* name, GLsizei count, const GLfloat* value) + void AbstractShader::SetUniform3fv(const GLchar* name, GLsizei count, const GLfloat* value) { glUniform3fv(glGetUniformLocation(program, name), count, value); } - void Shader::SetUniform3fv(GLint location, GLsizei count, const GLfloat* value) + void AbstractShader::SetUniform3fv(GLint location, GLsizei count, const GLfloat* value) { glUniform3fv(location, count, value); } - void Shader::SetUniform4fv(const GLchar* name, GLsizei count, const GLfloat* value) + void AbstractShader::SetUniform4fv(const GLchar* name, GLsizei count, const GLfloat* value) { glUniform4fv(glGetUniformLocation(program, name), count, value); } - void Shader::SetUniform4fv(GLint location, GLsizei count, const GLfloat* value) + void AbstractShader::SetUniform4fv(GLint location, GLsizei count, const GLfloat* value) { glUniform4fv(location, count, value); } - void Shader::SetUniform1iv(const GLchar* name, GLsizei count, const GLint* value) + void AbstractShader::SetUniform1iv(const GLchar* name, GLsizei count, const GLint* value) { glUniform1iv(glGetUniformLocation(program, name), count, value); } - void Shader::SetUniform1iv(GLint location, GLsizei count, const GLint* value) + void AbstractShader::SetUniform1iv(GLint location, GLsizei count, const GLint* value) { glUniform1iv(location, count, value); } - void Shader::SetUniform2iv(const GLchar* name, GLsizei count, const GLint* value) + void AbstractShader::SetUniform2iv(const GLchar* name, GLsizei count, const GLint* value) { glUniform2iv(glGetUniformLocation(program, name), count, value); } - void Shader::SetUniform2iv(GLint location, GLsizei count, const GLint* value) + void AbstractShader::SetUniform2iv(GLint location, GLsizei count, const GLint* value) { glUniform2iv(location, count, value); } - void Shader::SetUniform3iv(const GLchar* name, GLsizei count, const GLint* value) + void AbstractShader::SetUniform3iv(const GLchar* name, GLsizei count, const GLint* value) { glUniform3iv(glGetUniformLocation(program, name), count, value); } - void Shader::SetUniform3iv(GLint location, GLsizei count, const GLint* value) + void AbstractShader::SetUniform3iv(GLint location, GLsizei count, const GLint* value) { glUniform3iv(location, count, value); } - void Shader::SetUniform4iv(const GLchar* name, GLsizei count, const GLint* value) + void AbstractShader::SetUniform4iv(const GLchar* name, GLsizei count, const GLint* value) { glUniform4iv(glGetUniformLocation(program, name), count, value); } - void Shader::SetUniform4iv(GLint location, GLsizei count, const GLint* value) + void AbstractShader::SetUniform4iv(GLint location, GLsizei count, const GLint* value) { glUniform4iv(location, count, value); } - void Shader::SetUniform1uiv(const GLchar* name, GLsizei count, const GLuint* value) + void AbstractShader::SetUniform1uiv(const GLchar* name, GLsizei count, const GLuint* value) { glUniform1uiv(glGetUniformLocation(program, name), count, value); } - void Shader::SetUniform1uiv(GLint location, GLsizei count, const GLuint* value) + void AbstractShader::SetUniform1uiv(GLint location, GLsizei count, const GLuint* value) { glUniform1uiv(location, count, value); } - void Shader::SetUniform2uiv(const GLchar* name, GLsizei count, const GLuint* value) + void AbstractShader::SetUniform2uiv(const GLchar* name, GLsizei count, const GLuint* value) { glUniform2uiv(glGetUniformLocation(program, name), count, value); } - void Shader::SetUniform2uiv(GLint location, GLsizei count, const GLuint* value) + void AbstractShader::SetUniform2uiv(GLint location, GLsizei count, const GLuint* value) { glUniform2uiv(location, count, value); } - void Shader::SetUniform3uiv(const GLchar* name, GLsizei count, const GLuint* value) + void AbstractShader::SetUniform3uiv(const GLchar* name, GLsizei count, const GLuint* value) { glUniform3uiv(glGetUniformLocation(program, name), count, value); } - void Shader::SetUniform3uiv(GLint location, GLsizei count, const GLuint* value) + void AbstractShader::SetUniform3uiv(GLint location, GLsizei count, const GLuint* value) { glUniform3uiv(location, count, value); } - void Shader::SetUniform4uiv(const GLchar* name, GLsizei count, const GLuint* value) + void AbstractShader::SetUniform4uiv(const GLchar* name, GLsizei count, const GLuint* value) { glUniform4uiv(glGetUniformLocation(program, name), count, value); } - void Shader::SetUniform4uiv(GLint location, GLsizei count, const GLuint* value) + void AbstractShader::SetUniform4uiv(GLint location, GLsizei count, const GLuint* value) { glUniform4uiv(location, count, value); } - void Shader::SetUniformMatrix2fv(const GLchar* name, GLsizei count, GLboolean transpose, const GLfloat* value) + void AbstractShader::SetUniformMatrix2fv(const GLchar* name, GLsizei count, GLboolean transpose, const GLfloat* value) { glUniformMatrix2fv(glGetUniformLocation(program, name), count, transpose, value); } - void Shader::SetUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat* value) + void AbstractShader::SetUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat* value) { glUniformMatrix2fv(location, count, transpose, value); } - void Shader::SetUniformMatrix3fv(const GLchar* name, GLsizei count, GLboolean transpose, const GLfloat* value) + void AbstractShader::SetUniformMatrix3fv(const GLchar* name, GLsizei count, GLboolean transpose, const GLfloat* value) { glUniformMatrix3fv(glGetUniformLocation(program, name), count, transpose, value); } - void Shader::SetUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat* value) + void AbstractShader::SetUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat* value) { glUniformMatrix3fv(location, count, transpose, value); } - void Shader::SetUniformMatrix4fv(const GLchar* name, GLsizei count, GLboolean transpose, const GLfloat* value) + void AbstractShader::SetUniformMatrix4fv(const GLchar* name, GLsizei count, GLboolean transpose, const GLfloat* value) { glUniformMatrix4fv(glGetUniformLocation(program, name), count, transpose, value); } - void Shader::SetUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat* value) + void AbstractShader::SetUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat* value) { glUniformMatrix4fv(location, count, transpose, value); } - void Shader::SetUniformMatrix2x3fv(const GLchar* name, GLsizei count, GLboolean transpose, const GLfloat* value) + void AbstractShader::SetUniformMatrix2x3fv(const GLchar* name, GLsizei count, GLboolean transpose, const GLfloat* value) { glUniformMatrix2x3fv(glGetUniformLocation(program, name), count, transpose, value); } - void Shader::SetUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat* value) + void AbstractShader::SetUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat* value) { glUniformMatrix2x3fv(location, count, transpose, value); } - void Shader::SetUniformMatrix3x2fv(const GLchar* name, GLsizei count, GLboolean transpose, const GLfloat* value) + void AbstractShader::SetUniformMatrix3x2fv(const GLchar* name, GLsizei count, GLboolean transpose, const GLfloat* value) { glUniformMatrix3x2fv(glGetUniformLocation(program, name), count, transpose, value); } - void Shader::SetUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat* value) + void AbstractShader::SetUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat* value) { glUniformMatrix3x2fv(location, count, transpose, value); } - void Shader::SetUniformMatrix2x4fv(const GLchar* name, GLsizei count, GLboolean transpose, const GLfloat* value) + void AbstractShader::SetUniformMatrix2x4fv(const GLchar* name, GLsizei count, GLboolean transpose, const GLfloat* value) { glUniformMatrix2x4fv(glGetUniformLocation(program, name), count, transpose, value); } - void Shader::SetUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat* value) + void AbstractShader::SetUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat* value) { glUniformMatrix2x4fv(location, count, transpose, value); } - void Shader::SetUniformMatrix4x2fv(const GLchar* name, GLsizei count, GLboolean transpose, const GLfloat* value) + void AbstractShader::SetUniformMatrix4x2fv(const GLchar* name, GLsizei count, GLboolean transpose, const GLfloat* value) { glUniformMatrix4x2fv(glGetUniformLocation(program, name), count, transpose, value); } - void Shader::SetUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat* value) + void AbstractShader::SetUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat* value) { glUniformMatrix4x2fv(location, count, transpose, value); } - void Shader::SetUniformMatrix3x4fv(const GLchar* name, GLsizei count, GLboolean transpose, const GLfloat* value) + void AbstractShader::SetUniformMatrix3x4fv(const GLchar* name, GLsizei count, GLboolean transpose, const GLfloat* value) { glUniformMatrix3x4fv(glGetUniformLocation(program, name), count, transpose, value); } - void Shader::SetUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat* value) + void AbstractShader::SetUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat* value) { glUniformMatrix3x4fv(location, count, transpose, value); } - void Shader::SetUniformMatrix4x3fv(const GLchar* name, GLsizei count, GLboolean transpose, const GLfloat* value) + void AbstractShader::SetUniformMatrix4x3fv(const GLchar* name, GLsizei count, GLboolean transpose, const GLfloat* value) { glUniformMatrix4x3fv(glGetUniformLocation(program, name), count, transpose, value); } - void Shader::SetUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat* value) + void AbstractShader::SetUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat* value) { glUniformMatrix4x3fv(location, count, transpose, value); } #pragma endregion Uniforms - void Shader::LoadShaderSource(const char* filename, char** buffer) + void AbstractShader::LoadShaderSource(const char* filename, char** buffer) { std::ifstream file(filename); if (!file.good())