movax, I think that we might help you if you give us the whole code. I mean in the above pasted code snippet there is a lot of initialization mixed with drawing calls… If it is what you are doing in the drawing function this is not the good way.
Moreover, you are drawing to a texture, but the above code does not show how you vizualize its content.
If it is necessary clean up your code, minimize using comments and uncomment when you are completly sure of what you have written. I am pretty sure that the error is more simple than you think and is not where you are looking.
I have written a little program with glut inspired from yours. Maybe it can help you to find the solution. It just draw a yellow quad centered on the screen in a texture using a fbo. Then, it pastes the previous drawn texture on the screen to display its content. The clear color for the texture is blue and green for the framebuffer.
#include <iostream>
#include <GL/glew.h>
#include <GL/glut.h>
using namespace std;
GLuint fbo, texture, depthBuffer;
//------------------------------------------------------------ Reshape()
void OnReshape(int w, int h)
{
// prevent a division by zero when minimising the window
if (h == 0)
h = 1;
// set the drawable region of the window
glViewport(0, 0, w, h);
// set up the projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// just use a perspective projection
glOrtho(-1.0f, 1.0f, -1.0f, 1.0f, -100.0f, 1000.0f);
// go back to modelview matrix so we can move the objects about
glMatrixMode(GL_MODELVIEW);
}
//------------------------------------------------------------ Draw()
//
void OnDraw()
{
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
glClearColor(0.0, 0.0, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glDisable(GL_TEXTURE_2D);
glPushAttrib(GL_VIEWPORT_BIT);
glViewport(0, 0, 512, 512);
glColor4f(1.0, 1.0, 0.0, 0.0);
glBegin(GL_QUADS);
glVertex2f(0.5, 0.5);
glVertex2f(0.5, -0.5);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glEnd();
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
glClearColor(0.0, 1.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glPopAttrib();
glColor4f(1.0, 1.0, 1.0, 0.0);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex2f(1.0, 1.0);
glTexCoord2f(0.0, 1.0); glVertex2f(1.0, -1.0);
glTexCoord2f(1.0, 1.0); glVertex2f(-1.0, -1.0);
glTexCoord2f(1.0, 0.0); glVertex2f(-1.0, 1.0);
glEnd();
glutSwapBuffers();
cout<<"coucou"<<endl;
}
void idle()
{
}
//------------------------------------------------------------ OnInit()
//
void OnInit()
{
glewInit();
glDisable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, 512, 512, 0, GL_RGB, GL_UNSIGNED_INT, NULL);
//glGenerateMipmapEXT(GL_TEXTURE_2D);
glGenFramebuffersEXT(1, &fbo);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, texture, 0);
if(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT)!=GL_FRAMEBUFFER_COMPLETE_EXT)
{
cout<<"The fbo is not complete"<<endl;
cout<<"Error: "<<glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT)<<endl;
}
}
//------------------------------------------------------------ OnExit()
//
void OnExit()
{
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
glDeleteFramebuffersEXT(1, &fbo);
glutSwapBuffers();
glutPostRedisplay();
}
//------------------------------------------------------------ main()
//
int main(int argc, char** argv)
{
// initialise glut
glutInit(&argc, argv);
// request a depth buffer, RGBA display mode, and we want double buffering
glutInitDisplayMode(GLUT_DEPTH | GLUT_RGBA | GLUT_DOUBLE);
// set the initial window size
glutInitWindowSize(512, 512);
// create the window
glutCreateWindow("A basic glut example");
// set the function to use to draw our scene
glutDisplayFunc(OnDraw);
// set the function to handle changes in screen size
glutReshapeFunc(OnReshape);
// run our custom initialisation
OnInit();
// this function runs a while loop to keep the program running.
glutMainLoop();
OnExit();
return 0;
}