I'm trying to implement shadow mapping in my game. The shaders you see below result in correctly drawn shadows for the game's map, but all the models walking around on the map are completely black.
Here's a screenshot:
I suspect the problem lies with the calculation of the world position. The gl_Vertex is not transformed in any way. Because the map is generated with absolute world coordinates, the transformation with the light matrix results in a correct relative position that can be used to perform the shadow mapping.
However, my 3D models are all very close to the origin. So if their coordinates are plainly transformed using the light matrix, they will most likely never be lit.
I'm wondering if this could be the case, and if so, how I could fix it.
Here's my vertex shader:
#version 120
uniform mat4x4 LightMatrixValue;
varying vec4 shadowMapPosition;
varying vec3 worldPos;
void main(void)
{
vec4 modelPos = gl_Vertex;
worldPos=modelPos.xyz/modelPos.w;
vec4 lightPos = LightMatrixValue*modelPos;
shadowMapPosition = 0.5 * (lightPos.xyzw +lightPos.wwww);
gl_Position = ftransform();
}
And the fragment shader: varying vec4 shadowMapPosition; varying vec3 worldPos;
uniform sampler2D depthMap;
uniform vec4 LightPosition;
void main(void)
{
vec4 textureColour = gl_Color;
vec3 realShadowMapPosition = shadowMapPosition.xyz/shadowMapPosition.w;
float depthSm = texture2D(depthMap, realShadowMapPosition.xy).r;
if (depthSm < realShadowMapPosition.z-0.0001)
{
textureColour = vec4(0, 0, 0, 1);
}
gl_FragColor= textureColour;
}
LightMatrixValue*modelPos
for transformation in the light pov. And the corresponding lookup. But without knowing how the shadow map is created it is hard to guess what is wrong.0.5 * (lightPos.xyzw +lightPos.wwww)
could be the problem. But also therealShadowMapPosition.xy
in the texture lookup. – t.niesez
value. Why do you do this0.5 * (lightPos.xyzw +lightPos.wwww)
(what do you expect from this) lightPos this is the vector from the origin of the light position to the vertex (assumingLightMatrixValue
is the same matrix used in the Shadow shader) . So with this calculation (depending on thew
) you move the point towards the light source or in the other direction. – t.niese