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 / Common / MathHelper.h < prev    next >
Encoding:
C/C++ Source or Header  |  2016-03-02  |  2.4 KB  |  101 lines

  1. //***************************************************************************************
  2. // MathHelper.h by Frank Luna (C) 2011 All Rights Reserved.
  3. //
  4. // Helper math class.
  5. //***************************************************************************************
  6.  
  7. #pragma once
  8.  
  9. #include <Windows.h>
  10. #include <DirectXMath.h>
  11. #include <cstdint>
  12.  
  13. class MathHelper
  14. {
  15. public:
  16.     // Returns random float in [0, 1).
  17.     static float RandF()
  18.     {
  19.         return (float)(rand()) / (float)RAND_MAX;
  20.     }
  21.  
  22.     // Returns random float in [a, b).
  23.     static float RandF(float a, float b)
  24.     {
  25.         return a + RandF()*(b-a);
  26.     }
  27.  
  28.     static int Rand(int a, int b)
  29.     {
  30.         return a + rand() % ((b - a) + 1);
  31.     }
  32.  
  33.     template<typename T>
  34.     static T Min(const T& a, const T& b)
  35.     {
  36.         return a < b ? a : b;
  37.     }
  38.  
  39.     template<typename T>
  40.     static T Max(const T& a, const T& b)
  41.     {
  42.         return a > b ? a : b;
  43.     }
  44.      
  45.     template<typename T>
  46.     static T Lerp(const T& a, const T& b, float t)
  47.     {
  48.         return a + (b-a)*t;
  49.     }
  50.  
  51.     template<typename T>
  52.     static T Clamp(const T& x, const T& low, const T& high)
  53.     {
  54.         return x < low ? low : (x > high ? high : x); 
  55.     }
  56.  
  57.     // Returns the polar angle of the point (x,y) in [0, 2*PI).
  58.     static float AngleFromXY(float x, float y);
  59.  
  60.     static DirectX::XMVECTOR SphericalToCartesian(float radius, float theta, float phi)
  61.     {
  62.         return DirectX::XMVectorSet(
  63.             radius*sinf(phi)*cosf(theta),
  64.             radius*cosf(phi),
  65.             radius*sinf(phi)*sinf(theta),
  66.             1.0f);
  67.     }
  68.  
  69.     static DirectX::XMMATRIX InverseTranspose(DirectX::CXMMATRIX M)
  70.     {
  71.         // Inverse-transpose is just applied to normals.  So zero out 
  72.         // translation row so that it doesn't get into our inverse-transpose
  73.         // calculation--we don't want the inverse-transpose of the translation.
  74.         DirectX::XMMATRIX A = M;
  75.         A.r[3] = DirectX::XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f);
  76.  
  77.         DirectX::XMVECTOR det = DirectX::XMMatrixDeterminant(A);
  78.         return DirectX::XMMatrixTranspose(DirectX::XMMatrixInverse(&det, A));
  79.     }
  80.  
  81.     static DirectX::XMFLOAT4X4 Identity4x4()
  82.     {
  83.         static DirectX::XMFLOAT4X4 I(
  84.             1.0f, 0.0f, 0.0f, 0.0f,
  85.             0.0f, 1.0f, 0.0f, 0.0f,
  86.             0.0f, 0.0f, 1.0f, 0.0f,
  87.             0.0f, 0.0f, 0.0f, 1.0f);
  88.  
  89.         return I;
  90.     }
  91.  
  92.     static DirectX::XMVECTOR RandUnitVec3();
  93.     static DirectX::XMVECTOR RandHemisphereUnitVec3(DirectX::XMVECTOR n);
  94.  
  95.     static const float Infinity;
  96.     static const float Pi;
  97.  
  98.  
  99. };
  100.  
  101.