Search code examples
directxdirect3ddirectxtk

DirectX bounding box drawing


The idea is to draw the bounding box as 2D corners on the screen after my DiretXTK model is displayed.

I got the bounding box corners form DirectXKT in a std::vector<DirectX::SimpleMath::Vector3>, so

auto mf = m_world * m_view * m_proj;
D3D11_VIEWPORT vp;
UINT j = 1;
ctx->RSGetViewports(&j,&vp);
for (auto& c : corners)
{
    DirectX::SimpleMath::Vector3 ft = DirectX::XMVector3Transform(c, mf);

    float scalex = vp.Width / 2;
    float scaley = vp.Height / 2;

    float screenx = ft.x * scalex;
    float screeny = ft.y * scaley;

}

These coordinates are invalid, something wrong I'm doing in the transforming the XYZ of each corner to the screen projection.

Thanks a lot for your help.


Solution

  • In the DirectX Tool Kit wiki, I have a DebugDraw helper which is specifically designed for drawing DirectXMath bounding volumes using DirectX Tool Kit's PrimitiveBatch.

    See this topic page.

    You may also want to take a look at the Collision sample.