2
\$\begingroup\$

I'm working in C++ with DirectX 9 on a 3D game, but I want a 2D overlay for rendering a HUD.

I've heard that I could use D3DXMatrixOrthoLH, but I'm very confused on how to use it. I'm more familiar with OpenGL, where I could simply switch between 3D and 2D by initiating the view.

How can I do this in DirectX 9?

EDIT:

I was able to achieve this like (partial psudocode):

But I have a good feeling this is not the right way to do it.

void render()
{
    beginscene();

    D3DXMATRIX matrix;
    D3DXMatrixIdentity(&matrix);

    D3DXMatrixTranslation(&matrix, 0, 0, 0);
    device->SetTransform(D3DTS_WORLD, &matrix);

    // Render 3D.

    D3DXMatrixIdentity(&matrix);

    device->SetTransform(D3DTS_WORLD, &matrix);
    device->SetTransform(D3DTS_VIEW, &matrix);

    D3DXMatrixOrthoOffCenterRH();
    device->SetTransform(D3DTS_PROJECTION, &matrix);

    // Render 2D.

    endscene();
}
\$\endgroup\$
  • \$\begingroup\$ Thanks for all the help guys. I've decided to scratch DirectX and go back to good old OpenGL, which I'm much more familiar with. \$\endgroup\$ – oliveryas01 Nov 1 '15 at 12:21
1
\$\begingroup\$

The simplest way to achieve this, is probably the ID3DXSprite interface.

The ID3DXSprite interface provides a set of methods that simplify the process of drawing sprites using Microsoft Direct3D.

The ID3DXSprite interface is obtained by calling the D3DXCreateSprite(...) function

This call requires a valid pointer to an IDirect3DDevice9 object.

To use this interface, simply call Draw for each texture you want to be rendered, between a call to Begin and End. Since Begin will alter the device state, you should make sure your other rendering happens before Begin, or after End, which restores the previous state.

To rotate or scale a texture, you can create a matrix and pass it to SetMatrix before your Draw call.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.