Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 7 of 7

Thread: Depth Test not Working

  1. #1
    Junior Member Newbie
    Join Date
    Jul 2004
    Location
    Oklahoma
    Posts
    21

    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

  2. #2
    Advanced Member Frequent Contributor plasmonster's Avatar
    Join Date
    Mar 2004
    Posts
    750

    Re: Depth Test not Working

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

  3. #3
    Junior Member Newbie
    Join Date
    Jul 2004
    Location
    Oklahoma
    Posts
    21

    Re: Depth Test not Working

    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.

    Code :
    #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(&amp;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

  4. #4
    Guest

    Re: Depth Test not Working

    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:

    Code :
    gluPerspective(45, 4/3, 0.1, 2);
    everything will be fine.

  5. #5
    Intern Newbie
    Join Date
    Jun 2004
    Location
    Berlin, Germany
    Posts
    30

    Re: Depth Test not Working

    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.

  6. #6
    Junior Member Newbie
    Join Date
    Jul 2004
    Location
    Oklahoma
    Posts
    21

    Re: Depth Test not Working

    I should have realized that. Thanks.

  7. #7
    Guest

    Re: Depth Test not Working

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


    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •