首页 > 解决方案 > 半透明地形问题(可能是深度缓冲区问题)

问题描述

我有渲染问题。即时渲染的块有些半透明。只要我使用 Nvidia 显卡,一切都很好。在 Intel(集成)或 AMD 显卡上,它看起来像这样:

在此处输入图像描述

如您所见,有正在渲染的块,应该在其他块之后。

这是我的准备方法。

    public void prepare()
    {
        GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
        GL.ClearColor(skyColour.X, skyColour.Y, skyColour.Z, 0f);
    }

    public void enableGLCaps()
    {
        GL.Enable(EnableCap.DepthTest);
        GL.Enable(EnableCap.DepthClamp);

        GL.Enable(EnableCap.CullFace);
        GL.CullFace(CullFaceMode.Back);

        GL.Enable(EnableCap.Blend);
        GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);
    }

这是我的顶点着色器:

#version 440
in vec3 position;
in vec2 textureCoords;

out vec2 pass_textureCoords;
out float visibility;

uniform mat4 transformationMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform float density;
uniform float gradient;

void main() {
    vec4 worldPosition = transformationMatrix * vec4(position, 1.0);
    vec4 positionRelativeToCam = viewMatrix * worldPosition;
    gl_Position = projectionMatrix * positionRelativeToCam;

    pass_textureCoords = textureCoords;
    float distance = length(positionRelativeToCam.xyz);
    visibility = exp(-pow((distance*density), gradient));
    visibility = clamp(visibility, 0.0, 1.0);

}

这里是我的片段着色器:

#version 440

uniform sampler2D textureSampler;
uniform vec3 skyColour;

in vec2 pass_textureCoords;
in float visibility;

out vec4 color;

void main() {

    vec4 textureColour = texture(textureSampler, pass_textureCoords);
    if (textureColour.a < 0.5)
        discard;

    color = mix(vec4(skyColour, 1.0), textureColour, visibility);
    color.a = 1;
}

改变近平面没有帮助。

编辑1:

我已经渲染了 z 值并注意到,有些面孔肯定没有正确的值。我将一个区域(16*16*16 块)中的每个纹理渲染为单个 VAO(稍后将使用纹理图集,但我遇到了使用它的 mipmapping 闪烁/重叠)。被渲染为一个 VAO 的面没有相互重叠,因此 z 缓冲区在那里似乎没问题。看起来它在每次渲染调用后都会重新测试。这是我的渲染调用:

GL.DrawElements(BeginMode.Quads, vertices, DrawElementsType.UnsignedInt, 0);

解决方案:

我使用的深度函数是错误的。您应该使用 Lequal。我还必须将深度范围设置为 (0, 1)。

标签: c#openglrenderingopentkdepth-buffer

解决方案


推荐阅读