18
votes

In OpenGL, I display a simple model. When I enable the depth buffer,

glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);

everything disappears. I read this OpenGL FAQ, but it didn't help me.

My perspective settings are: angle=45deg, near=1, far=40, I put model at (0,0,0) and I place my eye at (0,0,4). Without the Z-buffer, I can see the model.

What could be wrong?

3
maybe your you render your model on the wrong side? try disabling GL_CULL_FACE. This is just not enough code to say what's wrong.Dave Halter
Nope, didn't help. What do you mean "wrong side"? That the normals are in opposite direction? I load a standard model from a file. I do not assure any particular order of rendering, but this is z-buffer job, right?Jakub M.
@JakubM.: Normals don't influence culling. Face culling depends on the direction on screen in which the vertices come in. It's fully configurable, bue default is, to cull faces whose vertices are drawn in clockwise order on screen.datenwolf
-1: For not including more complete code, thus forcing people to guess at the answer.Nicol Bolas
It could be the winding order of your vertices if GL_CULL_FACE is enabled; and you have to clear the GL_COLOR_BUFFERT_BIT | GL_DEPTH_BUFFER_BIT)...Francis Cugler

3 Answers

56
votes

Did you make sure you clear the depth buffer?

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
                              ^^^^^^^^^^^^^^^^^^^
6
votes

If the other answer does not work, Try what user886079 mentioned in a comment

If the above doesn't work, check your near plane in the gluPerspective, glFrustum, or glOrtho function. Depth buffering doesn't seem to work if it is set to 0.

I had tried almost everything but could not solve the overlapping issue. I had put the near value 0.0f. I changed it to 0.01f and the results now are how it needed to be.

1
votes

If the following answers above do not work try the following:

  • If you have GL_CULL_FACE enabled check the winding order of your model's vertices. (LH) vs (RH).
  • If GL_DEPTH_TEST is enabled; then check the planes to your frustum against your model view projection matrix along with the camera's position & view direction.
  • If GL_BLEND is enabled; check the rendering order of your objects in comparison to the z-buffer.