diff --git a/examples/debug/main.cpp b/examples/debug/main.cpp index 3fefb4d..411924a 100644 --- a/examples/debug/main.cpp +++ b/examples/debug/main.cpp @@ -86,12 +86,9 @@ int main(int argc, char** argv) oglu::ClearScreen(GL_COLOR_BUFFER_BIT, oglu::Color(0.29f, 0.13f, 0.23f)); - crate->BindAs(0); - opengl->BindAs(1); - shader->Use(); - shader->SetUniform("texture1", 0); - shader->SetUniform("texture2", 1); + shader->SetUniform("texture1", crate, 0); + shader->SetUniform("texture2", opengl, 1); square->BindAndDraw(); diff --git a/include/shader.hpp b/include/shader.hpp index d458f9e..f4ffd0b 100644 --- a/include/shader.hpp +++ b/include/shader.hpp @@ -281,6 +281,34 @@ namespace oglu */ void SetUniform(GLint location, const Color& v0, bool ignoreAlpha = false); + /** + * @brief Set uniform sampler2D. + * + * Activates and binds the given texture, then sets the uniform. + * Note: In many cases, using AbstractTexture::BindAs(GLbyte index) to activate and bind the textures + * seperately and then using SetUniform(GLint location, GLint v0) to set the uniform might + * be more efficient. + * + * @param[in] name Name of the uniform + * @param[in] v0 Value to set the uniform to + * @param[in] index Index of the texture unit + */ + void SetUniform(const GLchar* name, const Texture& v0, GLbyte index = 0); + + /** + * @brief Set uniform color. + * + * Activates and binds the given texture, then sets the uniform + * Note: In many cases, using AbstractTexture::BindAs(GLbyte index) to activate and bind the textures + * seperately and then using SetUniform(const GLchar* name, GLint v0) to set the uniform might + * be more efficient. + * + * @param[in] location Location of the uniform + * @param[in] v0 Value to set the uniform to + * @param[in] ignoreAlpha Index of the texture unit + */ + void SetUniform(GLint location, const Texture& v0, GLbyte index = 0); + /** * @brief Set array of uniform float. * diff --git a/src/shader.cpp b/src/shader.cpp index f3edb4d..9e3e6d3 100644 --- a/src/shader.cpp +++ b/src/shader.cpp @@ -6,6 +6,7 @@ #include #include +#include namespace oglu { @@ -235,6 +236,17 @@ namespace oglu glUniform4f(location, v0.r, v0.g, v0.b, v0.a); } + void AbstractShader::SetUniform(const GLchar* name, const Texture& v0, GLbyte index) + { + SetUniform(glGetUniformLocation(program, name), v0, index); + } + + void AbstractShader::SetUniform(GLint location, const Texture& v0, GLbyte index) + { + v0->BindAs(index); + glUniform1i(location, index); + } + void AbstractShader::SetUniform1fv(const GLchar* name, GLsizei count, const GLfloat* value) { glUniform1fv(glGetUniformLocation(program, name), count, value);