Display texture depth

I cannot get my depth for proper visualization. No errors occur, glCheckFramebufferStatus says it is also complete.

Below is the code, the screen always displays white. The depths are not 1, but very close:

EDIT:

So, I tried linearizing the depth inside the depth shader and then drawing it directly on the screen to make sure the values ​​are correct. They are true. However, even if I send this linearized depth to a full-screen quad-core shader (second below), the screen is still white.

public void initFramebuffers() {
    glBindFramebuffer(GL_FRAMEBUFFER, depthShader.fbo);
    depthShader.initTexture(width, height, GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthShader.tex, 0);
    glDrawBuffer(GL_NONE);
    glReadBuffer(GL_NONE);
}

public void initTexture(int width, int height, int format, int internalFormat) {
    tex = glGenTextures();
    glBindTexture(GL_TEXTURE_2D, tex);
    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_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height, 0, format, GL_FLOAT, (ByteBuffer)null);
}

Depth shader:

#version 400

in vec3 pos;
in float radius;

uniform mat4 mView;
uniform mat4 projection;
uniform vec2 screenSize;
uniform vec3 lightPos;

out float depth;

float linearizeDepth(float depth) {
    float n = 0.01;
    float f = 100;
    return (2.0 * n) / (f + n - depth * (f - n));
}    

void main() {
    //calculate normal
    vec3 normal;
    normal.xy = gl_PointCoord * 2.0 - 1.0;
    float r2 = dot(normal.xy, normal.xy);

    if (r2 > 1.0) {
        discard;
    }

    normal.z = sqrt(1.0 - r2);

    //calculate depth
    vec4 pixelPos = vec4(pos + normal * radius, 1.0);
    vec4 clipSpacePos = projection * pixelPos;

    depth = clipSpacePos.z / clipSpacePos.w * 0.5f + 0.5f;
    depth = linearizeDepth(depth);
}

A shader that reads in depth. The values ​​in linearizeDepth are my near and long distances:

#version 400

in vec2 coord;

uniform sampler2D depthMap;
uniform vec2 screenSize;
uniform mat4 projection;

out vec4 color;

float linearizeDepth(float depth) {
    float n = 0.01;
    float f = 100;
    return (2.0 * n) / (f + n - depth * (f - n));
}    

void main() {
    float curDepth = texture2D(depthMap, coord).x;
    //float d = linearizeDepth(curDepth);

    color = vec4(d, d, d, 1.0f);
}

Code for drawing everything:

//--------------------Particle Depth-----------------------
{
    glUseProgram(depthShader.program);
    glBindFramebuffer(GL_FRAMEBUFFER, depthShader.fbo);

    depthShader.particleDepthVAO(points);

    //Sets uniforms
    RenderUtility.addMatrix(depthShader, mView, "mView");
    RenderUtility.addMatrix(depthShader, projection, "projection");
    RenderUtility.addVector2(depthShader, screenSize, "screenSize");
    RenderUtility.addVector3(depthShader, lightPosition, "lightPos");

    glDisable(GL_BLEND);
    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glBindVertexArray(depthShader.vao);

    glDrawArrays(GL_POINTS, 0, points.size());  
}

    //Draw full screen
{
    glUseProgram(blurShader.program);
    glBindFramebuffer(GL_FRAMEBUFFER, 0);

    blurShader.blurDepthVAO();

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, depthShader.tex);
    glUniform1i(blurShader.depthMap, 0);

    //Sets uniforms 
    RenderUtility.addMatrix(blurShader, mView, "mView");
    RenderUtility.addMatrix(blurShader, projection, "projection");
    RenderUtility.addVector2(blurShader, screenSize, "screenSize");

    //glEnable(GL_DEPTH_TEST);

    glBindVertexArray(blurShader.vao);

    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    glViewport(0, 0, width, height);
}
+4
2

, (doh). 100% , - .

+2

.

FBO . :

glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthShader.tex, 0);
glDrawBuffer(GL_NONE);

, :

out float depth;
...
    depth = clipSpacePos.z / clipSpacePos.w * 0.5f + 0.5f;
    depth = linearizeDepth(depth);

, gl_FragDepth. , out depth , . , GL_COLOR_ATTACHMENT0. .

linearizeDepth()

float linearizeDepth(float depth) {
    float n = 0.01;
    float f = 100;
    return (2.0 * n) / (f + n - depth * (f - n));
}

depth = clipSpacePos.z / clipSpacePos.w * 0.5f + 0.5f;
depth = linearizeDepth(depth);

clipSpacePos, , linarizeDepth() 0.0 1.0. :

0.0 --> (2.0 * n) / (f + n)
1.0 --> 1.0

1.0, 0.0. , :

depth = clipSpacePos.z / clipSpacePos.w;

-1.0 1.0 , :

-1.0 --> n / f
1.0  --> 1.0

, 0.0 1.0, , .

,

. , , .

, . , . , , , :

vec4 pixelPos = vec4(pos + normal * radius, 1.0);
float f = 100.0;  // far plane
depth = -pixelPos.z / f;

, , z.

0.0 1.0, :

float n = 0.01;   // near plane
float f = 100.0;  // far plane
depth = (-pixelPos.z - n) / (f - n);
0

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


All Articles