Enable Depth Buffering
Your application needs to do at least the following to get depth buffering to work:
- Ask for a depth buffer when you create your window.
- Place a call to glEnable (GL_DEPTH_TEST) in your program's initialization routine, after a context is created and made current.
- Ensure that your zNear and zFar clipping planes are set correctly and in a way that provides adequate depth buffer precision.
- Pass GL_DEPTH_BUFFER_BIT as a parameter to glClear, typically bitwise OR'd with other values such as GL_COLOR_BUFFER_BIT.
There are a number of OpenGL example programs available on the Web, which use depth buffering. If you're having trouble getting depth buffering to work correctly, you might benefit from looking at an example program to see what is done differently. This FAQ contains links to several web sites that have example OpenGL code.