OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
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, v;
41  float nx, ny, nz;
42  float x, y, z;
43 
46 
47  char _padding[16];
48 
49  StdMeshVertex() : u(0), v(0), nx(0), ny(0), nz(0), x(0), y(0), z(0)
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 
58 struct StdMeshQuaternion
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 
72 struct StdMeshTransformation
73 {
74  StdMeshVector scale;
75  StdMeshQuaternion rotate;
76  StdMeshVector translate;
77 
78  static StdMeshTransformation Zero();
79  static StdMeshTransformation Identity();
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 
89 class StdMeshMatrix
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;
109  StdMeshTransformation Decompose() 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
119 class StdProjectionMatrix
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);
145 StdMeshMatrix& operator*=(StdMeshMatrix& lhs, const StdMeshMatrix& rhs);
146 StdMeshMatrix operator+(const StdMeshMatrix& lhs, const StdMeshMatrix& rhs);
147 
148 StdProjectionMatrix operator*(const StdProjectionMatrix& lhs, const StdProjectionMatrix& rhs);
149 StdProjectionMatrix& operator*=(StdProjectionMatrix& lhs, const StdProjectionMatrix& rhs);
150 
151 StdMeshQuaternion operator-(const StdMeshQuaternion& rhs);
152 StdMeshQuaternion operator*(const StdMeshQuaternion& lhs, const StdMeshQuaternion& rhs);
153 StdMeshQuaternion& operator*=(StdMeshQuaternion& lhs, float rhs);
154 StdMeshQuaternion operator*(const StdMeshQuaternion& lhs, float rhs);
155 StdMeshQuaternion operator*(float lhs, const StdMeshQuaternion& rhs);
156 StdMeshQuaternion& operator+=(StdMeshQuaternion& lhs, const StdMeshQuaternion& rhs);
157 StdMeshQuaternion operator+(const StdMeshQuaternion& lhs, const StdMeshQuaternion& rhs);
158 StdMeshQuaternion operator-(const StdMeshQuaternion& lhs, const StdMeshQuaternion& rhs);
159 StdMeshTransformation operator*(const StdMeshTransformation& lhs, const StdMeshTransformation& 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
175 StdMeshVector operator*(const StdMeshQuaternion& lhs, const StdMeshVector& rhs);
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 z
char _padding[16]
Definition: StdMeshMath.h:47
float bone_weight[MaxBoneWeightCount]
Definition: StdMeshMath.h:44
StdMeshQuaternion & operator+=(StdMeshQuaternion &lhs, const StdMeshQuaternion &rhs)
#define z2
StdMeshQuaternion operator-(const StdMeshQuaternion &rhs)
#define a
StdMeshMatrix operator+(const StdMeshMatrix &lhs, const StdMeshMatrix &rhs)
StdMeshVector & operator-=(StdMeshVector &lhs, const StdMeshVector &rhs)
StdMeshVector operator/(const StdMeshVector &lhs, const StdMeshVector &rhs)
StdMeshMatrix & operator*=(StdMeshMatrix &lhs, const StdMeshMatrix &rhs)
static StdMeshVector Translate(float dx, float dy, float dz)
Definition: StdMeshMath.cpp:44
StdMeshMatrix operator*(const StdMeshMatrix &lhs, const StdMeshMatrix &rhs)
void Normalize()
Definition: StdMeshMath.cpp:62
static StdMeshVector UnitScale()
Definition: StdMeshMath.cpp:35
static StdMeshVector Zero()
Definition: StdMeshMath.cpp:26
uint16_t bone_index[MaxBoneWeightCount]
Definition: StdMeshMath.h:45
static const size_t MaxBoneWeightCount
Definition: StdMeshMath.h:37
static StdMeshVector Cross(const StdMeshVector &lhs, const StdMeshVector &rhs)
Definition: StdMeshMath.cpp:53