openGL线性深度信息

时间:2015-03-29 19:25:19

标签: opengl glsl

我想实现Screen Space渲染流体,但我在获取深度信息方面遇到了麻烦。

我得到的深度图像太白了。我想是因为深度不是线性的。当我看到这个图像非常接近时,它会有一个合理的性能,我可以看到深度变化。在同一个圆圈中,中心的颜色是黑色,当靠近边缘时颜色会变成白色和白色。

所以我对深度做了线性,它看起来更好,但深度也不会在同一个圆圈中改变。这是我的顶点着色器:

#version 400

uniform float pointScale;

layout(location=0) in vec3 position;

uniform mat4 viewMat, projMat, modelMat;
uniform mat3 normalMat;

out vec3 fs_PosEye;
out vec4 fs_Color;

void main() 
{

vec3 posEye = (viewMat * vec4(position.xyz, 1.0f)).xyz;
    float dist = length(posEye);
    gl_PointSize = 1.0f * (pointScale/dist);

    fs_PosEye = posEye;
    fs_Color = vec4(0.5,0.5,0.5,1.0f);
    gl_Position = projMat * viewMat * vec4(position.xyz, 1.0);
}

这是我的片段着色器:

#version 400
uniform mat4 viewMat, projMat, modelMat;
uniform float pointScale;   // scale to calculate size in pixels

in vec3 fs_PosEye;
in vec4 fs_Color;

out vec4 out_Color;
out vec4 out_Position;

float linearizeDepth(float exp_depth, float near, float far) {
return  (2 * near) / (far + near - exp_depth * (far - near));
}

void main()
{
    // calculate normal from texture coordinates
    vec3 N;
    N.xy = gl_PointCoord.xy* 2.0 - 1;
    float mag = dot(N.xy, N.xy);
    if (mag > 1.0) discard;   // kill pixels outside circle
    N.z = sqrt(1.0-mag);

    //calculate depth
    vec4 pixelPos = vec4(fs_PosEye + normalize(N)*1.0f,1.0f);
    vec4 clipSpacePos = projMat * pixelPos;
    //clipSpacePos.z = linearizeDepth(clipSpacePos, 1, 400);
    float depth = clipSpacePos.z / clipSpacePos.w;
    //depth = linearizeDepth(depth, 1, 100);
    gl_FragDepth = depth;
    float  diffuse = max(0.0, dot(N, vec3(0.0,0.0,1.0)));
    out_Color = diffuse * vec4(0.0f, 0.0f, 1.0f, 1.0f);
    out_Color = vec4(N, 1.0);
    out_Color = vec4(depth, depth, depth, 1.0);
}

我想在线性后基于正常进行插值。

0 个答案:

没有答案