Triangles Problem

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.

Any help would be appreciated.

Colby Toland

I agree with you. It sounds like your depth buffering is not enabled.

first off, to state the obvious…
are you sure you enabled it?
glEnable(GL_DEPTH_TEST); ?

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).

Colby

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.

I didn’t have “glDepthMask(GL_TRUE);” set but even after I fixed that mistake the problem persists. I clear the depth bit twice prior to drawing the surface but never while drawing the surface.

Colby

do you even have a depth buffer? are you using GLUT?

glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );

why do you clear the depth bit twice?

friendly suggestion: please dont cross-post your topic.
:slight_smile:

I do have a depth buffer and I clear the bit twice because I draw two planes (blue and green) just because I’m tired of a monochrome background.

Here is that code in my prog:
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA);

Colby

P.S. The double posting was a brief laps of judgement. I tried to delete it afterwards but as in the real world egg doesn’t come off one’s face so easily.

:slight_smile:

from someone one is all too familiar with egg on the face, i know what you mean.

could you post a screen-shot of the problem?

i still dont understand the need for 2 clears. could you post the codes?