Problem with depth-testing

Hi!

I have problems getting hidden surface removal to work. I the following example, three quads are rendered. I clear the depth-buffer and enable depth-testing. The near clipping plane’s z is not 0.0. But strangely, the quads get rendered in the order in which they appear in the code (= part of the last one gets rendered on top of the second one!). No depth testing seems to happen. I suppose I simply missed something important here (I’m a newbie…). I’d be happy if someone could look at my code:

  • (void)drawRect NSRect)aRect
    {
    float ambientLight = { 0.3f, 0.3f, 0.5f, 1.0f };
    float diffuseLight = { 0.25f, 0.25f, 0.25f, 1.0f };
    float lightPosition = { 2.5f, 1.5f, 5.0f, 1.0f };

    float matAmbient = { 1.0f, 1.0f, 1.0f, 1.0f };
    float matDiffuse = { 1.0f, 1.0f, 1.0f, 1.0f };

    glViewport( 0,0,NSWidth([self bounds]),NSHeight([self bounds]) );
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();

    gluPerspective( 60, 0.9, 1.0, 20 );
    glMatrixMode( GL_MODELVIEW );
    glClearColor( 0,0,0,0 );

    glLoadIdentity();
    gluLookAt( 3, 8, 5, // looking from
    3, 1, 3, // looking at
    0, 0, -1 ); // up-vector (0, 0, -1): camera top pointing down the negative z-axis

    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);
    glFrontFace(GL_CCW);
    glCullFace(GL_BACK);
    glEnable(GL_LIGHTING);

    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    glShadeModel(GL_SMOOTH);

    glMaterialfv(GL_FRONT, GL_AMBIENT, matAmbient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE, matDiffuse);

    glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
    glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
    glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.5);

    glEnable(GL_LIGHT0);

    glPolygonMode(GL_FRONT, GL_FILL);

    glBegin( GL_QUADS );
    glNormal3f( 1.0, 0.0, 0.0 );
    glVertex3f( 2.0, 0.0, 4.0 );
    glVertex3f( 2.0, 0.0, 2.0 );
    glVertex3f( 2.0, 2.0, 2.0 );
    glVertex3f( 2.0, 2.0, 4.0 );
    glEnd();
    glBegin( GL_QUADS );
    glNormal3f( 0.0, 0.0, 1.0 );
    glVertex3f( 2.0, 0.0, 4.0 );
    glVertex3f( 4.0, 0.0, 4.0 );
    glVertex3f( 4.0, 2.0, 4.0 );
    glVertex3f( 2.0, 2.0, 4.0 );
    glEnd();
    glBegin( GL_QUADS );
    glNormal3f( -1.0, 0.0, 0.0 );
    glVertex3f( 4.0, 0.0, 2.0 );
    glVertex3f( 4.0, 0.0, 4.0 );
    glVertex3f( 4.0, 2.0, 4.0 );
    glVertex3f( 4.0, 2.0, 2.0 );
    glEnd();

    glFlush();
    }

thanks for help!

dunken

Silly question: do you actually have a depth buffer?

Um. Good question. I always assumed I did… How do I find out? And if I don’t, how do I get one?

Thanks,

dunken

Requesting capabilities is platform/implementation dependent. Are you on win32 or unix (X-windows)? Do you use a toolkit to handle your window-management?

Determining the capabilities is also platform dependent. On MS you use DescribePixelFormat. For X-windows there is an equivalent query (don’t know X-windows myself yet).

Post some code of your window-initialization routine (the place where you also request the index-color/RGB(A) and single/double buffer capabilities).

Jean-Marc.

My platform is Cocoa on Mac OS X, so it’s neither win32 nor x-windows. Until you mentioned it now, I have never really thought about pixel-formats… However, a quick look-up in the reference gave me a chapter “Creating and Managing a Pixel Format”. I suppose that is where I find the answer. Thanks for the hint!
As for your request for more code: the code I posted is the only OpenGL-Code there is in my project. The rest is likely being inherited from the class NSOpenGLView I am subclassing.

Thanks!

Dunk