Jump to content
  • Advertisement
Sign in to follow this  
theprogrammer12

Shadow Mapping (GLSL) doesn't work

This topic is 2795 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi!

I tried to create shadows using GLSL by reading this tutorial:
http://www.fabiensanglard.net/shadowmapping/index.php

But in the end everything is dark.

that's my code:

This bit creates the FBO and the depth texture.
    // ---------------------- shadow ----------------------

glGenTextures(1, &shadow.tex);
glBindTexture(GL_TEXTURE_2D, shadow.tex);
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 );
glTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, shadow.width, shadow.height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, 0);
glBindTexture(GL_TEXTURE_2D, 0);
glGenRenderbuffersEXT(1, &shadow.rbo);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, shadow.rbo);
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA, shadow.width, shadow.height);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);

glGenFramebuffersEXT(1, &shadow.fbo);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, shadow.fbo);
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT,GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, shadow.rbo);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, shadow.tex, 0);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);


here I'm rendering the shadows:
void CGame::SetShadowTextureMatrix(void)
{
static double modelView[16];
static double projection[16];

// Moving from unit cube [-1,1] to [0,1]
const GLdouble bias[16] = {
0.5, 0.0, 0.0, 0.0,
0.0, 0.5, 0.0, 0.0,
0.0, 0.0, 0.5, 0.0,
0.5, 0.5, 0.5, 1.0};

// Grab modelview and transformation matrices
glGetDoublev(GL_MODELVIEW_MATRIX, modelView);
glGetDoublev(GL_PROJECTION_MATRIX, projection);

glMatrixMode(GL_TEXTURE);
glActiveTexture(GL_TEXTURE5_ARB);

glLoadIdentity();
glLoadMatrixd(bias);

// concatating all matrices into one.
glMultMatrixd (projection);
glMultMatrixd (modelView);

// Go back to normal matrix mode
glMatrixMode(GL_MODELVIEW);
}

void CGame::RenderShadows(void)
{
CVector cam_pos, focus, top;
int i;

shader_enabled = 1;
UseNoShader();

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, shadow.fbo);
glViewport(0, 0, shadow.width, shadow.height);
glClear(GL_DEPTH_BUFFER_BIT);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (double)shadow.width / (double)shadow.height, 0.001, 10000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(light.pos.x, light.pos.y, light.pos.z, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
//glCullFace(GL_FRONT);
RenderScene();
SetShadowTextureMatrix();
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

shader_enabled = 1;
}


and these are the shaders:
Vertex Shader
"attribute vec3 Normal_a;\n"
"attribute vec3 TangX_a;\n"
"attribute vec3 TangY_a;\n"
"\n"
"uniform sampler2D HeightTex;\n"
"uniform vec3 HeightFactor;\n"
"\n"
"varying vec2 UVCoord;\n"
"varying vec4 ShadowCoord;\n"
"varying vec3 RealPos;\n"
"varying float CamDist;\n"
"varying vec3 CamDir;\n"
"varying vec3 Normal;\n"
"varying vec3 TangX;\n"
"varying vec3 TangY;\n"
"\n"
"void main(void)\n"
"{\n"
" vec3 normal = Normal = normalize(Normal_a);\n"
" vec3 tangx = TangX = normalize(TangX_a);\n"
" vec3 tangy = TangY = normalize(TangY_a);\n"
" vec3 displacement = texture2D(HeightTex, gl_MultiTexCoord0.xy).rgb;\n"
" displacement -= vec3(0.5, 0.5, 0.5);\n"
" vec3 shift = tangx * displacement.x;\n"
" shift += tangy * displacement.y;\n"
" shift += normal * displacement.z;\n"
" shift *= HeightFactor;\n"
" vec4 pos = vec4(gl_Vertex.xyz + shift, 1.0);\n"
" UVCoord = gl_MultiTexCoord0.xy;\n"
" ShadowCoord = gl_TextureMatrix[5] * gl_Vertex;\n"
" gl_Position = gl_ModelViewProjectionMatrix * pos;\n"
" RealPos = gl_Vertex.xyz;\n"
" vec3 view_pos = vec3(gl_ModelViewMatrix * gl_Vertex);\n"
" CamDist = view_pos.z;\n"
" vec4 camera_OS = gl_ModelViewMatrixInverse * vec4(0.0, 0.0, 0.0, 1.0);"
" CamDir = normalize(camera_OS.xyz * gl_Vertex.w - gl_Vertex.xyz * camera_OS.w);\n"
"}"


Fragment Shader:
"varying vec2 UVCoord;\n"
"varying vec4 ShadowCoord;\n"
"varying vec3 RealPos;\n"
"varying float CamDist;\n"
"varying vec3 CamDir;\n"
"varying vec3 Normal;\n"
"varying vec3 TangX;\n"
"varying vec3 TangY;\n"
"\n"
"\n"
"void main(void)\n"
"{\n"
" // Here stands the code for calculating the color\n"
" if(ShadowEnabled == 1)\n"
" {\n"
" vec4 shadow_coord_wd = ShadowCoord / ShadowCoord.w;\n"
" shadow_coord_wd.z += 0.0005;\n"
" float light_dist = texture2D(ShadowMap, shadow_coord_wd.st).z;\n"
" if(ShadowCoord.w > 0.0)\n"
" frag_color *= light_dist < shadow_coord_wd.z ? 0.5 : 1.0;\n"
" }\n"
" gl_FragColor = vec4(frag_color, alpha);\n"
"}\n";


Can you see any mistakes?

Share this post


Link to post
Share on other sites
Advertisement
First, check the contents of the depth texture you've rendered. This will help you narrow down your problem. Right now, you've no idea whether your problem is in pass 1 or pass 2. Reduce the problem as much as you can.

Share this post


Link to post
Share on other sites
like geometrian said you should narrow down the problem a bit. try rendering a quad with the depth texture applied and go from there.

however, your bias matrix looks weird. i think for opengl you need to have your translation in the right column. And you might need to flip your y-axis aswell but i'm not sure about that

Share this post


Link to post
Share on other sites
Check to make sure you are operating in world space, not camera space. That's what got me when I first wrote a shadow mapping shader.

Share this post


Link to post
Share on other sites
well, I am pretty sure that the matrix is correct because it is the same as in the test program of the tutorial and this one works. I also tried to render the color instead of the depth and this worked as well, so I think the problem is in the fragment shader. I have already compared it to the one of the tutorial but I didn't find any mistakes.

Share this post


Link to post
Share on other sites
Quote:
Original post by theprogrammer12
well, I am pretty sure that the matrix is correct because it is the same as in the test program of the tutorial and this one works. I also tried to render the color instead of the depth and this worked as well, so I think the problem is in the fragment shader. I have already compared it to the one of the tutorial but I didn't find any mistakes.


I could be wrong, but I don't think you pass the light projection matrix to the shaders. gl_TextureMatrix is used to generate the shadow coord but I don't see it actually being set anywhere in your code.

Quote:
Original post by theprogrammer12
RenderScene();
SetShadowTextureMatrix();


Also isn't this in reverse order?

Share this post


Link to post
Share on other sites
I already noticed that the order is incorrect and changed it, but as the model view and the projection matrix don't change there and the texture matrix is not used when rendering from the light origin this should not matter.

The Texture Matrix is set here:

void CGame::SetShadowTextureMatrix(void)
{
static double modelView[16];
static double projection[16];

const GLdouble bias[16] = {
0.5, 0.0, 0.0, 0.0,
0.0, 0.5, 0.0, 0.0,
0.0, 0.0, 0.5, 0.0,
0.5, 0.5, 0.5, 1.0};

// Grab modelview and transformation matrices
glGetDoublev(GL_MODELVIEW_MATRIX, modelView);
glGetDoublev(GL_PROJECTION_MATRIX, projection);

glMatrixMode(GL_TEXTURE);
glActiveTexture(GL_TEXTURE5_ARB);

glLoadIdentity();
glLoadMatrixd(bias);

// concatating all matrices into one.
glMultMatrixd (projection);
glMultMatrixd (modelView);

// Go back to normal matrix mode
glMatrixMode(GL_MODELVIEW);
}

Share this post


Link to post
Share on other sites
gluPerspective(60.0, (double)shadow.width / (double)shadow.height, 0.001, 10000.0);

That seems quite crazy. I tried it on my shadow maps and the precision was so bad the shadows no longer showed up. Try changing it to 200 or something, just make sure it still captures the scene.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!