I have a terrible behavior with one of my textures on OpenGL.
The texture is correctly displayed on the first frame, but after a few renders, it turns black.
After analyzing the opengl call stack, it seems that the “glBindTexture” method is called on the first frame but is no longer called when the texture turns black.
If the “glbindtexture” is not called, the texture is logically black, but I don’t understand why the “glbindtexture” is not called after some rendering .
////////////////
Setup texture :
////////////////
//SET UNPRESSED BUTTON TEXTURE
glBindTexture(GL_TEXTURE_2D, 0);
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
// Decode image into its raw image data
if (convertImageToRawImage(buttonImage))
{
//if decompression was successful, set the texture parameters
// set the texture wrapping parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// set the texture magnification/minification parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load the image data into the current bound texture buffer
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, imageWidth, imageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE,
&image[0]);
}
image.clear();
/////////////////
Draw Texture :
/////////////////
ViewerContext->browser->ButtonShader->use();
setTransformation();
// Bind the VAO
glBindVertexArray(vertexArrayObject);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_DEPTH_TEST);
// If the isPressed value is true, then update the state of the button
if (isPressed == true)
{
glUniform1i(ButtonStateUniformLocation, 1);
glBindTexture(GL_TEXTURE_2D, 0);
glActiveTexture(GL_TEXTURE0 + 1);
glBindTexture(GL_TEXTURE_2D, texturePID);
glEnable(GL_TEXTURE_2D);
glUniform1i(ButtonAPressedTextureUniformLocation, 1);
}
else
{
glUniform1i(ButtonStateUniformLocation, 0);
glBindTexture(GL_TEXTURE_2D, 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textureID);
glEnable(GL_TEXTURE_2D);
glUniform1i(ButtonATextureUniformLocation, 0);
}
// Start the rendering process
// 1rst attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
// 2nd attribute buffer : UVs
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, UVBufferObject);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
// Draw call
glDrawArrays(GL_TRIANGLES, 0, buttonVertices.size());
Images :
Call Stack When texture display:
Call Stack When texture not display:
Render: