1 /* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
2
3 #ifndef MATRIX44F_H
4 #define MATRIX44F_H
5
6 #include "System/float3.h"
7 #include "System/float4.h"
8
9 class CMatrix44f
10 {
11 public:
12 CR_DECLARE_STRUCT(CMatrix44f)
13
14 CMatrix44f();
15 CMatrix44f(const CMatrix44f& mat);
16
17 CMatrix44f(const float3& pos, const float3& x, const float3& y, const float3& z);
18 CMatrix44f(const float rotX, const float rotY, const float rotZ);
19 explicit CMatrix44f(const float3& pos);
20
21 // these return zero on success, non-zero otherwise
22 int IsOrthoNormal(float eps = 0.01f) const;
23 int IsIdentity(float eps = 0.01f) const;
24
25 CMatrix44f& LoadIdentity();
26
27 void SetUpVector(const float3& up);
28 CMatrix44f& RotateX(float rad);
29 CMatrix44f& RotateY(float rad);
30 CMatrix44f& RotateZ(float rad);
31 CMatrix44f& Rotate(float rad, const float3& axis); //! axis is assumed to be normalized
32 CMatrix44f& Translate(const float x, const float y, const float z);
Translate(const float3 & pos)33 CMatrix44f& Translate(const float3& pos) { return Translate(pos.x, pos.y, pos.z); }
34
35 CMatrix44f& Scale(const float3 scales);
36
37 void SetPos(const float3& pos);
GetPos()38 float3 GetPos() const { return float3(m[12], m[13], m[14]); }
GetX()39 float3 GetX() const { return float3(m[0], m[1], m[ 2]); }
GetY()40 float3 GetY() const { return float3(m[4], m[5], m[ 6]); }
GetZ()41 float3 GetZ() const { return float3(m[8], m[9], m[10]); }
42
43 inline void operator*= (const float a) {
44 for (size_t i=0; i < 16; ++i)
45 m[i] *= a;
46 }
47
48 CMatrix44f& Transpose();
49
50 /// general matrix inversion
51 bool InvertInPlace();
52 CMatrix44f Invert(bool* status = NULL) const;
53
54 /// affine matrix inversion
55 CMatrix44f& InvertAffineInPlace();
56 CMatrix44f InvertAffine() const;
57
58 /// vector multiply
59 float3 operator* (const float3& v) const;
Mul(const float3 & v)60 float3 Mul(const float3& v) const { return (*this) * v; }
61 float4 operator* (const float4& v) const;
62
63 /// matrix multiply
64 CMatrix44f operator* (const CMatrix44f& mat) const;
65 CMatrix44f& operator>>= (const CMatrix44f& mat);
66 CMatrix44f& operator<<= (const CMatrix44f& mat);
67 CMatrix44f& operator*= (const CMatrix44f& mat) { return (*this <<= mat); }
68
69 float& operator[](int a) { return m[a]; }
70 float operator[](int a) const { return m[a]; }
71
72 /// Allows implicit conversion to float* (for passing to gl functions)
73 operator const float* () const { return m; }
74 operator float* () { return m; }
75
76 public:
77 /// OpenGL ordered (ie. column-major)
78 union {
79 float m[16];
80 float md[4][4]; // WARNING: it still is column-major, means md[j][i]!!!
81 };
82 };
83
84
85 // Templates for simple 2D/3D matrixes that behave
86 // pretty much like statically allocated matrixes,
87 // but can also be casted to and used as pointers.
88 template<class T>
newmat2(int x,int y)89 T **newmat2(int x, int y) {
90 T *mat2 = new T[x*y], **mat = new T *[x];
91 for (int i = 0; i < x; ++i)
92 mat[i] = mat2 + i*y;
93 return mat;
94 }
95
96 template<class T>
newmat3(int x,int y,int z)97 T ***newmat3(int x, int y, int z) {
98 T *mat3=new T[x*y*z], **mat2=new T *[x*y], ***mat=new T **[x];
99 for (int i = 0; i < x; ++i) {
100 for(int j = 0; j < y; ++j)
101 mat2[i*y+j] = mat3 + (i*y+j)*z;
102 mat[i] = mat2 + i*y;
103 }
104 return mat;
105 }
106
107 template<class T>
delmat2(T ** mat)108 void delmat2(T** mat) {
109 delete [] *mat;
110 delete [] mat;
111 }
112
113 template<class T>
delmat3(T *** mat)114 void delmat3(T*** mat) {
115 delete [] **mat;
116 delete [] *mat;
117 delete [] mat;
118 }
119
120 #endif /* MATRIX44F_H */
121