Sign in to follow this  
Argo15

Making a depth texture.

Recommended Posts

I'm attempting to do shadow mapping but I'm having trouble creating a depth texture. My init code is as follows.
        int shadowMapSize = 512;

	// Generate shadow map Texture
	glGenTextures(1, &shadowMap);
	glBindTexture(GL_TEXTURE_2D, shadowMap);
	glTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, shadowMapSize, shadowMapSize, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);
	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_TO_EDGE);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	glBindTexture(GL_TEXTURE_2D, 0);
	
	// Create a render buffer
	glGenRenderbuffersEXT(1,&renderBufferShadow);
	glBindRenderbuffer(GL_RENDERBUFFER_EXT,renderBufferShadow);
	glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT,GL_DEPTH_COMPONENT,shadowMapSize,shadowMapSize);
	glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);

	// Create FBO
	glGenFramebuffersEXT(1,&frameBufferShadow);
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, frameBufferShadow);
	glDrawBuffer(GL_NONE);
	glReadBuffer(GL_NONE);
	glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,GL_TEXTURE_2D, shadowMap, 0);
	glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT,GL_DEPTH_ATTACHMENT_EXT,GL_RENDERBUFFER_EXT,renderBufferShadow);

	// check FBO status
	GLenum FBOstatus = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
	if(FBOstatus != GL_FRAMEBUFFER_COMPLETE_EXT)
		printf("GL_FRAMEBUFFER_COMPLETE failed, CANNOT use FBO\n");

	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);


For my rendering I do
void Game::CreateShadowMap(){
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, frameBufferShadow);
	glBindRenderbufferEXT(GL_RENDERBUFFER_EXT,renderBufferShadow);

	glPushAttrib( GL_VIEWPORT_BIT );
	glViewport( 0, 0, 512, 512 );
	scene.DrawShadowMap();
	glPopAttrib();

	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,0);
	glBindRenderbufferEXT(GL_RENDERBUFFER_EXT,0);
}


The when I try to draw the texture on the screen it is just completely black, and in my shader the shadow mapping is not doing anything. Vertex Shader
void main() {
	//other stuff
	shadowTexCoord = gl_TextureMatrix[7]*gl_ModelViewMatrix*gl_Vertex;
}

Fragment Shader
void main() {
        // other stuff
	vec3 shadowPersp = shadowTexCoord.xyz/shadowTexCoord.w;
	float depth = texture2D(shadowMap,shadowPersp.xy).r;
	bool IsShadowed = false;

	if (depth>shadowPersp.z)
		IsShadowed=true;
        // other stuff

[Edited by - Argo15 on April 3, 2010 11:11:46 AM]

Share this post


Link to post
Share on other sites
Depth textures are tricky. I believe by default you cannot access a depth texture via texture2D (you'd call shadow2D instead, which automatically compares depth) unless you specified the texture parameter GL_TEXTURE_COMPARE_MODE as GL_NONE:
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_COMPARE_MODE,GL_NONE);
That should solve your problem

Share this post


Link to post
Share on other sites
Haha, it always seems that I solve my 10 hour problem a few minutes after I resort to making a thread. The problem was really that I was not clearing the depth buffer every frame. Anyway here's a picture of my shadows, aren't they beautiful?

http://i628.photobucket.com/albums/uu9/Argo15/ArgoEngine.jpg

Share this post


Link to post
Share on other sites
Heh, ok, guess that works too :D
Nice screenshot btw ;)
And yes i know the solution-found-shortly-after-question-problem, too. Reason i stopped posting questions here altogether :P

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