Developer Documentation
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 Matrix4x4T<OtherScalar>& _rhs)
98  : Matrix4x4T<Scalar>(_rhs)
99  {}
100 
102  inline GLMatrixT(const GLMatrixT<Scalar>& _rhs)
103  : Matrix4x4T<Scalar>(_rhs)
104  {}
105 
108  inline GLMatrixT(const Scalar _array[16]) : Matrix4x4T<Scalar>(_array) {}
109 
113  inline GLMatrixT(const Vec3 &col1, const Vec3 &col2, const Vec3 &col3) {
114  /*
115  * Don't try to optimize anything by hand, here. gcc -O2 does the right thing:
116  *
117  * mov %rax,-0x70(%rsp)
118  * mov %rdx,-0x68(%rsp)
119  * mov %rsi,-0x50(%rsp)
120  * lea -0x68(%rsp),%rdx
121  * mov %rax,0x8(%rsp)
122  * mov %rcx,-0x60(%rsp)
123  * lea 0x10(%rsp),%rsi
124  * movq $0x0,-0x58(%rsp)
125  * mov %rdi,-0x48(%rsp)
126  * xor %eax,%eax
127  * mov %r8,-0x40(%rsp)
128  * movq $0x0,-0x38(%rsp)
129  * mov %r9,-0x30(%rsp)
130  * mov %r10,-0x28(%rsp)
131  * mov %r11,-0x20(%rsp)
132  * movq $0x0,-0x18(%rsp)
133  * movq $0x0,-0x10(%rsp)
134  * movq $0x0,-0x8(%rsp)
135  * movq $0x0,(%rsp)
136  *
137  */
138  memcpy(this->mat_ + 0, col1.data(), sizeof(Scalar) * 3);
139  this->mat_[3] = 0;
140  memcpy(this->mat_ + 4, col2.data(), sizeof(Scalar) * 3);
141  this->mat_[7] = 0;
142  memcpy(this->mat_ + 8, col3.data(), sizeof(Scalar) * 3);
143  for (int i = 11; i < 15; ++i) this->mat_[i] = 0;
144  this->mat_[15] = 1;
145  }
146 
147 
150 
151 
153  template<typename otherScalar>
155  { Matrix4x4T<Scalar>::operator=(_rhs); return *this; }
156 
158  template<typename otherScalar>
160  { Matrix4x4T<Scalar>::operator=(_rhs); return *this; }
161 
162 
163 
165  inline void scale( Scalar _x, Scalar _y, Scalar _z,
166  MultiplyFrom _mult_from = MULT_FROM_RIGHT );
168  inline void scale( const Vec3& _v,
169  MultiplyFrom _mult_from = MULT_FROM_RIGHT ) {
170  scale(_v[0], _v[1], _v[2], _mult_from);
171  }
172 
173 
175  inline void translate( Scalar _x, Scalar _y, Scalar _z,
176  MultiplyFrom _mult_from = MULT_FROM_RIGHT );
178  inline void translate( const Vec3& _v,
179  MultiplyFrom _mult_from = MULT_FROM_RIGHT ) {
180  translate(_v[0], _v[1], _v[2], _mult_from);
181  }
182 
183 
186  void rotate( Scalar angle, Scalar x, Scalar y, Scalar z,
187  MultiplyFrom _mult_from = MULT_FROM_RIGHT );
190  void rotate( Scalar _angle, const Vec3& _axis,
191  MultiplyFrom _mult_from = MULT_FROM_RIGHT ) {
192  rotate(_angle, _axis[0], _axis[1], _axis[2], _mult_from);
193  }
194 
195 
196 
198  inline void rotateX( Scalar _angle,
199  MultiplyFrom _mult_from = MULT_FROM_RIGHT ) {
200  rotateXYZ( X, _angle, _mult_from );
201  }
202 
204  inline void rotateY( Scalar _angle,
205  MultiplyFrom _mult_from = MULT_FROM_RIGHT ) {
206  rotateXYZ( Y, _angle, _mult_from );
207  }
208 
210  inline void rotateZ( Scalar _angle,
211  MultiplyFrom _mult_from = MULT_FROM_RIGHT ) {
212  rotateXYZ( Z, _angle, _mult_from );
213  }
214 
215 
216 
217 
221  void lookAt(const Vec3& eye,
222  const Vec3& center,
223  const Vec3& up);
224 
226  void inverse_lookAt(const Vec3& eye,
227  const Vec3& center,
228  const Vec3& up);
229 
230 
241  void perspective(Scalar fovY, Scalar aspect,
242  Scalar near_plane, Scalar far_plane);
243 
245  void inverse_perspective(Scalar fovY, Scalar aspect,
246  Scalar near_plane,Scalar far_plane);
247 
249  void frustum(Scalar left, Scalar right,
250  Scalar bottom, Scalar top,
251  Scalar near_plane, Scalar far_plane);
252 
254  void inverse_frustum(Scalar left,Scalar right,
255  Scalar bottom, Scalar top,
256  Scalar near_plane, Scalar far_plane);
257 
259  void ortho(Scalar left, Scalar right,
260  Scalar bottom, Scalar top,
261  Scalar near_plane, Scalar far_plane);
262 
264  void inverse_ortho(Scalar left, Scalar right,
265  Scalar bottom, Scalar top,
266  Scalar near_plane, Scalar far_plane);
267 
268 
269 
272 
275 
277  bool isPerspective() const;
278 
280  bool isOrtho() const;
281 
284 
285  //----------------------------------------------------- overloaded operators
286 
287  GLMatrixT& operator+= ( const Matrix4x4T<Scalar>& _rhs) {
288  Matrix4x4T<Scalar>::operator+=(_rhs); return *this;
289  }
290  GLMatrixT& operator-= ( const Matrix4x4T<Scalar>& _rhs) {
291  Matrix4x4T<Scalar>::operator-=(_rhs); return *this;
292  }
293  GLMatrixT& operator*= ( const Matrix4x4T<Scalar>& _rhs) {
294  Matrix4x4T<Scalar>::operator*=(_rhs); return *this;
295  }
296  GLMatrixT& leftMult(const Matrix4x4T<Scalar>& _rhs) {
297  Matrix4x4T<Scalar>::leftMult(_rhs); return *this;
298  }
299 
300  GLMatrixT operator+ (const Matrix4x4T<Scalar>& _rhs) const {
301  return GLMatrixT<Scalar>(*this) += _rhs;
302  }
303  GLMatrixT operator- (const Matrix4x4T<Scalar>& _rhs) const {
304  return GLMatrixT<Scalar>(*this) -= _rhs;
305  }
306  GLMatrixT operator*(const Matrix4x4T<Scalar>& _rhs) const {
307  return GLMatrixT<Scalar>(*this) *= _rhs;
308  }
309 
310  template <typename T>
311  inline VectorT<T,4> operator*(const VectorT<T,4>& _v) const {
313  }
314 
315 
316 
317 private:
318 
319  enum Axis { X, Y, Z };
320  void rotateXYZ( Axis _axis, Scalar _angle, MultiplyFrom _mult_from );
321 };
322 
323 
324 
325 
326 //=============================================================================
327 
328 
333 
334 
335 //=============================================================================
336 } // namespace ACG
337 //=============================================================================
338 #if defined(INCLUDE_TEMPLATES) && !defined(ACG_GLMATRIX_C)
339 #define ACG_GLMATRIX_TEMPLATES
340 #include "GLMatrixT.cc"
341 #endif
342 //=============================================================================
343 #endif // ACG_GLMATRIX_HH defined
344 //=============================================================================
345 
void inverse_lookAt(const Vec3 &eye, const Vec3 &center, const Vec3 &up)
multiply self from left with inverse lookAt matrix
Definition: GLMatrixT.cc:253
GLMatrixT(const Matrix4x4T< OtherScalar > &_rhs)
construct from other matrix type
Definition: GLMatrixT.hh:97
GLMatrixT(const GLMatrixT< Scalar > &_rhs)
copy constructor
Definition: GLMatrixT.hh:102
GLMatrixT< float > GLMatrixf
typedef
Definition: GLMatrixT.hh:330
void rotate(Scalar _angle, const Vec3 &_axis, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
Definition: GLMatrixT.hh:190
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
~GLMatrixT()
destructor
Definition: GLMatrixT.hh:149
void rotateX(Scalar _angle, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
multiply self with a rotation matrix (angle in degree, x-axis)
Definition: GLMatrixT.hh:198
Matrix4x4T & operator*=(const Matrix4x4T< Scalar > &_rhs)
self *= _rhs
Definition: Matrix4x4T.cc:115
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 scale(const Vec3 &_v, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
multiply self with scaling matrix (x,y,z)
Definition: GLMatrixT.hh:168
VectorT< Scalar, 2 > extract_planes() const
detect type of projection matrix and extract near and far clipping planes
Definition: GLMatrixT.cc:582
Matrix4x4T & operator+=(const Matrix4x4T< Scalar > &_rhs)
self += _rhs
Definition: Matrix4x4T.hh:182
GLMatrixT(const Scalar _array[16])
Definition: GLMatrixT.hh:108
MultiplyFrom
Definition: GLMatrixT.hh:79
void rotate(Scalar angle, Scalar x, Scalar y, Scalar z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
Definition: GLMatrixT.cc:161
void translate(const Vec3 &_v, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
multiply self with translation matrix (x,y,z)
Definition: GLMatrixT.hh:178
void rotateY(Scalar _angle, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
multiply self with a rotation matrix (angle in degree, y-axis)
Definition: GLMatrixT.hh:204
GLMatrixT< double > GLMatrixd
typedef
Definition: GLMatrixT.hh:332
GLMatrixT< Scalar > & operator=(const GLMatrixT< otherScalar > &_rhs)
assignement from other matrix type
Definition: GLMatrixT.hh:154
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
VectorT< Scalar, 2 > extract_planes_perspective() const
extract near and far clipping planes from a perspective projection matrix
Definition: GLMatrixT.cc:444
GLMatrixT(const Vec3 &col1, const Vec3 &col2, const Vec3 &col3)
Definition: GLMatrixT.hh:113
4x4 matrix implementing OpenGL commands.
Definition: GLMatrixT.hh:85
Matrix4x4T & leftMult(const Matrix4x4T< Scalar > &_rhs)
multiply from left: self = _rhs * self
Definition: Matrix4x4T.cc:143
void perspective(Scalar fovY, Scalar aspect, Scalar near_plane, Scalar far_plane)
multiply self with a perspective projection matrix
Definition: GLMatrixT.cc:283
Matrix4x4T & operator-=(const Matrix4x4T< Scalar > &_rhs)
self -= _rhs
Definition: Matrix4x4T.hh:188
VectorT< Scalar, 2 > extract_planes_ortho() const
extract near and far clipping planes from an orthographic projection matrix
Definition: GLMatrixT.cc:457
Namespace providing different geometric functions concerning angles.
Definition: DBSCANT.cc:51
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
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
GLMatrixT()
constructor: uninitialized values
Definition: GLMatrixT.hh:93
bool isOrtho() const
check if the matrix is an orthographic projection matrix
Definition: GLMatrixT.cc:529
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
Matrix4x4T< Scalar > & operator=(const Matrix4x4T< otherScalar > &_rhs)
assignment from other matrix type
Definition: Matrix4x4T.hh:123
void lookAt(const Vec3 &eye, const Vec3 &center, const Vec3 &up)
Definition: GLMatrixT.cc:222
Matrix4x4T operator*(const Matrix4x4T< Scalar > &inst) const
self * _rhs
Definition: Matrix4x4T.cc:85
GLMatrixT< Scalar > & operator=(const Matrix4x4T< otherScalar > &_rhs)
assignement from other matrix type
Definition: GLMatrixT.hh:159
bool isPerspective() const
check if the matrix is a perspective projection matrix
Definition: GLMatrixT.cc:470
void rotateZ(Scalar _angle, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
multiply self with a rotation matrix (angle in degree, z-axis)
Definition: GLMatrixT.hh:210
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
4x4 matrix implementing OpenGL commands.
Definition: MeshNode2T.cc:85
Scalar * data()
access to Scalar array
Definition: Vector11T.hh:193