How to use OpenGL orthographic projection with the depth buffer? -
How to use OpenGL orthographic projection with the depth buffer? -
i've rendered 3d scene using glfrustum()
perspective mode. have 2d object place on 3d scene deed label particular 3d object. have calculated 2d position of 3d object using using gluproject()
@ position place 2d label object. 2d label object rendered using glortho()
orthographic mode. works , 2d label object hovers on 3d object.
now, want give 2d object z value can hidden behind other 3d objects in scene using depth buffer. have given 2d object z value know should hidden depth buffer, when render object visible.
so question is, why 2d object still visible , not hidden?
i did read somewhere orthographic , perspective projections store incompatible depth buffer values. true, , if how convert between them?
i don't want transformed, instead want appear flat 2d label faces photographic camera , remains same size on screen @ times. however, if hidden behind want appear hidden.
first, should have set that in question; explains far more you're trying question.
to accomplish this, need find z-coordinate in orthographic projection matches z-coordinate in pre-projective space of want label appear.
when used gluproject
, got three coordinates back. z coordinate important. need reverse-transform z coordinate based on znear , zfar values give glortho
.
pedantic note: gluproject
doesn't transform z coordinate window space. so, have take gldepthrange
parameters. assume depth range of near = 0.0 , far = 1.0.
so our first step transform window space z normalized device coordinate (ndc) space z. utilize simple equation:
ndcz = (2 * winz) - 1
simple enough. now, need go clip space. no-op, because orthographic projection, w coordinate assumed 1.0. , partition w difference between clip space , ndc space.
clipz = ndcz
but don't need clip space z. need pre-orthographic projection space z (aka: photographic camera space z). , requires znear , zfar parameters gave glortho
. photographic camera space, this:
cameraz = ((clipz + (zfar + znear)/(zfar - znear)) * (zfar - znear))/-2
and you're done. utilize z position in rendering. oh, , create sure modelview matrix doesn't include transforms in z direction (unless you're using modelview matrix apply z position label, fine).
opengl buffer depth orthographic
Comments
Post a Comment