gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 13:30:56 25 4
gpt4 key购买 nike

我的深度纹理定义如下:

    //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/自己的小框架写的,但是思路应该很清晰。

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

#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。

所以我我可以使用纹理给我返回的红色 channel ,但是我看不到深度上的差异。除此之外,它并不完全是 1.0,而是稍低一些。如果我不除以 2.0,那么一切都会显示为白色。

screenshot

请注意,地板在某些地方是黑色的,但这是由于阴影贴图失败造成的,因此我将其可视化 - 但它暂时设置为使用普通 View 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

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

最佳答案

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

关于java - 从片段着色器中读取深度纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21576367/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com