Reputation: 3106
I'm trying to understand how to restore the z from the depthbuffer and trying to do the math, based on these two posts:
Getting the true z value from the depth buffer (stackoverflow) http://ogldev.atspace.co.uk/www/tutorial46/tutorial46.html
The two posts use different projection matrices (specifically the lower-right 2x2 part is different, one difference being use of a -1 vs a +1 in [ 3 ][ 4 ]). Not really sure why that would be, afaik the one with -1 is "the correct OpenGL projection-matrix" (right?)
Now I tried to do the calculation for both and the weird thing is that in the SO-post it mentions -A-B/z (or in my calculation -S-T/z). Then the code shows
And solving this for A and B (or S and T) gives
Okay, now doing the calculation for both Projection-matrices from scratch, (left=ogldev.atspace.co.uk, right=stackoverflow) and now it gets confusing because up to the -S-T/z part everything is fine, but when we compare the solved formula for which should have been the stackoverflow-case (-1 projection-matrix) it matches the one from ogldev.atspace.co.uk (+1 projection matrix) - colored in red...
this is confusing, any clues what I'm doing wrong?!
Updated calculations, see comments from "derhass" below:
Upvotes: 2
Views: 419
Reputation: 45332
The two posts use different projection matrices (specifically the lower-right 2x2 part is different, one difference being use of a -1 vs a +1 in [ 3 ][ 4 ]). Not really sure why that would be, afaik the one with -1 is "the correct OpenGL projection-matrix" (right?)
No. There is no "right" and "wrong" here. There are just conventions. The "correct" matrix is the one which does the right thing, for the conventions you chose to use. Classic GL's glFrustum
function did indeed use the matrix from that StackOverflow post. The convention here is that projection center is at origin, view direction is -z
, x
is right and y
up. But you can use any convention, with arbitrary principal point, and arbitrary projection direction. The other matrix is just +z
as the projection direction, which can be interpreted as a flipped handedness of the coordinate space. It can also be interpreted as just looking in the opposing direction while still keep the left-handed coordinate system.
this is confusing, any clues what I'm doing wrong?!
I'm not sure what you're trying to prove here, besides the fact that introducing small sign errors will give bogus results...
Your derivations for the "+z" projection matrix seem OK. It maps depth=0
to z=n
, and depth=1
to z=f
, which is the standard way of mapping these - and just another convention. You could also use a Reversed-Z mapping, where the near plane is mapped to depth 1, and far plane mapped to depth 0.
UPDATE
For the second matrix, you flipped the sign again, even after the corrections from my comment. When you substitued [Now after you fixed the calulation again, you have got]
a formala which is exactly the negated one as in the S
and T
back in that final formula, you actually substituted in -S
. If you did the correct substitutions, you would have gotten+z
matrix case - depth = 0 is mapped to -n
, and depth=1 to -f
, which is excatly how those parameters are defined in the classic GL convention, where n
and f
just describe the distances to those plane, in viewing direction (-z
).
Upvotes: 2