I'm having problem with rendering depth in OpenGL
Following code is a simple example of code in which problem occurs. It renders 2 trapezoids in same place, and one of them rotates. But the rotating one is always displayed on top, even though it should go behind the first trapezoid while rotating.
I guess I messed up something with initializing OpenGL. Also while searching through stackoverflow i found a post where someone suggested to execute following code and see the output.
int depth; glGetIntegerv(GL_DEPTH_BITS, &depth); printf("%i bits depth", depth);
well the output is, 0 bits depth, which isn't good I guess :(
I'm developing on a Mac, in xCode with glfw library
#include <GL/glfw.h> #include <stdlib.h> int main( void ) { int running = GL_TRUE; if( !glfwInit() ) { exit( EXIT_FAILURE ); } // Open an OpenGL window if( !glfwOpenWindow( 640,480, 0,0,0,0,0,0, GLFW_WINDOW )) { glfwTerminate(); exit( EXIT_FAILURE ); } glEnable(GL_DEPTH_TEST); float angle = 0; // Main loop while( running ) { double elapsedTime = glfwGetTime(); glfwSetTime(0); angle += 90 * elapsedTime; // OpenGL rendering goes here... glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glPushMatrix(); //Save the transformations performed thus far glColor3f(1.0f, 1.0, 0.0); glTranslatef(0.0f, 0.0f, 0.0f); //Move to the center of the trapezoid //glRotatef(angle, 0.0f, 1.0f, 0.0f); //Rotate about the y-axis glBegin(GL_QUADS); //Trapezoid glVertex3f(-0.7f, -0.5f, 0.0f); glVertex3f(0.7f, -0.5f, 0.0f); glVertex3f(0.4f, 0.5f, 0.0f); glVertex3f(-0.4f, 0.5f, 0.0f); glEnd(); glPopMatrix(); glPushMatrix(); //Save the transformations performed thus far glColor3f(1.0f, 0.0, 0.0); glTranslatef(0.0f, 0.0f, 0.0f); //Move to the center of the trapezoid glRotatef(angle, 0.0f, 1.0f, 0.0f); //Rotate about the y-axis glBegin(GL_QUADS); //Trapezoid glVertex3f(-0.7f, -0.5f, 0.0f); glVertex3f(0.7f, -0.5f, 0.0f); glVertex3f(0.4f, 0.5f, 0.0f); glVertex3f(-0.4f, 0.5f, 0.0f); glEnd(); glPopMatrix(); //Undo the move to the center of the trapezoid glfwSwapBuffers(); // Check if ESC key was pressed or window was closed running = !glfwGetKey( GLFW_KEY_ESC ) && glfwGetWindowParam( GLFW_OPENED ); } // Close window and terminate GLFW glfwTerminate(); // Exit program exit( EXIT_SUCCESS ); }