Hey all,
I’m new to using framebuffer objects and am having some trouble getting a simple coding example to work with it:
///
/// Texturing is already enabled at this point
///
glEnable(GL_LIGHTING);
GLfloat ambientColor[] = {0.5, 0.5, 0.5,1.0};
GLfloat lightPosition[] = {1.0,1.0,1.0,1.0};
glLightfv(GL_LIGHT1, GL_AMBIENT, ambientColor);
glLightfv(GL_LIGHT1, GL_POSITION, lightPosition);
glEnable(GL_LIGHT1);
GLuint color_tex;
GLuint fb;
glGenFramebuffersEXT(1, &fb);
glGenTextures(1, &color_tex);
glBindTexture(GL_TEXTURE_2D, color_tex);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, 512, 512, 0,
GL_RGB, GL_INT, 0);
// Enable render-to-texture
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);
// Set up color_tex and depth_rb for render-to-texture
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
GL_COLOR_ATTACHMENT0_EXT,
GL_TEXTURE_2D, color_tex, 0);
///
/// Attempting to draw a red square to the texture. Note that my
/// clear color isn't black, its a dark bluish (to help see black objects)
///
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);
glBegin(GL_POLYGON);
glVertex3f(-1.0, -1.0, 0.0);
glVertex3f(1.0, -1.0, 0.0);
glVertex3f(1.0, 1.0, 0.0);
glVertex3f(-1.0, 1.0, 0.0);
glEnd();
// Re-enable rendering to the window
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
glBindTexture(GL_TEXTURE_2D, color_tex);
///
/// Attempt to draw a textured quad using the texture we rendered to.
/// The quad should be a dark blue background with a red square on it.
/// Instead it is just black.
///
glColor3f(1.0,1.0,1.0);
glBegin(GL_POLYGON);
glTexCoord2f(0.0, 0.0); glVertex3f(0.0, 0.0, -3.0);
glTexCoord2f(1.0, 0.0); glVertex3f(1.0, 0.0, -3.0);
glTexCoord2f(1.0, 1.0); glVertex3f(1.0, 1.0, -3.0);
glTexCoord2f(0.0, 1.0); glVertex3f(0.0, 1.0, -3.0);
glEnd();
This is derived pretty closely from the examples in the specs. As you can see, it tries to render a red square to texture, and then tries to texture a quad based on that texture. I end up with a black quad on the screen. Can anyone see what I might be doing wrong here?