Problem with Color or Polygones in distance

I have the following problem.
I create one Object with yellow Color and moove it away from my camera. Everthing is OK.
But now I create 2 Objects with different Colors (yellow and green for example) behind each other and moove them togehter away from my camera.
Firts look everything OK, but far away from the camera the colors seems to be switch between the colors of these 2 Objects.
It looks as like as OpenGL try to draw the 2 colors on one surface.

Does anybody know this problem and if so can someone told me how i can solve this problem.

Sorry for my bad english!

The depth buffering precision decreases with increasing distance.
(I should create a macro for this answer :wink: )

  • When selecting a pixelformat make sure you get a 24 bits depth buffer
  • Make the ratio of zFar/zNear in your glFrustum or gluPerspective call as small as possible by pushing zNear out and pulling zFar in, so that it encloses your scene as close as possible.

thx a lot, the time where i set up the depth buffer and the zFar and zNear is far away.
Ok now i reduced the values and the result is much better.
One question more.
You wrote i have to set up a 24 bit depth buffer.
I set up a 32 bit depth buffer.
I programming OpenGL under Windows and until now i believed i have to use 32 bit or 16 bit, because 24 bit is not suportet.
Is this wrong?

Most hardware accelerates only 16 and 24 bits of depth, very few have 32 bits, but Microsoft’s software OpenGL supports it too, and this is dangerous.

24 bit is enough normally if you consider that the mantissa of a IEEE 32 bit float is 23 bits and the depth buffer is scaled to values 0.0 to 1.0.

If you actually got 32 bits of depth runing an ATI or NVIDIA board, you probably haven’t got hardware support, that is why asking for 24 bits depth is safer.
Always check with glGetString(GL_VENDOR) which OpenGL implementation you got.
ChoosePixelFormat() is buggy, too. wglChoosePixelFormatARB is more accurate.
It doesn’t hurt during debugging to do a DescribePixelFormat after ChoosePixelFormat and check the real pixelformatdescriptor you got.