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

时间:2014-02-05 11:45:26

标签: java opengl textures fragment depth-buffer

我的深度纹理定义如下:

    //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个字节的浮点值存储为0.0和1.0。

所以我思考我可以使用texture给我回来的红色通道,但是我看不出深度上的差异。除了它不完全是1.0,但有点低。如果我没有除以2.0,那么一切都会显示为白色。

screenshot

请注意,地板在某些位置是黑色的,但这是由于阴影映射失败,因此我可视化它的原因 - 但它暂时设置为使用普通视图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);
}

screenshot - kind of working

我仍然想澄清一下,如果访问红色组件是正确的,以检索深度值?

1 个答案:

答案 0 :(得分:1)

在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代码中的疏忽,因为您的着色器应该正在生成未定义的结果。

相关问题