半透明地形问题(可能是深度缓冲区问题)

时间:2018-11-11 22:56:14

标签: c# opengl rendering opentk depth-buffer

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

enter image description here

您可以看到正在渲染的块应该在其他块之后。

这是我的准备方法。

    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(稍后将使用纹理图集,但im会因使用它进行mipmapping而出现闪烁/重叠)。渲染为一个VAO的人脸没有相互重叠,因此z缓冲区似乎还可以。似乎在每个渲染调用之后都对其进行了重新测试。 这是我的渲染调用:

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

解决方案:

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

0 个答案:

没有答案
相关问题