简体   繁体   中英

Converting DirectX SDK code to new Windows 8.1 SDK Code

I'm currently creating a Video Game + Engine. I found some really amazing tutorials on DirectX 11 programming at RasterTek . They unfortunately use the depreciated DirectX SDK, and I am using VS 2013 with the new DirectX SDK included in the Windows SDK.

I am converting the code to use the Windows SDK, but I've run into some problems on tutorial 4 (yes I will be converting all 49 tutorials, and there will probably be more issues)

From Working with D3DXMath , I am told that D3DXVECTOR3 should be converted to XMFLOAT3. I am then trying to use these XMFLOAT3 in D3DXVec3TransformCoord, which has been converted to XMVector3TransformCoord .

Example:

XMFLOAT3 up, position, lookAt;

// Setup the vector that points upwards.
up.x = 0.0f
up.y = 1.0f
up.z = 0.0f

// Setup the position of the camera in the world.
position.x = m_positionX;
position.y = m_positionY;
position.z = m_positionZ;

// Setup where the camera is looking by default.
lookAt.x = 0.0f;
lookAt.y = 0.0f;
lookAt.z = 1.0f;

// Transform the lookAt and up vector by the rotation matrix so the view is correctly rotated at the origin.
XMVector3TransformCoord(lookAt, rotationMatrix);
XMVector3TransformCoord(up, rotationMatrix);

I get an error:

IntelliSense: no suitable user-defined conversion from "DirectX::XMFLOAT3" to "DirectX::XMVECTOR" exists

I have found that I can TypeCast lookAt and up using XMLoadFloat3 :

XMVector3TransformCoord(XMLoadFloat3(&lookAt), rotationMatrix);
XMVector3TransformCoord(XMLoadFloat3(&up), rotationMatrix);

Should I be doing this at all, and / or is it the best way to do this?

After doing this, I try to:

// Translate the rotated camera position to the location of the viewer.
lookAt = position + lookAt;

and get the error:

IntelliSense: no operator "+" matches these operands operand types are: DirectX::XMFLOAT3 + DirectX::XMFLOAT3

I was able to use the + operator on D3DXVECTOR3, but now not XMFLOAT3? How do I add these together?

Should I be doing this at all, and / or is it the best way to do this?

Use XMVECTOR instead of XMFLOAT3 since most of the functions in DriectXMath use XMVECTOR. thus you can avoid the boring type cast and make your code clean.

I was able to use the + operator on D3DXVECTOR3, but now not XMFLOAT3? How do I add these together?

Again, use XMVECTOR instead of XMFLOAT3

XMVECTOR also has some disadvantage(I mean the code seems a little complicated sometimes). with D3DXVECTOR3, you can simply initialize a variable as D3DXVECTOR3 a(x, y, z, w) and get it's component as ax, ay, ... but XMVECTOR use a different way.

To initialize an XMVECTOR, use XMVectorSet function.

XMVECTOR lookAt = XMVectorSet(x, y, z, w);

To get component of an XMVECTOR, use the following functions.

  • XMVectorGetX
  • XMVectorGetY
  • XMVectorGetZ
  • XMVectorGetW

1. Should I?

Should I be doing this at all, and / or is it the best way to do this?

Generally speaking, you use Windows SDK to write your new code, but you don't have to convert any old code (yours or, especially, others). It`s just a waste of time. You can install DirectX SDK to be able to build it.

On the other hand, it is a good opportunity to learn a little more about DrectX and its infrastructure. For example, portng from D3DXMath to DirectXMath (previously XMMath), you will learn pros and cons of SSE-based math libraries, and it is a good place to begin to write your own.

2. How to?

If you really decided to do this hard work, then 90% of info you will find in a good article by Chuck Walbourn - MSFT: Living without D3DX . Another 9.9% you will figure out by yourself from documentation , and 0.1% more from looking at DirectXMath source code (it is a header library, all functions inlined and you cann freely learn them). You will, probably, also interested in another articles by that guy on MSDN.

3. operator+ problem

Instead of single D3DXVECTORn type (where n is a number of components), new math contains two types: XMFLOATn and XMVECTOR . Same for matrices: XMMATRIX and XMFLOATaXb (where a and b is a dimensions of matrix).

  • XMFLOAT folks are to be used to just store values. It is just a plain floats. No operators defined for them and no functions, except XMLoad* / XMStore* accept them.
  • XMVECTOR and XMMATRIX is a work horses: they used in all functions and also have overloaded operators (which is not recommended to use. Use XM* functions instead). They use supersonic speed SSE internals. But.. it is a hard to use it to store values. For example, you will have alignment problems if you make XMVECTOR class member.
  • So, basically, you store and moving around your values in XMFLOAT s, but before any calculations, you transform them to XMVECTOR and XMMATRIX , using XMLoad* functions (as you've done with XMVector3TransformCoord() ), and after calculations, you transform them back to XMFLOAT s, using XMStore* functions. Or you can gain a little more profit, by aligning properly your class es and struct s, so you can store XMVECTOR and XMMATRIX directly (probably, you will need aligned memory allocator).

Well, now you are ready to go. Happy porting! =)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM