2

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

enter image description here

And solving this for A and B (or S and T) gives

enter image description here

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...

enter image description here

this is confusing, any clues what I'm doing wrong?!

Updated calculations, see comments from "derhass" below: enter image description here

Community
  • 1
  • 1
kalmiya
  • 2,988
  • 30
  • 38
  • no clue as your images are not accessible for me right now... anyway may be storing the original depth values into depth buffer itself will bypass your problems see [How to correctly linearize depth in OpenGL ES in iOS?](http://stackoverflow.com/a/42515399/2521214) – Spektre Apr 22 '17 at 09:29
  • Hmm, I do see the images... do they need to be 'approved' or sth? Here's a link: http://www.kalmiya.com/images/stackoverflow/so-math1.png / http://www.kalmiya.com/images/stackoverflow/so-math2.png / http://www.kalmiya.com/images/stackoverflow/so-depthproj-compare.png... Thanks for your link, will check it out. Generally it's more about understanding how and why. – kalmiya Apr 22 '17 at 09:57
  • So what exactly is your question? The sign in the last row distinguishes left-handed from right-handed systems. Have you checked if your derivations are correct? – Nico Schertler Apr 22 '17 at 10:03
  • The stackoverflow-link uses the matrix on the right (with -1), gets to -S-T/z and then when subsituting S and T and solving it doesn't end up with nf / (-nd + n + fd - f) but with nf / (f+d(n-f) )... as if halfway through the calculation they started using the +1 projection-matrix... I checked the derivations as good as I can. – kalmiya Apr 22 '17 at 10:14
  • @kalmiya the images are viewable now ... it was not your fault the imgur server you used was down or its connection to it in my part of world. anyway if I see it right f the red therm is bothering you then: `f-fd+nd = f+d(-f+n) = f+d(n-f)` so I do not see any problem with that as they are identities. – Spektre Apr 22 '17 at 11:27
  • 2
    You did not use the matrix from the SO page. There, S should be `-(f+n)/(f-n)`, while you wrote `(-f+n)/(f-n)`. – derhass Apr 22 '17 at 12:25
  • @Spektre: I'm having difficulties explaining, let's try again: see the right column of my overview above: The stackoverflow calculation uses the -1 matrix, arrives at -S - T/z (so far ok), then rewriting to Z=... and substituting S and T, and then it *should* arrive at the blue answer. But somehow they arrive at the red answer ( which is the correct answer *if* they had used the projection-matrix from ogldev.atspace - which they didn't). – kalmiya Apr 22 '17 at 14:43
  • Thanks @derhass, good catch, that was indeed a mistake! That helps, but it still doesn't explain the thing I'm bothered by. I added an updated picture. – kalmiya Apr 22 '17 at 14:43
  • @kalmiya: OK, after you updated the question again, I updated my answer. But I'm really not sure what the question is any more. The results you got are the results you should get, as explained in my answer. – derhass Apr 22 '17 at 21:14

1 Answers1

2

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 S and T back in that final formula, you actually substituted in -S. If you did the correct substitutions, you would have gotten [Now after you fixed the calulation again, you have got] a formala which is exactly the negated one as in the +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).

derhass
  • 43,833
  • 2
  • 57
  • 78
  • Thanks for your patient explanation. I updated the image to match this. As you noticed math is not my strongest skill - but in my defence at least I did try to calculate it instead of simply copy & pasting the shader-code from the so-post ^^. It starts to make more sense now. Also nice to see how to verify the results with depth – kalmiya Apr 22 '17 at 22:57
  • You don't need to defend yourself, nobody is attacking you... ;) If you try to understand these things, I suggest actually plotting the equations. – derhass Apr 22 '17 at 23:06