Sign in to follow this  
MefistoManna

Picking and world transformation.

Recommended Posts

MefistoManna    139
Sorry for my english ;).
I picked mesh with this code:
[CODE]
void :Pick(int Num, std::vector<LPD3DXMESH> g_Mesh)
{
if(m_Input::MouseButtonDown(0))
{
POINT Cursor;
GetCursorPos( &Cursor );
ScreenToClient( m_Input::GetHandle(), &Cursor );
D3DVIEWPORT9 m_Viewport;
g_D3DDevice_->GetViewport( &m_Viewport );

D3DXVECTOR3 v;
D3DXMATRIX matProj;
g_D3DDevice_->GetTransform( D3DTS_PROJECTION, &matProj );
v.x = ( ( ( 2.0f * Cursor.x ) / m_Viewport.Width ) - 1 ) / matProj._11;
v.y = -( ( ( 2.0f * Cursor.y ) / m_Viewport.Height ) - 1 ) / matProj._22;
v.z = 1.0f;

D3DXMATRIX m;
D3DXVECTOR3 rayOrigin,rayDir;
D3DXMATRIX view;
g_D3DDevice_->GetTransform(D3DTS_VIEW, &view);
D3DXMatrixInverse( &m, NULL, &view );
rayDir.x = v.x*m._11 + v.y*m._21 + v.z*m._31;
rayDir.y = v.x*m._12 + v.y*m._22 + v.z*m._32;
rayDir.z = v.x*m._13 + v.y*m._23 + v.z*m._33;
rayOrigin.x = m._41;
rayOrigin.y = m._42;
rayOrigin.z = m._43;

D3DXMATRIX matInverse;
D3DXMATRIX world;
g_D3DDevice_->GetTransform(D3DTS_WORLD, &world);
D3DXMatrixInverse(&matInverse,NULL,&world);

D3DXVECTOR3 rayObjOrigin,rayObjDirection;
D3DXVec3TransformCoord(&rayObjOrigin,&rayOrigin,&matInverse);
D3DXVec3TransformNormal(&rayObjDirection,&rayDir,&matInverse);
D3DXVec3Normalize(&rayObjDirection,&rayObjDirection);

D3DXIntersect(g_Mesh[Num], &rayObjOrigin, &rayObjDirection, &picked, NULL, NULL, NULL, &dist, NULL, NULL);

if(picked)
{
picked[Num] = true;
}
}
}
[/CODE]
Now, if the mesh is scaled or rotated, this work perfectly. But when it is translated the code doesn't work because after the trasformation, i can pick the mesh only at initial point (0, 0, 0). What can I do? I readed other discussion but i don't find the solution.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this