glEnable(GL.GL_DEPTH_TEST) causing fragmented polygons

I’ve been working on a level abstraction for jogl and I finally finished enough of to test and as usual it has some interesting bugs in it. The only one that has me stuck currently is that in my engine, when I enable the depth test, the polygons become fragmented (see pictures). I made a test case that did basically the exact same thing as my engine, but hardcoded in immediate mode and it worked fine, even with depth test enabled.

Fragmented Image:

http://www.imagehosting.com/out.php/i1193674_badimage.jpg

Desired Image:

http://www.imagehosting.com/out.php/i1193680_goodimage.jpg

I used a traceGL on both and per frame, I couldn’t tell where the commands went wrong.
Fragmented Polygon Trace output:

glClearColor(0.0,0.0,0.0,1.0)
glEnable(GL_DEPTH_TEST)
glEnable(GL_CULL_FACE)
glCullFace(GL_FRONT)
glViewport(0,0,500,478)
glViewport(0,0,500,478)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glMultMatrixd(java.nio.DirectDoubleBufferU[pos=0 lim=16 cap=16])
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glEnable(GL_LIGHTING)
glEnable(GL_LIGHT0)
glLightfv(GL_LIGHT0,GL_DIFFUSE,[0.4,0.4,0.6,1.0],0)
glLightfv(GL_LIGHT0,GL_SPECULAR,[1.0,1.0,1.0,1.0],0)
glLightfv(GL_LIGHT0,GL_POSITION,[5.0,5.0,5.0,1.0],0)
glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,[0.0,0.0,1.0,0.0],0)
glLightf(GL_LIGHT0,GL_SPOT_CUTOFF,180.0)
glLightf(GL_LIGHT0,GL_CONSTANT_ATTENUATION,1.0)
glLightf(GL_LIGHT0,GL_LINEAR_ATTENUATION,0.0)
glLightf(GL_LIGHT0,GL_QUADRATIC_ATTENUATION,0.0)
glPushMatrix()
glTranslatef(0.0,0.0,0.0)
glRotatef(0.0,0.0,0.0,0.0)
glIsEnabled(GL_BLEND) = false
glLoadIdentity()
glTranslatef(0.0,0.0,0.0)
glRotatef(0.0,0.0,0.0,0.0)
glIsEnabled(GL_LIGHTING) = true
glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,90.0)
glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,[1.0,1.0,1.0,1.0],0)
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,[1.0,1.0,1.0,1.0],0)

<!-- internals of glutSolidTeapot(1)
glPushAttrib(73736)
glEnable(3456)
glEnable(2977)
glEnable(3511)
glEnable(3508)
glPushMatrix()
glRotatef(270.0,1.0,0.0,0.0)
glScalef(0.5,0.5,0.5)
glTranslatef(0.0,0.0,-1.5)
glMap2f(3508,0.0,1.0,2,2,0.0,1.0,4,2,[0.0,0.0,1.0,0.0,0.0,1.0,1.0,1.0],0)
glMap2f(3511,0.0,1.0,3,4,0.0,1.0,12,4,[1.4,0.0,2.4,1.4,-0.784,2.4,0.784,-1.4,2.4,0.0,-1.4,2.4,1.3375,0.0,2.53125,1.3375...48],0)
glMapGrid2f(14,0.0,1.0,14,0.0,1.0)
...
glMap2f(3511,0.0,1.0,3,4,0.0,1.0,12,4,[3.3,-0.0,2.4,3.3,0.25,2.4,2.7,0.25,2.4,2.7,-0.0,2.4,3.525,-0.0,2.49375,3.525...48],0)
glEvalMesh2(6914,0,14,0,14)
glPopMatrix()
glPopAttrib() -->

glIsEnabled(GL_LIGHTING) = true
glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,[1.0,1.0,1.0,1.0],0)
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,[1.0,1.0,1.0,1.0],0)
glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,90.0)
glPopMatrix()
glDisable(GL_LIGHTING)
glDisable(GL_LIGHT0)

And here’s the proper trace:

