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 ??
w=1
for you, there'sD3DXVec3TransformCoord
. There are also-Array
functions to avoid your loops. \$\endgroup\$D3DXVec3Project
? BTW, D3DXMath is deprecated. See DirectXMath. \$\endgroup\$