problem: geometry flashing...

I have a problem with the geometry of a terrain mesh… as you can see in the files attached (a wired and textured view of the terrain), the geometry is flashing (in the textured view) in some areas of the mesh…

I’m using glut, and this is the configuration…

// Setup GL States
glShadeModel(GL_SMOOTH); glClearColor (0.0f, 0.0f, 0.0f, 0.5f);
glClearDepth (1.0f);
glEnable (GL_DEPTH_TEST);


glEnable( GL_TEXTURE_2D);

glViewport(0, 0, _glutWindowWidth, _glutWindowHeight);
glLoadIdentity(); gluPerspective(45, 800/600, 0.0001f, 10000000.0f);

Triangle-fans are sent to the graphics card in counterclockwise order…

Any idea why this happens?

Looks like Z fighting. Probably not a coincidence that where you see this is where you have polys that are close together in Z.

If you enable back-face culling (enable GL_CULL_FACE and call glCullFace( GL_BACK ) it’ll probably go away in this case, but this is not the general solution to your problem.

The probable cause is this:

gluPerspective(45, 800/600, 0.0001f, 10000000.0f);

The closer you pull your near Z clip plane toward the eyepoint (0), the less Z precision you have out in the world. 0.0001 is insanely close. Push your near clip value out from 0.0001 to something like 1, 10, 50. Any of these will give you more and more Z precision out in the scene.

There are other solutions (higher precision depth buffers, etc.), but this is the simplest. Just push that near clip value of 0.0001 up to something more reasonable. By default you only have a 24-bit fixed-point depth buffer and you have to be careful to make good use of it.

In practice, the far clip value doesn’t affect your precision near as much as the near. In fact there are perspective projections which use an infinite distance far clip, and it works just fine.

Yeah, I’ve also read it is possible to implement a linear depth buffer in a shader, so you can try that.

I’ve done this and unfortunately precision gets really badly lost when things get close to the near clipping plane, so you start seeing “holes” in nearby objects. :frowning:

I’d definitely second Dark Photon’s suggestion of pushing out the near plane anyway, even if you do decide to go down the linear-depth route. From the looks of your geometry you certainly don’t need it to be that near; it is insanely close.

Thanks to all of you guys, I did the change of the near clipping plane to 1 and now all is ok.

I had not paid attention to that value because I have the same example coded in C# (using Tao) and everything is fine…

Thanks again… kind regards…