OpenGL Weird depth problem with ortho projection

This topic is 3196 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi, I just started using OpenGL and I am currently trying to make a simple 2D game using the orthographic projection of OpenGL. However, I ran into a problem with the z-coordinates of my, still very basic, shapes when I render them to the screen. Here's what I am trying to accomplish: render objects with a larger z-coordinate behind objects with a smaller z-coordinate, in orthographic projection. I am doing the following: 1) Create a window with an OpenGL context, here I specify that I want a depth buffer. 2) Setup my ortho projection with: glOrtho(0, windowWidth, windowHeight, 0, -1, 1); 3) Enable depth testing with:
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glClearDepth(1.0);
4) In my main loop I do the following: clear the color and depth buffers with glClear(), draw all objects to the screen, and swap the buffers with glXSwapBuffers() (I'm on Linux). When I draw my objects, which are all very simple, single-colored quads, I specify their x, y, z coordinates with glTranslatef(x, y, z). The problem is the following: 1) The object that I draw first are always ON TOP of objects I draw later, shouldn't this be the other way around? 2) No matter how I adjust the z-coordinates of the objects, it stays this way. 3) When I set the z-coordinate of one of the objects to -1.0, it isn't drawn at all. What am I doing wrong?

Share on other sites
Depth testing is based on the projected Z-coordinate. That means you have to consider the visible depth range (near and far clip planes, basically), and where the Z-axis is increasing in depth as treated by the depth buffer logics.

Your setup has the near clip plane at Z=1 and the far clip plane at Z=-1. The clip plane parameters to glOrtho are the distances to the respective planes along the negative Z-axis where you want the plane to be located. Thus, a near plane at -1 means -1 units along the negative Z-axis, and it will be located at Z=1.

Since depth increases from the near plane to the far plane, the depth increases as the object goes from Z=1 to Z=-1. Thus, an object at Z=0.6 occludes an object at Z=0.4, since it's closer to the near plane. So as you can see, even though the Z-coordinate is larger, it is closer to the near plane than a smaller Z-coordinate. But these are different Z-coordinates than the ones the depth buffer uses.

Share on other sites

I'm afraid however that you left me a bit confused...
You're saying that my setup has the near clip plane at z=1 and the far clip plane at z=-1, but I'm using glOrtho(0, windowWidth, windowHeight, 0, -1, 1), and according to the OpenGL 2.1 reference pages the parameters are as follows: void glOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble nearVal, GLdouble farVal), which would mean that it's the other way around, no?

An I don't quite understand the difference between the depth buffer z-coordinates and those that I specify in the above function call. What exactly do I have to change to make this work?

EDIT: Alright, I got it...I changed absolutely nothing and now it works, so there ya go...

[Edited by - FalFire on December 23, 2009 8:14:24 AM]

Share on other sites
The near and far clip plane values are, as I said, the distances along the negative Z-axis. That is the opposite of the actual Z-values in eye-space, hence the sign change of the clip plane positions.

It is the same in perspective projection as set by glFrustum or gluPerspective. If you set the near and far clip planes to, say, 1 and 100, the visible Z-range is -1 to -100 along the Z-axis.

1. 1
2. 2
3. 3
Rutin
19
4. 4
5. 5

• 9
• 19
• 9
• 31
• 16
• Forum Statistics

• Total Topics
632618
• Total Posts
3007460

×