Need Help with FBOs

SplitPersona

Newcomer
I've been trying to implement shadow mapping, but so far I've had trouble actually creating the shadow map, let alone applying it. So I figured I'd first try to render the shadow map (ie, the depth buffer) to the screen before worrying about applying it.

Here's my code so far (note: using GLEW, GLUT, and Cg for shaders)
Code:
/**
 * Just render a simple texture to the screen (only used for debugging)
 */
void render_texture(GLuint tex)
{
	// set up projection
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
	gluOrtho2D(-1, 1, -1, 1);

	// setup modelview
	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();

	glDisable(GL_LIGHTING);
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, tex);
	glColor3f(1, 1, 1);
	glBegin(GL_QUADS);
        glTexCoord2f(0, 0); glVertex3f(-1, -1, -0.5f);
        glTexCoord2f(1, 0); glVertex3f( 1, -1, -0.5f);
        glTexCoord2f(1, 1); glVertex3f( 1,  1, -0.5f);
        glTexCoord2f(0, 1); glVertex3f(-1,  1, -0.5f);
	glEnd();
	glBindTexture(GL_TEXTURE_2D, 0);
	glDisable(GL_TEXTURE_2D);
	glEnable(GL_LIGHTING);

	// reset modelview
	glFlush();
	glPopMatrix();

	// reset projection
	glMatrixMode(GL_PROJECTION);
	glPopMatrix();
	glMatrixMode(GL_MODELVIEW);
}// end render_texture


/**
 * Renders the current frame
 */
void render_frame(void)
{
	static GLuint frame_buffer, depth_buffer, color_attachment;
	static bool init = false;
	if(!init)
	{
		init = true;
#ifndef USE_DB
		glGenTextures(1, &color_attachment);
		glBindTexture(GL_TEXTURE_2D, color_attachment);
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 512, 512,
			0, GL_RGB, GL_FLOAT, 0);
#else
		glGenTextures(1, &depth_buffer);
		glBindTexture(GL_TEXTURE_2D, depth_buffer);
		glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16, 512,
			512, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, 0);
#endif
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
			GL_LINEAR);
		glBindTexture(GL_TEXTURE_2D, 0);

		glGenFramebuffersEXT(1, &frame_buffer);
		glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, frame_buffer);
#ifndef USE_DB
		glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
			GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D,
			color_attachment, 0);

#else
		glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
			GL_DEPTH_ATTACHMENT_EXT,
			GL_TEXTURE_2D, depth_buffer, 0);
#endif
		glDrawBuffer(GL_NONE);
		glReadBuffer(GL_NONE);
		
		switch(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT))
		{
		case GL_FRAMEBUFFER_COMPLETE_EXT:
			std::cout << "Status: Framebuffer Initialisation OK" 
				<< std::endl;
			break;
		case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
			std::cout << "Error: Framebuffer Configuration"
				<< " Unsupported" << std::endl;
			break;
		default:
			std::cout << "Error: Unknown Framebuffer"
				<< " Configuration Error" << std::endl;
			break;
		}// end switch
		glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
	}

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glPushMatrix();

	camera->apply();
	light->apply();

	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, frame_buffer);
#ifndef USE_DB
	glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
#else
	glDrawBuffer(GL_DEPTH_ATTACHMENT_EXT);
#endif

	if(g_useShader)
		shader->enable();

	scene->render();

	if(g_useShader)
		shader->disable();

	glDrawBuffer(GL_NONE);
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

	glPopMatrix();
	glFlush();

#ifndef USE_DB
	render_texture(color_attachment);
#else
	render_texture(depth_buffer);
#endif

	glutSwapBuffers();
}

I used the color attachment just to make sure I was using the FBO correctly. However, if I define USE_DB to try to capture the depth buffer, I get a blank screen. Can anyone help me find what I am doing wrong?
 
Do you check any errors or the framebuffer status?

For depth only rendering you should use:
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);

This is an invalid call and should return an error:
glDrawBuffer(GL_DEPTH_ATTACHMENT_EXT);
 
Back
Top