Question about depth component result using differents projections but with same near and far value, and same view matrix

Hi, I have an OpenGL program where I need to render a 2D image as background ( using ortho projection ) and a few 3D models over this background ( using perspective ).
Due to others requirements of the application I only can clear the depth buffer once, before render any object, and I can’t disable the glDepthMask.

So, I have built two projection matrices one as ortho for 2D and another one as perspective for 3D, where the near and far component of each projection are the same.
Moreover I use the same view matrix for the all objects, the background ( 2D ) and the others ( 3D ).

So I expected that giving a farther Z value to the 2D background position and rendering later the 3D objects with a closer Z position, the scene would be fine.

But it’s not what it happens, the background is the unique visible in the resulting scene.

After it I have tested multiply a same vector by both matrix, ( ortho and perspective ) and I have noticed that the Z component of te resulting vector are diferents.

So, it’s this result correct? If it is, how can I dertermine when the object drawn with the ortho matrix will be behind the rest of the objetcts?

Thanks in advance.

In both cases, Z_eye=-zNear should transform to Z_ndc=-1 while Z_eye=-zFar should transform to Z_ndc=1. However, intermediate values of Z_eye will produce different values of Z_ndc. In particular, an orthographic projection transforms Z linearly (Z_ndc=A-B*Z_eye) whereas an orthographic projection follows a reciprocal law (Z_ndc=A-B/Z_eye). For an orthographic projection, Z_ndc=0 when Z_eye=-(zNear+zFar)/2 (i.e. the mean of -zNear and -zFar); for a perspective projection, Z_ndc=0 when Z_eye=-2*zNear*zFar/(zNear+zFar) (=> 1/Z_eye=(1/-zNear)+(1/-zFar))/2, i.e. it’s the harmonic mean rather than the arithmetic mean).

A couple of other things to consider:

  • conventional projection matrices (as generated by glOrtho, glFrustum, and gluPerspective) have the positive Z axis pointing toward the viewer, so more negative Z_eye values are farther away (and for a perspective projection, only negative Z_eye values are visible; the near plane must be in front of the viewpoint).

  • An orthographic projection will always have W_clip=1 so Z_ndc=Z_clip, a perspective projection will have it proportional to -Z_eye, so you need to consider Z_ndc=Z_clip/W_clip, not Z_clip itself.