Convert DirectX SDK code to new Windows 8.1 SDK code

I am currently creating a Video Game + Engine. I found some really awesome DirectX 11 programming guides in RasterTek . Unfortunately, they use the discounted DirectX SDK, and I use VS 2013 with the new DirectX SDK included in the Windows SDK.

I am converting code to use the Windows SDK, but I ran into some problems in tutorial 4 (yes, I will convert all 49 lessons, and there will probably be more problems)

From Working with D3DXMath , I was told that D3DXVECTOR3 should be converted to XMFLOAT3. Then I try to use these XMFLOAT3 in D3DXVec3TransformCoord, which was 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 message:

IntelliSense: There is no suitable user conversion from "DirectX :: XMFLOAT3" to "DirectX :: XMVECTOR" exists

I found that I can use TypeCast lookAt and use XMLoadFloat3 :

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

Should I do this at all and / or is this the best way to do this?

After that I try:

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

and get the error:

IntelliSense: the + operator does not match these operands Operand types: DirectX :: XMFLOAT3 + DirectX :: XMFLOAT3

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

+4
source share
2 answers

Should I do this at all and / or is this the best way to do this?

Use XMVECTOR instead of XMFLOAT3, since most functions in DriectXMath use XMVECTOR. this way you can avoid drilling type resetting and make your code clean.

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

Use XMVECTOR again instead of XMFLOAT3

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

To initialize an XMVECTOR, use the XMVectorSet function.

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

To get the XMVECTOR component, use the following functions.

  • XMVectorGetX
  • XMVectorGetY
  • XMVectorGetZ
  • XMVectorGetW
+2
source

1. Should I?

Should I do this at all and / or is this the best way to do this?

Generally speaking, you use the Windows SDK to write new code, but you do not need to convert any old code (yours, or especially others). It is just a waste of time. You can install the DirectX SDK to be able to create it.

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

2. How?

If you really decide to do this hard work, then 90% of the information you will find in a good article by Chuck Walborn - MSFT: Life without D3DX . Another 9.9% you yourself will learn from the documentation and 0.1% more from searching for the source code of DirectXMath (this is a library of headers, all functions and you cannot study them freely). You are probably also interested in other articles on this guy on MSDN.

3. operator+ problem

Instead of the single type D3DXVECTORn (where n is the number of components), the new math contains two types: XMFLOATn and XMVECTOR . The same goes for matrices: XMMATRIX and XMFLOATaXb (where a and b are matrix sizes).

  • XMFLOAT people should just store values. It just floats. No operators are defined for them and there are no functions except XMLoad* / XMStore* to accept them.
  • XMVECTOR and XMMATRIX are workhorses: they are used in all functions and also have overloaded operators (which are not recommended for use. Use XM* functions instead). They use internal SSE supersonic speeds. But ... it's hard to use to store values. For example, you will have alignment problems if you make a member of the XMVECTOR class.
  • So basically, you save and move your values ​​to XMFLOAT s, but before any calculations, you convert them to XMVECTOR and XMMATRIX using XMLoad* functions (as you did with XMVector3TransformCoord() ), and after the calculations you convert them back to XMFLOAT s using the XMStore* functions. Or you can make a little more profit by combining your class es and struct s correctly, so you can directly store XMVECTOR and XMMATRIX (you might need a aligned memory allocator).

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

+2
source

Source: https://habr.com/ru/post/1501364/


All Articles