1
$\begingroup$

The code is

glColor3f(1, 1, 1);
gluSphere(gluq, 0.2, 10, 10);

glColor3f(1, 0, 0);
glTranslatef(1, 0, 0);
gluSphere(gluq, 0.2, 10, 10);

In short, when I draw directly to screen, it's correct.

enter image description here

But when I create a framebuffer object, attach a texture as color component, draw on the fbo, then draw the texture as rectangle on to the screen, the colors went wrong

enter image description here

It seems like the right sphere at (1,0,0) changed the color of the sphere drawn before it.

And even more bizzare is that if i change the first sphere's color to blue (0,0,1), it becomes black after the second sphere is drawn.

enter image description here

The way I rendered it is like this :

glBindFramebuffer(GL_FRAMEBUFFER, fbo[0]);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
{
    glViewport(0, 0, SCRWIDTH, SCRHEIGHT);
    glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45, (float)SCRWIDTH / (float)SCRHEIGHT, 0.0f, 100.0f);
    glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0, 0, 5, 0, 0, 0, 0, 1, 0);


    glColor3f(0, 0, 1);
    gluSphere(gluq, 0.2, 10, 10);

    glColor3f(1, 0, 0);
    glTranslatef(1, 0, 0);
    gluSphere(gluq, 0.2, 10, 10);

}
glBindFramebuffer(GL_FRAMEBUFFER, 0);

glBindTexture(GL_TEXTURE_2D, fbo_tex[0]);
glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, SCRWIDTH, SCRHEIGHT, 0, -1, 1);
glMatrixMode(GL_MODELVIEW); glLoadIdentity();
{
    glBegin(GL_QUADS);
    glTexCoord2f(0, 0); glVertex3i(0, 0, 0);
    glTexCoord2f(0, 1); glVertex3i(0, SCRHEIGHT, 0);
    glTexCoord2f(1, 1); glVertex3i(SCRWIDTH, SCRHEIGHT, 0);
    glTexCoord2f(1, 0); glVertex3i(SCRWIDTH, 0, 0);
    glEnd();
}
glBindTexture(GL_TEXTURE_2D, 0);

And I created the fbo and texture like this :

glGenTextures(1, &fbo_tex[0]);

glBindTexture(GL_TEXTURE_2D, fbo_tex[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, SCRWIDTH, SCRHEIGHT, 0, GL_RGBA, GL_FLOAT, 0);
glBindTexture(GL_TEXTURE_2D, 0);

glGenFramebuffers(1, &fbo[0]);
glBindFramebuffer(GL_FRAMEBUFFER, fbo[0]);
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, fbo_tex[0], 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);

If feels like that consequtive glColor3f caused the colors to mix, taking the minimum RGB values of previous used colors. So white becomes red, but blue turns to black. And the first sphere has the color written to the texture, then the second sphere somehow picks the mixed color and overwrite the first one's pixels. How can I fix this?

$\endgroup$
  • $\begingroup$ Try calling glDisable(GL_TEXTURE_2D) before calling gluSphere(...) $\endgroup$ – PaulHK Aug 14 at 3:10
  • $\begingroup$ Intuitive guess from looking at the images: Looks like you only get or show the red channel. Try to set the white color to grey and see if it turns up dark red. $\endgroup$ – beyond Aug 19 at 8:32
0
$\begingroup$

You need to set the colour back to white before drawing the framebuffer to the screen. Otherwise, the whole framebuffer gets multiplied with red.

glBindFramebuffer(GL_FRAMEBUFFER, 0);

glColor3f(1, 1, 1); // <------- add this (doesn't have to be exactly here)

glBindTexture(GL_TEXTURE_2D, fbo_tex[0]);
glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, SCRWIDTH, SCRHEIGHT, 0, -1, 1);
glMatrixMode(GL_MODELVIEW); glLoadIdentity();
{
    glBegin(GL_QUADS);
    glTexCoord2f(0, 0); glVertex3i(0, 0, 0);
    glTexCoord2f(0, 1); glVertex3i(0, SCRHEIGHT, 0);
    glTexCoord2f(1, 1); glVertex3i(SCRWIDTH, SCRHEIGHT, 0);
    glTexCoord2f(1, 0); glVertex3i(SCRWIDTH, 0, 0);
    glEnd();
}
glBindTexture(GL_TEXTURE_2D, 0);
$\endgroup$
  • $\begingroup$ Or even better, change the texture environment to GL_REPLACE rather than GL_MODULATE, if that is really the problem. (Provided entirely doing away with this legacy fixed function stuff is out of the question already.) $\endgroup$ – Christian Rau Aug 22 at 11:41
  • $\begingroup$ @ChristianRau Why is that better? $\endgroup$ – immibis Aug 23 at 0:13
  • $\begingroup$ Because that's the actual problem and a more dirtect solution. You don't want to combine your texture's color with a constant color in this scenario anyway. So rather than setting the constant to 1, changing the texture to actually not combine with the color feels like the more natural solution to the problem. (If anything, though, it might at least be good to add to the answer that that, the texture environment, is the actual cause of the problem, if it is.) $\endgroup$ – Christian Rau Aug 23 at 0:16
  • $\begingroup$ @ChristianRau I'd wager that most fixed-function apps draw un-colour-modulated textures by using GL_MODULATE and a white colour. That is natural because it's the default. $\endgroup$ – immibis Aug 23 at 0:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.