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 4 of 4

Thread: depth buffer problem

  1. #1
    Junior Member Newbie
    Join Date
    Nov 2002
    Location
    Canada
    Posts
    2

    depth buffer problem

    I've reduced my problem to the simplest program I can. The more distant quad is drawn in front of the nearer one. They draw correctly if I reverse their draw order but that shouldn't matter. The near/far ratio looks good. It happens on two different machines. I'm sure it's something dumb. Please help!


    # include "glutinit.h"
    # include <stdio.h>
    # include <GL/glut.h>


    void RenderAll()
    {
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity ();

    gluLookAt( 204.59, 255.48, 5.37, // cam position x,y,z
    50, 50, 0, // cam target x,y,z
    0, 0, 1 ); // up vector x,y,z

    // draw 2 squares
    {
    glBegin( GL_QUADS );

    glColor3f( 0.f, 0.f, 1.f );
    glVertex3f( -50, 50, 50 );
    glVertex3f( -50, 50, -50 );
    glVertex3f( 50, 50, -50 );
    glVertex3f( 50, 50, 50 );

    glColor3f( 1.f, 0.f, 0.f );
    glVertex3f( -50, -50, 50 );
    glVertex3f( -50, -50, -50 );
    glVertex3f( 50, -50, -50 );
    glVertex3f( 50, -50, 50 );

    glEnd();
    }

    glFlush ();
    glutSwapBuffers();
    }

    void ReshapeWindow( int width, int height )
    {
    glViewport (0, 0, (GLsizei) width, (GLsizei) height);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    float aspect = (float)height / (float)width;
    glFrustum (-0.7f, 0.7f, -0.7f * aspect, 0.7f * aspect, 2.f, 500.0f);
    glMatrixMode (GL_MODELVIEW);
    }


    void HandleInput(unsigned char key, int x, int y)
    {
    if( key == 27 ) exit( 0 );
    }

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

    glClearColor (0.0, 0.0, 0.0, 0.f );
    glShadeModel (GL_SMOOTH);

    glutInitDisplayMode( GLUT_DEPTH |
    GLUT_DOUBLE |
    GLUT_RGB );

    glEnable( GL_DEPTH_TEST );

    glutInitWindowSize (800, 800);
    glutInitWindowPosition (100, 50);
    glutCreateWindow (argv[0]);

    glutDisplayFunc( RenderAll );
    glutReshapeFunc( ReshapeWindow );
    glutKeyboardFunc( HandleInput );
    glutIdleFunc( RenderAll );
    glutMainLoop();
    }

  2. #2
    Member Regular Contributor
    Join Date
    Feb 2002
    Posts
    377

    Re: depth buffer problem

    The winding of your quads is clockwise while default for OpenGL is counterclockwise.
    Just change the order of your glVertex calls
    and it should work.

  3. #3
    Junior Member Newbie
    Join Date
    Nov 2002
    Location
    Canada
    Posts
    2

    Re: depth buffer problem

    hey, thanks for looking at this. that didn't fix it though, nor did it invert the problem; the wrong one was still in front if drawn in the same order. This makes sense to me because i'm not backface culling based on the winding (=normal) of a polygon. so, if the winding is the wrong way in camera space, then we're just looking at the back of the quad, which we're still drawing. it should still occlude poly's in the distance, but alas it doesn't. still puzzled. any other ideas?


  4. #4
    Junior Member Regular Contributor
    Join Date
    Dec 2001
    Location
    London, England
    Posts
    163

    Re: depth buffer problem

    Gotcha! I sat and played with this for a few minutes, and realised what the problem is.
    You must call glEnable(GL_DEPTH_TEST) AFTER creating your window. i.e. you should have:

    Code :
    void main( int argc, char** argv )
    {
    glutInit(&amp;argc, argv);
     
    glClearColor (0.0, 0.0, 0.0, 0.f );
    glShadeModel (GL_SMOOTH);
     
    glutInitDisplayMode( GLUT_DEPTH | 
    GLUT_DOUBLE |
    GLUT_RGB );
     
    glutInitWindowSize (800, 800); 
    glutInitWindowPosition (100, 50);
    glutCreateWindow (argv[0]);
     
    glEnable( GL_DEPTH_TEST );
     
    glutDisplayFunc( RenderAll ); 
    glutReshapeFunc( ReshapeWindow );
    glutKeyboardFunc( HandleInput );
    glutIdleFunc( RenderAll );
    glutMainLoop();
    }

    [This message has been edited by bakery2k (edited 11-15-2002).]

Posting Permissions

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