Depth calculation troubles

I’m having some problems with (maybe) the depth buffer…
I’m using plain OGL with VC++ and Win2k on a g400.

The problem :

It seems like OpenGL draws objects in the order the drawing subroutines are called instead of the Z order they should look.
I’ll explain, if I draw a GL_QUAD near the camera and THEN I draw a GL_QUAD far away it draws the second oone in front of the first.
And if I rotate the whole scene 180^ I get the same effect.
The objects seems to be drawn not using the Z values but using the “time” their drawing routines are called.

My gl configs are just

glShadeModel(GL_SMOOTH);							// Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);				// Black Background

glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

then…

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glEnable(GL_DEPTH_TEST);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
glPushMatrix();
DrawFirstNearQuad();
glPopMatrix();
glPushMatrix();
DrawSecondFarQuad();
glPopMatrix();

the result is that a small quad is drawn on a bigger one, because the far quad is drawn AFTER the near one.

Does someone knows how to solve this problem ???

10x, bye.

All primitives are draw in the exact order as you draw then in your code. This is why you use the z-buffer, to solve the problem when you draw a polygon further away in the second pass. If you don’t want to use the z-buffer, you have to sort the primitives yourself. But then another problem will arise, HOW to sort them (not too hard to do the actual sorting, but to get it correct in all cases is nearly impossible).

One first guess would be that you are using the depthbuffer the wrong way. You haven’t posted some pieces of your code, and the problem might be in there.

But another strange thing I noticed is your call to glLoadIdentity just before drawing each primitive. If you have the modelview matrix enabled, this will reset the modelview matrix, and your 180 degree rotation will be lost.

The code above is ALL my GL code, the 2 subs just do a call to glBegin(GL_TRIANGLES); and glEnd();

The first part is the Init code, the second is the MainDraw routine…

The problem is that I tought using glEnable(GL_DEPTH_BUFFER) I should be able to call the glBegin/glEnd routines in the order I want…

Isn’t true that I must draw sorting in the Z only if I don’t enable depth testing ???

Thanks anyway for your answer… waiting some more help