Changed Texture::bind and Shader::bind to be static functions, that can accept a null argument

This commit is contained in:
Laurent Gomila 2013-01-08 22:24:43 +01:00
parent 38da3f4338
commit 91e1743516
5 changed files with 125 additions and 88 deletions

View file

@ -391,10 +391,7 @@ void RenderTarget::applyTransform(const Transform& transform)
////////////////////////////////////////////////////////////
void RenderTarget::applyTexture(const Texture* texture)
{
if (texture)
texture->bind(Texture::Pixels);
else
glCheck(glBindTexture(GL_TEXTURE_2D, 0));
Texture::bind(texture, Texture::Pixels);
m_cache.lastTextureId = texture ? texture->m_cacheId : 0;
}
@ -403,10 +400,7 @@ void RenderTarget::applyTexture(const Texture* texture)
////////////////////////////////////////////////////////////
void RenderTarget::applyShader(const Shader* shader)
{
if (shader)
shader->bind();
else
glCheck(glUseProgramObjectARB(0));
Shader::bind(shader);
}
} // namespace sf

View file

@ -410,21 +410,26 @@ void Shader::setParameter(const std::string& name, CurrentTextureType)
////////////////////////////////////////////////////////////
void Shader::bind() const
void Shader::bind(const Shader* shader)
{
if (m_shaderProgram)
{
ensureGlContext();
ensureGlContext();
if (shader && shader->m_shaderProgram)
{
// Enable the program
glCheck(glUseProgramObjectARB(m_shaderProgram));
glCheck(glUseProgramObjectARB(shader->m_shaderProgram));
// Bind the textures
bindTextures();
shader->bindTextures();
// Bind the current texture
if (m_currentTexture != -1)
glCheck(glUniform1iARB(m_currentTexture, 0));
if (shader->m_currentTexture != -1)
glCheck(glUniform1iARB(shader->m_currentTexture, 0));
}
else
{
// Bind no shader
glCheck(glUseProgramObjectARB(0));
}
}
@ -550,7 +555,7 @@ void Shader::bindTextures() const
GLint index = static_cast<GLsizei>(i + 1);
glCheck(glUniform1iARB(it->first, index));
glCheck(glActiveTextureARB(GL_TEXTURE0_ARB + index));
it->second->bind();
Texture::bind(it->second);
++it;
}

View file

@ -373,45 +373,6 @@ void Texture::update(const Window& window, unsigned int x, unsigned int y)
}
////////////////////////////////////////////////////////////
void Texture::bind(CoordinateType coordinateType) const
{
// Bind the texture
glCheck(glBindTexture(GL_TEXTURE_2D, m_texture));
// Check if we need to define a special texture matrix
if (m_texture && ((coordinateType == Pixels) || m_pixelsFlipped))
{
GLfloat matrix[16] = {1.f, 0.f, 0.f, 0.f,
0.f, 1.f, 0.f, 0.f,
0.f, 0.f, 1.f, 0.f,
0.f, 0.f, 0.f, 1.f};
// If non-normalized coordinates (= pixels) are requested, we need to
// setup scale factors that convert the range [0 .. size] to [0 .. 1]
if (coordinateType == Pixels)
{
matrix[0] = 1.f / m_actualSize.x;
matrix[5] = 1.f / m_actualSize.y;
}
// If pixels are flipped we must invert the Y axis
if (m_pixelsFlipped)
{
matrix[5] = -matrix[5];
matrix[13] = static_cast<float>(m_size.y / m_actualSize.y);
}
// Load the matrix
glCheck(glMatrixMode(GL_TEXTURE));
glCheck(glLoadMatrixf(matrix));
// Go back to model-view mode (sf::RenderTarget relies on it)
glCheck(glMatrixMode(GL_MODELVIEW));
}
}
////////////////////////////////////////////////////////////
void Texture::setSmooth(bool smooth)
{
@ -470,6 +431,55 @@ bool Texture::isRepeated() const
}
////////////////////////////////////////////////////////////
void Texture::bind(const Texture* texture, CoordinateType coordinateType)
{
ensureGlContext();
if (texture && texture->m_texture)
{
// Bind the texture
glCheck(glBindTexture(GL_TEXTURE_2D, texture->m_texture));
// Check if we need to define a special texture matrix
if ((coordinateType == Pixels) || texture->m_pixelsFlipped)
{
GLfloat matrix[16] = {1.f, 0.f, 0.f, 0.f,
0.f, 1.f, 0.f, 0.f,
0.f, 0.f, 1.f, 0.f,
0.f, 0.f, 0.f, 1.f};
// If non-normalized coordinates (= pixels) are requested, we need to
// setup scale factors that convert the range [0 .. size] to [0 .. 1]
if (coordinateType == Pixels)
{
matrix[0] = 1.f / texture->m_actualSize.x;
matrix[5] = 1.f / texture->m_actualSize.y;
}
// If pixels are flipped we must invert the Y axis
if (texture->m_pixelsFlipped)
{
matrix[5] = -matrix[5];
matrix[13] = static_cast<float>(texture->m_size.y / texture->m_actualSize.y);
}
// Load the matrix
glCheck(glMatrixMode(GL_TEXTURE));
glCheck(glLoadMatrixf(matrix));
// Go back to model-view mode (sf::RenderTarget relies on it)
glCheck(glMatrixMode(GL_MODELVIEW));
}
}
else
{
// Bind no texture
glCheck(glBindTexture(GL_TEXTURE_2D, 0));
}
}
////////////////////////////////////////////////////////////
unsigned int Texture::getMaximumSize()
{