Matrix question - Vertex position in screen space.

Started by
4 comments, last by DividedByZero 1 year, 6 months ago

Hi Guys,

I'm presently brushing up of some 3D math and am wondering how I would go about working out the final step of where a singular vertex would be positioned in screen space in the -1 to +1 range.

I have done the following calculations based on the following parameters and the results listed below.

At this stage I haven't multiplied anything against the world matrix, so that would effectively mean that the vertex would be located at space 0, 0, 0.

So my question is, what calculation would I need to do against the WVP matrix in order to work out the screen location X & Y on the screen? Expecting the result to be 0, 0 (being direct centre of the screen).

Many thanks!

	float fovY = 60.0f;
	float aspect = 1280.0f / 800.0f;
	float near = 1.0f;
	float far = 1000.0f;

	XMVECTOR from = { 0.0f, 0.0f, -2.5f, 1.0f };
	XMVECTOR to = { 0.0f, 0.0f, 0.0f, 1.0f };
	XMVECTOR up = { 0.0f, 1.0f, 0.0f, 0.0f };
	XMMATRIX matView = XMMatrixLookAtLH(from, to, up);

View Matrix
1       0       0       0
0       1       0       0
0       0       1       0
0       0       2.5     1


Proj Matrix
-0.0975741      0       0       0
0       -0.156119       0       0
0       0       1.001   1
0       0       -1.001  0


View * Proj Matrix
-0.0975741      0       0       0
0       -0.156119       0       0
0       0       3.501   1
0       0       -1.001  0


Identity Matrix
1       0       0       0
0       1       0       0
0       0       1       0
0       0       0       1


WVP Matrix
-0.0975741      0       0       0
0       -0.156119       0       0
0       0       3.501   1
0       0       -1.001  0
Advertisement

After the perspective projection the vertex is in clip space, and the rasterizer performs the perspective division to get the NDC coordinates in [-1, 1].

If you want to calculate the NDC coordinates, just divide the clip coordinates by the w-coordinate.

If you want to calculate the screen coordinates, you need to apply the viewport transformation to the vertex in NDC space.

Thanks for the reply Pam.

Sorry for my ignorance, but how would you work out the clip co-ordinates, and w co-ordinate of what?

After multiplying a vertex in local space with the WVP matrix you get the vertex clip coordinates (x, y, z, w).

Then, the NDC coordinates are (x/w, y/w, z/w).

@PAM79 Ah! Awesome. Thanks for that. I'll have a ply around now.

This topic is closed to new replies.

Advertisement