I'm trying to "read" the texture attached to a first FBO (fboA), modifying it (with fragment shader) and render to a second FBO (fboB).
I'm not able to figure it out, all I got is a black or white texture.
Here is the code:
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboB);
glEnable(GL_TEXTURE_RECTANGLE_ARB);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textureFromFboA);
GLuint t1Location = glGetUniformLocation(shaderProgram, "texture");
glUniform1i(t1Location, 0);
glUseProgram(shaderProgram);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex2f (0.0f, 0.0f);
glTexCoord2f(imageRect.size.width, 0.0f);
glVertex2f (imageRect.size.width, 0.0f);
glTexCoord2f(imageRect.size.width, imageRect.size.height);
glVertex2f (imageRect.size.width, imageRect.size.height);
glTexCoord2f(0.0f, imageRect.size.height);
glVertex2f (0.0f,imageRect.size.height);
glEnd();
glDisable(GL_TEXTURE_RECTANGLE_ARB);
glFlush();
glUseProgram(0);
This is the fragment shader code:
#version 120
uniform sampler2D texture;
void main(void)
{
gl_FragColor = texture2D(texture, gl_TexCoord[0].st) * 0.8;
}
I would expect to have a darker texture rendered in fboB but I only get an all black texture. This happens also if I write gl_FragColor = texture2D(texture, gl_TexCoord[0].st);
.
On the contrary, if I write gl_FragColor = vec2(1.0, 0.0, 0.0, 1.0);
I correctly have an all red texture as output.
If I comment out the glUseProgram()
statement, the code works fine and texture in fboB is an exact copy of texture in fboA.
Why this happens? Am I missing something?
uniform sampler2D texture;
Rectangle textures are not the same texture type as 2D textures. Yes, they're two-dimensional, but they still maintain a distinct texture type. Therefore, they cannot be accessed via a sampler2D
.
So change that to a samplerRect
. You will also need to use proper texture coordinates, because rectangle textures take texel-space coordinates instead of normalized coordinates.
Alternatively, you can just use a 2D texture. NPOT textures have been around for well over half a decade; you don't have to use rectangle textures to have non-power-of-two render targets.