Rendering the depth buffer to texture using a FBO

Started by
3 comments, last by parmar 14 years, 2 months ago
Hi guys, I'm having trouble implementing a basic shadow mapping algorithm. At the moment I am trying to render the depth buffer to a texture and then render it to see if it is correct. I have been following the guide from here: http://www.fabiensanglard.net/shadowmapping/index.php When I render the depth texture to a quad I'm getting nothing. I have had render to texture working by rendering the colour buffer to texture and using the a renderbuffer for the depth. Here is the code I use to set up the texture and the FBO:-

void Scene::generateShadowFBO()
{
	int shadowMapWidth = 512;
	int shadowMapHeight = 512;
	
	GLenum FBOstatus;
	glActiveTexture(GL_TEXTURE0);
	
	glGenTextures(1, &m_depthTexture);
	glBindTexture(GL_TEXTURE_2D, m_depthTexture);
	
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
	glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_INTENSITY);
	
	glTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, shadowMapWidth, shadowMapHeight, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, 0);
		
	// create a framebuffer object
	glGenFramebuffers(1, &m_shadowFBO);
	glBindFramebuffer(GL_FRAMEBUFFER, m_shadowFBO);
	
	// Instruct openGL that we won't bind a color texture with the currently binded FBO
	glDrawBuffer(GL_NONE);
	glReadBuffer(GL_NONE);
	
	// attach the texture to FBO depth attachment point
	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,GL_TEXTURE_2D, m_depthTexture, 0);

	// check FBO status
	FBOstatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
	if(FBOstatus != GL_FRAMEBUFFER_COMPLETE)
		printf("GL_FRAMEBUFFER_COMPLETE failed, CANNOT use FBO\n");
	
	// switch back to window-system-provided framebuffer
	glBindFramebuffer(GL_FRAMEBUFFER, 0);
	glBindTexture(GL_TEXTURE_2D, 0);
}


I then render the fbo pass using:-

	glDisable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, 0);
	glBindFramebuffer(GL_FRAMEBUFFER, m_shadowFBO);
	glClear(GL_DEPTH_BUFFER_BIT);
	glColorMask(false, false, false, false);
	glCullFace(GL_BACK);
Then to render the texture to a quad I use:-

	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, m_depthTexture);
	glBindFramebuffer(GL_FRAMEBUFFER, 0);
	glColorMask(true, true, true, true);
The texture and viewport sizes are all the same and the fbo status test is saying it is complete. Can anyone see anything glaringly obvious that I am missing?
Advertisement
r u sure its not working
a couple issue ppl have is since the depth values tend to be high ~0.995 they tend to look white when displayed thus ppl think its not working when in fact it is

use

glFragColor.xyz = vec3( pow( depthtex, 128.0 ) );
Some tips:

1) Often the light or camera is just pointing the wrong way so all you see is a white or black square. Try spinning them on a loop.

2) The Color buffer versus depth buffer may have unique RGB versus RGBA settings.

3) (as zedz noted) You need to normalize the depth data to make it more obvious (take the bounding box Z and make that be the range of 0 - 1). Otherwise you might see super-subtle shades of gray in the depth buffer.

http://www.fabiensanglard.net/shadowmapping/index.php
I have also tried this link to get the shadow but not getting shadow rendering.
I am using ATI HD 2400 pro card. I have tried suggestion from zedz and
begnilem but still its not working.Any suggestion on this.
_Unicorn_

just comment the update function you will get the
shadow.Now i am getting the shadow.

This topic is closed to new replies.

Advertisement