2012-07-30 71 views
0

我只是写一些代码来测试D3DXVec3Project函数,试图让它将文本对齐到一个3d位置。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(test);

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等

我只是想知道为什么这可能是这种情况。

回答

0

之所以你得到两倍的金额,是因为你用一个使其价值翻了一番的世界矩阵来翻译这个问题。只需提供身份矩阵作为D3DXVec3Project函数的世界矩阵。

+0

谢谢,我认为奇怪的是,我必须通过一个位置和一个世界矩阵,猜测这解释了为什么>< 再次感谢。 – user1539405 2012-08-02 10:18:03