Mat4.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #pragma once
  2. #include "Vec3.h"
  3. namespace Framework
  4. {
  5. template< typename T >
  6. class Mat4
  7. {
  8. public:
  9. // [ Zeile ][ Spalte ] ( [ y ][ x ] )
  10. T elements[ 4 ][ 4 ];
  11. // nicht constant
  12. Mat4 &operator=( const Mat4 &r )
  13. {
  14. memcpy( elements, r.elements, sizeof( elements ) );
  15. return *this;
  16. }
  17. Mat4 &operator*=( const T r )
  18. {
  19. for( T &e : elements )
  20. r *= r;
  21. return *this;
  22. }
  23. Mat4 &operator*=( const Mat4 &r )
  24. {
  25. return *this = *this * r;
  26. }
  27. // constant
  28. Mat4 operator*( const T r ) const
  29. {
  30. Mat4 result = *this;
  31. return result *= r;
  32. }
  33. Mat4 operator*( const Mat4 &r ) const
  34. {
  35. Mat4 result;
  36. for( int j = 0; j < 4; j++ )
  37. {
  38. for( int k = 0; k < 4; k++ )
  39. {
  40. T sum = 0;
  41. for( int i = 0; i < 4; i++ )
  42. sum += elements[ j ][ i ] * r.elements[ i ][ k ];
  43. result.elements[ j ][ k ] = sum;
  44. }
  45. }
  46. return result;
  47. }
  48. Vec3< T > operator*( const Vec3< T > &r ) const
  49. {
  50. Vec3< T > result;
  51. result.x = elements[ 0 ][ 0 ] * r.x + elements[ 0 ][ 1 ] * r.y + elements[ 0 ][ 2 ] * r.z + elements[ 0 ][ 3 ];
  52. result.y = elements[ 1 ][ 0 ] * r.x + elements[ 1 ][ 1 ] * r.y + elements[ 1 ][ 2 ] * r.z + elements[ 1 ][ 3 ];
  53. result.z = elements[ 2 ][ 0 ] * r.x + elements[ 2 ][ 1 ] * r.y + elements[ 2 ][ 2 ] * r.z + elements[ 2 ][ 3 ];
  54. return result;
  55. }
  56. // static
  57. static Mat4 rotationZ( T radian )
  58. {
  59. const T cosTheta = (T)cos( radian );
  60. const T sinTheta = (T)sin( radian );
  61. Mat4 r = { cosTheta, -sinTheta, 0, 0, sinTheta, cosTheta, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 };
  62. return r;
  63. }
  64. static Mat4 rotationX( T radian )
  65. {
  66. const T cosTheta = (T)cos( radian );
  67. const T sinTheta = (T)sin( radian );
  68. Mat4 r = { 1, 0, 0, 0, 0, cosTheta, -sinTheta, 0, 0, sinTheta, cosTheta, 0, 0, 0, 0, 1 };
  69. return r;
  70. }
  71. static Mat4 rotationY( T radian )
  72. {
  73. const T cosTheta = (T)cos( radian );
  74. const T sinTheta = (T)sin( radian );
  75. Mat4 r = { cosTheta, 0, sinTheta, 0, 0, 1, 0, 0, -sinTheta, 0, cosTheta, 0, 0, 0, 0, 1 };
  76. return r;
  77. }
  78. static Mat4 scaling( T faktor )
  79. {
  80. Mat4 s = { faktor, 0, 0, 0, 0, faktor, 0, 0, 0, 0, faktor, 0, 0, 0, 1 };
  81. return s;
  82. }
  83. static Mat4 scaling( T faktorX, T faktorY, T faktorZ )
  84. {
  85. Mat4 s = { faktorX, 0, 0, 0, 0, faktorY, 0, 0, 0, 0, faktorZ, 0, 0, 0, 1 };
  86. return s;
  87. }
  88. static Mat4 translation( const Vec3< T > offset )
  89. {
  90. Mat4 t = { 1, 0, 0, offset.x, 0, 1, 0, offset.y, 0, 0, 1, offset.z, 0, 0, 0, 1 };
  91. return t;
  92. }
  93. static Mat4 projektion( float öffnungswinkel, float bildschirmXY, float minZ, float maxZ )
  94. {
  95. Mat4 p = { (float)( 1 / tan( öffnungswinkel / 2 ) ) / bildschirmXY, 0, 0, 0,
  96. 0, (float)( 1 / tan( öffnungswinkel / 2 ) ), 0, 0,
  97. 0, 0, maxZ / ( maxZ - minZ ), -( minZ * maxZ ) / ( maxZ - minZ ),
  98. 0, 0, 1, 0 };
  99. return p;
  100. }
  101. static Mat4 identity()
  102. {
  103. Mat4 i = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 };
  104. return i;
  105. }
  106. };
  107. }