Sign in to follow this  
Enalis

Depth Buffer with FBO

Recommended Posts

I am trying to write a depth buffer into an fbo attachment here is my fbo initialization code.
int cRenderer::InitFBO(void){
	glGenTextures(4, &FBOTextures[0]); // create (reference to) a new texture

	// Initialize the FBO attachment textures

	// Init first four color attachments
	for(int i = 0; i < 4; i++){
		glBindTexture(GL_TEXTURE_2D, FBOTextures[i]);
		// (set texture parameters here)
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
		//create the texture
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16, 800, 600, 0, GL_RGBA, GL_FLOAT, 0);
	}

	glGenTextures (1, &depthTexture[0]);
	glBindTexture (GL_TEXTURE_2D, depthTexture[0]);
	glTexImage2D (GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, screenSettings->width, screenSettings->height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
	glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

	// generate the frame buffer object
	glGenFramebuffersEXT(1, &FBO);

	// bind the frame buffer object and attach all of the texturs to it.
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, FBO);
	glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, FBOTextures[0], 0);
	glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_TEXTURE_2D, FBOTextures[1], 0);
	glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT2_EXT, GL_TEXTURE_2D, FBOTextures[2], 0);
	glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT3_EXT, GL_TEXTURE_2D, FBOTextures[3], 0);
	glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, depthTexture[0], 0);
	glDrawBuffer (GL_FALSE);
	glReadBuffer (GL_FALSE);

	// verify all is well and restore state
	GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
	switch (status){
		case GL_FRAMEBUFFER_COMPLETE_EXT:
			break;
		case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
			cerr << "FBO configuration unsupported" << endl;
		default:
			cerr << "FBO programmer error" << endl;
	}
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

	return 0;
}


So how would one take the frame buffer and draw a scene only getting the depth buffer into the fifth attachment the depth buffer attachment?

Share this post


Link to post
Share on other sites
Here is the drawing code I'm trying, Draw some objects just will draw a few quadrics in the middle of the screen, my current results though then attempting to view the depth buffer show simply a black square unfortunately.


void DrawDepthTexture(void){
// Save the current Draw buffer
GLint currentDrawBuf;
glGetIntegerv(GL_DRAW_BUFFER, &currentDrawBuf);

// disable textureing, lighting, and blending
glDisable(GL_TEXTURE_2D | GL_LIGHTING | GL_BLEND);

glPushMatrix();
glViewport(0, 0, 800, 600);

// set up our current context
glClear(GL_DEPTH_BUFFER_BIT);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glDepthMask(GL_TRUE);
glStencilMask(GL_FALSE);

// bind our frame buffer
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, renderer.FBO);
glDrawBuffer(GL_DEPTH_ATTACHMENT_EXT);

// draw our scene
DrawSomeObjects(0.0f, 0.0f, 0.0f, 1.0f);

glPopMatrix();

// reenable color masking and stencil masking should I turn off depth mask?
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glDepthMask(GL_TRUE); // does this need to be turned off?
glStencilMask(GL_TRUE);

// re-enable textureing, lighting, and blending
glEnable(GL_TEXTURE_2D | GL_BLEND);

// draw the depth buffer in the bottom right corner in 2d
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
glDrawBuffer(currentDrawBuf);
glViewport(0, 0, 800, 600);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, renderer.depthTexture[0]);

glPushMatrix();
orthoMode(0, 16, 0, 12);

glBegin(GL_QUADS);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex2i(0, 0);
glTexCoord2f(1.0f, 0.0f); glVertex2i(4, 0);
glTexCoord2f(1.0f, 1.0f); glVertex2i(4, 3);
glTexCoord2f(0.0f, 1.0f); glVertex2i(0, 3);
glEnd();

perspectiveMode();
glPopMatrix();
}

Share this post


Link to post
Share on other sites
my ATI only supports GL_NEAREST filtering for depth buffer textures (leads to some fbo errors)...

...another reasong for the black depth textures might be a bad (zFar-zNear)-to-scene-extends ratio (e.g. depth buffer's range is [1..1000000], but you're rendering within [2..10])...

...cheers!
zimerman

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this