glCullFace(GL_FRONT) 1028
glEnable(GL_CULL_FACE) 2884
glEnable(GL_DEPTH_TEST) 2929
glEnable(GL_LIGHTING) 2896
glEnable(GL_LIGHT0)
glLightfv(GL_LIGHT0,GL_DIFFUSE,[0.4,0.4,0.6,1.0],0)
glLightfv(GL_LIGHT0,GL_SPECULAR,[1.0,1.0,1.0,1.0],0)
glLightfv(GL_LIGHT0,GL_POSITION,[5.0,5.0,5.0,1.0],0)
glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,[0.0,0.0,1.0,0.0],0)
glLightf(GL_LIGHT0,GL_SPOT_CUTOFF,180.0)
glLightf(GL_LIGHT0,GL_CONSTANT_ATTENUATION,1.0)
glLightf(GL_LIGHT0,GL_LINEAR_ATTENUATION,0.0)
glLightf(GL_LIGHT0,GL_QUADRATIC_ATTENUATION,0.0)
glViewport(0,0,500,478)
glViewport(0,0,500,478)
glMatrixMode(GL_PROJECTION) 5889
glMultMatrixd(java.nio.DirectDoubleBufferU[pos=0 lim=16 cap=16])
glMatrixMode(GL_MODELVIEW) 5888
glClearColor(0.0,0.0,0.0,1.0)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) 1604
glLoadIdentity()
glTranslatef(0.0,0.0,0.0)

<!--internals of glutSolidTeapot(1)
glPushAttrib(73736)
glEnable(3456)
glEnable(2977)
glEnable(3511)
glEnable(3508)
glPushMatrix()
glRotatef(270.0,1.0,0.0,0.0)
glScalef(0.5,0.5,0.5)
glTranslatef(0.0,0.0,-1.5)
glMap2f(3508,0.0,1.0,2,2,0.0,1.0,4,2,[0.0,0.0,1.0,0.0,0.0,1.0,1.0,1.0],0)
glMap2f(3511,0.0,1.0,3,4,0.0,1.0,12,4,[1.4,0.0,2.4,1.4,-0.784,2.4,0.784,-1.4,2.4,0.0,-1.4,2.4,1.3375,0.0,2.53125,1.3375...48],0)
glMapGrid2f(14,0.0,1.0,14,0.0,1.0)
glEvalMesh2(6914,0,14,0,14)
...
glMap2f(3511,0.0,1.0,3,4,0.0,1.0,12,4,[3.3,-0.0,2.4,3.3,0.25,2.4,2.7,0.25,2.4,2.7,-0.0,2.4,3.525,-0.0,2.49375,3.525...48],0)
glEvalMesh2(6914,0,14,0,14)
glPopMatrix()
glPopAttrib() -->

I’d appreciate any advice or hearing if anyone has ever had this problem before. Also, I ran the same code on multiple machines when the problem first arose (but before I pinpointed it to the depth test) and the same thing happened, so I doubt it’s a driver issue, especially because one was nvidia and another ati.

Thankyou and sorry for the long post

I wrote another test case where I copied exactly the jogl calls from the trace of the fragmented polygon program and ran it and no problems occurred. I switched the engine’s displaying to use an FPSAnimator instead of calling GLCanvas.display(), but the problem persists. In both cases (where it works and doesn’t work), I just use the default constructor for GLCanvas, ,without setting any GLCapabilities, so I can’t tell what the difference is.

Has anyone on this forum ever experienced something similar and how did they fix it?

The artifacts you show don’t look like anything I’ve seen before, like z-fighting or depth buffer imprecision. My best guess is that you’re setting some OpenGL state that has bad interactions with the depth test. Try iteratively simplifying your program.

As I mentioned earlier, I tested the program on both a Mac (with a gforce 8600 if I remember correctly) and a windows xp machine with an ati x700. The artifacts occurred on both, although the pc had significantly less fragmentation and where it fragmented, it appeared as if entire triangles were getting thrown out. The opengl traces posted were the only commands executed in the test cases and in one, it worked and in another it didn’t, which leads me to believe that it has something to do with the way I initialized the GLAutoDrawable (switching between GLCanvas and GLJPanel didn’t make a difference), how I added it to the frame and its visibility, or how I drove the animation.

I don’t have my code present at the moment, and I plan on writing another test case, but I’ll post more results as soon as possible.

I have figured it out. There was a subtle bug in my engine’s code where the near z clipping distance for gluPerspective was set to 0, even though I desired it to be 1. So for anyone out there who ever sees what I saw, it can get caused by the clipping plane being 0.