繁体   English   中英

在片段着色器中从深度纹理读取

[英]Reading from a depth texture in a fragment shader

我有如下定义的深度纹理:

    //shadow FBO and texture
    depthFBO = new FrameBufferObject().create().bind();
    depthTexture = new Texture2D().create().bind()
            .storage2D(12, GL_DEPTH_COMPONENT32F, 4096, 4096)
            .minFilter(GL_LINEAR)
            .magFilter(GL_LINEAR)
            .compareMode(GL_COMPARE_REF_TO_TEXTURE)
            .compareFunc(GL_LEQUAL);
    depthFBO.texture2D(GL_DEPTH_ATTACHMENT, GL11.GL_TEXTURE_2D, depthTexture, 0)
            .checkStatus().unbind();
    depthTexture.unbind();

它是用Java / LWJGL / Own小型框架编写的,但想法应该很明确。

然后在某个时候使用片段着色器可视化其中的数据(片段的深度):

#version 430 core

layout(location = 7) uniform int screen_width;
layout(location = 8) uniform int screen_height;

layout(binding = 0) uniform sampler2D shadow_tex;

out vec4 color;

void main(void) {
    vec2 tex_coords = vec2(
        (gl_FragCoord.x - 100) / (screen_width / 5),
        (gl_FragCoord.y - (screen_height - (screen_height / 5) - 100)) / (screen_height / 5)
    );
    float red_channel = texture(shadow_tex, tex_coords).r;
    if (red_channel < 0.999) {
        red_channel = red_channel / 2.0;
    }
    color = vec4(vec3(red_channel), 1.0);
}

我的tex_coordsshadow_tex是正确的,但是在读取GL_DEPTH_COMPONENT32F格式时,我需要更多说明。

我想读取深度,并假设它以4字节的float值存储为0.0和1.0。

所以我当时我可以使用带红色texture的红色通道,但是看不到深度的差异。 除了不是精确地1.0,而是更低。 如果我不除以2.0,那么所有内容都会显示为白色。

截图

请注意,地板在某些点上是黑色的,但这是由于阴影映射失败,因此,我为什么要使其可视化-但是暂时将其设置为使用普通视图MVP而不是灯光,以确保深度信息正确保存。

更新 :深度值的着色现在可以在以下情况下正常工作:

#version 430 core

layout(location = 7) uniform int screen_width;
layout(location = 8) uniform int screen_height;

layout(binding = 0) uniform sampler2D shadow_tex;

out vec4 color;

float linearize_depth(float original_depth) {
    float near = 0.1;
    float far = 1000.0;
    return (2.0 * near) / (far + near - original_depth * (far - near));
}

void main(void) {
    //calculate texture coordinates, based on current dimensions and positions of the viewport
    vec2 tex_coords = vec2(
        (gl_FragCoord.x - 100) / (screen_width / 5),
        (gl_FragCoord.y - (screen_height - (screen_height / 5) - 100)) / (screen_height / 5)
    );

    //retrieve depth value from the red channel
    float red_channel = texture(shadow_tex, tex_coords).r;

    //colorize depth value, only if there actually is an object
    if (red_channel < 0.999) {
        red_channel = linearize_depth(red_channel) * 4.0;
    }
    color = vec4(vec3(red_channel), 1.0);
}

屏幕截图-工作方式

我仍然想澄清一下,尽管访问红色分量是否正确以获取深度值?

在GLSL 4.30中?

没有。

如果这确实是深度纹理(内部格式= GL_DEPTH_COMPONENT[...] ),则GLSL会以这种方式自动对其进行采样: vec4 (r, r, r, 1.0) 根据“深度纹理模式”(从GL 3.1 / GLSL 1.30中删除),旧版本的行为会有所不同。

现在,如果您的代码暗示它是一个具有比较功能的深度纹理,则应使用sampler2D对其进行采样。 但是,如果使用sampler2DShadow ,则使用texture (...)进行采样将返回单个 float与所有其他texture (...)重载不同(均会返回vec4 )。

希望这是对您粘贴的Java代码的疏忽,因为您的着色器应立即产生未定义的结果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM