# extract (zfar-znear) from projection matrix

Is it possibe to reverse the calculation of a perspective matris to get the distance between znear and zfar?

Originally posted by Mazy:
Is it possibe to reverse the calculation of a perspective matris to get the distance between znear and zfar?

Yes, but not a good idea. The glPerspective has a fix expression for the matrix, but it might be driver specific (not exact).

ok, but i think i still want to try it out… have any calculation?

Originally posted by Mazy:
[b]ok, but i think i still want to try it out… have any calculation?

[/b]

The NVIDIA use this matrix:
2n/(r-l) 0 0 0
0 2n/(t-b) 0 0
(l+r)/(r-l) (t+b)/(t-b) (n+f)/(n-f) -1
0 0 (2nf)/(n-f) 0

So a=(n+f)/(n-f), b=(2nf)/(n-f).
(1/((a+1)(a-1)))/b=c=(n-f)/2
(a+1)/c=n, (a-1)/c=f

Check it.

Wasn’t there something like glGet(GL_ZNEAR)??? I’ve got the specs at another computer, so I’m unable to check it now.

Yes, but not a good idea. The glPerspective has a fix expression for the matrix, but it might be driver specific (not exact).

The opengl specification describes what projection matrix to create when calling and glFrustum. I’d be very, ~very~ surprised if a driver deviated from this specification. At the end of the day, however, points are projected to the unit cube coordinate system and you can easily reverse that process as soon as you know P.

Originally posted by Zengar:
Wasn’t there something like glGet(GL_ZNEAR)??? I’ve got the specs at another computer, so I’m unable to check it now.

The glGet* token is GL_DEPTH_RANGE. Two values are returned, the first being the near clipping distance and the second being the far.

Well, that doenst help me, since im trying to extract it in a vertexprogram Originally posted by Mazy:
[b]Well, that doenst help me, since im trying to extract it in a vertexprogram [/b]

Then give it as a parameter.
It is good to count the znear and zfar parameters from the projection matrix at each vertex???

I know that i can send it as a parameter I just wanted to try go get it in the vertexprogram anyway… Im not aiming for 800+ fps here, and i dont have so many vertices that it really matters…

add: And GL_DEPTH_RANGE doenst gice you near/far plane, it only gives you the values set by glDepthRange.

[This message has been edited by Mazy (edited 06-02-2003).]