Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 2 of 2 FirstFirst 12
Results 11 to 16 of 16

Thread: Spotlight and shadowmapping issues

  1. #11
    Intern Newbie
    Join Date
    Jan 2014
    Posts
    33
    Alright, will check that.
    I'd like to add all depth values sampled from the shadowmap appear to be in 0.97+; must be inconsistent with the 2nd picture?

  2. #12
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    3,191
    Quote Originally Posted by TheKaiser View Post
    Alright, will check that.
    I'd like to add all depth values sampled from the shadowmap appear to be in 0.97+; must be inconsistent with the 2nd picture?
    For a depth buffer rendered with a perspective projection (e.g. that from a point light source shadow map), the values are non-linear and do tend to move toward 1.0 fairly quickly past the near clip plane. You can use something like this to get back eye.z from the depth buffer and then render that as intensity (i.e. map -n..-f to 0..1).

  3. #13
    Intern Newbie
    Join Date
    Jan 2014
    Posts
    33
    I made the matrix bias multiplication with the light view-projection matrix before the shader execution and removed the added shadow bias constant, and here's the result:

    http://s18.postimg.org/9t6sft7s9/shadowmap.png

    The white artifacts are due to lack of shadow bias constant, but it shows there is actual shadowing; the strange thing is, you can see the models are lit, but the ground is not. Why would the shadowFactor cause the ground plane to be dark?

    Code :
        const std::string gDirLightFragmentShader =
        "#version 430                                                                                                                   \n \
                                                                                                                                        \n \
        layout(std140) uniform;                                                                                                         \n \
                                                                                                                                        \n \
        uniform UnifDirLight                                                                                                            \n \
        {                                                                                                                               \n \
            mat4 mWVPMatrix;                                                                                                            \n \
            mat4 mVPMatrix;                                                                                                             \n \
            vec4 mLightColor;                                                                                                           \n \
            vec4 mLightDir;                                                                                                             \n \
            vec4 mGamma;                                                                                                                \n \
            vec2 mScreenSize;                                                                                                           \n \
        } UnifDirLightPass;                                                                                                             \n \
                                                                                                                                        \n \
        layout (binding = 2) uniform sampler2D unifPositionTexture;                                                                     \n \
        layout (binding = 3) uniform sampler2D unifNormalTexture;                                                                       \n \
        layout (binding = 4) uniform sampler2D unifDiffuseTexture;                                                                      \n \
        layout (binding = 5) uniform sampler2D unifShadowTexture;                                                                       \n \
                                                                                                                                        \n \
        out vec4 fragColor;                                                                                                             \n \
                                                                                                                                        \n \
        void main()                                                                                                                     \n \
        {                                                                                                                               \n \
            vec2 texcoord = gl_FragCoord.xy / UnifDirLightPass.mScreenSize;                                                             \n \
                                                                                                                                        \n \
            vec3 worldPos = texture(unifPositionTexture, texcoord).xyz;                                                                 \n \
            vec3 normal   = normalize(texture(unifNormalTexture, texcoord).xyz);                                                        \n \
            vec3 diffuse  = texture(unifDiffuseTexture, texcoord).xyz;                                                                  \n \
                                                                                                                                        \n \
            vec4 lightClipPos  = UnifDirLightPass.mVPMatrix * vec4(worldPos, 1.0);                                                      \n \
            vec3 projCoords    = lightClipPos.xyz / lightClipPos.w;                                                                   \n \
                                                                                                                                        \n \
            float depthValue   = texture(unifShadowTexture, projCoords.xy).x;                                                           \n \
            float shadowFactor = 0.0;                                                                                                   \n \
            if (depthValue >= (projCoords.z))                                                                                  \n \
                 shadowFactor = 1.0;                                                                                                    \n \
                                                                                                                                        \n \
            float angleNormal = clamp(dot(normal, UnifDirLightPass.mLightDir.xyz), 0, 1);                                               \n \
                                                                                                                                        \n \
            fragColor = vec4(diffuse, 1.0) * shadowFactor * angleNormal * UnifDirLightPass.mLightColor;                                 \n \
        }                                                                                                                               \n";
    Last edited by TheKaiser; 01-19-2014 at 09:55 AM.

  4. #14
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    3,191
    Quote Originally Posted by TheKaiser View Post
    Why would the shadowFactor cause the ground plane to be dark?
    In your code it's always 0 or 1, and you're multiplying it directly into the output color. If it's 0, so's the output color.

  5. #15
    Intern Newbie
    Join Date
    Jan 2014
    Posts
    33
    I managed to get it working using the sampler2Dshadow and the following adjustments:

    Code :
        const std::string gDirLightFragmentShader =
        "#version 430                                                                                                                   \n \
                                                                                                                                        \n \
        layout(std140) uniform;                                                                                                         \n \
                                                                                                                                        \n \
        uniform UnifDirLight                                                                                                            \n \
        {                                                                                                                               \n \
            mat4 mWVPMatrix;                                                                                                            \n \
            mat4 mVPMatrix;                                                                                                             \n \
            vec4 mLightColor;                                                                                                           \n \
            vec4 mLightDir;                                                                                                             \n \
            vec4 mGamma;                                                                                                                \n \
            vec2 mScreenSize;                                                                                                           \n \
        } UnifDirLightPass;                                                                                                             \n \
                                                                                                                                        \n \
        layout (binding = 2) uniform sampler2D unifPositionTexture;                                                                     \n \
        layout (binding = 3) uniform sampler2D unifNormalTexture;                                                                       \n \
        layout (binding = 4) uniform sampler2D unifDiffuseTexture;                                                                      \n \
        layout (binding = 5) uniform sampler2DShadow unifShadowTexture;                                                                 \n \
                                                                                                                                        \n \
        out vec4 fragColor;                                                                                                             \n \
                                                                                                                                        \n \
        void main()                                                                                                                     \n \
        {                                                                                                                               \n \
            vec2 texcoord = gl_FragCoord.xy / UnifDirLightPass.mScreenSize;                                                             \n \
                                                                                                                                        \n \
            vec3 worldPos = texture(unifPositionTexture, texcoord).xyz;                                                                 \n \
            vec3 normal   = normalize(texture(unifNormalTexture, texcoord).xyz);                                                        \n \
            vec3 diffuse  = texture(unifDiffuseTexture, texcoord).xyz;                                                                  \n \
                                                                                                                                        \n \
            vec4 lightClipPos = UnifDirLightPass.mVPMatrix * vec4(worldPos, 1.0);                                                       \n \
            vec3 projCoords   = lightClipPos.xyz / lightClipPos.w;                                                                      \n \
            projCoords.z      = projCoords.z - 0.00001;                                                                                 \n \
                                                                                                                                        \n \
            float visibilty  = texture(unifShadowTexture, projCoords); //1.0;                                                           \n \
                                                                                                                                        \n \
            float angleNormal = clamp(dot(normal, UnifDirLightPass.mLightDir.xyz), 0, 1);                                               \n \
                                                                                                                                        \n \
            fragColor = vec4(diffuse, 1.0) * visibilty * angleNormal * UnifDirLightPass.mLightColor;                                    \n \
        }                                                                                                                               \n";

    As for why I couldnt get it to work without the sampler2Dshadow, I do not know - from what I understand, the two should perform the same function.
    Nevertheless, thanks the information!

  6. #16
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    3,191
    In order to use a sampler2DShadow, you need to provide a texture which is of base type GL_DEPTH_COMPONENT and have the depth compare mode (GL_TEXTURE_COMPARE_MODE) enabled (e.g. set to GL_COMPARE_R_TO_TEXTURE). If you do so, then the GPU will do the depth comparison behind-the-scenes, possibly with PCF filting (if you enable it with GL_LINEAR and if your GPU supports it).

    If you just use a sampler2D, then you shouldn't enable the compare mode and you of course have to do the depth comparisons yourself.

    Sorry, I missed seeing that you were just using a sampler2D (and thus getting 0..1 depth components back from your lookup) and not doing the depth compare logic in your shader.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •