Depth Buffer

Hi all -
I am trying to use the Depth Buffer feature of OpenGL to determine when one pixel should or should not be drawn on top of another. I enable the Depth Buffer and have confirmed that it is active and working.

The documentation I read indicates that the z-axis value determines whether or not a pixel will overwrite the value of a previous pixel. If the z-axis value is numerically higher, the new pixel will overwrite the old one, else it will not. I have actually confirmed that this is working by changing the z-axis value in the drawing functions used, which is simply glVertex3f. If I change the last parameter, which is the z-axis value, it affects which points overwrite others, as stated by the documentation.

My problem is that when I change the z-axis value, the value of the point shifts on the graph. It’s as if the z-axis value was being interpreted as an actual z value in a 3D coordinate system. It was my understanding that if you have the Depth Buffer enabled, the z-axis value is used for the Depth test only, and not as part of the point coordinate. My understanding is very limited, I just started working with OpenGL a few days ago.

Since our plot is really a 2D plot, I don’t need to specify a z-axis value, and want to use this value solely to control the Depth Buffer and not have it interpreted as the z-axis value of a 3D coordinate. Is this possible?

Thanks in advance for any help you can give me.

Yes, it is possible, but your understanding of the pipeline is indeed very limited.

With glVertex3f you specify points in 3D space. That means the z-value in 3D space has nothing to do with the “Z-value” used in the depth test. The depth test operates in a different space (screen-space) that is always relative to what you are looking at. For example, if you look down the X-axis, the depth-test will still sort your geometry correctly, because it sorts everything by the depth from it’s camera center, no matter which axis that actually is. It only names that depth as “Z”, but it is not the same as the z-axis given with glVertex.

If you really only want to render 2D geometry, you should look at the function glOrtho. It sets up an orthographic projection, that is one where you don’t use any perspective foreshortening depending on the depth, but the depth is mostly ignored (except for the depth-test, which can still use it for sorting). Right now you most certainly use gluPerspective or glFrustum to set up your camera matrix. That piece of code needs to be replaced with glOrtho.

Hope that helps,