Hi Everybody!
While migrating my XNA games to DirectX 11, I stumbled into a problem trying to draw a texture on the screen in the location where a 3D model is supposed to be standing. In XNA, the answer is simple:
Graphics.GraphicsDevice.Viewport.Project()
I spent days trying to find the equivalent in DirectX 11, and although there is plenty of documentation about viewports available within the DeviceContext class, I couldn't find an equivalent method. Then, it hit me: The answer
is back to the basics, back to math. The solution is rather simple, and even elegant:
XMVECTOR vecPixel = XMVectorSet(0, 0, 0, 0);
XMFLOAT3 vecThis = XMFLOAT3(0, 0, 0);
vecPixel = vecAPositionIn3DSpace;
vecPixel = XMVector4Transform(vecPixel, mtxWorldRotation);
vecPixel = XMVector4Transform(vecPixel, mtxView);
vecPixel = XMVector4Transform(vecPixel, mtxProjection);
XMStoreFloat3(&vecThis, vecPixel);
if (0 < vecThis.z)// *** <- No Div0!!! *** //
{
lngDisplayPosX = (vecThis.x / vecThis.z) * (lngDisplayWidth / 2);
lngDisplayPosY = -(vecThis.y / vecThis.z) * (lngDisplayHeight / 2);
...
It works like a charm. Nothing like home-made code ;)
I hope it helps!!!
Tarh Ik
PS: This posting has been posted "AS IS"
Tarh ik