I'm a beginner in shadow mapping. I have in my scene two meshes : a cube and a floor. The scene is lighted by a single point light.
Here's for the following display the light properties :
lightFrustrum->setFovy(70.0f);
lightFrustrum->setAspectRatio(500.0f/500.0f);
lightFrustrum->setNearClipDistance(0.1f);
lightFrustrum->setFarClipDistance(1000.0f);
lightFrustrum->setPosition(glm::vec4(-20.0f, 10.0f, 0.0f, 1.0f));
lightFrustrum->setOrientation(glm::vec3(1.0f, -1.0f, 0.0f));
lightFrustrum->setUpVectorDirection(glm::vec3(0.0f, 1.0f, 0.0f));
And the depth texture dimensions :
width = 512
height = 512
And the code to load the depth texture :
GLfloat border[] = {1.0f, 1.0f, 1.0f,1.0f};
glGenTextures(1, &this->m_TextId);
glBindTexture(GL_TEXTURE_2D, this->m_TextId);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24,
this->m_Width, this->m_Height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, border);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LESS);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, 0);
But I have the strange display like below :
As you can see, there's two problems : there's on the bottom of the screen a black strip. And on the left of the correct shadow, there's an other smallest one which should not be here either.
Now if I put the light just above the cube at 0.0f, 15.0f, 0.0f with the same depth texture propertiesI have the following result :
And now the same light properties but a depth texture with a width and a height of 64.
The shadow seems to be correct now but if I keep the same depth texture properties but I put the light at position -20.0f, 15.0f, 0.0f like in the first case it looks like the first case but this time without the line strip on the bottom.
Here's in the vertex shader for the first pass (depth texture creation):
#version 400
layout (location = 0) in vec3 VertexPosition;
uniform mat4 MVP;
void main(void)
{
gl_Position = MVP * vec4(VertexPosition, 1.0f);
}
The vertex shader for the second pass :
#version 400
layout (location = 0) in vec3 VertexPosition;
layout (location = 1) in vec3 VertexNormal;
layout (location = 2) in vec2 VertexTexture;
uniform mat3 NormalMatrix;
uniform mat4 ModelViewMatrix;
uniform mat4 MVP;
uniform mat4 ShadowMatrix;
out vec3 Position;
out vec3 Normal;
out vec2 TexCoords;
out vec4 ShadowCoords;
void main(void)
{
TexCoords = VertexTexture;
Normal = normalize(NormalMatrix * VertexNormal);
Position = vec3(ModelViewMatrix * vec4(VertexPosition, 1.0f)).xyz;
ShadowCoords = ShadowMatrix * vec4(VertexPosition,1.0);
gl_Position = MVP * vec4(VertexPosition, 1.0f);
}
And the fragment shader for the second pass :
#version 400
in vec3 Position;
in vec3 Normal;
in vec2 TexCoords;
in vec4 ShadowCoords;
layout (location = 0) out vec4 FragColor;
uniform sampler2D Tex1;
uniform sampler2DShadow ShadowMap;
uniform int lightCount;
struct PointLight
{
vec4 Position;
vec3 La, Ld, Ls;
float Kc, Kl, Kq;
};
struct Material
{
vec3 Ka, Kd, Ks, Ke;
float Shininess;
};
uniform PointLight LightInfos[10];
uniform Material MaterialInfos;
vec3 Basic_ADS_Phong_Shading(int idx, vec3 normal, vec3 lightDir)
{
vec3 reflectDir = reflect(-lightDir, normal);
vec3 viewDir = normalize(-Position.xyz);
vec3 Ambient = LightInfos[idx].La * MaterialInfos.Ka;
vec3 Diffuse = LightInfos[idx].Ld * MaterialInfos.Kd * max(dot(lightDir, normal), 0.0f);
vec3 Specular = LightInfos[idx].Ls * MaterialInfos.Ks * pow(max(dot(reflectDir, viewDir), 0.0f), MaterialInfos.Shininess);
vec3 Emissive = MaterialInfos.Ke;
return (Ambient + Diffuse + Specular + Emissive);
}
vec3 getLightIntensity(void)
{
vec3 LightIntensity = vec3(0.0f);
for (int idx = 0; idx < lightCount; idx++)
{
vec3 tnorm = (gl_FrontFacing ? -normalize(Normal) : normalize(Normal));
vec3 lightDirNorm = vec3(0.0f);
if (LightInfos[idx].Position.w == 0.0f)
lightDirNorm = normalize(vec3(LightInfos[idx].Position));
else if (LightInfos[idx].Position.w == 1.0f)
{
vec3 lightDir = vec3(LightInfos[idx].Position) - Position;
lightDirNorm = normalize(lightDir);
LightIntensity = Basic_ADS_Phong_Shading(idx, -tnorm, lightDirNorm);
}
}
return (LightIntensity);
}
void main(void)
{
vec4 Ambient = vec4(LightInfos[0].La * MaterialInfos.Ka, 1.0f);
vec4 LightIntensity = vec4(getLightIntensity(), 1.0f);
vec4 TexColor = texture(Tex1, TexCoords);
float shadow = textureProj(ShadowMap, ShadowCoords);
if (TexColor != 0)
FragColor = LightIntensity * TexColor * shadow + Ambient * TexColor;
else
FragColor = LightIntensity * shadow + Ambient;
}
So I really lost in front of this situation. Does anyone have had the same problem using shadow mapping ? Thanks a lot in advance for your help.