Thread: 2D in directX

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    62

    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. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    62
    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. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    62
    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. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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.
    Last edited by VirtualAce; 01-24-2009 at 02:41 PM.

  7. #7
    Registered User
    Join Date
    Aug 2006
    Posts
    62
    Thanks, it's working now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. text on 2d tiles in DirectX?
    By Quantum1024 in forum Game Programming
    Replies: 4
    Last Post: 04-26-2006, 11:40 PM
  2. 2D in DirectX 9
    By maxthecat in forum Game Programming
    Replies: 12
    Last Post: 01-08-2006, 09:17 PM
  3. OpenGL DirectX 2D
    By c++.prog.newbie in forum Game Programming
    Replies: 11
    Last Post: 03-01-2005, 01:54 PM
  4. 2D Game Project with DirectX and C++
    By VMJC in forum Projects and Job Recruitment
    Replies: 4
    Last Post: 12-23-2004, 12:28 AM
  5. DirectX 2D Frames Per Second
    By LuckY in forum Game Programming
    Replies: 9
    Last Post: 09-17-2004, 11:11 PM