Problem with shadow mapping (fragment depth)

I have tryed to impement shadow mapping in my OpenGL project, but if I do this :
shadowCoords = biaslightProjectionMatrixlightViewMatrixlightModelMatrixvertexPosition;
and then
shadowCoords/=shadowCoords.w;
the z and w value is always 1.

the light matrices should be right, they are generated the same was as my cameraViewMatrix, cameraProjectionMAtrix, and so on…

So I cant compare the depth texture to my coords in lightspace(shadowCoords);

Do anyone know what I did wrong ?

Try this.

I didn’t based mine from this tutorial but I recently found they have good articles, well written, and oriented to modern OpenGL.

Its the same calculation of the shadowCoords :
depthMVP = depthProjectionMatrixdepthViewMatrixdepthModelMatrix;
biasDepthMatrix = bias*depthMVP;

shadowCoord = biasDepthMatrix*vertexPosition;
(depth is in my case light)

I use the same calculation but it don’t works…

That w==1 is not a bad thing (depending on your matrices).

For your z that always equals to 1. This is weird. I can suggest you to check if depth test is enabled, if the texture/fbo is well set for depth. It seems there’s something wrong here.

More description and code is also welcome.

this is my vertex shader :


#version 440

layout(location = 0) in vec3 vertexPosition;
layout(location = 3) in vec4 vertexColor;
layout(location = 2) in vec4 vertexNormal;
layout(location = 8) in vec2 vertexTexCoord;

out vec4 Color;
out vec4 Normal;
out vec4 Position;
out vec2 TexCoord;

out vec4 shadowMapCoords;

out mat3 normalMapMatrix;

uniform mat4 modelMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat4 modelViewMatrix;
uniform mat4 normalMatrix;


uniform mat4 lightModelMatrix;
uniform mat4 lightViewMatrix;
uniform mat4 lightProjectionMatrix;

uniform vec3 ambientLight;
uniform vec3 diffuseLight;
uniform vec3 specularLight;
uniform vec3 lightPosition;
uniform float shininess;
uniform vec3 eyePosition;

vec3 surfaceToLight;
vec3 surfaceToCam;
vec3 reflectionVector;
float cosAngle;


mat4 bias = mat4(0.5, 0, 0, 0,
         0, 0.5, 0, 0,
         0, 0, 0.5, 0,
         0.5, 0.5, 0.5, 1);

void SetNormalMapMatrix()
{
  float xAngle = 1-dot( Normal.yz, vec2(1, 0) );
  float zAngle = 1-dot( Normal.xy, vec2(0, 1) );
  
  mat3 xMatrix = mat3(1, 0, 0,
              0, cos(xAngle), -sin(xAngle),
              0, sin(xAngle), cos(xAngle));
  mat3 zMatrix = mat3(cos(zAngle), -sin(zAngle), 0,
              sin(zAngle), cos(zAngle), 0,
              0, 0, 1);
  xMatrix = transpose(xMatrix);
  zMatrix = transpose(zMatrix);
  
  normalMapMatrix = zMatrix*xMatrix;
}


void main() 
{
  
  gl_Position = projectionMatrix*viewMatrix*modelMatrix*vec4(vertexPosition, 1);
  Color = vertexColor;
  Normal = normalize(normalMatrix*vertexNormal);
  Position = modelViewMatrix*vec4(vertexPosition, 1);
  TexCoord = vertexTexCoord;
  
  mat4 lightSpaceMatrix = bias*lightProjectionMatrix*lightViewMatrix*lightModelMatrix;
  
  shadowMapCoords = lightSpaceMatrix*vec4(vertexPosition, 1);
  
  SetNormalMapMatrix();
}

//END 

and this is my fragment shader :


#version 440

in vec4 Color;
in vec4 Normal;
in vec4 Position;
in vec2 TexCoord;

in vec4 shadowMapCoords;

in mat3 normalMapMatrix;

out vec4 fragmentColor;

vec3 viewVector;

uniform sampler2D Texture0;
uniform sampler2D Texture1;

uniform vec3 ambientLight;
uniform vec3 diffuseLight;
uniform vec3 specularLight;
uniform vec3 lightPosition;
uniform float shininess;
uniform vec3 eyePosition;

vec3 surfaceToLight;
vec3 surfaceToCam;
vec3 reflectionVector;

vec3 diffuseColor;
vec3 specularColor;

float cosAngle;


void Phong(vec3 position, vec3 normal)
{
  normal = normalize(normal);
  surfaceToLight = normalize(lightPosition.xyz-position);
  surfaceToCam = normalize(eyePosition.xyz-position);
  reflectionVector = normalize(reflect(-surfaceToLight, normal));
  cosAngle = max(dot(reflectionVector, surfaceToCam), 0);
  
  float specularCoefficient = pow(cosAngle, 3);
  float diffuseCoefficient = max(dot(normalize(normal), normalize(surfaceToLight)), 0);
  
  diffuseColor = diffuseColor + diffuseCoefficient*diffuseLight;
  specularColor = specularColor+ specularCoefficient*specularLight; 
}



void main()
{
  //SetNormalMapMatrix();
  
  
  vec4 tex0 = texture(Texture0, TexCoord);
  vec4 tex1 = texture(Texture1, TexCoord);
  
  vec4 shadowCoords = shadowMapCoords;
  shadowCoords/=shadowCoords.w;
  
  float shadowMapDepth = texture(Texture0, shadowCoords.xy).r;
  
  vec3 t = vec3( tex1.rgb-vec3(0.5, 0.5, 0.5) )*2;
  
  Phong(Position.xyz, normalMapMatrix*t.xzy);

  fragmentColor = vec4(shadowCoords.zzz, 1);
}

//END

I have deleted the lightning and displayes the fragmentdepth in lightspace but I get a white result if I use 1-depth I get a black result.
The same thing hapens If I write the camera space coords in a vec4 and use this for displaying the depth.
But If I use the value given to gl_Position–> gl_FragCoord in the fragmentshader I get a correct result…

Maybe his helps…

I have deleted the lightning and displayes the fragmentdepth in lightspace but I get a white result

This generally means that the depth texture/fbo is not set correctly.
You can see this thread for example.

I don’t use the shadow maps depth value to define my fragment color, I use the Matrix of the Light multiplied with my vertex, I need this for checking if the fragment is behind the nearest depth which is stored in the shadow map.
Momently I do not render to a FBO, I use a grayscale texture, only for trying if the shader works.

I solved it I had to devide 1 by my w component :
dragmentDepthInLightSpace = 1/shadowMapCoords.w;