Ramilol
Nov 22, 2010, 07:12 PM
So I'm working with my picking in directx but it didint work :/ I have no idea why.
Here is my code
BOOL D3dDevice::Picking(HWND hWnd, LPDIRECT3DDEVICE9 d3ddev, CXFileEntity *entity, int z)
{
POINT ptCursor;
GetCursorPos( &ptCursor );
ScreenToClient( hWnd, &ptCursor );
D3DXMATRIX matProj;
D3ddev->GetTransform( D3DTS_PROJECTION, &matProj );
// Compute the vector of the pick ray in screen space
D3DXVECTOR3 v;
V.x = ( ( ( 2.0f * ptCursor.x ) / 800 ) - 1 ) / matProj._11;
V.why = -( ( ( 2.0f * ptCursor.why ) / 600 ) - 1 ) / matProj._22;
V.z = 1.0f;
// Get the inverse view matrix
D3DXMATRIX m, matView;
D3ddev->GetTransform( D3DTS_VIEW, &matProj );
D3DXMatrixInverse( &m, NULL, &matView );
D3DXVECTOR3 vPickRayDir, vPickRayOrig;
// Transform the screen space pick ray into 3D space
VPickRayDir.x = v.x*m._11 + v.y*m._21 + v.z*m._31;
VPickRayDir.why = 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;
D3DXVec3Normalize(&vPickRayDir,&vPickRayDir);
VPickRayOrig.x = m._41;
VPickRayOrig.why = m._42;
VPickRayOrig.z = m._43;
// calc origin as intersection with near frustum
VPickRayOrig+=vPickRayDir*NEAR_Z;
D3DXVECTOR3 vNear,vDir;
D3DXMATRIX invMat, matCombined;
D3ddev->GetTransform( D3DTS_WORLD, &matCombined );
D3DXMatrixInverse(&invMat,NULL,&matCombined);
D3DXVec3TransformCoord(&vNear,&vPickRayOrig,&invMat);
D3DXVec3TransformNormal(&vDir,&vPickRayDir,&invMat);
// test for intersection
BOOL bHit;
Float dist;
D3DXIntersect(entity->pDrawMesh,&vNear,&vDir,&bHit,NULL,NULL,NULL,&dist,NULL,NULL);
If(bHit)
{
PostQuitMessage(0);
}
RayObjSpace=vNear;
Return bHit;
}
I tried that but it didn't work ;/ I also tried this I think this one is little closer but still doesn't work
BOOL D3dDevice::Picking(HWND hWnd, LPDIRECT3DDEVICE9 d3ddev, CXFileEntity *entity, int z)
{
D3DXVECTOR3 v;
D3DXMATRIX matProj;
POINT pt;
D3DVIEWPORT9 vp;
D3DXMATRIX matInverse, matWorld;
D3DXMATRIX m;
D3DXVECTOR3 rayOrigin,rayDir;
D3DXMATRIX matView;
D3DXVECTOR3 rayObjOrigin,rayObjDirection, rayDirection;
GetCursorPos(&pt);
ScreenToClient(hWnd, &pt);
D3ddev->GetTransform(D3DTS_PROJECTION, &matProj);
D3ddev->GetViewport(&vp);
D3ddev->GetTransform(D3DTS_VIEW, &matView);
// Use inverse of matrix
D3ddev->GetTransform(D3DTS_WORLD, &matWorld);
D3DXVECTOR3 vec3( pt.x, pt.why, 1.0f );
D3DXVec3Unproject( &rayObjSpace, &vec3, &vp, &matProj, &matView, &matWorld );
// Transform ray origin and direction by inv matrix
D3DXMATRIX invWorld;
D3DXMatrixInverse( &invWorld, NULL, &matWorld );
D3DXVECTOR3 camObjSpace;
CamPos.x=0.0;
CamPos.y=0.0;
CamPos.z=z;
D3DXVec3TransformCoord( &camObjSpace, &camPos, &invWorld );
RayDir = rayObjSpace - camObjSpace;
BOOL ha****;
Float distanceToCollision;
If(FAILED(D3DXIntersect(entity->pDrawMesh, &rayObjSpace, &rayDir, &ha****, NULL, NULL, NULL, &distanceToCollision, NULL, NULL)))
{
PostQuitMessage(0);
};``
If(ha****==1)
{
PostQuitMessage(0);
}
Return bHit;
}
But no luck at all any ideas
Here is my code
BOOL D3dDevice::Picking(HWND hWnd, LPDIRECT3DDEVICE9 d3ddev, CXFileEntity *entity, int z)
{
POINT ptCursor;
GetCursorPos( &ptCursor );
ScreenToClient( hWnd, &ptCursor );
D3DXMATRIX matProj;
D3ddev->GetTransform( D3DTS_PROJECTION, &matProj );
// Compute the vector of the pick ray in screen space
D3DXVECTOR3 v;
V.x = ( ( ( 2.0f * ptCursor.x ) / 800 ) - 1 ) / matProj._11;
V.why = -( ( ( 2.0f * ptCursor.why ) / 600 ) - 1 ) / matProj._22;
V.z = 1.0f;
// Get the inverse view matrix
D3DXMATRIX m, matView;
D3ddev->GetTransform( D3DTS_VIEW, &matProj );
D3DXMatrixInverse( &m, NULL, &matView );
D3DXVECTOR3 vPickRayDir, vPickRayOrig;
// Transform the screen space pick ray into 3D space
VPickRayDir.x = v.x*m._11 + v.y*m._21 + v.z*m._31;
VPickRayDir.why = 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;
D3DXVec3Normalize(&vPickRayDir,&vPickRayDir);
VPickRayOrig.x = m._41;
VPickRayOrig.why = m._42;
VPickRayOrig.z = m._43;
// calc origin as intersection with near frustum
VPickRayOrig+=vPickRayDir*NEAR_Z;
D3DXVECTOR3 vNear,vDir;
D3DXMATRIX invMat, matCombined;
D3ddev->GetTransform( D3DTS_WORLD, &matCombined );
D3DXMatrixInverse(&invMat,NULL,&matCombined);
D3DXVec3TransformCoord(&vNear,&vPickRayOrig,&invMat);
D3DXVec3TransformNormal(&vDir,&vPickRayDir,&invMat);
// test for intersection
BOOL bHit;
Float dist;
D3DXIntersect(entity->pDrawMesh,&vNear,&vDir,&bHit,NULL,NULL,NULL,&dist,NULL,NULL);
If(bHit)
{
PostQuitMessage(0);
}
RayObjSpace=vNear;
Return bHit;
}
I tried that but it didn't work ;/ I also tried this I think this one is little closer but still doesn't work
BOOL D3dDevice::Picking(HWND hWnd, LPDIRECT3DDEVICE9 d3ddev, CXFileEntity *entity, int z)
{
D3DXVECTOR3 v;
D3DXMATRIX matProj;
POINT pt;
D3DVIEWPORT9 vp;
D3DXMATRIX matInverse, matWorld;
D3DXMATRIX m;
D3DXVECTOR3 rayOrigin,rayDir;
D3DXMATRIX matView;
D3DXVECTOR3 rayObjOrigin,rayObjDirection, rayDirection;
GetCursorPos(&pt);
ScreenToClient(hWnd, &pt);
D3ddev->GetTransform(D3DTS_PROJECTION, &matProj);
D3ddev->GetViewport(&vp);
D3ddev->GetTransform(D3DTS_VIEW, &matView);
// Use inverse of matrix
D3ddev->GetTransform(D3DTS_WORLD, &matWorld);
D3DXVECTOR3 vec3( pt.x, pt.why, 1.0f );
D3DXVec3Unproject( &rayObjSpace, &vec3, &vp, &matProj, &matView, &matWorld );
// Transform ray origin and direction by inv matrix
D3DXMATRIX invWorld;
D3DXMatrixInverse( &invWorld, NULL, &matWorld );
D3DXVECTOR3 camObjSpace;
CamPos.x=0.0;
CamPos.y=0.0;
CamPos.z=z;
D3DXVec3TransformCoord( &camObjSpace, &camPos, &invWorld );
RayDir = rayObjSpace - camObjSpace;
BOOL ha****;
Float distanceToCollision;
If(FAILED(D3DXIntersect(entity->pDrawMesh, &rayObjSpace, &rayDir, &ha****, NULL, NULL, NULL, &distanceToCollision, NULL, NULL)))
{
PostQuitMessage(0);
};``
If(ha****==1)
{
PostQuitMessage(0);
}
Return bHit;
}
But no luck at all any ideas





