PDA

View Full Version : depth buffer problem

deans
11-14-2002, 10:41 PM
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! http://www.opengl.org/discussion_boards/ubb/smile.gif

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

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

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
{

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);
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 );

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();
}

satan
11-15-2002, 01:13 AM
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.

deans
11-15-2002, 01:51 AM
hey, thanks for looking at this. http://www.opengl.org/discussion_boards/ubb/smile.gif 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?

http://www.opengl.org/discussion_boards/ubb/smile.gif

bakery2k
11-15-2002, 02:58 AM
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:

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

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

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).]