GL_DEPTH_TEST not working?

Hello!

I am having a very newbie’ish problem.

I have my eye position set at (0, 0, 8), my look at to (0, 0, 0), and my up in the y axis. I have enabled GL_DEPTH_TEST, cleared the depth buffer before drawing the scene, and the draw.

Yet, it appears as though the scene depends on the ordering of the drawing and not the depth! That is, I drew a sphere at the origin and a rectangle with negative z value. This should result in the sphere appearing before the rectangle right? But quite the opposite if I draw the rectangle after the sphere. I would understand if I were still in 2d mode…

Here’s the code for my scene drawing algorithm:


	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glEnable(GL_DEPTH_TEST);

	glViewport(0, 0, width, height);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(90, (GLdouble) width/height, near, far);

	gluLookAt(eye_pos.x, eye_pos.y, eye_pos.z, eye_lookat.x, eye_lookat.y,
		eye_lookat.z, eye_up.x, eye_up.y, eye_up.z);

	glMatrixMode(GL_MODELVIEW);
	
	glEnable(GL_DEPTH_TEST);

	glLoadIdentity();
	
	glColor4d(0.0, 1.0, 0.0, 1.0);
	glutSolidSphere(2, 20, 20);

	glColor4d(1.0, 0.0, 0.0, 1.0);
	glBegin(GL_POLYGON);
	glVertex3d(5, 5, -10);
	glVertex3d(-5, 5, -10);
	glVertex3d(-5, -5, -10);
	glVertex3d(5, -5, -10);
	glEnd();

I added the enable depth test multiple time in some suspicion that it somehow magically disables itself. Perhaps depth test is hardware dependent?

Did you ask for a depth buffer when you created the context? Because if you didn’t, there will be no depth buffer to use the depth test with.

I did not. I thought all I had to do to enable depth test was glEnable(GL_DEPTH_TEST).

I tried looking up what you mentioned, but-OH!

I GOT IT!~!

THANKS!

It turns out I forgot
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
to | GLUT_DEPTH.

Ahahaha!

Thank you very much for your advice!