mirror of
https://github.com/SFML/SFML.git
synced 2024-11-25 12:51:05 +08:00
Minor modifications to shader and image activation
git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1426 4e206d99-4929-0410-ac5d-dfc041789085
This commit is contained in:
parent
341b499180
commit
76a76a784f
@ -493,11 +493,7 @@ void Image::Bind() const
|
||||
EnsureTextureUpdate();
|
||||
|
||||
// Bind it
|
||||
if (myTexture)
|
||||
{
|
||||
GLCheck(glEnable(GL_TEXTURE_2D));
|
||||
GLCheck(glBindTexture(GL_TEXTURE_2D, myTexture));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -58,6 +58,7 @@ void Renderer::Initialize()
|
||||
// Default render states
|
||||
GLCheck(glDisable(GL_LIGHTING));
|
||||
GLCheck(glDisable(GL_DEPTH_TEST));
|
||||
GLCheck(glEnable(GL_TEXTURE_2D));
|
||||
GLCheck(glEnable(GL_ALPHA_TEST));
|
||||
GLCheck(glAlphaFunc(GL_GREATER, 0));
|
||||
|
||||
@ -241,7 +242,7 @@ void Renderer::SetTexture(const Image* texture)
|
||||
if (texture)
|
||||
texture->Bind();
|
||||
else
|
||||
GLCheck(glDisable(GL_TEXTURE_2D));
|
||||
GLCheck(glBindTexture(GL_TEXTURE_2D, 0));
|
||||
|
||||
// Store it
|
||||
myTexture = texture;
|
||||
@ -254,17 +255,20 @@ void Renderer::SetTexture(const Image* texture)
|
||||
void Renderer::SetShader(const Shader* shader)
|
||||
{
|
||||
if ((shader != myShader) || !myShaderIsValid)
|
||||
{
|
||||
if (Shader::IsAvailable()) // to avoid calling possibly unsupported functions
|
||||
{
|
||||
// Apply the new shader
|
||||
if (shader)
|
||||
shader->Bind();
|
||||
else if (myShaderIsValid && myShader)
|
||||
myShader->Unbind();
|
||||
else
|
||||
GLCheck(glUseProgramObjectARB(0));
|
||||
|
||||
// Store it
|
||||
myShader = shader;
|
||||
myShaderIsValid = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -45,8 +45,7 @@ Shader::Shader() :
|
||||
myShaderProgram (0),
|
||||
myCurrentTexture(-1)
|
||||
{
|
||||
// Make sure that GLEW is initialized
|
||||
priv::EnsureGlewInit();
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -209,6 +208,8 @@ void Shader::SetParameter(const std::string& name, const Vector3f& v)
|
||||
////////////////////////////////////////////////////////////
|
||||
void Shader::SetTexture(const std::string& name, const Image& texture)
|
||||
{
|
||||
if (myShaderProgram)
|
||||
{
|
||||
// Check if there is a texture unit available
|
||||
GLint maxUnits;
|
||||
GLCheck(glGetIntegerv(GL_MAX_TEXTURE_COORDS_ARB, &maxUnits));
|
||||
@ -231,16 +232,15 @@ void Shader::SetTexture(const std::string& name, const Image& texture)
|
||||
myTextures[location] = &texture;
|
||||
else
|
||||
myCurrentTexture = location;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
void Shader::Bind() const
|
||||
{
|
||||
// Make sure that we have a valid program
|
||||
if (!myShaderProgram)
|
||||
return;
|
||||
|
||||
if (myShaderProgram)
|
||||
{
|
||||
// Enable the program
|
||||
GLCheck(glUseProgramObjectARB(myShaderProgram));
|
||||
|
||||
@ -261,30 +261,14 @@ void Shader::Bind() const
|
||||
// Bind the current texture
|
||||
if (myCurrentTexture != -1)
|
||||
GLCheck(glUniform1iARB(myCurrentTexture, 0));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
void Shader::Unbind() const
|
||||
{
|
||||
// First make sure that the program is currently bound
|
||||
GLhandleARB program = glGetHandleARB(GL_PROGRAM_OBJECT_ARB);
|
||||
if (!myShaderProgram || (myShaderProgram != program))
|
||||
return;
|
||||
|
||||
// Unbind the program
|
||||
GLCheck(glUseProgramObjectARB(0));
|
||||
|
||||
// Disable texture units
|
||||
for (std::size_t i = 0; i < myTextures.size(); ++i)
|
||||
{
|
||||
GLint index = static_cast<GLsizei>(i + 1);
|
||||
GLCheck(glActiveTextureARB(GL_TEXTURE0_ARB + index));
|
||||
GLCheck(glBindTexture(GL_TEXTURE_2D, 0));
|
||||
}
|
||||
|
||||
// Make sure that the texture unit which is left active is the number 0
|
||||
GLCheck(glActiveTextureARB(GL_TEXTURE0_ARB));
|
||||
}
|
||||
|
||||
|
||||
@ -326,6 +310,9 @@ bool Shader::CompileProgram()
|
||||
return false;
|
||||
}
|
||||
|
||||
// Make sure that GLEW is initialized (extra safety -- it is already done in IsAvailable())
|
||||
priv::EnsureGlewInit();
|
||||
|
||||
// Destroy the shader if it was already created
|
||||
if (myShaderProgram)
|
||||
GLCheck(glDeleteObjectARB(myShaderProgram));
|
||||
|
Loading…
Reference in New Issue
Block a user