# calculate world space position by view space and camera position

This topic is 2100 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi!

I need help with some probably fairly math. In my DX9 pixel shader I've got

1. The screen space uv-position/coordinate of a certain pixel

2. The view space position of that pixel (sampled through position sampler state)

3. The world space position of the camera/eye

4. The direction of the camera/eye

Now I need formulas for to

1. Calculate the world space position of that given pixel

2. The opposite, i.e. calculate the screen space uv-coordinate of an other pixel whose world position I have.

How would I do those calculations in the HLSL pixel shader?

Edited by Meltac

##### Share on other sites

screen_space_pos = MVPMatrix * world_pos;

world_pos = InvMVPMatrix * screen_space_pos;

you need the MVPMatrix of the pixel who's screen space you have. OR the InvMVPMatrix of the pixel who's screen space you have.

##### Share on other sites

you need the MVPMatrix of the pixel who's screen space you have. OR the InvMVPMatrix of the pixel who's screen space you have.

Thanks, but it should be possible without. I mean, I could do it myself with the given inputs as described above, but since I'm lacking the proper math formulas it would require me a hell of a time to get it working properly. But logically spoken the given parameters must be sufficient to calculate the world position.

Anyways, my engine provides the following matrices, unfortunately without any documentation. So which one(s) would I need to do it with your approach?

uniform half3x4 m_W;
uniform half3x4 m_V;
uniform half4x4 m_P;
uniform half3x4 m_WV;
uniform half4x4 m_VP;
uniform half4x4 m_WVP;

Edited by Meltac

##### Share on other sites

The screenspace position of your pixel is calculated like

Pos_ss = m_P * m_ V * m_W * Input.pos = m_VP * m_W * Input.pos = m_P * m_ WV * Input.pos = m_MVP * Input.pos

which are all equivalent terms.

m_W would be the world matrix, it handles the transformation of vertices from their loclal coordinate space in the model to the world space of your scene.

m_V would be the view matrix, it handles the transformation from the world space to the view space. You can think of that like this: A model that is in world space can be watched from differend angles and positions. this is what the view transformation does.

m_P is the projection matrix, it will transform the viewspace coordinates into screenspace. Basically the projection matrix defines what "kind of camera you use to shoot a frame".

properties like how large is the angle of th lense, when to clip primitives, which are too far or too close to the lense (for perspective projection).

m_WV, m_VP, m_WVP are the single matrices multiplied in the manner the top formula shows (using associativity of matrix multiplication)

In any case you should be fine using the formulas hdxpete provided using m_MVP.

For the second you will have to calculate the inverse matrix, which i recommend to do this in you application and pass it as another parameter.

##### Share on other sites

Thank you very much for those explanations!

I should have mentioned that "my" engine isn't actually an application developed by me, but a game engine (X-Ray 1.0) that I cannot change. All I can change are the HLSL pixel shaders. So, calculating an inverse matrix engine-wise is not an option, I need a way to do all the calculation with what I got in HLSL only. Any idea?

##### Share on other sites

i guess that if you assume that these two pixels belong to the same object then using the inverse matrix of the current pixel could work. though i really doubt that you could make that assumption. and they would have to be "relatively" close for it to work somewhat correctly.

while i don't know if they have a inverse matrix page euclidean space has a bunch of matrix information

http://www.euclideanspace.com/maths/algebra/matrix/index.htm

or just pick up a intro to linear algebra book.

ohh and while you could do an inverse matrix in the vertex/pixel shader i HIGHLY recommend against it.

if this engine you are using has something called a "normal matrix" that is the tranpose of the inverse of the MVP matrix. you could transpose that however it wont contain the translation.

i've not seen an engine that doesn't let you specify your own uniforms to shaders.

##### Share on other sites

Thanks again. Not sure if I understand what you say... but I'll try to figure it out somehow.

i've not seen an engine that doesn't let you specify your own uniforms to shaders.

Well, NOW you've seen one    It's not open, nor does it have any official API - I'm just modding a game built on it.

##### Share on other sites

Ok, I got it working, nearly...

I have one issue still: When the object in question is opposite to the camera direction (i.e. in the viewer's back) the screen space position will return a value sayings it's right in front of the viewer/camera. So I get a screen coordinate as if the object was mirrored by the camera's position, or rotated 180 degrees around the camera/viewer.

I've tried angle checks but since the shader "thinks", according to the sceen pos calculation, that the object is in front of the viewer rather than right in the viewer's back, it considers all ok.

Any ideas how to prevent this?

EDIT:

Nevermind, found it - I needed to abs() the devide-by-w I made.

Edited by Meltac

• 18
• 18
• 11
• 21
• 16