Sign in to follow this  

Help! I got some problems in picking mesh.

This topic is 3627 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I render the mesh following this:
HRESULT Entity::Render(D3DXVECTOR3 pos)
{
        D3DXMATRIX matWorld;
	D3DXMATRIX matPos;
	D3DXMatrixScaling(&matWorld, 1.0f, 1.0f, 1.0f);
	D3DXMatrixTranslation(&matPos, pos.x, pos.y, pos.z);
	matWorld = matWorld * matPos;
	m_pD3dDevice9->SetTransform(D3DTS_WORLD, &matWorld);
	for( DWORD i=0; i<m_dwNumMaterials; i++ )
	{
		m_pD3dDevice9->SetMaterial( &m_pMeshMaterials[i] );
		m_pD3dDevice9->SetTexture( 0, m_pMeshTextures[i] );
		m_pMesh->DrawSubset( i );
	}
}

And I pick mesh like this:
HRESULT PickTest(CWnd* pWnd, ID3DXMesh* pMesh)
{
	D3DXVECTOR3 vPickRayDir;
	D3DXVECTOR3 vPickRayOrig;
	D3DXMATRIX matProj;
	D3DVIEWPORT9 viewport;
 
	m_pD3dDevice9->GetTransform(D3DTS_PROJECTION,&matProj);   
	m_pD3dDevice9->GetViewport(&viewport);
	POINT ptCursor;
	GetCursorPos( &ptCursor );
	ScreenToClient( pWnd->GetSafeHwnd(), &ptCursor );

	D3DXVECTOR3 v;
	v.x =  ( ( ( 2.0f * ptCursor.x ) / viewport.Width  ) - 1 ) / matProj._11;
	v.y = -( ( ( 2.0f * ptCursor.y ) / viewport.Height ) - 1 ) / matProj._22;
	v.z =  1.0f;

	D3DXMATRIX matView;
	D3DXMATRIX matWorld;
	m_pD3dDevice9->GetTransform(D3DTS_VIEW, &matView);
	m_pD3dDevice9->GetTransform(D3DTS_WORLD, &matWorld);

	D3DXMATRIX mWorldView = matView * matWorld;
	D3DXMatrixInverse( &m, NULL, &mWorldView );

	vPickRayDir.x  = v.x*m._11 + v.y*m._21 + v.z*m._31;
	vPickRayDir.y  = v.x*m._12 + v.y*m._22 + v.z*m._32;
	vPickRayDir.z  = v.x*m._13 + v.y*m._23 + v.z*m._33;
	vPickRayOrig.x = m._41;
	vPickRayOrig.y = m._42;
	vPickRayOrig.z = m._43;

	BOOL bHit = FALSE;
	DWORD dwFace;
	FLOAT fBary1, fBary2, fDist;
	if (FAILED(D3DXIntersect(pMesh, &vPickRayOrig, &vPickRayDir, &bHit, &dwFace, &fBary1, &fBary2, &fDist, 
		NULL, NULL)))
              return E_FAIL;
        if (bHit == TRUE)
        {
               ......
        }
        return S_OK;
}

But it dosen't work correctly, anyone can tell me why? thanks.

Share this post


Link to post
Share on other sites
You need to provide is with more information.

What is the current output? It must be doing something otherwise it will return an error code, which you should be stepping through to look at. The SDK docs will have all the info on this.

Share this post


Link to post
Share on other sites
Thank you for your reply.
In my project, the Render() is called in every frame to render a mesh to a window. When the left button down, I test every mesh by calling the PickTest(), but I can't pick my mesh accurately. I am perplexed.

Share this post


Link to post
Share on other sites

This topic is 3627 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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