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.
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
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.
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?