# Thread: 2D in directX

1. ## 2D in directX

Hello!
I use both 3D and 2D in my DirectX program. I use the rhw method for 2D. My question is probably surprising: How can I draw 2D in a given location on the screen? In 3D I use matrix transform. Do I have to do something similar in 2D? I load the 2D data and when I render it, I can only render it in the upper left corner as it is (0;0) point. What do do to render it to other places?

2. Any translation done with vertices using D3DFVF_XYZRHW must be manually transformed. RHW effectively turns the pipeline off. SetTransform() will have no effect on RHW vertices.

For 2D you will want to translate your object to 0.0f,0.0f or the upper left corner of the screen. You can then rotate and scale. Now re-translate your object back to it's world position + any offset to the position to arrive at it's new position.

3. I'm not sure if I understood correctly what you meant by re-translate into world's position. The problem is these 2D details don't actually have a world position as they are menus, their details etc. That's why I use 2D rhw.

I didn't realize it's so difficult to change their position. Is there some better method for it?

4. If this is just a menu then it's simpler than that. You won't need rotation or scaling or any transforms. If you want to create menus in screen space and do not care about ever changing their position then I would just bake the screen coords right into the vertices that make up the menu boxes.

Any vertices using RHW are expressed in screen coords.

5. I really don't need to rotate or scale, but I need to use some detail at multiple places on the screen, so I still need to be able to move them.

6. If you need to move them you will need their original vertices and their 'working' vertices.

Code:
```void Menu::TransformMenu(const D3DXVECTOR3 &pos,float rotation,float scale)
{
// Translate to origin
D3DXMATRIX matToOrigin;
D3DXMatrixTranslation(&matToOrigin,-m_vecMenuPos.x,-m_vecMenuPos.y,1.0f);

// Translation
D3DXMATRIX matTrans;
D3DXMatrixTranslation(&matTrans,&pos.x,&pos.y,1,0f);

// Rotation - z rotation for 2D
D3DXMATRIX matRot;
D3DXMaxtrixRotationZ(&matRot,rotation);

// Scaling matrix
D3DXMATRIX matScale;
D3DXMatrixScaling(&matScale,scale,scale,1.0f);

// Final world matrix - local to world transform
D3DXMATRIX matWorld = matToOrigin * matRot * matScale * matTrans;

// Transform vertices by matWorld
for (unsigned int i = 0;i < 4; ++i)
{
D3DXVec3TransformCoord(&m_workingVerts[i],&m_originalVerts[i],&matWorld);
}

m_vecMenuPos = pos;
}```
I included scaling and rotation just in case you might need them later. Even though pos.z and m_vecMenuPos.z are not used I chose D3DXVECTOR3 to make it simpler.

You could also use D3DXVec3TransformCoordArray() instead of doing the manual for loop.

At creation time you will need to do this:

Code:
`memcpy(&m_workingVerts,&m_originalVerts,4 * sizeof(ScreenVertex));`
Where screen vertex is your vertex structure.

7. Thanks, it's working now.

Popular pages Recent additions