Improved Binding of textures

This commit is contained in:
Robert 2021-01-21 16:27:41 +01:00
parent 8174ebaee4
commit f1f076acf4
5 changed files with 27 additions and 4 deletions

View file

@ -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);

View file

@ -14,6 +14,9 @@
namespace oglu
{
class Color;
class AbstractTexture;
typedef std::shared_ptr<AbstractTexture> Texture;
/**
* @brief An object representing an OpenGL Shader Program.

View file

@ -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.
*/

View file

@ -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);
}

View file

@ -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);