I am constructing a figure from a set of points via a delaunay triangulation (the implementation I’m using is from MatLab 6.5). I then use a for loop to construct the figure (the delaunay alg returns an n x 3 array with the indices of points in another n x 3 array).
When I display this figure (it looks like a 30 degree segment of a sphere) there are no problems, however when I rotate the figure 90 degrees the side that is suppose to be hidden has some of its triangles superimposed over the nearest side’s triangles.
I have discussed this with someone else and they thought it was due to non-uniform normals (some of the triangles wind cw others ccw) but I believe it is due to something I am doing wrong with the depth buffer. I tried to eliminate the depth problem by playing with glDepthFunc(…) but no value helped. I tried to eliminate the normal problem by drawing the figure twice (once cyling forward through the triangle vertices and once backward) (with and without face culling) but that didn’t help either. So I’m left to believe there is a third possibility.
After thinking about it for a while I decided to determine the number of triangles to draw via the arrow keys (so I pressed up and more triangles of the figure were drawn I press down less).
The result of this was I found the problem. The triangles that are appearing in front when the should be behind are drawn AFTER the initial set. I thought depth testing wouldn’t have any problem with that.
Any ideas how to correct it (sorting the array of triangles is not a desirable solution).
The order in which you draw your polygons makes no difference to the depth test.
make sure you have the following states set:
glDepthMask(GL_TRUE); //enables writing to the depth buffer
glEnable(GL_DEPTH_TEST); //performs depth testing
glDepthFunc(GL_LESS); //should be default, but specifies that a fragment is drawn if its z value is less than that stored in the depth buffer.
also, ensure you aren’t clearing the depth buffer at the wrong times.
From what it sounds like you need to render,
you should have 1 clear before you render your triangles, that’s it.