# near/far & depthtest

By default we have:

right: x=+1
left: x=-1
top: y=+1
botton: y=-1
near: z=+1
far: z=-1

(camera is viewing by default towards negative Z)
Z-coordinates get translated into depth-coords:

near: d= 0
far: d=+1

So the Z-axis and the D-axis point in opposite directions.

depth buffer when cleared is filled with 1.0s by default, en depthtest is GL_LESS by default (Passes if the incoming depth value is less than the stored depth value).

the nearest polygon has the smallest depth so this will always be drawn. That’s what I expect, but my program however shows me the farest polygon (the dark one). Can someone explain me what is happening?

int DrawGLScene(GLvoid)
{
glEnable(GL_DEPTH_TEST);
//glDepthFunc(GL_LESS); //default
//glClearDepth(1.0); //default
//glDepthRange(0.0,1.0); //default

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glOrtho(-1.0, 1.0,-1.0, 1.0, 1.0,-1.0); // default

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

cube90();
return TRUE;
};

void cube90() {
glBegin(GL_QUADS);
// right x, draw bright red
glColor4f( 1.0, 0.0, 0.0, 0.5);
glVertex3f( 0.9, 0.9, 0.9 );
glVertex3f( 0.9,-0.9, 0.9 );
glVertex3f( 0.9,-0.9,-0.9 );
glVertex3f( 0.9, 0.9,-0.9 );
// left x, draw dark red
glColor4f( 0.4, 0.0, 0.0, 0.5);
glVertex3f(-0.9, 0.9, 0.9 );
glVertex3f(-0.9, 0.9,-0.9 );
glVertex3f(-0.9,-0.9,-0.9 );
glVertex3f(-0.9,-0.9, 0.9 );
// top y, draw bright green
glColor4f( 0.0, 1.0, 0.0, 0.5);
glVertex3f( 0.9, 0.9, 0.9 );
glVertex3f( 0.9, 0.9,-0.9 );
glVertex3f(-0.9, 0.9,-0.9 );
glVertex3f(-0.9, 0.9, 0.9 );
// bottom y, draw dark green
glColor4f( 0.0, 0.4, 0.0, 0.5);
glVertex3f( 0.9,-0.9, 0.9 );
glVertex3f(-0.9,-0.9, 0.9 );
glVertex3f(-0.9,-0.9,-0.9 );
glVertex3f( 0.9,-0.9,-0.9 );
// near z, draw bright blue
glColor4f( 0.0, 0.0, 1.0, 0.5);
glVertex3f( 0.9, 0.9, 0.9 );
glVertex3f(-0.9, 0.9, 0.9 );
glVertex3f(-0.9,-0.9, 0.9 );
glVertex3f( 0.9,-0.9, 0.9 );
// far z, draw dark blue
glColor4f( 0.0, 0.0, 0.4, 0.5);
glVertex3f( 0.9, 0.9,-0.9 );
glVertex3f( 0.9,-0.9,-0.9 );
glVertex3f(-0.9,-0.9,-0.9 );
glVertex3f(-0.9, 0.9,-0.9 );
glEnd();
return;
}

commented lines in DrawGLScene() are default and don’t make a difference.

Does your hardware support depth buffering, and have you requested a depth buffer when you created the context?

yes I did.
when using “glDepthRange(1.0,0.0);” I’m seeing the bright blue plane.