Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
GLMatrixT.hh
1 /*===========================================================================*\
2  * *
3  * OpenFlipper *
4  * Copyright (C) 2001-2014 by Computer Graphics Group, RWTH Aachen *
5  * www.openflipper.org *
6  * *
7  *---------------------------------------------------------------------------*
8  * This file is part of OpenFlipper. *
9  * *
10  * OpenFlipper is free software: you can redistribute it and/or modify *
11  * it under the terms of the GNU Lesser General Public License as *
12  * published by the Free Software Foundation, either version 3 of *
13  * the License, or (at your option) any later version with the *
14  * following exceptions: *
15  * *
16  * If other files instantiate templates or use macros *
17  * or inline functions from this file, or you compile this file and *
18  * link it with other files to produce an executable, this file does *
19  * not by itself cause the resulting executable to be covered by the *
20  * GNU Lesser General Public License. This exception does not however *
21  * invalidate any other reasons why the executable file might be *
22  * covered by the GNU Lesser General Public License. *
23  * *
24  * OpenFlipper is distributed in the hope that it will be useful, *
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
27  * GNU Lesser General Public License for more details. *
28  * *
29  * You should have received a copy of the GNU LesserGeneral Public *
30  * License along with OpenFlipper. If not, *
31  * see <http://www.gnu.org/licenses/>. *
32  * *
33 \*===========================================================================*/
34 
35 /*===========================================================================*\
36  * *
37  * $Revision: 18555 $ *
38  * $Author: tenter $ *
39  * $Date: 2014-04-29 13:33:01 +0200 (Tue, 29 Apr 2014) $ *
40  * *
41 \*===========================================================================*/
42 
43 
44 
45 //=============================================================================
46 //
47 // CLASS GLMatrixT
48 //
49 //=============================================================================
50 
51 
52 #ifndef ACG_GLMATRIX_HH
53 #define ACG_GLMATRIX_HH
54 
55 
56 //== INCLUDES =================================================================
57 
58 
59 #include "Matrix4x4T.hh"
60 #include "../Config/ACGDefines.hh"
61 #include <cmath>
62 
63 
64 namespace ACG {
65 
66 
67 //== CLASS DEFINITION =========================================================
68 
69 
72 enum MultiplyFrom { MULT_FROM_RIGHT, MULT_FROM_LEFT };
73 
74 
75 
77 template <class Scalar>
78 class GLMatrixT : public Matrix4x4T<Scalar>
79 {
80 public:
81 
82  typedef VectorT<Scalar, 3> Vec3;
83 
84 
86  GLMatrixT() {}
87 
89  template <class OtherScalar>
90  inline GLMatrixT(const GLMatrixT<OtherScalar>& _rhs)
91  : Matrix4x4T<Scalar>(_rhs)
92  {}
93 
96  inline GLMatrixT(const Scalar _array[16]) : Matrix4x4T<Scalar>(_array) {}
97 
101  inline GLMatrixT(const Vec3 &col1, const Vec3 &col2, const Vec3 &col3) {
102  /*
103  * Don't try to optimize anything by hand, here. gcc -O2 does the right thing:
104  *
105  * mov %rax,-0x70(%rsp)
106  * mov %rdx,-0x68(%rsp)
107  * mov %rsi,-0x50(%rsp)
108  * lea -0x68(%rsp),%rdx
109  * mov %rax,0x8(%rsp)
110  * mov %rcx,-0x60(%rsp)
111  * lea 0x10(%rsp),%rsi
112  * movq $0x0,-0x58(%rsp)
113  * mov %rdi,-0x48(%rsp)
114  * xor %eax,%eax
115  * mov %r8,-0x40(%rsp)
116  * movq $0x0,-0x38(%rsp)
117  * mov %r9,-0x30(%rsp)
118  * mov %r10,-0x28(%rsp)
119  * mov %r11,-0x20(%rsp)
120  * movq $0x0,-0x18(%rsp)
121  * movq $0x0,-0x10(%rsp)
122  * movq $0x0,-0x8(%rsp)
123  * movq $0x0,(%rsp)
124  *
125  */
126  memcpy(this->mat_ + 0, col1.data(), sizeof(double) * 3);
127  this->mat_[3] = 0;
128  memcpy(this->mat_ + 4, col2.data(), sizeof(double) * 3);
129  this->mat_[7] = 0;
130  memcpy(this->mat_ + 8, col3.data(), sizeof(double) * 3);
131  for (int i = 11; i < 15; ++i) this->mat_[i] = 0;
132  this->mat_[15] = 1;
133  }
134 
135 
138 
139 
141  template<typename otherScalar>
143  { Matrix4x4T<Scalar>::operator=(_rhs); return *this; }
144 
146  template<typename otherScalar>
148  { Matrix4x4T<Scalar>::operator=(_rhs); return *this; }
149 
150 
151 
153  inline void scale( Scalar _x, Scalar _y, Scalar _z,
154  MultiplyFrom _mult_from = MULT_FROM_RIGHT );
156  inline void scale( const Vec3& _v,
157  MultiplyFrom _mult_from = MULT_FROM_RIGHT ) {
158  scale(_v[0], _v[1], _v[2], _mult_from);
159  }
160 
161 
163  inline void translate( Scalar _x, Scalar _y, Scalar _z,
164  MultiplyFrom _mult_from = MULT_FROM_RIGHT );
166  inline void translate( const Vec3& _v,
167  MultiplyFrom _mult_from = MULT_FROM_RIGHT ) {
168  translate(_v[0], _v[1], _v[2], _mult_from);
169  }
170 
171 
174  void rotate( Scalar angle, Scalar x, Scalar y, Scalar z,
175  MultiplyFrom _mult_from = MULT_FROM_RIGHT );
178  void rotate( Scalar _angle, const Vec3& _axis,
179  MultiplyFrom _mult_from = MULT_FROM_RIGHT ) {
180  rotate(_angle, _axis[0], _axis[1], _axis[2], _mult_from);
181  }
182 
183 
184 
186  inline void rotateX( Scalar _angle,
187  MultiplyFrom _mult_from = MULT_FROM_RIGHT ) {
188  rotateXYZ( X, _angle, _mult_from );
189  }
190 
192  inline void rotateY( Scalar _angle,
193  MultiplyFrom _mult_from = MULT_FROM_RIGHT ) {
194  rotateXYZ( Y, _angle, _mult_from );
195  }
196 
198  inline void rotateZ( Scalar _angle,
199  MultiplyFrom _mult_from = MULT_FROM_RIGHT ) {
200  rotateXYZ( Z, _angle, _mult_from );
201  }
202 
203 
204 
205 
209  void lookAt(const Vec3& eye,
210  const Vec3& center,
211  const Vec3& up);
212 
214  void inverse_lookAt(const Vec3& eye,
215  const Vec3& center,
216  const Vec3& up);
217 
218 
229  void perspective(Scalar fovY, Scalar aspect,
230  Scalar near_plane, Scalar far_plane);
231 
233  void inverse_perspective(Scalar fovY, Scalar aspect,
234  Scalar near_plane,Scalar far_plane);
235 
237  void frustum(Scalar left, Scalar right,
238  Scalar bottom, Scalar top,
239  Scalar near_plane, Scalar far_plane);
240 
242  void inverse_frustum(Scalar left,Scalar right,
243  Scalar bottom, Scalar top,
244  Scalar near_plane, Scalar far_plane);
245 
247  void ortho(Scalar left, Scalar right,
248  Scalar bottom, Scalar top,
249  Scalar near_plane, Scalar far_plane);
250 
252  void inverse_ortho(Scalar left, Scalar right,
253  Scalar bottom, Scalar top,
254  Scalar near_plane, Scalar far_plane);
255 
256 
257 
259  VectorT<Scalar, 2> extract_planes_perspective() const;
260 
262  VectorT<Scalar, 2> extract_planes_ortho() const;
263 
265  bool isPerspective() const;
266 
268  bool isOrtho() const;
269 
271  VectorT<Scalar, 2> extract_planes() const;
272 
273  //----------------------------------------------------- overloaded operators
274 
275  GLMatrixT& operator+= ( const Matrix4x4T<Scalar>& _rhs) {
276  Matrix4x4T<Scalar>::operator+=(_rhs); return *this;
277  }
278  GLMatrixT& operator-= ( const Matrix4x4T<Scalar>& _rhs) {
279  Matrix4x4T<Scalar>::operator-=(_rhs); return *this;
280  }
281  GLMatrixT& operator*= ( const Matrix4x4T<Scalar>& _rhs) {
282  Matrix4x4T<Scalar>::operator*=(_rhs); return *this;
283  }
284  GLMatrixT& leftMult(const Matrix4x4T<Scalar>& _rhs) {
285  Matrix4x4T<Scalar>::leftMult(_rhs); return *this;
286  }
287 
288  GLMatrixT operator+ (const Matrix4x4T<Scalar>& _rhs) const {
289  return GLMatrixT<Scalar>(*this) += _rhs;
290  }
291  GLMatrixT operator- (const Matrix4x4T<Scalar>& _rhs) const {
292  return GLMatrixT<Scalar>(*this) -= _rhs;
293  }
294  GLMatrixT operator*(const Matrix4x4T<Scalar>& _rhs) const {
295  return GLMatrixT<Scalar>(*this) *= _rhs;
296  }
297 
298  template <typename T>
299  inline VectorT<T,4> operator*(const VectorT<T,4>& _v) const {
301  }
302 
303 
304 
305 private:
306 
307  enum Axis { X, Y, Z };
308  void rotateXYZ( Axis _axis, Scalar _angle, MultiplyFrom _mult_from );
309 };
310 
311 
312 
313 
314 //=============================================================================
315 
316 
321 
322 
323 //=============================================================================
324 } // namespace ACG
325 //=============================================================================
326 #if defined(INCLUDE_TEMPLATES) && !defined(ACG_GLMATRIX_C)
327 #define ACG_GLMATRIX_TEMPLATES
328 #include "GLMatrixT.cc"
329 #endif
330 //=============================================================================
331 #endif // ACG_GLMATRIX_HH defined
332 //=============================================================================
333 
void inverse_lookAt(const Vec3 &eye, const Vec3 &center, const Vec3 &up)
multiply self from left with inverse lookAt matrix
Definition: GLMatrixT.cc:246
GLMatrixT< Scalar > & operator=(const Matrix4x4T< otherScalar > &_rhs)
assignement from other matrix type
Definition: GLMatrixT.hh:147
void lookAt(const Vec3 &eye, const Vec3 &center, const Vec3 &up)
Definition: GLMatrixT.cc:215
Matrix4x4T & leftMult(const Matrix4x4T< Scalar > &_rhs)
multiply from left: self = _rhs * self
Definition: Matrix4x4T.cc:136
void inverse_ortho(Scalar left, Scalar right, Scalar bottom, Scalar top, Scalar near_plane, Scalar far_plane)
multiply self from left with inverse orthographic projection matrix
Definition: GLMatrixT.cc:407
void perspective(Scalar fovY, Scalar aspect, Scalar near_plane, Scalar far_plane)
multiply self with a perspective projection matrix
Definition: GLMatrixT.cc:276
VectorT< Scalar, 2 > extract_planes() const
detect type of projection matrix and extract near and far clipping planes
Definition: GLMatrixT.cc:575
4x4 matrix implementing OpenGL commands.
Definition: MeshNode2T.cc:78
GLMatrixT(const GLMatrixT< OtherScalar > &_rhs)
construct from other matrix type
Definition: GLMatrixT.hh:90
void translate(Scalar _x, Scalar _y, Scalar _z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
multiply self with translation matrix (x,y,z)
Definition: GLMatrixT.cc:95
Matrix4x4T & operator-=(const Matrix4x4T< Scalar > &_rhs)
self -= _rhs
Definition: Matrix4x4T.hh:181
GLMatrixT< float > GLMatrixf
typedef
Definition: GLMatrixT.hh:318
Matrix4x4T & operator+=(const Matrix4x4T< Scalar > &_rhs)
self += _rhs
Definition: Matrix4x4T.hh:175
void scale(const Vec3 &_v, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
multiply self with scaling matrix (x,y,z)
Definition: GLMatrixT.hh:156
Matrix4x4T & operator*=(const Matrix4x4T< Scalar > &_rhs)
self *= _rhs
Definition: Matrix4x4T.cc:108
void rotate(Scalar _angle, const Vec3 &_axis, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
Definition: GLMatrixT.hh:178
Matrix4x4T operator*(const Matrix4x4T< Scalar > &inst) const
self * _rhs
Definition: Matrix4x4T.cc:78
void frustum(Scalar left, Scalar right, Scalar bottom, Scalar top, Scalar near_plane, Scalar far_plane)
multiply self with a perspective projection matrix
Definition: GLMatrixT.cc:310
GLMatrixT< double > GLMatrixd
typedef
Definition: GLMatrixT.hh:320
void ortho(Scalar left, Scalar right, Scalar bottom, Scalar top, Scalar near_plane, Scalar far_plane)
multiply self with orthographic projection matrix
Definition: GLMatrixT.cc:372
void rotate(Scalar angle, Scalar x, Scalar y, Scalar z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
Definition: GLMatrixT.cc:154
void inverse_perspective(Scalar fovY, Scalar aspect, Scalar near_plane, Scalar far_plane)
multiply self from left with inverse of perspective projection matrix
Definition: GLMatrixT.cc:293
GLMatrixT< Scalar > & operator=(const GLMatrixT< otherScalar > &_rhs)
assignement from other matrix type
Definition: GLMatrixT.hh:142
bool isOrtho() const
check if the matrix is an orthographic projection matrix
Definition: GLMatrixT.cc:522
MultiplyFrom
Definition: GLMatrixT.hh:72
GLMatrixT(const Scalar _array[16])
Definition: GLMatrixT.hh:96
~GLMatrixT()
destructor
Definition: GLMatrixT.hh:137
VectorT< Scalar, 2 > extract_planes_perspective() const
extract near and far clipping planes from a perspective projection matrix
Definition: GLMatrixT.cc:437
Matrix4x4T< Scalar > & operator=(const Matrix4x4T< otherScalar > &_rhs)
assignment from other matrix type
Definition: Matrix4x4T.hh:116
void rotateZ(Scalar _angle, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
multiply self with a rotation matrix (angle in degree, z-axis)
Definition: GLMatrixT.hh:198
GLMatrixT()
constructor: uninitialized values
Definition: GLMatrixT.hh:86
Namespace providing different geometric functions concerning angles.
Definition: DBSCANT.cc:10
void rotateX(Scalar _angle, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
multiply self with a rotation matrix (angle in degree, x-axis)
Definition: GLMatrixT.hh:186
void inverse_frustum(Scalar left, Scalar right, Scalar bottom, Scalar top, Scalar near_plane, Scalar far_plane)
multiply self from left with inverse of perspective projection matrix
Definition: GLMatrixT.cc:341
VectorT< Scalar, 2 > extract_planes_ortho() const
extract near and far clipping planes from an orthographic projection matrix
Definition: GLMatrixT.cc:450
4x4 matrix implementing OpenGL commands.
Definition: GLMatrixT.hh:78
void scale(Scalar _x, Scalar _y, Scalar _z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
multiply self with scaling matrix (x,y,z)
Definition: GLMatrixT.cc:74
bool isPerspective() const
check if the matrix is a perspective projection matrix
Definition: GLMatrixT.cc:463
GLMatrixT(const Vec3 &col1, const Vec3 &col2, const Vec3 &col3)
Definition: GLMatrixT.hh:101
void rotateY(Scalar _angle, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
multiply self with a rotation matrix (angle in degree, y-axis)
Definition: GLMatrixT.hh:192
void translate(const Vec3 &_v, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
multiply self with translation matrix (x,y,z)
Definition: GLMatrixT.hh:166