🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Shadow Mapping does not display correctly - dark square on the ground

Started by
4 comments, last by Enzio599 2 years, 6 months ago

I try to make shadow mapping with opengl es 3 but it does not work correctly. I tried with several scenes but each time I have a part of projected shadow and another part of black square. I can provide other visuals or explaination if needed.

This is an example where the spot light is on the right side and oriented to the left, it is the shadow mapping which display the dark part on the image :

#version 300 es

layout(location = 0) in vec4 position;

uniform mat4 projLightWorldMatrix;
out highp vec2 fragmentDepth;

void main()
{
    vec4 posInLightSpace = projLightWorldMatrix * position;
    fragmentDepth = vec2(posInLightSpace.zw);
    gl_Position = posInLightSpace;/
}


#version 300 es
precision mediump float;

layout(location = 0) out highp float finalDepth;
in highp vec2 fragmentDepth;

void main()
{
    finalDepth = gl_FragCoord.z;
    //finalDepth = fragmentDepth.x / fragmentDepth.y;// * 0.5 + 0.5 ; //vec4( depth, depth, depth, 1.0);
}

out highp vec4 normal;
out highp vec4 position ;
out lowp vec4 texCoordinates ;
out highp vec4 shadowCoordinates ;
out mediump vec4 normalFromNormalMap;

uniform mat4 modelIdentityMatrix;
uniform mat4 modelMatrix;
uniform mat4 viewMatrix;
uniform mat4 projMatrix;

uniform mat4 biasProjLightWorldMatrix;
uniform mat4 projLightWorldMatrix2;
uniform mat4 projLightWorldMatrix;

//out vec4 shadowCoordinates;


void main()
{
    //colors = inColors ;
    mat4 viewWorld = viewMatrix * modelMatrix ;
    
    normal = normalize(viewWorld * inNormal) ;//normalize here !!! ??
    
    position = viewWorld * vPosition;//useful ??
    texCoordinates = inTexCoordinates ;
    
    position = vPosition;
    
    //shadowCoordinates = projLightWorldMatrix * vPosition ;
    //shadowCoordinates = shadowCoordinates * 0.5 + 0.5 ;
    
    
    //shadowCoordinates = biasProjLightWorldMatrix * vPosition ;
    //shadowCoordinates = vPosition * biasProjLightWorldMatrix ;
    shadowCoordinates = projLightWorldMatrix * vPosition ;
    
    gl_Position = (projMatrix * viewWorld * vPosition);

}


void main()
{

    float shadowingLevel = 1.0 ;
    
    vec3 shadowMapping = shadowCoordinates.xyz / shadowCoordinates.www;
    shadowMapping = shadowMapping * 0.5 + 0.5 ;
    //shadowMapping.y = 1.0 - shadowMapping.y;
    
        if( (shadowMapping.x ) >= 0.0 && (shadowMapping.x) <= 1.0 && (shadowMapping.y) >= 0.0 && (shadowMapping.y) <= 1.0)
        //if((clamp(shadowCoordinates.x, 0.0, 1.0) == shadowCoordinates.x) && (clamp(shadowCoordinates.y, 0.0, 1.0) == shadowCoordinates.y) )
        {
            
            //if ( texture( shadowMapSampler, shadowMapping.xy/*, 5.0*/ ).z  <  ( shadowMapping.z ) )
            //if ( textureProj( shadowMapSampler, shadowCoordinates.xyw, 0.00050).z  <  ( (shadowCoordinates.z) / shadowCoordinates.w ) )
            {
                shadowingLevel = (shadowMapping.z - 0.0) > texture( shadowMapSampler, shadowMapping.xy ).r  ? 1.0 : 0.45;
                
                //fragmentColor = texture(textureSampler, texCoordinates.xy)*0.45 ;
            }
            /*if( shadowMapping.z > 1.0)
                shadowingLevel = 1.0;*/
            
        }
    if(shadowMapping.z > 1.0 )
        shadowingLevel = 1.0;

    fragmentColor = texture(textureSampler, texCoordinates.xy) * shadowingLevel;



    glActiveTexture(GL_TEXTURE4);
    glGenTextures(1, shadowTextureId);
    glBindTexture(GL_TEXTURE_2D, shadowTextureId[0]);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);//OR GL_LINEAR for filtering ??
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_LOD, 0);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LOD, 0);
     

    //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_NONE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);//remettre ??
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL); //GL_LEQUAL);//remettre ?? GL_ALWAYS
    
    CheckGLError();
    
    //NULL means reserve texture memory, but texels are undefined
    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F /*GL_DEPTH_COMPONENT16*/, SHADOW_MAP_WIDTH, SHADOW_MAP_HEIGHT, 0, GL_DEPTH_COMPONENT, GL_FLOAT /*GL_UNSIGNED_SHORT*/, NULL);
    
    CheckGLError();
    
    glGenFramebuffers(1, frameBufferShadowMapId);
    glBindFramebuffer(GL_DRAW_FRAMEBUFFER, frameBufferShadowMapId[0]);
    
    const GLenum attachement = GL_NONE ;
    glDrawBuffers(1, &attachement);
    glReadBuffer(GL_NONE);
    
    //glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, GL_TEXTURE_2D, shadowTextureId, 0);
    glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, shadowTextureId[0], 0);


