2

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?

genpfault
  • 51,148
  • 11
  • 85
  • 139
Andrea3000
  • 1,018
  • 1
  • 11
  • 26

2 Answers2

2

You cannot use sampler2D for texture rectangles... you must use GL_TEXTURE_2D not GL_TEXTURE_RECTANGLE_ARB.

here you have a nice tutorial on the FBO: http://www.songho.ca/opengl/gl_fbo.html

fen
  • 9,835
  • 5
  • 34
  • 57
  • Thank you very much for your help. At the end I've change sampler2D to sampler2DRect as it's easier for me but thank you anyway! – Andrea3000 Feb 10 '12 at 16:22
2
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.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • I was changing all my GL_TEXTURE_RECTANGLE_ARG to GL_TEXTURE_2D but using sampler2DRect is easier for me. Thank you very much! – Andrea3000 Feb 10 '12 at 16:21