Search code examples
windowsmatrixsdktranslationdirectx-9

DirectX 9 With No SDK Installed - How To Translate a D3DMATRIX?


I'm trying to follow a tutorial on DirectX, and the tutorial is working with DX9. I do not have the older SDK (June 2010) installed, and would prefer to keep it that way. All the blurb I can find says 'everything's now in the Windows SDK'.

I've got a basic app running, showing a triangle on the screen - by supplying pre-transformed vertices. Now I want to start doing the transforms from model to world space (and the other pipeline bits).

The problem is, the tutorial uses D3DXMATRIX, and that is deprecated in the new SDK so I don't have the header files for it.

The code calls SetTransform(D3DTS_WORLD, &matTranslate), which actually takes a D3DMATRIX. I can declare a D3DMATRIX, no problem. The problem is that the tutorial uses D3DMatrixTranslation(), which is also in the older SDK.

Irritatingly, whenever I search for "D3DMATRIX translation" it always comes up with D3DXMATRIX - I cannot for the life of me find anything for a D3DMATRIX.

I have scanned directxmath.h, the advised replacement, but that only deals with XMMATRIX (and for some reason Visual Studio says XMMatrixTranslation is undefined). I've also scanned d3d9.h where SetTransform() is defined, and d3d9types.h where D3DMatrix is defined... but I cannot find anything about translating, scaling, and rotating.

Are there functions - in the Windows SDK stuff for DirectX - that transform D3DMATRIX?

A link to a DX10 tutorial using XMMATRIX would be OK, if anyone has one - as long as it starts from the ground up - but since D3DMATRIX and SetTransform() exist in the Windows SDK, I'd prefer to find that 'missing link' and know how to manipulate D3DMATRIX.

Can anyone help?


Solution

  • The tutorial you are following is teaching you legacy Direct3D 9 using the even-when-Direct3D9-was-a-thing legacy fixed-function transform & lighting hardware pipeline. I strongly recommend you focus on HLSL programmable shading tutorials for Direct3D 11 or even Direct3D 9. All modern GPUs use programmable shader pipelines and NOT the fixed-function transform & lighting pipelines of the 90s.

    The D3DMATRIX struct is just a row-major array of 16 floats. The D3DXMATRIX class is derived from the D3DMATRIX struct to add some methods to it.

    When using DirectXMath you can just cast a XMFLOAT4X4 to it.

    using namespace DirectX;
    
    XMMATRIX mat = XMMatrixTranslation(x,y,z);
    
    XMFLOAT4X4 mem;
    XMStoreFloat4x4(&mem, mat);
    d3d9dev->SetTransform(D3DTS_WORLD, reinterpret_cast<const D3DMATRIX*>(&mem));
    

    For a full list of equivalences between legacy D3DXMath and DirectXMath, see Microsoft Learn: Working with D3DXMath.

    If you are working with DirectXMath for the first time, I strongly suggest (a) you read the Programmer's Guide and (b) consider using the SimpleMath C++ wrapper for DirectXMath.

    If sticking with Direct3D 9, don't use the legacy DirectX SDK at all and instead look at the NuGet package for D3DX9. For a full list of modern alternatives to D3DX, see this blog post.