我需要在 OpenGL 计算着色器中读取深度纹理数据。
这是纹理初始化代码。
glActiveTexture(GL_TEXTURE4);
glGenTextures(1, &(framebufferDesc->m_DepthTextureId));
glBindTexture(GL_TEXTURE_2D, framebufferDesc->m_DepthTextureId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(
GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, renderWidth, renderHeight, 0, GL_DEPTH_COMPONENT,
GL_UNSIGNED_BYTE, nullptr);
我的目标是找到“最大”深度。 1.0 或 0xffffff。
首先,我尝试这个。
//bind texture, (c++)
glBindImageTexture(2, m_EyeDesc[nEye].m_HMDDepthTextureId, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R32UI);
//access texture (glsl)
layout (local_size_x = 16, local_size_y = 16) in;
layout(binding = 0, rgba8) uniform image2D img_input;
layout(binding = 1) uniform writeonly uimage2D img_output;
layout(binding = 2, r32ui) uniform uimage2D depthMap;
void main() {
ivec2 p = ivec2(gl_GlobalInvocationID.xy), temp_p;
uint u_empty_color = 0xffffffff;
//uint u_empty_color = 4294967295;
uvec4 depth;
depth = imageLoad(depthMap, p);
if (u_empty_color == depth.x) {//hole
imageStore(img_output, p, hole);
}
}
但上面的代码不起作用。
我尝试一些方法,将深度组件24更改为深度组件32,将0xffffffff更改为0xffffff,将r32ui更改为r32f。
但一切都失败了。
我尝试另一种方式,它的工作。
uniform sampler2D depthMap;
layout (local_size_x = 16, local_size_y = 16) in;
layout(binding = 0, rgba8) uniform image2D img_input;
layout(binding = 1) uniform writeonly uimage2D img_output;
void main() {
ivec2 p = ivec2(gl_GlobalInvocationID.xy), temp_p;
float u_empty_color = 1.0;
vec2 pos = vec2(gl_GlobalInvocationID.x+1, gl_GlobalInvocationID.y+1);
pos.x = pos.x / u_width;
pos.y = pos.y / u_height;
vec4 depth;
depth = texture(depthMap, pos);
if (u_empty_color == depth.x) {//hole
imageStore(img_output, p, hole);
}
}
但是,我认为“/”太糟糕了!上面的代码很简单,真正的代码那么长......我调用了很多次。
我如何访问 gl_depth_component 24 格式?我不能?
请帮我!