NeomerArcana
NeomerArcana

Reputation: 2301

What am I doing wrong in sampling my shadowMap?

I'm using RenderDoc to verify that my shadowMap is being created properly and it has the correct pixels from the lights point of view.

It's just depth. And I can see the shadows have a depth of 0 and the rest has a depth of 1.

I'm passing my Lights MVP Matrix to my shader as well as this ShadowMap:

My vertex shader: #version 400

layout (location = 0) in vec4 in_position;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

uniform mat4 lightMVP;

out vec4 ex_positionLightSpace;

void main()
{
    gl_Position = projection * view * model * in_position;
    ex_positionLightSpace = lightMVP * in_position;
}

and here's my frag shader:

#version 400

layout (location = 0) out vec4 color;

in vec4 ex_positionLightSpace;

uniform sampler2D shadowMap;

float CalcShadowFactor(sampler2D shadowMapp, vec4 lightSpacePos)
{
    vec3 projectedCoords = lightSpacePos.xyz / lightSpacePos.w;
    vec2 coords;
    coords.x = 0.5 * projectedCoords.x + 0.5;
    coords.y = 0.5 * projectedCoords.y + 0.5;
    float z = 0.5 * projectedCoords.z + 0.5;
    float depth = texture(shadowMapp,coords).x;
    if(depth < z + 0.00001)
        return 0.2;
    return 1.0;
}

void main(void)
{
    float shadowFactor = CalcShadowFactor(shadowMap,ex_positionLightSpace);
    color = vec4(shadowFactor,shadowFactor,shadowFactor,1.0);
}

I've simplified my shaders so that all I want to see is either a very dim pixel or a very bright pixel. But all I'm getting is the very dim pixel.

I've checked that it's definitely the same Light MVP that was used to generate the shadowmap.

Edit: The issue turned out to be an incorrect LightMVP. I wasn't sending through the model matrix of the object, just the lights view and projection. Lesson learned.

Upvotes: 0

Views: 251

Answers (1)

mafian89
mafian89

Reputation: 81

have you tried sampler2DShadow? Usage is pretty simple:

uniform sampler2DShadow depthTexture; //does the depth comparison for you
//other uniforms
vec4 coord = shadowCoord / shadowCoord.w; //perspective division or use function textureProj() which does perspective division for you
float shadow = texture(depthTexture, vec3(coord.xyz));
//some other code, lighting etc.

Or are you limited to use only sampler2D?

Edit: You need to set GL_TEXTURE_COMPARE_FUNC Parameter and texture type must be GL_DEPTH_COMPONENT16 (24/32).

Upvotes: 1

Related Questions