home *** CD-ROM | disk | FTP | other *** search
/ Introduction to 3D Game …ogramming with DirectX 12 / Introduction-to-3D-Game-Programming-with-DirectX-12.ISO / Code.Textures / Chapter 1 Vector Algebra / XMVECTOR / xmvec3.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2016-03-02  |  2.8 KB  |  91 lines

  1.  
  2. #include <windows.h> // for XMVerifyCPUSupport
  3. #include <DirectXMath.h>
  4. #include <DirectXPackedVector.h>
  5. #include <iostream>
  6. using namespace std;
  7. using namespace DirectX;
  8. using namespace DirectX::PackedVector;
  9.  
  10. // Overload the  "<<" operators so that we can use cout to 
  11. // output XMVECTOR objects.
  12. ostream& XM_CALLCONV operator << (ostream& os, FXMVECTOR v)
  13. {
  14.     XMFLOAT3 dest;
  15.     XMStoreFloat3(&dest, v);
  16.  
  17.     os << "(" << dest.x << ", " << dest.y << ", " << dest.z << ")";
  18.     return os;
  19. }
  20.  
  21. int main()
  22. {
  23.     cout.setf(ios_base::boolalpha);
  24.  
  25.     // Check support for SSE2 (Pentium4, AMD K8, and above).
  26.     if (!XMVerifyCPUSupport())
  27.     {
  28.         cout << "directx math not supported" << endl;
  29.         return 0;
  30.     }
  31.  
  32.     XMVECTOR n = XMVectorSet(1.0f, 0.0f, 0.0f, 0.0f);
  33.     XMVECTOR u = XMVectorSet(1.0f, 2.0f, 3.0f, 0.0f);
  34.     XMVECTOR v = XMVectorSet(-2.0f, 1.0f, -3.0f, 0.0f);
  35.     XMVECTOR w = XMVectorSet(0.707f, 0.707f, 0.0f, 0.0f);
  36.  
  37.     // Vector addition: XMVECTOR operator + 
  38.     XMVECTOR a = u + v;
  39.  
  40.     // Vector subtraction: XMVECTOR operator - 
  41.     XMVECTOR b = u - v;
  42.  
  43.     // Scalar multiplication: XMVECTOR operator * 
  44.     XMVECTOR c = 10.0f*u;
  45.  
  46.     // ||u||
  47.     XMVECTOR L = XMVector3Length(u);
  48.  
  49.     // d = u / ||u||
  50.     XMVECTOR d = XMVector3Normalize(u);
  51.  
  52.     // s = u dot v
  53.     XMVECTOR s = XMVector3Dot(u, v);
  54.  
  55.     // e = u x v
  56.     XMVECTOR e = XMVector3Cross(u, v);
  57.  
  58.     // Find proj_n(w) and perp_n(w)
  59.     XMVECTOR projW;
  60.     XMVECTOR perpW;
  61.     XMVector3ComponentsFromNormal(&projW, &perpW, w, n);
  62.  
  63.     // Does projW + perpW == w?
  64.     bool equal = XMVector3Equal(projW + perpW, w) != 0;
  65.     bool notEqual = XMVector3NotEqual(projW + perpW, w) != 0;
  66.  
  67.     // The angle between projW and perpW should be 90 degrees.
  68.     XMVECTOR angleVec = XMVector3AngleBetweenVectors(projW, perpW);
  69.     float angleRadians = XMVectorGetX(angleVec);
  70.     float angleDegrees = XMConvertToDegrees(angleRadians);
  71.  
  72.     cout << "u                   = " << u << endl;
  73.     cout << "v                   = " << v << endl;
  74.     cout << "w                   = " << w << endl;
  75.     cout << "n                   = " << n << endl;
  76.     cout << "a = u + v           = " << a << endl;
  77.     cout << "b = u - v           = " << b << endl;
  78.     cout << "c = 10 * u          = " << c << endl;
  79.     cout << "d = u / ||u||       = " << d << endl;
  80.     cout << "e = u x v           = " << e << endl;
  81.     cout << "L  = ||u||          = " << L << endl;
  82.     cout << "s = u.v             = " << s << endl;
  83.     cout << "projW               = " << projW << endl;
  84.     cout << "perpW               = " << perpW << endl;
  85.     cout << "projW + perpW == w  = " << equal << endl;
  86.     cout << "projW + perpW != w  = " << notEqual << endl;
  87.     cout << "angle               = " << angleDegrees << endl;
  88.  
  89.     return 0;
  90. }
  91.