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

  1. //***************************************************************************************
  2. // MathHelper.cpp by Frank Luna (C) 2011 All Rights Reserved.
  3. //***************************************************************************************
  4.  
  5. #include "MathHelper.h"
  6. #include <float.h>
  7. #include <cmath>
  8.  
  9. using namespace DirectX;
  10.  
  11. const float MathHelper::Infinity = FLT_MAX;
  12. const float MathHelper::Pi       = 3.1415926535f;
  13.  
  14. float MathHelper::AngleFromXY(float x, float y)
  15. {
  16.     float theta = 0.0f;
  17.  
  18.     // Quadrant I or IV
  19.     if(x >= 0.0f) 
  20.     {
  21.         // If x = 0, then atanf(y/x) = +pi/2 if y > 0
  22.         //                atanf(y/x) = -pi/2 if y < 0
  23.         theta = atanf(y / x); // in [-pi/2, +pi/2]
  24.  
  25.         if(theta < 0.0f)
  26.             theta += 2.0f*Pi; // in [0, 2*pi).
  27.     }
  28.  
  29.     // Quadrant II or III
  30.     else      
  31.         theta = atanf(y/x) + Pi; // in [0, 2*pi).
  32.  
  33.     return theta;
  34. }
  35.  
  36. XMVECTOR MathHelper::RandUnitVec3()
  37. {
  38.     XMVECTOR One  = XMVectorSet(1.0f, 1.0f, 1.0f, 1.0f);
  39.     XMVECTOR Zero = XMVectorZero();
  40.  
  41.     // Keep trying until we get a point on/in the hemisphere.
  42.     while(true)
  43.     {
  44.         // Generate random point in the cube [-1,1]^3.
  45.         XMVECTOR v = XMVectorSet(MathHelper::RandF(-1.0f, 1.0f), MathHelper::RandF(-1.0f, 1.0f), MathHelper::RandF(-1.0f, 1.0f), 0.0f);
  46.  
  47.         // Ignore points outside the unit sphere in order to get an even distribution 
  48.         // over the unit sphere.  Otherwise points will clump more on the sphere near 
  49.         // the corners of the cube.
  50.  
  51.         if( XMVector3Greater( XMVector3LengthSq(v), One) )
  52.             continue;
  53.  
  54.         return XMVector3Normalize(v);
  55.     }
  56. }
  57.  
  58. XMVECTOR MathHelper::RandHemisphereUnitVec3(XMVECTOR n)
  59. {
  60.     XMVECTOR One  = XMVectorSet(1.0f, 1.0f, 1.0f, 1.0f);
  61.     XMVECTOR Zero = XMVectorZero();
  62.  
  63.     // Keep trying until we get a point on/in the hemisphere.
  64.     while(true)
  65.     {
  66.         // Generate random point in the cube [-1,1]^3.
  67.         XMVECTOR v = XMVectorSet(MathHelper::RandF(-1.0f, 1.0f), MathHelper::RandF(-1.0f, 1.0f), MathHelper::RandF(-1.0f, 1.0f), 0.0f);
  68.  
  69.         // Ignore points outside the unit sphere in order to get an even distribution 
  70.         // over the unit sphere.  Otherwise points will clump more on the sphere near 
  71.         // the corners of the cube.
  72.         
  73.         if( XMVector3Greater( XMVector3LengthSq(v), One) )
  74.             continue;
  75.  
  76.         // Ignore points in the bottom hemisphere.
  77.         if( XMVector3Less( XMVector3Dot(n, v), Zero ) )
  78.             continue;
  79.  
  80.         return XMVector3Normalize(v);
  81.     }
  82. }