glOrtho + -negative z depth + selection

I’m a little confused on this. Let’s say I have a cube that is 4x4x4 and I use glOrtho to set the size of my view volume to, for example:

glOrtho( -5, 5, -5, 5, -5, 5 );

Now, let’s say we use the default view which is looking down the z axis with our location at the origin. When I enter selection mode and attempt to select an object that is at, say, (0,0,-2), I get a negative z depth in my hit record instead of, as I would expect, a large positive number i.e. [0…1] * 2^32-1, with 0 representing the front plane and 1 representing the back plane. Does anyone know why this is happening, especially since depth testing seems to be working properly when I draw the objects on the screen?

Sounds like you’re retrieving the Z value in the coordinate system you set up with glOrtho, whereas what you want is the depth buffer value. Those are two very different things. I believe you can retrieve the depth buffer value using glReadPixels with the ‘format’ option set to GL_DEPTH_COMPONENT (though I’ve never tried it).

I found the problem. I was using a Hit Record class from a long time ago. Turns out the minZDepth and maxZDepth variables were set as int’s not unsigned int’s. So, when my depth values got very large the sign was changing!