0

I am using omnidirectional shadow maps for point lights, but the precision of the depth buffer is sorely lacking. I'm trying to apply a log z-buffer, but I don't understand what I am doing wrong. If I use it in a vertex shader, then the shadows do not work, and if in a geometric shader, then there is no effect.

Vertex shader:

#version 460 core
layout (location = 0) in vec3 aPos;

uniform mat4 model;
uniform float zCoef; // 2.0f / glm::log2(camera.GetFar() + 1.0);

void main() {
    gl_Position = model * vec4(aPos, 1.0f);

    // Log z-buffer (here the shadows stop working!!!)
    gl_Position.r = log2(max(1e-6, gl_Position.w + 1.0)) * zCoef - 1.0;
    gl_Position.r *= gl_Position.w;
}

Geometry shader:

#version 460 core
layout (triangles) in;
layout (triangle_strip, max_vertices = 18) out;

uniform mat4 shadowMatrices[6];

out vec4 FragPos;
uniform float zCoef; // 2.0f / glm::log2(camera.GetFar() + 1.0);

void main()
{
    for(int face = 0; face < 6; ++face) {
        gl_Layer = face;
        for(int i = 0; i < 3; ++i) {
            FragPos = gl_in[i].gl_Position;
            gl_Position = shadowMatrices[face] * FragPos;
            
            // There is no effect here
            gl_Position.z = log2(max(1e-6, gl_Position.w + 1.0)) * zCoef - 1.0;
            gl_Position.z *= gl_Position.w;
            EmitVertex();
        }
        EndPrimitive();
    }
}

Fragment shader:

#version 460 core
in vec4 FragPos;

uniform vec3 lightPos;
uniform float farPlane;

void main() {
    float lightDistance = length(FragPos.xyz - lightPos);
    gl_FragDepth = lightDistance / farPlane;
}

If such an implementation is impossible, then please give me ideas on what to do.

0 Answers0