所以我正在研究我的 Picking 功能。我已经阅读了几个教程并阅读了一些帖子,我已经到了这一点。但它仍然不起作用。
我的代码有什么问题?
BOOL Directx::Picking(HWND hWnd, AnimatedMesh *entity)
{
POINT pt;
D3DVIEWPORT9 vp;
D3DXMATRIX matWorld, matView, matProj;
GetCursorPos(&pt);
ScreenToClient(hWnd, &pt);
d3ddev->GetTransform(D3DTS_PROJECTION, &matProj);
d3ddev->GetViewport(&vp);
d3ddev->GetTransform(D3DTS_WORLD, &matWorld);
d3ddev->GetTransform(D3DTS_VIEW, &matView);
// Use inverse of matrix
D3DXVECTOR3 rayPos(pt.x, pt.y,0); // near-plane position
D3DXVECTOR3 rayDir(pt.x, pt.x,1); // far-plane position
D3DXVec3Unproject(&rayPos,&rayPos,&vp,&matProj,&matView,&matWorld);
D3DXVec3Unproject(&rayDir,&rayDir,&vp,&matProj,&matView,&matWorld);
rayDir -= rayPos; // make a direction from the 2 positions
D3DXVec3Normalize(&rayDir,&rayDir);
// Transform ray origin and direction by inv matrix
BOOL hasHit;
float distanceToCollision;
if(FAILED(D3DXIntersect(entity->pDrawMesh, &rayPos, &rayDir, &hasHit, NULL, NULL, NULL, &distanceToCollision, NULL, NULL)))
{
PostQuitMessage(0);
};
if(hasHit!=0)
PostQuitMessage(0);
else
{
s=rayPos;
}
return hasHit;
}