Jump to content
  • Advertisement
Sign in to follow this  

Help! I got some problems in picking mesh.

This topic is 3930 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;
	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 );
		m_pD3dDevice9->SetTexture( 0, m_pMeshTextures );
		m_pMesh->DrawSubset( i );

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

	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;

	DWORD dwFace;
	FLOAT fBary1, fBary2, fDist;
	if (FAILED(D3DXIntersect(pMesh, &vPickRayOrig, &vPickRayDir, &bHit, &dwFace, &fBary1, &fBary2, &fDist, 
              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
What do you mean by "I can't pick my mesh accurately"? What exactly do you expect to happen and what exactly happens?

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!