Hello. I’ve noticed that shadows in my game have offsets when I looked closer to the edges. This is how they look like: http://i.imgur.com/LeG60xx.jpg http://i.imgur.com/TPTSnLx.jpg Any suggestions how to solve this problem? Here is the shader code if someone needs (removed all unnecessary codes that is not related to shadow mapping):
Vertex
#version 420
layout(location = 0) in vec3 VertexPosition;
layout(location = 1) in vec3 VertexNormal;
out vec3 Position;
out vec3 Normal;
out vec4 ShadowCoord;
uniform mat4 MVP;
uniform mat4 ModelViewMatrix;
uniform mat3 NormalMatrix;
uniform mat4 ShadowMatrix;
void main()
{
vec3 pos = vec3(ModelViewMatrix * vec4(VertexPosition, 1.0));
vec3 norm = normalize(NormalMatrix * VertexNormal);
Position = pos;
Normal = norm;
//Shadow mapping
ShadowCoord = ShadowMatrix * vec4(VertexPosition, 1.0);
gl_Position = MVP * vec4(VertexPosition, 1.0);
}
Fragment:
#version 420
layout(binding = 0) uniform sampler2D Texture;
layout(binding = 1) uniform sampler2DShadow ShadowTexture;
in vec3 Position;
in vec3 Normal;
in vec4 ShadowCoord;
subroutine void RenderPassType();
subroutine uniform RenderPassType RenderPass;
uniform float xPixelOffset;
uniform float yPixelOffset;
layout(location = 0) out vec4 FragColor;
float lookup(vec2 offset)
{
return textureProj(ShadowTexture, ShadowCoord +
vec4(offset.x * xPixelOffset * ShadowCoord.w,
offset.y * yPixelOffset * ShadowCoord.w,
0.0, 0.0)
);
}
//SUBROUTINES
subroutine(RenderPassType)
void shadowPass()
{
vec4 textureColor = texture2D(Texture, TexCoords);
vec3 ambient, diffuse, specular;
phongModelDirectional(Position, Normal, ambient, diffuse, specular);
LightColorDS = diffuse + specular;
LightColorA = ambient;
float shadow;
if(ShadowCoord.w > 1.0){
float x,y;
for(y=-1.5; y<=1.5; y+=1.0)
for(x=-1.5; x<=1.5; x+=1.0)
shadow += lookup(vec2(x,y));
shadow /= 16.0;
}
FragColor = textureColor * vec4(LightColorDS * shadow + LightColorA, 1.0);
}
subroutine(RenderPassType)
void depthPass()
{
}
void main()
{
RenderPass();
}