• Advertisement
Sign in to follow this  

Picking with Mouse using DirectX11 TriangleTests::Intersects

This topic is 423 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 am trying to upgrade my DirectX9 picking function to DirectX11.  Specifically, I am trying to replace the D3DXIntersectTri() function from DirectX9 with the TriangleTests::Intersects() function from DirectXMath.  The View matrix is equal to the identity matrix, so I left it out of these functions for clarity.

Here is the DirectX9 Version that has proven to work:

bool CButton::PickPoint()
{
    D3DXVECTOR3 v;
    POINT ptCursor;
    float distToCollision = FLT_MAX;
    float bary1 = 0.0, bary2 = 0.0;

    //Get screen coordinates of cursor point
    GetCursorPos(&ptCursor);
    ScreenToClient(pApp->GetWindow(), &ptCursor);

    //adjust screen coordinates according to projection matrix and (-1 to 1) range
    v.x = (((2.0f * ptCursor.x) / pApp->GetViewWidth()) - 1) / matProj._11;
    v.y = (((-2.0f * ptCursor.y) / pApp->GetViewHeight()) + 1) / matProj._22;
    v.z = 1.01f;

    D3DXVECTOR3 RayOrigin = D3DXVECTOR3(0.0, 0.0, 0.0);
    D3DXVECTOR3 RayDir = D3DXVECTOR3(v.x, v.y, v.z);

    D3DXVec3Normalize(&RayDir, &RayDir);


    D3DXVec3TransformCoord(&rendVertex[0].pos, &modelVertexPtr->pos, &matWorld);
    D3DXVec3TransformCoord(&rendVertex[1].pos, &(modelVertexPtr + 1)->pos, &matWorld);
    D3DXVec3TransformCoord(&rendVertex[2].pos, &(modelVertexPtr + 2)->pos, &matWorld);
    D3DXVec3TransformCoord(&rendVertex[3].pos, &(modelVertexPtr + 3)->pos, &matWorld);

    if (D3DXIntersectTri(&rendVertex[0].pos, &rendVertex[1].pos, &rendVertex[2].pos, &RayOrigin, &RayDir,                 &bary1, &bary2, &distToCollision) ||
    D3DXIntersectTri(&rendVertex[0].pos, &rendVertex[2].pos, &rendVertex[3].pos, &RayOrigin, &RayDir, &bary1,                 &bary2, &distToCollision))
    {
        Highlight = true;
        return true;
    }

    return false;


}// End PickPoint( )

Here is the DirectX11 version:

bool CButton::PickPoint()
{
    XMFLOAT3 v;
    POINT ptCursor;
    float distToCollision = FLT_MAX;
    float bary1 = 0.0, bary2 = 0.0;

    //Get screen coordinates of cursor point
    GetCursorPos(&ptCursor);
    ScreenToClient(pApp->GetWindow(), &ptCursor);

    //Bring screen coordinate into view space - equivalent to inverse projection
    //adjust screen coordinates according to projection matrix and (-1 to 1) range
    v.x = (((2.0f * ptCursor.x) / pApp->GetViewWidth()) - 1) / matProj._11;
    v.y = (((-2.0f * ptCursor.y) / pApp->GetViewHeight()) + 1) / matProj._22;
    v.z = 1.01f;

    
    XMFLOAT3 f_RayOrigin
= XMFLOAT3(0.0f, 0.0f, 0.0f);//PointINAABB method
    XMFLOAT3 f_RayOrigin = XMFLOAT3(v.x, v.y, v.z);
    
    XMVECTOR v_RayOrigin
, v_RayDir;
    v_RayOrigin = XMLoadFloat3(&f_RayOrigin);

    v_RayDir = XMLoadFloat3(&f_RayDir);
    
    
//Normalize RayDir
    v_RayDir = XMVector3Normalize(v_RayDir);

    //Transform model vertices into world space
    XMMATRIX worldmtx = XMLoadFloat4x4(&matWorld);//MVP
        
    XMVECTOR
TriPos0 = XMLoadFloat3(&modelVertexPtr->pos);
    TriPos0 = XMVector3TransformCoord(TriPos0, worldmtx);
    XMStoreFloat3(&rendVertex[0].pos, TriPos0);

    XMVECTOR TriPos1 = XMLoadFloat3(&(modelVertexPtr + 1)->pos);
    TriPos1 = XMVector3TransformCoord(TriPos1, worldmtx);
    XMStoreFloat3(&rendVertex[1].pos, TriPos1);

    XMVECTOR TriPos2 = XMLoadFloat3(&(modelVertexPtr + 2)->pos);
    TriPos2 = XMVector3TransformCoord(TriPos2, worldmtx);
    XMStoreFloat3(&rendVertex[2].pos, TriPos2);

    XMVECTOR TriPos3 = XMLoadFloat3(&(modelVertexPtr + 3)->pos);
    TriPos3 = XMVector3TransformCoord(TriPos3, worldmtx);
    XMStoreFloat3(&rendVertex[3].pos, TriPos3);

    if (TriangleTests::Intersects(v_RayOrigin, v_RayDir, TriPos0, TriPos1, TriPos2, distToCollision) ||
    TriangleTests::Intersects(v_RayOrigin, v_RayDir, TriPos0, TriPos2, TriPos3, distToCollision))
    {
        Highlight = true;
        return true;
    }

    return false;


}// End PickPoint( )

The DirectX11 version does not work.  Any Ideas why?  Any help is appreciated.

 

Derek

Share this post


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

  • Advertisement