Depth Test Begins to Fail as Distance Increases

Okay, I have some experience with OpenGL in Java (actually LWJGL), but when I began it in C++, I immediately noticed an issue with the depth testing. As I move away from my object (7.0f about), the depth testing starts to fail. As in, it starts to render the back faces instead of front and so on. I would be fine with this if the distance for this to happen was greater, but this is way too close for comfort. I’ll upload a picture of what happens if you want it.

Here are my GL settings:


glEnable(GL_TEXTURE_2D);
ilInit();
LoadImage(TEX_CRATE, "Resources/Textures/crate.jpg");
LoadImage(TEX_GRASS, "Resources/Textures/grass.jpg");

glShadeModel(GL_SMOOTH);

glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);

glClearColor(0.0f, 0.8f, 1.0f, 0.0f);

glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

I was using GL_LESS instead of GL_LEQUAL because when I had to objects at the same depth and same position, it gave a wavy effect that I didn’t want.

Does anyone know how I can fix this?

EDIT: I just realized that it actually starts around 2.0f, and the wavy thing that I was talking about starts to happen, which grows as we move farther away. This leads me to believe that the farther you move away, it starts to sort of “flatten” the image, so it is harder for it to determine the proper depth. Maybe?

EDIT2: Note that I can fix the wavy line by always drawing the closer object last. But if I could do that easily, then GL_DEPTH_TEST would be completely useless, as I am already doing it myself.

EDIT3: Okay, I can solve it using GL_CULL_FACE.

Yeah, that helps. And it sounds like you have your near clip plane pulled in pretty close. Push it out. This is the 5th param to glFrustum, or the 3rd param to gluPerspective.