Search code examples
c#openglglslshaderopentk

faces not fully drawing and disappearing


im trying to learn opengl and when i use depth test the faces slowly disappear im not sure what's happening and i can't find anything online

drawing code:

            GLHelper.Clear();
            GL.Enable(EnableCap.DepthTest);

            shader.Use();

            shader.SetMatrix("projection", Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver2, (float)Width / (float)Height, 0.01f, 1000.0f));
            shader.SetMatrix("view", Matrix4.LookAt(new Vector3(0,0,10),new Vector3(),Vector3.UnitY));
            shader.SetMatrix("transform",Matrix4.CreateRotationY((float)time));
            mesh.Draw();

            SwapBuffers();

mesh.draw:

            GL.EnableVertexAttribArray(1);
            GL.EnableVertexAttribArray(2);

            GL.BindBuffer(BufferTarget.ElementArrayBuffer, IBO);
            GL.DrawElements(BeginMode.Triangles, indices.Length, DrawElementsType.UnsignedInt, 0);
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);

            GL.DisableVertexAttribArray(1);
            GL.DisableVertexAttribArray(2);

shader code:

#version 440 core

layout (location = 0) in vec3 position;
layout(location = 1) in vec4 color;
uniform  mat4 transform;
uniform mat4 projection;
uniform mat4 view;

out vec4 fragcolor;

void main(void)
{
    gl_Position = projection * view * transform * vec4(position,1.0);

 //temp
 fragcolor = color;
}

Solution

  • When you enable the Depth Test, then you've to clear the depth buffer beside the color buffer.

    GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
    

    Since depth value of the fragment is tested against the corresponding depth value in the buffer, the depth buffer has to be cleared at the begin of the frame.