PDA

View Full Version : GLSL shadow mapping & depth buffer copy into a texture



dletozeun
01-17-2007, 03:33 AM
Hello,

Yes! Once again, a shadow mapping problem! I have search on the forum and google but I don't find the solution of my problem...

So, I would like to make shadow mapping with the glsl shaders. After somes issues with transformations matrices, now I am sure that the matrices that calculate the shadowmap coordinates are correct.
NB: I don't forgot to multiply this last matrix by this one:
{0.5f, 0.0f, 0.0f, 0.0f,
0.0f, 0.5f, 0.0f, 0.0f,
0.0f, 0.0f, 0.5f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f};
In order to have texture coordinates scaled in [0 1] range

But I am not sure that the shadowmap is correctly given to the shader.
This is what I do in the vertex shader to calculate shadowmap texture coordinates:


uniform mat4 LightModelViewProjectionMatrix;
...
ShadowCoord = LightModelViewProjectionMatrix * gl_Vertex;

gl_Position = ftransform();where LightModelViewProjectionMatrix is the matrice that give the transformations that we have to do if we want to see the scene from the light point of view. This matrice is correct.

then in the fragment shader I do that:


uniform sampler2DShadow ShadowMap;
float shadeFactor;

...

shadeFactor = shadow2DProj(ShadowMap, ShadowCoord).r;But what I see is an object completly shaded in the scene!

I create the shadowmap in this way:


glActiveTexture( GL_TEXTURE0_ARB );
glEnable(GL_TEXTURE_2D);
glBindTexture (GL_TEXTURE_2D, shadowTex);
glCopyTexImage2D (GL_TEXTURE_2D,
0,
GL_DEPTH_COMPONENT,
0,
0,
shadowMapSize,
shadowMapSize,
0);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB ) ;
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL);
glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY);where shadowtex is a GLuint variable that has been initialized with:

glGenTextures(1, &shadowTex);At this point depth buffer data should be in the shadowtex texture
Then when I render the scene, I enable this texture to create shadows:


glActiveTexture( GL_TEXTURE0_ARB );
glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, shadowTex );

glUniform1iARB(ShadowMapLoc, 0);
ShadowMapLoc is the location of the uniform sampler2DShadow ShadowMap variable.

I don't see where is the problem in this code...but all I see is a completely shadowed scene!

Otherwise, I have tried to do this job with FBO but nothing more, the scene is still entirely shadowed as if the depth buffer texture is entirely black...

here is how I get the depth buffer after the scene has been rendered:


glActiveTexture( GL_TEXTURE0_ARB );
glEnable(GL_TEXTURE_2D);
glBindTexture (GL_TEXTURE_2D, shadowTex);

glTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, shadowMapSize, shadowMapSize,
0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, frameBuffer);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, shadowTex, 0);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB ) ;
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL);
glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY);

printOglError();

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);I generate shadowTex and frameBuffer like that:


glGenTextures(1, &shadowTex);
glGenFramebuffersEXT(1, &frameBuffer);I found some pieces of code that explain how to render depth buffer in a texture but it seems to not works in my program...

Thanks a lot for your help.

Komat
01-17-2007, 01:52 PM
The FBO case looks like you do not understand how the FBOs are used.

First you need to bind the FBO with the attached depth texture and properly configure the rendering if the FBO does not have color buffer attached (see example "Render to depth texture with no color attachments" from EXT_framebuffer_object (http://oss.sgi.com/projects/ogl-sample/registry/EXT/framebuffer_object.txt) specification) After that you need to render the scene from the view of the light. Finally you will unbind the FBO and use the texture.

Probably the best way to find the error would be to use the GLIntercept (http://glintercept.nutty.org/) which can show you content of textures and color/depth buffers so you can check what does not work as expected.

dletozeun
01-18-2007, 02:52 AM
Thank you very much for your help Komat!

Yes I don't understand exactly how FBO works because I didn't read all the specification ( it particularly long!) and I was in hurry! ^^

GLIntercept seems to be very interesting! Thanks a lot lot I will look at it.

But why the code with the glCopyTexImage2D finction don't work. Before you suggest to use it, no?

Komat
01-18-2007, 03:42 AM
Originally posted by dletozeun:

But why the code with the glCopyTexImage2D finction don't work. Before you suggest to use it, no? I do not know why. In my program I am using something similiar when the FBO is not available and it works. The differences are that I am always using GL_DEPTH_COMPONENT16_ARB texture internal format, I am updating part of texture using the glCopyTexSubImage2D instead of redefining texture each time and I am leaving the GL_DEPTH_TEXTURE_MODE_ARB and GL_TEXTURE_COMPARE_FUNC_ARB parameters on default values (LUMINANCE and LEQUAL).