if(MouseState.rgbButtons[DIMOUSE_LEFTBUTTON] & 0x80){
POINT ptCursor;
GetCursorPos( &ptCursor );
ScreenToClient( han_Window, &ptCursor );
Matrix projMat;
p_Device->GetTransform(D3DTS_PROJECTION,&projMat);
D3DVIEWPORT9 view;
p_Device->GetViewport( &view );
Vector v;
v.x = ( ( ( 2.0f * ptCursor.x ) / view.Width ) - 1 ) / projMat._11;
v.y = -( ( ( 2.0f * ptCursor.y ) / view.Height ) - 1 ) / projMat._22;
v.z = 1.0f;
Matrix viewMat;
D3DXMatrixInverse( &viewMat, NULL, &projMat);
Vector rayOrigin,rayDir;
rayDir.x = v.x*viewMat._11 + v.y*viewMat._21 + v.z*viewMat._31;
rayDir.y = v.x*viewMat._12 + v.y*viewMat._22 + v.z*viewMat._32;
rayDir.z = v.x*viewMat._13 + v.y*viewMat._23 + v.z*viewMat._33;
rayOrigin.x = viewMat._41;
rayOrigin.y = viewMat._42;
rayOrigin.z = viewMat._43;
LPDIRECT3DVERTEXBUFFER9 pVB;
LPDIRECT3DINDEXBUFFER9 pIB;
terrain->Mesh->GetVertexBuffer( &pVB );
terrain->Mesh->GetIndexBuffer( &pIB );
WORD* pIndices;
D3DVERTEX* pVertices;
pIB->Lock( 0, 0, ( void** )&pIndices, 0 );
pVB->Lock( 0, 0, ( void** )&pVertices, 0 );
DWORD dwFace;
FLOAT fBary1, fBary2, fDist;
D3DXIntersect( terrain->Mesh, &rayOrigin, &rayDir, &picked, &dwFace, &fBary1, &fBary2, &fDist, NULL, NULL );
}
C++, DirectX9, mouse position to ray?
I think your viewMat should be the inverse of the view matrix, not the projection matrix.
As an aside, you lock the vertex and index buffers before you call D3DXIntersect. Do you unlock them at some point? That probably doesn't affect the pick but may not be good practice.
As an aside, you lock the vertex and index buffers before you call D3DXIntersect. Do you unlock them at some point? That probably doesn't affect the pick but may not be good practice.
Thanks for reply. I'm not sure what Locking does, it was pretty much copy/paste from tutorial. Other person gave me his code, and it worked flawlessly. Maybe you, or anyone else have idea how to find position of intersection in world? It returns number of face on the mesh, where intersection happened, and U V points, not sure what are those.
It is the inverse of the view matrix you need but the main area where people fall down with picking is to not transform the ray into the model space of the model being tested against. I have not read all your code but that may be where you are going wrong. There are some notes on this here: <a href="http://www.toymaker.info/Games/html/picking.html",Picking</a> although the site seems to be a bit slow at the moment.
There are builtin functions in DirectX/XNA for that purpose, use them :)
Here:
Device->(Project / Unproject).
Here:
Device->(Project / Unproject).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement