Skip to main content
GameDev.net gamedev.net
🔒 Locked

Matrix question - Vertex position in screen space.

Started by DividedByZero Nov 2, 2022 at 1:58 AM 4 replies 5.2k views
Original Post
DividedByZero
DividedByZero

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

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.

DividedByZero
DividedByZero

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?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.