Depth Test not Working

I build a structure up from triangles (not strips) going down the y-axis along the x-axis. However the triangles drawn last appear on top whether they actually exist behind or in front.

Here is my code setting up the depth buffer:

glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glDepthMask(GL_TRUE);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);

I clear the depth buffer one time before I draw the the object.

Any ideas on what could cause this?

Colby

I would post the code that does the rendering, along with all the state changes you make.

I believe it is either a hardware or driver problem because my depth testing is not working period. The program below shows a red sphere in back, a green box in the middle, and a blue sphere in front… the order in which they are drawn not the order of their depth relative to the camera.

#include <GL\glut.h>

void display()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glViewport(0, 0, 800, 600);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	gluPerspective(45, 4/3, 0, 2);
	gluLookAt(0, 0, 0, 0, 0, 1, 0, 1, 0);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	glPushMatrix();
		glColor3f(1, 0, 0);
		glTranslatef(.05, .05, .5);

		glutSolidSphere(.05, 20, 20);
	glPopMatrix();

	glPushMatrix();
		glColor3f(0, 1, 0);
		glTranslatef(0, 0, 1);

		glutSolidCube(.1);
	glPopMatrix();

	glPushMatrix();
		glColor3f(0, 0, 1);
		glTranslatef(-.1, -.1, 1.5);

		glutSolidSphere(.05, 20, 20);
	glPopMatrix();

	glutSwapBuffers();
}

int main(int argc, char ** argv)
{
	glutInit(&argc, argv);

	glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA);
	glutInitWindowSize(800, 600);
	glutInitWindowPosition(0, 0);
	glutCreateWindow("Demonstration");

	glEnable(GL_LIGHTING);
		GLfloat diffuseLight[] = {.75, .75, .75, 1};
		GLfloat lightPosition[] = {-1, -1, -1, 1};
		glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
		glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
		glEnable(GL_LIGHT0);

	glEnable(GL_COLOR_MATERIAL);
		glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);

	glDepthMask(GL_TRUE);
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);

        glShadeModel(GL_SMOOTH);

	glClearColor(0, 0, 0, 0);

	glutDisplayFunc(display);

	glutMainLoop();

	return 0;
}
 

My graphics card is a GeForce2 MX 400 and I’m using the Nvidia driver version 5.6.7.2 (released in March 2004). I also have Nvidia’s latest stereo driver installed. I’m compiling the code in MS VC++ 6 on a WinXP system.

Has anyone seen this before?

Colby

It amazes me everytime when I see someone questioning something that is proven to work for years.

Its quiet simple. The znear clipping plane must be a positive value (as in greater then zero) as defined in the manual.

So if you use:

gluPerspective(45, 4/3, 0.1, 2);

everything will be fine.

not fast enough, i wanted to post the same.

…by the way you should use 4.0/3 instead of 4/3 for the aspect ratio, because in c and c++ 4/3 = 1.

I should have realized that. Thanks.

Originally posted by Sumpfratte:
not fast enough, i wanted to post the same.

:smiley:

Originally posted by Sumpfratte:
…by the way you should use 4.0/3 instead of 4/3 for the aspect ratio, because in c and c++ 4/3 = 1.
Right you are, I missed that.