NGL  6.5
The NCCA Graphics Library
Mat4.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 Jon Macey
3 
4  This program is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 #ifndef MAT4_H_
18 #define MAT4_H_
19 //----------------------------------------------------------------------------------------------------------------------
22 //----------------------------------------------------------------------------------------------------------------------
23 // must include types.h first for Real and GLEW if required
24 #include "Types.h"
25 #include <ostream>
26 #include <array>
27 
28 namespace ngl
29 {
30 class Vec4;
31 class Vec3;
32 class Quaternion;
33 
34 
35 
36 //----------------------------------------------------------------------------------------------------------------------
55 //----------------------------------------------------------------------------------------------------------------------
56 
57 
59 {
60  // added after clang++ build
61  friend class Vec4;
62 public:
63 
64  //----------------------------------------------------------------------------------------------------------------------
66  //----------------------------------------------------------------------------------------------------------------------
67  Mat4() noexcept;
68  //----------------------------------------------------------------------------------------------------------------------
73  //----------------------------------------------------------------------------------------------------------------------
74  Mat4(Real _m[4][4]) noexcept;
75  //----------------------------------------------------------------------------------------------------------------------
78  //----------------------------------------------------------------------------------------------------------------------
79  Mat4(Real _00,Real _01,Real _02,Real _03,
80  Real _10,Real _11,Real _12,Real _13,
81  Real _20,Real _21,Real _22,Real _23,
82  Real _30,Real _31,Real _32,Real _33) noexcept;
83  //----------------------------------------------------------------------------------------------------------------------
85  //----------------------------------------------------------------------------------------------------------------------
86  Mat4(const Mat4& _m) noexcept;
87  //----------------------------------------------------------------------------------------------------------------------
89  //----------------------------------------------------------------------------------------------------------------------
90  Mat4(Real _m) noexcept;
91  //----------------------------------------------------------------------------------------------------------------------
93  //----------------------------------------------------------------------------------------------------------------------
94  Mat4& operator =(const Mat4& _m) noexcept;
95  //----------------------------------------------------------------------------------------------------------------------
100  //----------------------------------------------------------------------------------------------------------------------
101  void setAtXY(GLint _x, GLint _y, Real _equals) noexcept;
102 
103  //----------------------------------------------------------------------------------------------------------------------
105  //----------------------------------------------------------------------------------------------------------------------
106  const Mat4& null() noexcept;
107 
108  //----------------------------------------------------------------------------------------------------------------------
114  //----------------------------------------------------------------------------------------------------------------------
115  const Mat4& identity() noexcept;
116  //----------------------------------------------------------------------------------------------------------------------
120  //----------------------------------------------------------------------------------------------------------------------
121  Mat4 operator*(const Mat4 &_m) const noexcept;
122  //----------------------------------------------------------------------------------------------------------------------
126  //----------------------------------------------------------------------------------------------------------------------
127  const Mat4& operator*=(const Mat4 &_m) noexcept;
128  //----------------------------------------------------------------------------------------------------------------------
132  //----------------------------------------------------------------------------------------------------------------------
133  Mat4 operator+(const Mat4 &_m) const noexcept;
134  //----------------------------------------------------------------------------------------------------------------------
138  //----------------------------------------------------------------------------------------------------------------------
139  const Mat4& operator+=(const Mat4 &_m) noexcept;
140  //----------------------------------------------------------------------------------------------------------------------
144  //----------------------------------------------------------------------------------------------------------------------
145  Mat4 operator*(const Real _i) const noexcept;
146  //----------------------------------------------------------------------------------------------------------------------
150  //----------------------------------------------------------------------------------------------------------------------
151  const Mat4& operator*=(const Real _i) noexcept;
152  //----------------------------------------------------------------------------------------------------------------------
154  //----------------------------------------------------------------------------------------------------------------------
155  const Mat4& transpose() noexcept;
156 
157  //----------------------------------------------------------------------------------------------------------------------
161  //----------------------------------------------------------------------------------------------------------------------
162  void rotateX(const Real _deg) noexcept;
163  //----------------------------------------------------------------------------------------------------------------------
167  //----------------------------------------------------------------------------------------------------------------------
168  void rotateY(const Real _deg) noexcept;
169 
170  //----------------------------------------------------------------------------------------------------------------------
174  //----------------------------------------------------------------------------------------------------------------------
175  void rotateZ(const Real _deg) noexcept;
176  //----------------------------------------------------------------------------------------------------------------------
181  //----------------------------------------------------------------------------------------------------------------------
182  void scale(const Real _x,const Real _y,const Real _z) noexcept;
183  //----------------------------------------------------------------------------------------------------------------------
188  //----------------------------------------------------------------------------------------------------------------------
189  void translate(const Real _x, const Real _y, const Real _z) noexcept;
190  //----------------------------------------------------------------------------------------------------------------------
193  //----------------------------------------------------------------------------------------------------------------------
194  Real determinant() const noexcept;
195 
196  //----------------------------------------------------------------------------------------------------------------------
201  //----------------------------------------------------------------------------------------------------------------------
202  void subMatrix3x3(const int _i,const int _j,Real o_mat[]) const noexcept;
203 
204  //----------------------------------------------------------------------------------------------------------------------
207  //----------------------------------------------------------------------------------------------------------------------
208  void as3x3Array(Real _d[9]) const noexcept;
209 
210  //----------------------------------------------------------------------------------------------------------------------
213  //----------------------------------------------------------------------------------------------------------------------
214  Mat4 inverse() noexcept;
215  //----------------------------------------------------------------------------------------------------------------------
218  //----------------------------------------------------------------------------------------------------------------------
219  Quaternion asQuaternion() const noexcept;
220  //----------------------------------------------------------------------------------------------------------------------
224  //----------------------------------------------------------------------------------------------------------------------
225  Vec4 operator * (const Vec4 &_v) const noexcept;
226 
227  //----------------------------------------------------------------------------------------------------------------------
233  //----------------------------------------------------------------------------------------------------------------------
234  void euler(const Real _angle, const Real _x, const Real _y, const Real _z) noexcept;
235  //----------------------------------------------------------------------------------------------------------------------
238  //----------------------------------------------------------------------------------------------------------------------
239  Real * openGL() noexcept{return &m_openGL[0];}
240  //----------------------------------------------------------------------------------------------------------------------
243  //----------------------------------------------------------------------------------------------------------------------
244  Mat4 Adjacent(const Mat4 &_mat) noexcept;
245  //----------------------------------------------------------------------------------------------------------------------
248  //----------------------------------------------------------------------------------------------------------------------
249  Mat4 Adjacent() noexcept;
250  //----------------------------------------------------------------------------------------------------------------------
253  //----------------------------------------------------------------------------------------------------------------------
254  Vec3 getLeftVector() const noexcept;
255  //----------------------------------------------------------------------------------------------------------------------
258  //----------------------------------------------------------------------------------------------------------------------
259  Vec3 getRightVector() const noexcept;
260  //----------------------------------------------------------------------------------------------------------------------
263  //----------------------------------------------------------------------------------------------------------------------
264  Vec3 getUpVector() const noexcept;
265  //----------------------------------------------------------------------------------------------------------------------
268  //----------------------------------------------------------------------------------------------------------------------
269  Vec3 getDownVector() const noexcept;
270  //----------------------------------------------------------------------------------------------------------------------
273  //----------------------------------------------------------------------------------------------------------------------
274  Vec3 getForwardVector() const noexcept;
275  //----------------------------------------------------------------------------------------------------------------------
278  //----------------------------------------------------------------------------------------------------------------------
279  Vec3 getBackVector() const noexcept;
280 
281 public :
282  //----------------------------------------------------------------------------------------------------------------------
283  // allow access to some of the other classes
284  friend class Transformation;
285  friend class Quaternion;
286  friend class Camera;
287  //----------------------------------------------------------------------------------------------------------------------
288 #pragma pack(push,1)
289 
290  union
291  {
292  //----------------------------------------------------------------------------------------------------------------------
294  //----------------------------------------------------------------------------------------------------------------------
295  Real m_m[4][4];
296  //----------------------------------------------------------------------------------------------------------------------
299  //----------------------------------------------------------------------------------------------------------------------
300  std::array<Real,16> m_openGL={{
301  1.0f,0.0f,0.0f,0.0f,
302  0.0f,1.0f,0.0f,0.0f,
303  0.0f,0.0f,1.0f,0.0f,
304  0.0f,0.0f,0.0f,1.0f
305  }};
306 
307  struct
308  {
309  struct
310  {
327  };
328  };
329  };
330 #pragma pack(pop)
331  }; // end of class
332 
333 
334 // free function for matrix comparison use in unit tests etc
335 inline bool operator==(const ngl::Mat4 &_m1 , const ngl::Mat4 &_m2)
336 {
337  for(size_t i=0; i<_m1.m_openGL.size(); ++i)
338  {
339  if(!( FCompare(_m1.m_openGL[i] , _m2.m_openGL[i] )))
340  {
341  return false;
342  }
343  }
344  return true;
345 }
346 
347 
348 
349 }// end of namespace
350 
351 #endif
352 //----------------------------------------------------------------------------------------------------------------------
353 
bool operator==(const ngl::Mat3 &_m1, const ngl::Mat3 &_m2)
Definition: Mat3.h:297
GLsizei GLboolean transpose
Definition: glew.h:1867
Real m_11
individual matrix element maps to m_m[1][1] or m_openGL[5]
Definition: Mat4.h:316
Real m_21
individual matrix element maps to m_m[2][1] or m_openGL[9]
Definition: Mat4.h:320
GLenum GLenum GLenum GLenum GLenum scale
Definition: glew.h:14161
simple Vector class for OpenGL graphics, contains overloaded operators for most math functions...
Definition: Vec4.h:57
main definition of types and namespace
Real m_22
individual matrix element maps to m_m[2][2] or m_openGL[10]
Definition: Mat4.h:321
#define NGL_DLLEXPORT
Definition: Types.h:65
Real m_12
individual matrix element maps to m_m[1][2] or m_openGL[6]
Definition: Mat4.h:317
Real m_31
individual matrix element maps to m_m[3][1] or m_openGL[13]
Definition: Mat4.h:324
simple Vec3 encapsulates a 3 float object like glsl vec3 but not maths use the Vec3 class for maths a...
Definition: Vec3.h:51
implementation files for RibExport class
Definition: AABB.cpp:22
Real m_30
individual matrix element maps to m_m[3][0] or m_openGL[12]
Definition: Mat4.h:323
PRECISION Real
create a variable called Real which is the main data type we use (GLfloat for most cases) ...
Definition: Types.h:127
#define FCompare(a, b)
FCompare macro used for floating point comparision functions.
Definition: Types.h:136
Real m_02
individual matrix element maps to m_m[0][2] or m_openGL[2]
Definition: Mat4.h:313
simple camera class to allow movement in an opengl scene. a lot of this stuff is from the HILL book C...
Definition: Camera.h:54
Real m_01
individual matrix element maps to m_m[0][1] or m_openGL[1]
Definition: Mat4.h:312
Real m_00
individual matrix element maps to m_m[0][0] or m_openGL[0]
Definition: Mat4.h:311
Real m_10
individual matrix element maps to m_m[1][0] or m_openGL[4]
Definition: Mat4.h:315
Real m_13
individual matrix element maps to m_m[1][3] or m_openGL[7]
Definition: Mat4.h:318
Real m_23
individual matrix element maps to m_m[2][3] or m_openGL[11]
Definition: Mat4.h:322
Transformation describes a transformation (translate, scale, rotation) modifed by j macey and include...
Real m_32
individual matrix element maps to m_m[3][2] or m_openGL[14]
Definition: Mat4.h:325
Matrix Class to do simple matrix operations included operator overloaded functions for maths and matr...
Definition: Mat4.h:58
int GLint
Definition: glew.h:281
std::array< Real, 16 > m_openGL
The matrix in m_openGL 16 Real array format usefull for OpenGL fv formats mapped to m_m[][] elements ...
Definition: Mat4.h:300
Real m_20
individual matrix element maps to m_m[2][0] or m_openGL[8]
Definition: Mat4.h:319
Real m_03
individual matrix element maps to m_m[0][3] or m_openGL[3]
Definition: Mat4.h:314
Real m_33
individual matrix element maps to m_m[3][3] or m_openGL[15]
Definition: Mat4.h:326