# How can I get projected coordinates of vertex on screen

I want to check picking for faces and for that I need projected X Y coordinates of my vertexes in -1 to 1 range:
For that I multiply my vertex array with matrices and then get projected triangle coordinates from it and check if mouse is inside of that triangle ,but something goes wrong:
Here is my code

    for (int i = 0; i < m_vertexCount; ++i)
{
D3DXVec4Transform(&m_Pverts1[i], &m_Pverts[i], &worldmatrix);
D3DXVec4Transform(&m_Pverts1[i], &m_Pverts[i], &viewmatrix);
D3DXVec4Transform(&m_Pverts1[i], &m_Pverts[i], &projectionmatrix);
}
z = 0;
for (int i = 0; i < m_faceCount; ++i)
{
x1 = m_Pverts1[m_verts[z].x - 1].x / m_Pverts1[m_verts[z].x - 1].w;
y1 = m_Pverts1[m_verts[z].x - 1].y / m_Pverts1[m_verts[z].x - 1].w;
z1 = m_Pverts1[m_verts[z].x - 1].z / m_Pverts1[m_verts[z].x - 1].w; ++z;
x2 = m_Pverts1[m_verts[z].x - 1].x / m_Pverts1[m_verts[z].x - 1].w;
y2 = m_Pverts1[m_verts[z].x - 1].y / m_Pverts1[m_verts[z].x - 1].w;
z2 = m_Pverts1[m_verts[z].x - 1].z / m_Pverts1[m_verts[z].x - 1].w; ++z;
x3 = m_Pverts1[m_verts[z].x - 1].x / m_Pverts1[m_verts[z].x - 1].w;
y3 = m_Pverts1[m_verts[z].x - 1].y / m_Pverts1[m_verts[z].x - 1].w;
z3 = m_Pverts1[m_verts[z].x - 1].z / m_Pverts1[m_verts[z].x - 1].w; ++z;

//code .. //
}


Where m_Pverts is array of vertexes m_Pverts1 is empty array for transformed vertexes and m_verts[].x is array of vertex indexes: x1,y1,z1,x2,y2,z3,x3,y3,z3 must be X Y projected coordinates of triangle and Z depth:
Other code is checked and works right:

What I am doing wrong ??

• Elaborate on what values you get from the transform? Note that they will be in NDC. Note that if you want something that projects to w=1 for you, there's D3DXVec3TransformCoord. There are also -Array functions to avoid your loops. – Lars Viklund Nov 22 '15 at 20:56
• Have you looked at D3DXVec3Project? BTW, D3DXMath is deprecated. See DirectXMath. – Chuck Walbourn Nov 23 '15 at 20:03

I get the solution of my problem: Instead of using worldmatrix viewmatrix and projectionmatrix
i must use wvpwatrix which is multiplication of that three matrices

D3DXMATRIX wvp;

wvp = worldmatrix*viewmatrix*projectionmatrix;

for (int i = 0; i < m_vertexCount; ++i)
{
D3DXVec3TransformCoord(&m_Pverts1[i], &m_Pverts[i], &wvp);
}


But I dont know why is it working??

• It's working because matrix multiplication doesn't follow algebraic multiplication laws. So MVP * v isn't the same as P * (V * (M * v)) – EvilTak Nov 23 '15 at 11:34