Advertisement

near and far z values from worldviewprojection

Started by June 11, 2019 11:51 AM
7 comments, last by l0calh05t 5 years, 7 months ago

Hi, 

In direct3D (DX9), I am wondering if I have the worldviewprojection matrix and the world matrix if its possible to extract the near and far z values that you would use to construct the projection matrix (i.e the zn and zf parameters here  :https://docs.microsoft.com/en-us/windows/desktop/direct3d9/d3dxmatrixperspectivefovlh)

From what I can gather, if i have the inverse of the viewprojection I should be able to multiply it by corner points on the near and far planes (e.g {-1, -1, 0, 1} and {-1, -1, 1, 1}) and get the corner points in world space. In the examples I have seen, divide my w and the resulting z values would give me zn and zf. Is this correct or am I misunderstanding something here?

I would find the inverse of the world matrix, multiply it by the worldviewprojection to give the viewprojection, and then invert that?

I haven't managed to get anything close to what I am after, attempting the above, however, just wanted to check the theory actually makes sense.

Cheers

Dave

 

You only want the projection matrix not the viewProjection. But in principle, yes, just apply the inverse projection to the corners of the NDC and divide by the resulting w (you may need to flip the sign as well). Don't forget that the resulting w may be 0 if it is a projection matrix with zf=infty.

Advertisement
3 hours ago, l0calh05t said:

u only want the projection matrix not the viewProjection. But in principle, yes, just apply the inverse projection to the corners of the NDC and divide by the resulting w (you may need to flip the sign as well). Don't forget that the resulting w may be 0 if it is a projection matrix with zf=infty.

Ah ok. That's unfortunate, as I have access to neither the view, worldview, or projection matrix. I may need to try a different approach. Thanks anyway

Whilst opengl, this seems to imply inverse viewprojection should work, doesnt it? 

https://stackoverflow.com/questions/33436963/most-accurate-way-to-calculate-view-frustum-corners-in-world-space

11 hours ago, davegl1234 said:

Whilst opengl, this seems to imply inverse viewprojection should work, doesnt it? 

https://stackoverflow.com/questions/33436963/most-accurate-way-to-calculate-view-frustum-corners-in-world-space

No, it does not. Think about the following: Do zn and zf vary if the camera moves? The z component of the view frustum corners in world space do vary when the camera moves.

Yea, that makes sense, sorry, please excuse my lack of experience here.

If all I am trying to do is detect when the far/near plane ratio increases that should be possible using the inverse viewprojection though, right? (I am actually working on a directx wrapper, so only have access to what is used by the shaders of a particular game)

Does this make sense:

If I were to multiply (0, 0, 0, 1) and (0, 0, 1, 1) by the inverse viewprojection I would get two points on the near and far plane in world space (no relation to the camera but related to each other). Can I then just use trigonometry to calculate the distance between those points in world units, and thus detect changes in the near/far ratio? I do actually have access to the camera position in world coordinates, so could I then even calculate zn and zf based on the distances from the camera, using trigonometry?

Again, please excuse my lack of experience, if this makes no sense.

Thanks for your help

 

 

 

Advertisement

Maybe I'm missing something, but why not just save the values when you build the matrix?

Edit: ok sorry I missread the wrapper part.

20 hours ago, davegl1234 said:

Yea, that makes sense, sorry, please excuse my lack of experience here.

If all I am trying to do is detect when the far/near plane ratio increases that should be possible using the inverse viewprojection though, right? (I am actually working on a directx wrapper, so only have access to what is used by the shaders of a particular game)

Does this make sense:

If I were to multiply (0, 0, 0, 1) and (0, 0, 1, 1) by the inverse viewprojection I would get two points on the near and far plane in world space (no relation to the camera but related to each other). Can I then just use trigonometry to calculate the distance between those points in world units, and thus detect changes in the near/far ratio? I do actually have access to the camera position in world coordinates, so could I then even calculate zn and zf based on the distances from the camera, using trigonometry?

Again, please excuse my lack of experience, if this makes no sense.

Thanks for your help

 

 

 

If you have the world space position, sure, you can (estimate) the near and far planes:

Take three points on the near plane to form a plane and compute the distance between the plane and the camera position, that should give you zn (unless you have something weird like a oblique near plane going on http://www.terathon.com/gdc07_lengyel.pdf). Do the same with three points on the far plane for zf.

If you definitely don't have any off-center projections, you can also just take a point on the near and far plane at the center of your screen and just compute the distance between those points and the camera position.

All of this will fail if the view matrix contains any scaling/shear components.

This topic is closed to new replies.

Advertisement