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-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40 \*===========================================================================*/
41 
42 /*===========================================================================*\
43  * *
44  * $Revision$ *
45  * $Author$ *
46  * $Date$ *
47  * *
48 \*===========================================================================*/
49 
50 
51 
52 //=============================================================================
53 //
54 // CLASS GLMatrixT
55 //
56 //=============================================================================
57 
58 
59 #ifndef ACG_GLMATRIX_HH
60 #define ACG_GLMATRIX_HH
61 
62 
63 //== INCLUDES =================================================================
64 
65 
66 #include "Matrix4x4T.hh"
67 #include "../Config/ACGDefines.hh"
68 #include <cmath>
69 
70 
71 namespace ACG {
72 
73 
74 //== CLASS DEFINITION =========================================================
75 
76 
79 enum MultiplyFrom { MULT_FROM_RIGHT, MULT_FROM_LEFT };
80 
81 
82 
84 template <class Scalar>
85 class GLMatrixT : public Matrix4x4T<Scalar>
86 {
87 public:
88 
89  typedef VectorT<Scalar, 3> Vec3;
90 
91 
93  GLMatrixT() {}
94 
96  template <class OtherScalar>
97  inline GLMatrixT(const GLMatrixT<OtherScalar>& _rhs)
98  : Matrix4x4T<Scalar>(_rhs)
99  {}
100 
103  inline GLMatrixT(const Scalar _array[16]) : Matrix4x4T<Scalar>(_array) {}
104 
108  inline GLMatrixT(const Vec3 &col1, const Vec3 &col2, const Vec3 &col3) {
109  /*
110  * Don't try to optimize anything by hand, here. gcc -O2 does the right thing:
111  *
112  * mov %rax,-0x70(%rsp)
113  * mov %rdx,-0x68(%rsp)
114  * mov %rsi,-0x50(%rsp)
115  * lea -0x68(%rsp),%rdx
116  * mov %rax,0x8(%rsp)
117  * mov %rcx,-0x60(%rsp)
118  * lea 0x10(%rsp),%rsi
119  * movq $0x0,-0x58(%rsp)
120  * mov %rdi,-0x48(%rsp)
121  * xor %eax,%eax
122  * mov %r8,-0x40(%rsp)
123  * movq $0x0,-0x38(%rsp)
124  * mov %r9,-0x30(%rsp)
125  * mov %r10,-0x28(%rsp)
126  * mov %r11,-0x20(%rsp)
127  * movq $0x0,-0x18(%rsp)
128  * movq $0x0,-0x10(%rsp)
129  * movq $0x0,-0x8(%rsp)
130  * movq $0x0,(%rsp)
131  *
132  */
133  memcpy(this->mat_ + 0, col1.data(), sizeof(Scalar) * 3);
134  this->mat_[3] = 0;
135  memcpy(this->mat_ + 4, col2.data(), sizeof(Scalar) * 3);
136  this->mat_[7] = 0;
137  memcpy(this->mat_ + 8, col3.data(), sizeof(Scalar) * 3);
138  for (int i = 11; i < 15; ++i) this->mat_[i] = 0;
139  this->mat_[15] = 1;
140  }
141 
142 
145 
146 
148  template<typename otherScalar>
150  { Matrix4x4T<Scalar>::operator=(_rhs); return *this; }
151 
153  template<typename otherScalar>
155  { Matrix4x4T<Scalar>::operator=(_rhs); return *this; }
156 
157 
158 
160  inline void scale( Scalar _x, Scalar _y, Scalar _z,
161  MultiplyFrom _mult_from = MULT_FROM_RIGHT );
163  inline void scale( const Vec3& _v,
164  MultiplyFrom _mult_from = MULT_FROM_RIGHT ) {
165  scale(_v[0], _v[1], _v[2], _mult_from);
166  }
167 
168 
170  inline void translate( Scalar _x, Scalar _y, Scalar _z,
171  MultiplyFrom _mult_from = MULT_FROM_RIGHT );
173  inline void translate( const Vec3& _v,
174  MultiplyFrom _mult_from = MULT_FROM_RIGHT ) {
175  translate(_v[0], _v[1], _v[2], _mult_from);
176  }
177 
178 
181  void rotate( Scalar angle, Scalar x, Scalar y, Scalar z,
182  MultiplyFrom _mult_from = MULT_FROM_RIGHT );
185  void rotate( Scalar _angle, const Vec3& _axis,
186  MultiplyFrom _mult_from = MULT_FROM_RIGHT ) {
187  rotate(_angle, _axis[0], _axis[1], _axis[2], _mult_from);
188  }
189 
190 
191 
193  inline void rotateX( Scalar _angle,
194  MultiplyFrom _mult_from = MULT_FROM_RIGHT ) {
195  rotateXYZ( X, _angle, _mult_from );
196  }
197 
199  inline void rotateY( Scalar _angle,
200  MultiplyFrom _mult_from = MULT_FROM_RIGHT ) {
201  rotateXYZ( Y, _angle, _mult_from );
202  }
203 
205  inline void rotateZ( Scalar _angle,
206  MultiplyFrom _mult_from = MULT_FROM_RIGHT ) {
207  rotateXYZ( Z, _angle, _mult_from );
208  }
209 
210 
211 
212 
216  void lookAt(const Vec3& eye,
217  const Vec3& center,
218  const Vec3& up);
219 
221  void inverse_lookAt(const Vec3& eye,
222  const Vec3& center,
223  const Vec3& up);
224 
225 
236  void perspective(Scalar fovY, Scalar aspect,
237  Scalar near_plane, Scalar far_plane);
238 
240  void inverse_perspective(Scalar fovY, Scalar aspect,
241  Scalar near_plane,Scalar far_plane);
242 
244  void frustum(Scalar left, Scalar right,
245  Scalar bottom, Scalar top,
246  Scalar near_plane, Scalar far_plane);
247 
249  void inverse_frustum(Scalar left,Scalar right,
250  Scalar bottom, Scalar top,
251  Scalar near_plane, Scalar far_plane);
252 
254  void ortho(Scalar left, Scalar right,
255  Scalar bottom, Scalar top,
256  Scalar near_plane, Scalar far_plane);
257 
259  void inverse_ortho(Scalar left, Scalar right,
260  Scalar bottom, Scalar top,
261  Scalar near_plane, Scalar far_plane);
262 
263 
264 
267 
270 
272  bool isPerspective() const;
273 
275  bool isOrtho() const;
276 
279 
280  //----------------------------------------------------- overloaded operators
281 
282  GLMatrixT& operator+= ( const Matrix4x4T<Scalar>& _rhs) {
283  Matrix4x4T<Scalar>::operator+=(_rhs); return *this;
284  }
285  GLMatrixT& operator-= ( const Matrix4x4T<Scalar>& _rhs) {
286  Matrix4x4T<Scalar>::operator-=(_rhs); return *this;
287  }
288  GLMatrixT& operator*= ( const Matrix4x4T<Scalar>& _rhs) {
289  Matrix4x4T<Scalar>::operator*=(_rhs); return *this;
290  }
291  GLMatrixT& leftMult(const Matrix4x4T<Scalar>& _rhs) {
292  Matrix4x4T<Scalar>::leftMult(_rhs); return *this;
293  }
294 
295  GLMatrixT operator+ (const Matrix4x4T<Scalar>& _rhs) const {
296  return GLMatrixT<Scalar>(*this) += _rhs;
297  }
298  GLMatrixT operator- (const Matrix4x4T<Scalar>& _rhs) const {
299  return GLMatrixT<Scalar>(*this) -= _rhs;
300  }
301  GLMatrixT operator*(const Matrix4x4T<Scalar>& _rhs) const {
302  return GLMatrixT<Scalar>(*this) *= _rhs;
303  }
304 
305  template <typename T>
306  inline VectorT<T,4> operator*(const VectorT<T,4>& _v) const {
308  }
309 
310 
311 
312 private:
313 
314  enum Axis { X, Y, Z };
315  void rotateXYZ( Axis _axis, Scalar _angle, MultiplyFrom _mult_from );
316 };
317 
318 
319 
320 
321 //=============================================================================
322 
323 
328 
329 
330 //=============================================================================
331 } // namespace ACG
332 //=============================================================================
333 #if defined(INCLUDE_TEMPLATES) && !defined(ACG_GLMATRIX_C)
334 #define ACG_GLMATRIX_TEMPLATES
335 #include "GLMatrixT.cc"
336 #endif
337 //=============================================================================
338 #endif // ACG_GLMATRIX_HH defined
339 //=============================================================================
340 
Matrix4x4T & operator*=(const Matrix4x4T< Scalar > &_rhs)
self *= _rhs
Definition: Matrix4x4T.cc:115
GLMatrixT< Scalar > & operator=(const GLMatrixT< otherScalar > &_rhs)
assignement from other matrix type
Definition: GLMatrixT.hh:149
bool isPerspective() const
check if the matrix is a perspective projection matrix
Definition: GLMatrixT.cc:470
Namespace providing different geometric functions concerning angles.
Definition: DBSCANT.cc:51
GLMatrixT< float > GLMatrixf
typedef
Definition: GLMatrixT.hh:325
MultiplyFrom
Definition: GLMatrixT.hh:79
void inverse_lookAt(const Vec3 &eye, const Vec3 &center, const Vec3 &up)
multiply self from left with inverse lookAt matrix
Definition: GLMatrixT.cc:253
VectorT< Scalar, 2 > extract_planes_perspective() const
extract near and far clipping planes from a perspective projection matrix
Definition: GLMatrixT.cc:444
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:300
GLMatrixT(const GLMatrixT< OtherScalar > &_rhs)
construct from other matrix type
Definition: GLMatrixT.hh:97
bool isOrtho() const
check if the matrix is an orthographic projection matrix
Definition: GLMatrixT.cc:529
VectorT< Scalar, 2 > extract_planes() const
detect type of projection matrix and extract near and far clipping planes
Definition: GLMatrixT.cc:582
GLMatrixT(const Scalar _array[16])
Definition: GLMatrixT.hh:103
void rotateZ(Scalar _angle, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
multiply self with a rotation matrix (angle in degree, z-axis)
Definition: GLMatrixT.hh:205
~GLMatrixT()
destructor
Definition: GLMatrixT.hh:144
GLMatrixT< double > GLMatrixd
typedef
Definition: GLMatrixT.hh:327
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:414
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:102
VectorT< Scalar, 2 > extract_planes_ortho() const
extract near and far clipping planes from an orthographic projection matrix
Definition: GLMatrixT.cc:457
GLMatrixT()
constructor: uninitialized values
Definition: GLMatrixT.hh:93
4x4 matrix implementing OpenGL commands.
Definition: GLMatrixT.hh:85
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:81
GLMatrixT(const Vec3 &col1, const Vec3 &col2, const Vec3 &col3)
Definition: GLMatrixT.hh:108
Matrix4x4T & leftMult(const Matrix4x4T< Scalar > &_rhs)
multiply from left: self = _rhs * self
Definition: Matrix4x4T.cc:143
void translate(const Vec3 &_v, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
multiply self with translation matrix (x,y,z)
Definition: GLMatrixT.hh:173
Matrix4x4T & operator+=(const Matrix4x4T< Scalar > &_rhs)
self += _rhs
Definition: Matrix4x4T.hh:182
void rotateX(Scalar _angle, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
multiply self with a rotation matrix (angle in degree, x-axis)
Definition: GLMatrixT.hh:193
void scale(const Vec3 &_v, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
multiply self with scaling matrix (x,y,z)
Definition: GLMatrixT.hh:163
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:348
void rotate(Scalar angle, Scalar x, Scalar y, Scalar z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
Definition: GLMatrixT.cc:161
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:317
void perspective(Scalar fovY, Scalar aspect, Scalar near_plane, Scalar far_plane)
multiply self with a perspective projection matrix
Definition: GLMatrixT.cc:283
void rotateY(Scalar _angle, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
multiply self with a rotation matrix (angle in degree, y-axis)
Definition: GLMatrixT.hh:199
4x4 matrix implementing OpenGL commands.
Definition: MeshNode2T.cc:85
Matrix4x4T operator*(const Matrix4x4T< Scalar > &inst) const
self * _rhs
Definition: Matrix4x4T.cc:85
void lookAt(const Vec3 &eye, const Vec3 &center, const Vec3 &up)
Definition: GLMatrixT.cc:222
Matrix4x4T< Scalar > & operator=(const Matrix4x4T< otherScalar > &_rhs)
assignment from other matrix type
Definition: Matrix4x4T.hh:123
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:379
void rotate(Scalar _angle, const Vec3 &_axis, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
Definition: GLMatrixT.hh:185
Matrix4x4T & operator-=(const Matrix4x4T< Scalar > &_rhs)
self -= _rhs
Definition: Matrix4x4T.hh:188
GLMatrixT< Scalar > & operator=(const Matrix4x4T< otherScalar > &_rhs)
assignement from other matrix type
Definition: GLMatrixT.hh:154