0

I'm just writing some code to test the D3DXVec3Project function, trying to get it to align text to a 3d position.

3D to 2D code:

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

Testing code: D3DXVECTOR3 test; 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 );

The problem is that whatever values are placed in test.xyz provides the screen space coordinate for twice the amount.

i.e. for ToScreenSpace to properly align the text to 3D space 50,0,50 I need to give text the value 25,0,25. 90,10,70? 45,5,35 etc.

I am just wondering why this might be the case.

4

1 回答 1

0

The reason why you get twice the amount is that you translate the point in question with a world matrix that doubles its values. Just provide the identity matrix as the world matrix to the D3DXVec3Project function.

于 2012-08-01T15:49:17.353 回答