0

The problem is that artifacts appear in the shadows at a great distance. I want to try to make a logarithmic depth buffer, but I don’t understand where it should be done and how ... I use point light method for omnidirectional shadow maps

enter image description here

Vertex shader:

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

uniform mat4 model;
uniform float zCoef;
//out vec4 pos;

void main() {
    gl_Position = model * vec4(aPos, 1.0f);
//    gl_Position.z = log2(max(1e-6, gl_Position.w + 1.0)) * zCoef - 1.0;
//    gl_Position.z *= gl_Position.w;
}

Geometry shader:

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

uniform mat4 shadowMatrices[6];
uniform float zCoef;

out vec4 FragPos; // FragPos from GS (output per emitvertex)

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;
//            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;
uniform float zCoef;

void main() {
    float lightDistance = length(FragPos.xyz - lightPos);

    lightDistance = lightDistance / farPlane;

    gl_FragDepth = lightDistance;
}

Guys, pls help me,i tried to use the linearization depth, it didn't work too..

  • 1
    Perspective projection already uses logarithmic depth buffer ... I think you want to use linear depth buffer instead to avoid problems like this ... see [How to correctly linearize depth in OpenGL](https://stackoverflow.com/a/42515399/2521214) – Spektre Jul 08 '21 at 06:11
  • 1
    See also [How to render depth linearly in modern OpenGL with gl_FragCoord.z in fragment shader?](https://stackoverflow.com/questions/7777913/how-to-render-depth-linearly-in-modern-opengl-with-gl-fragcoord-z-in-fragment-sh/45710371#45710371) – Rabbid76 Jul 08 '21 at 11:37
  • @Spektre, I tried something like this `float z_n = 2.0 * lightDistance - 1.0; float z = 2.0 * nearPlane * farPlane / (farPlane + nearPlane - z_n * (farPlane - nearPlane));` but it doesn't work.. – Глеб Труфанов Jul 08 '21 at 15:53
  • 1
    Can you tell us a little more than just *"but it doesn't work."*? – Rabbid76 Jul 08 '21 at 16:11
  • @Rabbid76, shadows just doesn't works, everything is black..what else can i do? how can i turn on 32bit depth buffer? – Глеб Труфанов Jul 08 '21 at 16:12
  • @ГлебТруфанов 1. you need to pass the correct depth from vertex to fragment as `out/in` variable, and in fragment overide `gl_FragDepth` with it, do not forget to have it in range `<-1,+1>` and only half of it is visible... 2. for the 32bit depth see [complete GL+GLSL+VAO/VBO C++ example](https://stackoverflow.com/a/31913542/2521214) and [What is the proper OpenGL initialisation on Intel HD 3000?](https://stackoverflow.com/q/19099162/2521214) check the `PIXELFORMATDESCRIPTOR` usage while creating of GL context ... IIRC another option is to use FBO with 32bit depth texture attached ... – Spektre Jul 09 '21 at 05:02

0 Answers0