我的挑选正确吗?

发布于 2024-11-02 11:12:59 字数 1362 浏览 0 评论 0原文

所以我正在研究我的挑选功能。我已经阅读了一些教程并阅读了一些帖子,我已经做到了这一点。但还是不行。
我的代码有什么问题吗?

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;
}

So I'm working on my Picking function. I have read couple of tutorials and read some posts and I got to this point. But it still doesn't work.
What is wrong with my code?

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

↘人皮目录ツ 2024-11-09 11:12:59

好吧,试试这个:

BOOL Directx::Picking(HWND hWnd, AnimatedMesh *entity)
{
    POINT pt;
    D3DVIEWPORT9 vp;
    D3DXMATRIX matProj, matView;

    GetCursorPos(&pt);
    ScreenToClient(hWnd, &pt);

    float w = (float)backBufferWidth;
    float h = (float)backBufferHeight;

    d3ddev->GetTransform(D3DTS_VIEW, &matView);
    d3ddev->GetTransform(D3DTS_PROJECTION, &matProj);
    d3ddev->GetViewport(&vp);

    //Transform cursor position to view space
    float x = (2.0f*pt.x/w - 1.0f) / matProj(0,0);
    float y = (-2.0f*pt.y/h + 1.0f) / matProj(1,1);


    D3DXVECTOR3 rayOrigin(0.0f, 0.0f,0.0f); // near-plane position
    D3DXVECTOR3 rayDir(x, y, 1.0f); // far-plane position

    D3DXMATRIX matInvView;

    D3DXMatrixInverse(&matInvView, 0, &matView);

    D3DXVECTOR3 rayOriginW, rayDirW;

    // Transform picking ray to world space.
    D3DXVec3TransformCoord(&rayOriginW, &rayOrigin, &invView);
    D3DXVec3TransformNormal(&rayDirW, &rayDdir, &invView);
    D3DXVec3Normalize(&rayDirW, &rayDirW);

    BOOL hasHit;
    float distanceToCollision;

    if(FAILED(D3DXIntersect(entity->pDrawMesh, &rayOriginW, &rayDirW, &hasHit, NULL, NULL, NULL, &distanceToCollision, NULL, NULL)))
    {
        PostQuitMessage(0);
    };

    s=rayPos;

    return hasHit;
}

Well try this:

BOOL Directx::Picking(HWND hWnd, AnimatedMesh *entity)
{
    POINT pt;
    D3DVIEWPORT9 vp;
    D3DXMATRIX matProj, matView;

    GetCursorPos(&pt);
    ScreenToClient(hWnd, &pt);

    float w = (float)backBufferWidth;
    float h = (float)backBufferHeight;

    d3ddev->GetTransform(D3DTS_VIEW, &matView);
    d3ddev->GetTransform(D3DTS_PROJECTION, &matProj);
    d3ddev->GetViewport(&vp);

    //Transform cursor position to view space
    float x = (2.0f*pt.x/w - 1.0f) / matProj(0,0);
    float y = (-2.0f*pt.y/h + 1.0f) / matProj(1,1);


    D3DXVECTOR3 rayOrigin(0.0f, 0.0f,0.0f); // near-plane position
    D3DXVECTOR3 rayDir(x, y, 1.0f); // far-plane position

    D3DXMATRIX matInvView;

    D3DXMatrixInverse(&matInvView, 0, &matView);

    D3DXVECTOR3 rayOriginW, rayDirW;

    // Transform picking ray to world space.
    D3DXVec3TransformCoord(&rayOriginW, &rayOrigin, &invView);
    D3DXVec3TransformNormal(&rayDirW, &rayDdir, &invView);
    D3DXVec3Normalize(&rayDirW, &rayDirW);

    BOOL hasHit;
    float distanceToCollision;

    if(FAILED(D3DXIntersect(entity->pDrawMesh, &rayOriginW, &rayDirW, &hasHit, NULL, NULL, NULL, &distanceToCollision, NULL, NULL)))
    {
        PostQuitMessage(0);
    };

    s=rayPos;

    return hasHit;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文