//vertex shader
void main()
{
    //colors = inColors ;
    mat4 viewWorld = viewMatrix * modelMatrix ;
    
    normal = normalize(viewWorld * inNormal) ;//normalize here !!! ??
    
    position = viewWorld * vPosition;//useful ??
    texCoordinates = inTexCoordinates ;
    
    position = vPosition;
    
    //shadowCoordinates = projLightWorldMatrix * vPosition ;
    //shadowCoordinates = shadowCoordinates * 0.5 + 0.5 ;
    
    
    //shadowCoordinates = biasProjLightWorldMatrix * vPosition ;
    //shadowCoordinates = vPosition * biasProjLightWorldMatrix ;
    shadowCoordinates = projLightWorldMatrix * vPosition ;
    
    gl_Position = (projMatrix * viewWorld * vPosition);

}


//fragment shader

void main()
{

    float shadowingLevel = 1.0 ;
    
    vec3 shadowMapping = shadowCoordinates.xyz / shadowCoordinates.www;
    shadowMapping = shadowMapping * 0.5 + 0.5 ;
    //shadowMapping.y = 1.0 - shadowMapping.y;
    
        if( (shadowMapping.x ) >= 0.0 && (shadowMapping.x) <= 1.0 && (shadowMapping.y) >= 0.0 && (shadowMapping.y) <= 1.0)
        //if((clamp(shadowCoordinates.x, 0.0, 1.0) == shadowCoordinates.x) && (clamp(shadowCoordinates.y, 0.0, 1.0) == shadowCoordinates.y) )
        {
            
            //if ( texture( shadowMapSampler, shadowMapping.xy/*, 5.0*/ ).z  <  ( shadowMapping.z ) )
            //if ( textureProj( shadowMapSampler, shadowCoordinates.xyw, 0.00050).z  <  ( (shadowCoordinates.z) / shadowCoordinates.w ) )
            {
                shadowingLevel = (shadowMapping.z - 0.0) > texture( shadowMapSampler, shadowMapping.xy ).r  ? 1.0 : 0.45;
                
                //fragmentColor = texture(textureSampler, texCoordinates.xy)*0.45 ;
            }
            /*if( shadowMapping.z > 1.0)
                shadowingLevel = 1.0;*/
            
        }
    if(shadowMapping.z > 1.0 )
        shadowingLevel = 1.0;

    fragmentColor = texture(textureSampler, texCoordinates.xy) * shadowingLevel;
Advertisement

in DirectX 11 I have fixed a similar sort of issue by just confirming that I am send a transposed matrix to shader …

Thank you, I am going to try.

I tried and it didn't work. Which matrix did you transpose ?

theScore said:

I tried and it didn't work. Which matrix did you transpose ?

obviously world view and projection…

This topic is closed to new replies.

Advertisement