我只是在写一些代码来测试D3DXVec3Project函数,试图让它将文本对齐到三维位置。
3D到2D代码:
D3DXVECTOR3 ToScreenSpace(D3DXVECTOR3 position)
{
D3DXVECTOR3 out;
D3DVIEWPORT9 view;
D3DXMATRIX matView;
D3DXMATRIX matWorld;
D3DXMATRIX matProj;
D3DXMATRIX worldPos;
D3DXMATRIX worldRotX;
D3DXMATRIX worldRotY;
D3DXMATRIX worldRotZ;
D3DXMATRIX worldScl;
graphicsDevice->GetViewport(&view); //view in debug is showing all the correct values.
graphicsDevice->GetTransform(D3DTS_VIEW, &matView); //view is set each frame from the camera
graphicsDevice->GetTransform(D3DTS_PROJECTION, &matProj); //this is set once in the scene manager
D3DXMatrixTranslation(&matWorld, position.x, position.y, position.z); //uses the given position for the world matrix
D3DXVec3Project(&out, &position, &view, &matProj, &matView, &matWorld);
return out;
}
测试代码:D3DXVECTOR3试验;test.x=90;test.y=0;test。z=70;test=ToScreenSpace(测试);
RECT rct;
rct.left = test.x;
rct.right=test.x + 650;
rct.top = test.y;
rct.bottom=rct.top + 20;
m_font->DrawText(NULL, L"Hello World", -1, &rct, 0, fontColor );
问题是,无论在test.xyz中放置什么值,都会提供两倍的屏幕空间坐标。
即,为了使ToScreenSpace将文本正确地与3D空间50,0,50对齐,我需要给文本值25,0,25。90,10,70?45,5,35等
我只是想知道为什么会出现这种情况。
获得两倍数量的原因是,您使用一个值翻倍的世界矩阵来转换有问题的点。只需将单位矩阵作为世界矩阵提供给D3DXVec3Project
函数。