Just learning OpenGL, so I’m playing with simple programs to get the hang of things. I’m experimenting with 3d and I swear the following code should result with the red square under the blue square, but when I run it, the red’s always above (because it is drawn later) even though it should be hidden by the depth testing. What’s the problem?
Relevant code (in its entirety, no camera tricks or anything)
glClearColor(0.0, 0.0, 0.0, 1.0);
glOrtho(-3.0, 3.0, -3.0, 3.0, -20.0, 20.0);
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_QUADS);
/* FRONT , blue */
glColor3f(0.0, 0.0, 1.0);
glVertex3f(1.0, -1.0, -1.0);
glVertex3f(1.0, 1.0, -1.0);
glVertex3f(-1.0, 1.0, -1.0);
glVertex3f(-1.0, -1.0, -1.0);
/* BACK , red */
glColor3f(1.0, 0.0, 0.0);
glVertex3f(1.25, -1.25, -1.25);
glVertex3f(1.25, 0.75, -1.25);
glVertex3f(-0.75, 0.75, -1.25);
glVertex3f(-0.75, -1.25, -1.25);
glEnd();
glFlush();