首页 文章

对数深度缓冲区的世界空间位置

提问于
浏览
7

在将我当前的延迟渲染器更改为使用logarithmic depth buffer后,我无法解决,对于我来说,如何从深度缓冲区值重建世界空间深度 .

当我编写OpenGL默认z / w深度时,我可以通过从窗口空间转换到NDC空间来轻松计算此值,然后执行逆透视变换 .

我在第二遍片段着色器中完成了所有这些操作:

uniform sampler2D depth_tex;

uniform mat4 inv_view_proj_mat;

in vec2 uv_f;

vec3 reconstruct_pos(){
    float z = texture(depth_tex, uv_f).r;
    vec4 pos = vec4(uv_f, z, 1.0) * 2.0 - 1.0;
    pos = inv_view_proj_mat * pos;

    return pos.xyz / pos.w;
}

并得到一个看起来非常正确的结果:

Cube rendered with correct world-space position reconstruction

但现在通往一个简单的 z 值的道路并不那么容易(似乎也不应该这么难) .

我的第一次传递的顶点着色器具有日志深度:

#version 330 core
#extension GL_ARB_shading_language_420pack : require

layout(location = 0) in vec3 pos;
layout(location = 1) in vec2 uv;

uniform mat4 mvp_mat;

uniform float FC;

out vec2 uv_f;
out float logz_f;
out float FC_2_f;

void main(){
    gl_Position = mvp_mat * vec4(pos, 1.0);

    logz_f = 1.0 + gl_Position.w;

    gl_Position.z = (log2(max(1e-6, logz_f)) * FC - 1.0) * gl_Position.w;

    FC_2_f = FC * 0.5;
}

我的片段着色器:

#version 330 core
#extension GL_ARB_shading_language_420pack : require

// other uniforms and output variables

in vec2 uv_f;
in float FC_2_f;

void main(){
    gl_FragDepth = log2(logz_f) * FC_2_f;
}

我已经尝试了几种不同的方法来正确地取回z位置,都失败了 .

如果我在第二遍中重新定义我的 reconstruct_pos

vec3 reconstruct_pos(){
    vec4 pos = vec4(uv_f, get_depth(), 1.0) * 2.0 - 1.0;
    pos = inv_view_proj_mat * pos;

    return pos.xyz / pos.w;
}

这是我目前重建Z的尝试:

uniform float FC;

float get_depth(){
    float log2logz_FC_2 = texture(depth_tex, uv_f).r;
    float logz = pow(2, log2logz_FC_2 / (FC * 0.5));
    float pos_z = log2(max(1e-6, logz)) * FC - 1.0; // pos.z
    return pos_z;
}

解释:

log2logz_FC_2 :写入深度缓冲区的值,所以 log2(1.0 + gl_Position.w) * (FC / 2)

logz :只需 1.0 + gl_Position.w

pos_z :透视图之前 gl_Position.z 的值

返回值: gl_Position.z

当然,这只是我的工作 . 我不确定这些值到底是什么意思,因为我认为我搞砸了一些数学或者没有正确理解正在进行的转换 .

What is the correct way to get my world-space Z position from this logarithmic depth buffer?

1 回答

  • 0

    最后我发现这一切都错了 . 使用日志缓冲区返回世界空间位置的方法是:

    • 从纹理中检索深度

    • 重建gl_Position.w

    • 线性化重建深度

    • 转化为世界空间

    这是我在glsl中的实现:

    in vec2 uv_f;
    
    uniform float nearz;
    uniform float farz;
    
    uniform mat4 inv_view_proj_mat;
    
    float linearize_depth(in float depth){
        float a = farz / (farz - nearz);
        float b = farz * nearz / (nearz - farz);
        return a + b / depth;
    }
    
    float reconstruct_depth(){
        float depth = texture(depth_tex, uv_f).r;
        return pow(2.0, depth * log2(farz + 1.0)) - 1.0;
    }
    
    vec3 reconstruct_world_pos(){
        vec4 wpos =
            inv_view_proj_mat *
            (vec4(uv_f, linearize_depth(reconstruct_depth()), 1.0) * 2.0 - 1.0);
    
        return wpos.xyz / wpos.w;
    }
    

    当我使用默认的OpenGL深度缓冲区时,这给了我相同的结果(但具有更好的精度) .

相关问题