Have you read OpenGL FAQ 12?
The one thing that looks suspicious about your code is the negative near plane. Negative Z values are behind the camera, which is usually a mistake when rendering a 3D scene. However, that in itself shouldn't cause the problem you're seeing, and the range [-6,6] for Z should provide plenty of precision for this kind of scene.
Are you calling glEnable(GL_DEPTH_TEST)
? Are you passing GL_DEPTH_BUFFER_BIT
to glClear
each frame?
Update: you're calling glPolygonMode(GL_FRONT, GL_LINE)
. This means that front-facing triangles are drawn in outline only, which means that if front-facing triangle A overlaps another front-facing triangle B, you can see through A to the edges of B. With a convex body this can't happen, so you don't notice the problem.
If you want triangles to occlude the triangles behind them, then you are going to need to fill them in using mode GL_FILL
. To get a wireframe figure, you need to draw the model with white fill, then draw the model again with black outline, like this:
glDepthFunc(GL_LEQUAL); glPolygonMode(GL_FRONT, GL_FILL); /* draw the model in white */ glDepthFunc(GL_EQUAL); glPolygonMode(GL_FRONT, GL_LINE); /* draw the model again in black */
The idea is that on the second pass over the model, we only want to draw the outlines that are not obscured by some triangle drawn in the first pass that is closer to the camera (that is, with lower Z).
Another idea: I think your camera may be pointing the wrong way. This would explain why the scene is being drawn wrongly with the glOrtho perspective, and not at all with the glFrustum perspective. In the glOrtho case, the entire scene is being drawn behind the camera; that's why it's being drawn with the Z order the wrong way round. When you set the near plane to a positive number, the entire scene is culled.