From f1f076acf4702363d780075c0d92279cb417fa7b Mon Sep 17 00:00:00 2001 From: Robert Date: Thu, 21 Jan 2021 16:27:41 +0100 Subject: [PATCH] Improved Binding of textures --- examples/debug/main.cpp | 6 ++---- include/shader.hpp | 3 +++ include/texture.hpp | 10 ++++++++++ src/shader.cpp | 6 ++++++ src/texture.cpp | 6 ++++++ 5 files changed, 27 insertions(+), 4 deletions(-) diff --git a/examples/debug/main.cpp b/examples/debug/main.cpp index 97f6f6c..3fefb4d 100644 --- a/examples/debug/main.cpp +++ b/examples/debug/main.cpp @@ -86,10 +86,8 @@ int main(int argc, char** argv) oglu::ClearScreen(GL_COLOR_BUFFER_BIT, oglu::Color(0.29f, 0.13f, 0.23f)); - oglu::ActiveTexture(0); - crate->Bind(); - oglu::ActiveTexture(1); - opengl->Bind(); + crate->BindAs(0); + opengl->BindAs(1); shader->Use(); shader->SetUniform("texture1", 0); diff --git a/include/shader.hpp b/include/shader.hpp index 2e84cdb..d458f9e 100644 --- a/include/shader.hpp +++ b/include/shader.hpp @@ -14,6 +14,9 @@ namespace oglu { class Color; + class AbstractTexture; + + typedef std::shared_ptr Texture; /** * @brief An object representing an OpenGL Shader Program. diff --git a/include/texture.hpp b/include/texture.hpp index 4806b97..31afb5e 100644 --- a/include/texture.hpp +++ b/include/texture.hpp @@ -59,6 +59,16 @@ namespace oglu */ void Bind(); + /** + * @brief Sets active texture and binds this texture. + * + * In order to use multiple textures in the same shader the active texture unit needs to be specified. + * This function first sets the active texture unit and then binds the texture. + * + * @param[in] index Index of the texture unit (Note: This index is actually an offset to @p GL_TEXTURE0) + */ + void BindAs(GLbyte index); + /** * @brief Unbind this texture. */ diff --git a/src/shader.cpp b/src/shader.cpp index edc4567..f3edb4d 100644 --- a/src/shader.cpp +++ b/src/shader.cpp @@ -40,6 +40,7 @@ namespace oglu glGetShaderInfoLog(vertexShader, 512, NULL, infoLog); std::string err = ("Failed to compile shader " + std::string(vertexShaderFile) + "\n" + infoLog); delete source; + glDeleteShader(vertexShader); throw std::runtime_error(err); } delete source; @@ -57,6 +58,8 @@ namespace oglu glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog); std::string err = ("Failed to compile shader " + std::string(fragmentShaderFile) + "\n" + infoLog); delete source; + glDeleteShader(vertexShader); + glDeleteShader(fragmentShader); throw std::runtime_error(err); } delete source; @@ -72,6 +75,9 @@ namespace oglu { glGetProgramInfoLog(program, 512, NULL, infoLog); std::string err = ("Failed to link program.\n" + std::string(infoLog)); + glDeleteShader(vertexShader); + glDeleteShader(fragmentShader); + glDeleteProgram(program); throw std::runtime_error(err); } diff --git a/src/texture.cpp b/src/texture.cpp index aa550e1..0948bba 100644 --- a/src/texture.cpp +++ b/src/texture.cpp @@ -68,6 +68,12 @@ namespace oglu glBindTexture(GL_TEXTURE_2D, texture); } + void AbstractTexture::BindAs(GLbyte index) + { + glActiveTexture(GL_TEXTURE0 + index); + glBindTexture(GL_TEXTURE_2D, texture); + } + void AbstractTexture::Unbind() { glBindTexture(GL_TEXTURE_2D, 0);