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