help me understand rendering a quad down the z-axis

Started by
4 comments, last by bradbobak 11 years, 4 months ago
I've used the code from nehe's lesson2 to build a simple app that demonstrates the problem I'm having.
The code is original, except I've removed the gluPerspective line, made it non-fullscreen and changed the drawGLScene() routine.

Here is the new drawGLScene() :

int drawGLScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0, 0.0f, -0.5f);
glColor3f(1, 1, 1);
glBegin(GL_QUADS);
glVertex3f(-0.5f, 0.5f, 0.2f);
glVertex3f(0.5f, 0.5f, 0.2f);
glVertex3f(0.5f, -0.5f, 0.2f);
glVertex3f(-0.5f, -0.5f, 0.2f);
glEnd();
glColor3f(1, 0, 0);
glBegin(GL_QUADS);
glVertex3f(-0.5f, 0.5f, -0.2f);
glVertex3f(0.5f, 0.5f, -0.2f);
glVertex3f(0.5f, -0.5f, -0.2f);
glVertex3f(-0.5f, -0.5f, -0.2f);
glEnd();
if (GLWin.doubleBuffered)
{
glXSwapBuffers(GLWin.dpy, GLWin.win);
}
return True;
}


As you can see, I'm drawing 2 rectangles at the same position, except along the z-axis.
The first is colored white, and has its vectices for z at 0.2f. The second is red, and z is at -0.2f.
For some reason, the red quad appears in front of the white one. Seeing how opengl treats the negative z axis as going into the screen, I'm trying to understand why the white quad (z at 0.2f) isn't drawn in front of the red one.
Advertisement
Because you probably didn't enable depth test. In that case OpenGL draws objects in the order you issue commands (like in 2D drawing).
Depth testing is enabled. Even if I switch the drawing order, the red quad is still up front.
If you use lesson 2, you set glDepthFunc(GL_LEQUAL);, and therefore, lesser depth values will be drawn over higher depth values.
Two points:

  1. The near and far clip plane values, as specified by the glOrtho or glFrustum (or equivalent functions) are along the negative Z-axis. That means a clip plane value of 5 means that the clip plane is located at z=-5.
  2. If you leave out the projection matrix, the default matrix is the identity matrix which is equivalent to glOrtho(-1, 1, -1, 1, 1, -1).


Pay attention to the default clip plane values; near=1 and far=-1. This means that the near clip plane is located at z=-1, and the far clip plane is located at z=1. This means that a quad at z=-0.2 is close to the near clip plane than a quad at z=0.2, and thus your red quad occludes your white quad.
Ok. I get the idea. Thanks for the help.

This topic is closed to new replies.

Advertisement