-1

I'm struggling to get my depth testing working correctly in an app.

Depth seems to be based entirely on draw order, as seen here: https://www.youtube.com/watch?v=YErS_loJW7w&feature=youtu.be

You can see in the video, the point cloud of Suzanne is in front of the Mesh model, but when rotated it ends up behind it.

I think the code below is everything relevant

// clear the render buffer....
GL.Enable(EnableCap.DepthTest);
GL.DepthMask(true);
GL.ClearColor(Color.Black);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);          

// Render main 3d scene
GL.Enable(EnableCap.CullFace);
GL.CullFace(CullFaceMode.Back);
GL.Enable(EnableCap.DepthTest);
GL.Enable(EnableCap.DepthClamp);
GL.DepthFunc(DepthFunction.Always);
GL.DepthMask(true);

I was under the impression that enabling Depth testing would make this work correctly.

What's going wrong?

genpfault
  • 51,148
  • 11
  • 85
  • 139
Chris
  • 165
  • 1
  • 13
  • 1
    Did you request some depth buffer bits when you created your GL context? You may not get any unless you ask. – genpfault Oct 20 '14 at 20:57
  • OpenGl is relatively new to me. How do I tell? Looking at the docs around the OpenTK GLControl that I am using it seems to indicate that a depthbuffer is provided. "...Notice that the GLControl provides a color- and a depth buffer, which we have to clear using GL.Clear()." – Chris Oct 20 '14 at 21:33

1 Answers1

0

Setting GL.DepthFunc(DepthFunction.Always); is making things go wrong. This is telling the GL to never discard fragments, even if they lie behind previously drawn objects. You should use the GL_LESS or GL_LEQUAL comparision mode.

derhass
  • 43,833
  • 2
  • 57
  • 78
  • Thanks foe your response. I have modified that to a LESS. And I assume fragments are getting discarded, but they still seem to discard based on draw order rather than z-order. Is this something I need to tell GL to do explicitly? – Chris Oct 20 '14 at 21:35