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-axisglEnable(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