0

I have an OpenGL texture rendered to a first FBO and I need to edit its brightness, render to a second FBO and then draw it on screen.

If I render the texture to the second FBO (without modifying its brightness) and then draw it on screen, it shows up correctly.

If I try to edit brightness of rendered texture (using texture combiner functions like glTexEnv) I get wrong output like all black or all white texture, altered contrast, ecc..

If I apply brightness regulation while drawing on screen instead of the FBO, it works great..brightness is modified as expected.

So my question is: can I use texture combiner functions like glTexEnv when I render to FBO?

This is the code which I use to modify brightness while drawing to the FBO (it doesn't work):

// "secondFBO" is the FBO I'm intended to render to after brightness regulation.
// "textureId" is the OpenGL texture attached to the first FBO that contains
// the unmodified texture.
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, secondFBO);
glEnable(GL_TEXTURE_RECTANGLE_ARB);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textureId);

// apply alpha filter
double t = brightnessValue;     
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
if (t > 1.0f)
{
    glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB,      GL_ADD);
    glColor4f(t-1, t-1, t-1, t-1);
}
else
{
    glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB,      GL_SUBTRACT);
    glColor4f(1-t, 1-t, 1-t, 1-t);
}
glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_RGB,         GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_RGB,         GL_PRIMARY_COLOR);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA,    GL_REPLACE);
glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_ALPHA,       GL_TEXTURE);

// ... draw texture ...

glDisable(GL_TEXTURE_RECTANGLE_ARB);

// restore previous state
glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);

// draw on screen
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
glEnable(GL_TEXTURE_RECTANGLE_ARB);
// "textureId2" is the texture attached to the second FBO
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textureId2);

// ... draw texture ...

glDisable(GL_TEXTURE_RECTANGLE_ARB);
Andrea3000
  • 1,018
  • 1
  • 11
  • 26

1 Answers1

2

Why you are not using shaders? I think it would be much easier to write the code

You are using FBO and that means that Shaders - at least GLSL 1.2 are supported by your hardware

fen
  • 9,835
  • 5
  • 34
  • 57
  • I wasn't using shaders simply because I didn't even know that they exists :-) I program only as an hobby and this is one of the first time that I use OpenGL..I'm a self-taught. Anyway, few hours ago I discovered the existance of shaders and I'm now reading "OpenGL Shading Language" book. I'm trying to figure it out and after that I will try to implement a solution which uses shaders. There is only one thing that I don't seem to get here...using shaders is it possible to read a texture from FBO, modify it and render back to FBO? I'm only finding examples that render to the back-buffer directly. – Andrea3000 Feb 10 '12 at 10:05
  • go with the shaders then.. combiners are old, and have less functionality than the shaders... With the shader (actually the fragment shader) you can write to the frame buffer (back buffer) or to custom Framebuffer Object. You cannot read and write to the same texture (buffer) in the same shader. Here is one idea: set FBO you want to write (draw) to, then set textures, and shaders... draw the fullscreen quad on the screen... and then you have the result in your framebuffer (and the texture) – fen Feb 10 '12 at 12:09