Displaying Shadows with Delayed Rendering (OpenGL)

So, I tried to lure my head around the shadow mapping in OpenGL last week, and this is not so great. So I was hoping someone here would help me. I learned about shadow matching from LearnOpenGL's shadow binding tutorial, which uses forward rendering, and from what I collect, it looks like the rendering of shadows with deferred shadows is slightly different. To start, here is my shadow map:

Shadow map picture

- , , , , . , - . , , , . , , , .

//Create LSMatrix
    GLfloat near_plane = 1.0f;
    GLfloat far_plane = 10.0f;
    glm::mat4 lightProjMat = glm::ortho(-10.0f, 10.0f, -10.0f, 10.0f, near_plane, far_plane);
    glm::mat4 lightPerspective = glm::lookAt(glm::vec3(-2.0f, 6.0f, -1.0f),
                                                glm::vec3(0.0f, 0.0f, 0.0f),
                                                glm::vec3(0.0f, 1.0f, 0.0f));

    shadowBuf.lightSpaceMat = lightProjMat * lightPerspective;

, . , . . , , . , .

GLSL . (LSMat)

void main()
{
    gl_Position = vec4(position, 1.0f);
    vs_out.uv = uv;
    vs_out.normal = normal;
    vs_out.FragPos = vec3(model * vec4(position, 1.0f));
    vs_out.FragPosLS = LSMat * vec4(vs_out.FragPos, 1.0f); //Fragment position in light space
}

.

, , . (, )

, :

float shadowCalc()
{
    vec4 fragPosLS = texture(gFragPosLS, uvs); //Fetch the lightspace frag pos from the texture

    vec3 projCoords = fragPosLS.xyz / fragPosLS.w; //Manually do perspective divison
    projCoords = projCoords * 0.5 + 0.5; //Get the pos in [0,1] range

    float closestDepth = texture(shadowMap, projCoords.xy).r;
    float currentDepth = projCoords.z;

    float shadow = currentDepth > closestDepth ? 1.0 : 0.0;

    return shadow;
}

, :

    float shadowValue = shadowCalc();
    diffuse = diffuse * (1.0 - shadowValue);
    specular = specular * (1.0 - shadowValue);

    return ((diffuse * vec4(gColor, 1.0f)) + specular + ambient);

, , :

Picture of the result

- , UV- Maya . , . , , , , shadowCalc, . 100%.

- , , . , , . , , , . , , , . , - . - , - -, .

, -, , .

+4
1

: , , . Z . ,

gl.texImage2D(gl.TEXTURE_2D, 0, gl.DEPTH_COMPONENT16, 2048, 2048, 0, gl.DEPTH_COMPONENT, gl.UNSIGNED_SHORT, null);

(-1;+1),

texture(shadowMap, projCoords.xy).r - .5) * 2.

:

float bias = .01;
float visibility = (texture(shadowMap, projCoords.xy).r - .5) * 2. < projCoords.z - bias ? 0.1: 1.0;
0

Source: https://habr.com/ru/post/1651322/


All Articles