OpenClonk
StdMeshMath.h
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
5  * Copyright (c) 2011-2016, The OpenClonk Team and contributors
6  *
7  * Distributed under the terms of the ISC license; see accompanying file
8  * "COPYING" for details.
9  *
10  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11  * See accompanying file "TRADEMARK" for details.
12  *
13  * To redistribute this file separately, substitute the full license texts
14  * for the above references.
15  */
16 
17 #ifndef INC_StdMeshMath
18 #define INC_StdMeshMath
19 
20 // OGRE mesh
21 
23 {
24  float x, y, z;
25 
26  static StdMeshVector Zero();
27  static StdMeshVector UnitScale();
28  static StdMeshVector Translate(float dx, float dy, float dz);
29  static StdMeshVector Cross(const StdMeshVector& lhs, const StdMeshVector& rhs);
30 
31  void Normalize();
32 };
33 
34 
36 {
37  static const size_t MaxBoneWeightCount = 8;
38 
39  // Match GL_T2F_N3F_V3F
40  float u{0}, v{0};
41  float nx{0}, ny{0}, nz{0};
42  float x{0}, y{0}, z{0};
43 
46 
47  char _padding[16];
48 
50  {
51  std::uninitialized_fill(std::begin(bone_weight), std::end(bone_weight), 0);
52  std::uninitialized_fill(std::begin(bone_index), std::end(bone_index), 0);
53  std::uninitialized_fill(std::begin(_padding), std::end(_padding), 0);
54  }
55 };
56 static_assert((sizeof(StdMeshVertex) & 31) == 0, "StdMeshVertex should be a multiple of 32 bytes");
57 
59 {
60  float w;
61  float x, y, z;
62 
63  static StdMeshQuaternion Zero();
64  static StdMeshQuaternion AngleAxis(float theta, const StdMeshVector& axis);
65 
66  float LenSqr() const { return w*w+x*x+y*y+z*z; }
67  void Normalize();
68 
69  static StdMeshQuaternion Nlerp(const StdMeshQuaternion& lhs, const StdMeshQuaternion& rhs, float w);
70 };
71 
73 {
77 
78  static StdMeshTransformation Zero();
80  static StdMeshTransformation Inverse(const StdMeshTransformation& transform);
81  static StdMeshTransformation Translate(float dx, float dy, float dz);
82  static StdMeshTransformation Scale(float sx, float sy, float sz);
83  static StdMeshTransformation Rotate(float angle, float rx, float ry, float rz);
84 
85  // TODO: Might add path parameter if necessary
86  static StdMeshTransformation Nlerp(const StdMeshTransformation& lhs, const StdMeshTransformation& rhs, float w);
87 };
88 
90 {
91 public:
92  static const int NColumns = 4;
93  static const int NRows = 3;
94 
95  static StdMeshMatrix Zero();
96  static StdMeshMatrix Identity();
97  static StdMeshMatrix Inverse(const StdMeshMatrix& mat);
98  static StdMeshMatrix Translate(float dx, float dy, float dz);
99  static StdMeshMatrix Scale(float sx, float sy, float sz);
100  static StdMeshMatrix Rotate(float angle, float rx, float ry, float rz);
101  static StdMeshMatrix Transform(const StdMeshTransformation& transform);
102  static StdMeshMatrix TransformInverse(const StdMeshTransformation& transform);
103  static StdMeshMatrix LookAt(const StdMeshVector& eye, const StdMeshVector& center, const StdMeshVector& up);
104 
105  float& operator()(int i, int j) { return a[i][j]; }
106  float operator()(int i, int j) const { return a[i][j]; }
107 
108  float Determinant() const;
110 
111  const float* data() const { return &a[0][0]; }
112 
113 private:
114  // 3x3 orthogonal + translation in last column
115  float a[3][4];
116 };
117 
118 // Full 4x4 matrix with projection components
120 {
121 public:
122  static const int NColumns = 4;
123  static const int NRows = 4;
124 
125  static StdProjectionMatrix Identity();
126  static StdProjectionMatrix Translate(float dx, float dy, float dz);
127  static StdProjectionMatrix Scale(float sx, float sy, float sz);
128  static StdProjectionMatrix Rotate(float angle, float rx, float ry, float rz);
129 
130  static StdProjectionMatrix Orthographic(float left, float right, float bottom, float top);
131 
132  static StdMeshMatrix Upper3x4(const StdProjectionMatrix& matrix);
133 
134  float& operator()(int i, int j) { return a[i][j]; }
135  float operator()(int i, int j) const { return a[i][j]; }
136 
137  const float* data() const { return &a[0][0]; }
138 private:
139  float a[4][4];
140 };
141 
142 StdMeshMatrix operator*(const StdMeshMatrix& lhs, const StdMeshMatrix& rhs);
143 StdMeshMatrix operator*(float lhs, const StdMeshMatrix& rhs);
144 StdMeshMatrix operator*(const StdMeshMatrix& lhs, float rhs);
146 StdMeshMatrix operator+(const StdMeshMatrix& lhs, const StdMeshMatrix& rhs);
147 
150 
154 StdMeshQuaternion operator*(const StdMeshQuaternion& lhs, float rhs);
155 StdMeshQuaternion operator*(float lhs, const StdMeshQuaternion& rhs);
160 
163 StdMeshVector operator+(const StdMeshVector& lhs, const StdMeshVector& rhs);
165 StdMeshVector operator-(const StdMeshVector& lhs, const StdMeshVector& rhs);
166 StdMeshVector operator*(const StdMeshVector& lhs, const StdMeshVector& rhs);
167 StdMeshVector& operator*=(StdMeshVector& lhs, float rhs);
168 StdMeshVector operator*(const StdMeshVector& lhs, float rhs);
169 StdMeshVector operator*(float lhs, const StdMeshVector& rhs);
170 StdMeshVector operator/(const StdMeshVector& lhs, const StdMeshVector& rhs);
171 StdMeshVector operator/(float lhs, const StdMeshVector& rhs);
172 StdMeshVector operator/(const StdMeshVector& lhs, float rhs);
173 
174 StdMeshVector operator*(const StdMeshMatrix& lhs, const StdMeshVector& rhs); // does not apply translation part
176 
178 StdMeshVertex operator+(const StdMeshVertex& lhs, const StdMeshVertex& rhs);
179 StdMeshVertex operator*(float lhs, const StdMeshVertex& rhs);
180 StdMeshVertex operator*(const StdMeshVertex& lhs, float rhs);
181 StdMeshVertex operator*(const StdMeshMatrix& lhs, const StdMeshVertex& rhs);
182 
183 // Multiply in-place the given matrix with a translation matrix to the right
184 template<typename MatrixType>
185 void Translate(MatrixType& mat, float dx, float dy, float dz)
186 {
187  static_assert(MatrixType::NColumns >= 4, "Matrix must have at least 4 columns");
188 
189  for (int i = 0; i < MatrixType::NRows; ++i)
190  mat(i, 3) += mat(i,0)*dx + mat(i,1)*dy + mat(i,2)*dz;
191 }
192 
193 // Multiply in-place the given matrix with a scale matrix to the right
194 template<typename MatrixType>
195 void Scale(MatrixType& mat, float sx, float sy, float sz)
196 {
197  static_assert(MatrixType::NColumns >= 3, "Matrix must have at least 3 columns");
198 
199  for (int i = 0; i < MatrixType::NRows; ++i)
200  {
201  mat(i, 0) *= sx;
202  mat(i, 1) *= sy;
203  mat(i, 2) *= sz;
204  }
205 }
206 
207 // Multiply in-place the given matrix with a rotation matrix to the right
208 template<typename MatrixType>
209 void Rotate(MatrixType& mat, float angle, float x, float y, float z)
210 {
211  mat *= MatrixType::Rotate(angle, x, y, z);
212 }
213 
214 // Multiply in-place the given matrix with a perspective projection matrix to the right
215 template<typename MatrixType>
216 void Perspective(MatrixType& mat, float cot_fovy2, float aspect, float nearVal, float farVal)
217 {
218  static_assert(MatrixType::NColumns >= 4, "Matrix must have at least 4 columns");
219 
220  const float fa = cot_fovy2 / aspect;
221  const float fb = cot_fovy2;
222  const float z1 = (nearVal + farVal) / (nearVal - farVal);
223  const float z2 = 2 * nearVal * farVal / (nearVal - farVal);
224 
225  for (int i = 0; i < MatrixType::NRows; ++i)
226  {
227  const float mat2 = mat(i, 2);
228  mat(i, 0) *= fa;
229  mat(i, 1) *= fb;
230  mat(i, 2) = mat2 * z1 - mat(i, 3);
231  mat(i, 3) = mat2 * z2;
232  }
233 }
234 
235 #endif
#define z2
#define z
StdMeshMatrix operator+(const StdMeshMatrix &lhs, const StdMeshMatrix &rhs)
StdMeshVector & operator-=(StdMeshVector &lhs, const StdMeshVector &rhs)
StdMeshMatrix & operator*=(StdMeshMatrix &lhs, const StdMeshMatrix &rhs)
StdMeshQuaternion & operator+=(StdMeshQuaternion &lhs, const StdMeshQuaternion &rhs)
void Rotate(MatrixType &mat, float angle, float x, float y, float z)
Definition: StdMeshMath.h:209
StdMeshQuaternion operator-(const StdMeshQuaternion &rhs)
void Perspective(MatrixType &mat, float cot_fovy2, float aspect, float nearVal, float farVal)
Definition: StdMeshMath.h:216
void Scale(MatrixType &mat, float sx, float sy, float sz)
Definition: StdMeshMath.h:195
StdMeshMatrix operator*(const StdMeshMatrix &lhs, const StdMeshMatrix &rhs)
StdMeshVector operator/(const StdMeshVector &lhs, const StdMeshVector &rhs)
void Translate(MatrixType &mat, float dx, float dy, float dz)
Definition: StdMeshMath.h:185
static StdMeshMatrix Identity()
static StdMeshMatrix Inverse(const StdMeshMatrix &mat)
static StdMeshMatrix Rotate(float angle, float rx, float ry, float rz)
static StdMeshMatrix LookAt(const StdMeshVector &eye, const StdMeshVector &center, const StdMeshVector &up)
static StdMeshMatrix Scale(float sx, float sy, float sz)
static const int NRows
Definition: StdMeshMath.h:93
float & operator()(int i, int j)
Definition: StdMeshMath.h:105
static StdMeshMatrix TransformInverse(const StdMeshTransformation &transform)
const float * data() const
Definition: StdMeshMath.h:111
static const int NColumns
Definition: StdMeshMath.h:92
StdMeshTransformation Decompose() const
float operator()(int i, int j) const
Definition: StdMeshMath.h:106
static StdMeshMatrix Translate(float dx, float dy, float dz)
static StdMeshMatrix Transform(const StdMeshTransformation &transform)
float Determinant() const
static StdMeshMatrix Zero()
static StdProjectionMatrix Scale(float sx, float sy, float sz)
static StdMeshMatrix Upper3x4(const StdProjectionMatrix &matrix)
static const int NColumns
Definition: StdMeshMath.h:122
static const int NRows
Definition: StdMeshMath.h:123
float & operator()(int i, int j)
Definition: StdMeshMath.h:134
static StdProjectionMatrix Rotate(float angle, float rx, float ry, float rz)
static StdProjectionMatrix Translate(float dx, float dy, float dz)
float operator()(int i, int j) const
Definition: StdMeshMath.h:135
static StdProjectionMatrix Identity()
const float * data() const
Definition: StdMeshMath.h:137
static StdProjectionMatrix Orthographic(float left, float right, float bottom, float top)
static StdMeshQuaternion Zero()
Definition: StdMeshMath.cpp:62
float LenSqr() const
Definition: StdMeshMath.h:66
static StdMeshQuaternion Nlerp(const StdMeshQuaternion &lhs, const StdMeshQuaternion &rhs, float w)
Definition: StdMeshMath.cpp:93
static StdMeshQuaternion AngleAxis(float theta, const StdMeshVector &axis)
Definition: StdMeshMath.cpp:72
static StdMeshTransformation Scale(float sx, float sy, float sz)
static StdMeshTransformation Zero()
StdMeshVector translate
Definition: StdMeshMath.h:76
static StdMeshTransformation Nlerp(const StdMeshTransformation &lhs, const StdMeshTransformation &rhs, float w)
static StdMeshTransformation Identity()
static StdMeshTransformation Inverse(const StdMeshTransformation &transform)
static StdMeshTransformation Rotate(float angle, float rx, float ry, float rz)
StdMeshQuaternion rotate
Definition: StdMeshMath.h:75
StdMeshVector scale
Definition: StdMeshMath.h:74
static StdMeshTransformation Translate(float dx, float dy, float dz)
static StdMeshVector Zero()
Definition: StdMeshMath.cpp:20
static StdMeshVector UnitScale()
Definition: StdMeshMath.cpp:29
void Normalize()
Definition: StdMeshMath.cpp:56
static StdMeshVector Translate(float dx, float dy, float dz)
Definition: StdMeshMath.cpp:38
static StdMeshVector Cross(const StdMeshVector &lhs, const StdMeshVector &rhs)
Definition: StdMeshMath.cpp:47
char _padding[16]
Definition: StdMeshMath.h:47
uint16_t bone_index[MaxBoneWeightCount]
Definition: StdMeshMath.h:45
static const size_t MaxBoneWeightCount
Definition: StdMeshMath.h:37
float bone_weight[MaxBoneWeightCount]
Definition: StdMeshMath.h:44