0
votes

How can one mix ortographic and perspective projection in openGL? Some 2d elements have to be drawn in screen space (no scaling, rotation, etc..) These 2d elements have a z position, they have to appear in front/behind of other 3d elements. So i set up orographic projection, draw all 2d elements, then setup perspective projection and draw all 3d elements. The result is that all 2d elements are drawn on top. It seems that the z values from the orto projection and the z values from the perspective projection are not compatible (GL_DEPTH_TEST).

Separately all 2d and all 3d elements work fine, the problem is when i try to mix them. Does the prespective projection changes the z values? In what way? Is it possible to use z values from orto projection mixed with z values from perspective projection for depth test, or this whole concept is flawed?

Bare opengl1.5

1
Have a look at this question.derhass

1 Answers

0
votes

It seems that the z values from the orto projection and the z values from the perspective projection are not compatible (GL_DEPTH_TEST).

That is indeed the case. Perspective transformation maps the Z values nonlinear to the depth buffer values. The usual way to address this problem is to copy the depth buffer after the perspective pass into a depth texture and use that as an additional input in the fragment shader of the orthographic drawn stuff, reverse the nonlinearity in the depth input and compare the incoming Z coordinate with that; then discard appropriately.

It's also possible to emit linear depth values in the perspective drawn geometry fragment shaders, however the depth nonlinearity of perspective projection has its purpose; without it you loose depth precision where it matters most, close to the point of view.