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 
45 
46 //=============================================================================
47 //
48 // CLASS GLMatrixT
49 //
50 //=============================================================================
51 
52 
53 #ifndef ACG_GLMATRIX_HH
54 #define ACG_GLMATRIX_HH
55 
56 
57 //== INCLUDES =================================================================
58 
59 
60 #include "Matrix4x4T.hh"
61 #include "../Config/ACGDefines.hh"
62 #include <cmath>
63 
64 
65 namespace ACG {
66 
67 
68 //== CLASS DEFINITION =========================================================
69 
70 
73 enum MultiplyFrom { MULT_FROM_RIGHT, MULT_FROM_LEFT };
74 
75 
76 
78 template <class Scalar>
79 class GLMatrixT : public Matrix4x4T<Scalar>
80 {
81 public:
82 
83  typedef VectorT<Scalar, 3> Vec3;
84 
85 
87  GLMatrixT() {}
88 
90  template <class OtherScalar>
91  inline GLMatrixT(const Matrix4x4T<OtherScalar>& _rhs)
92  : Matrix4x4T<Scalar>(_rhs)
93  {}
94 
96  inline GLMatrixT(const GLMatrixT<Scalar>& _rhs)
97  : Matrix4x4T<Scalar>(_rhs)
98  {}
99 
102  inline GLMatrixT(const Scalar _array[16]) : Matrix4x4T<Scalar>(_array) {}
103 
107  inline GLMatrixT(const Vec3 &col1, const Vec3 &col2, const Vec3 &col3) {
108  /*
109  * Don't try to optimize anything by hand, here. gcc -O2 does the right thing:
110  *
111  * mov %rax,-0x70(%rsp)
112  * mov %rdx,-0x68(%rsp)
113  * mov %rsi,-0x50(%rsp)
114  * lea -0x68(%rsp),%rdx
115  * mov %rax,0x8(%rsp)
116  * mov %rcx,-0x60(%rsp)
117  * lea 0x10(%rsp),%rsi
118  * movq $0x0,-0x58(%rsp)
119  * mov %rdi,-0x48(%rsp)
120  * xor %eax,%eax
121  * mov %r8,-0x40(%rsp)
122  * movq $0x0,-0x38(%rsp)
123  * mov %r9,-0x30(%rsp)
124  * mov %r10,-0x28(%rsp)
125  * mov %r11,-0x20(%rsp)
126  * movq $0x0,-0x18(%rsp)
127  * movq $0x0,-0x10(%rsp)
128  * movq $0x0,-0x8(%rsp)
129  * movq $0x0,(%rsp)
130  *
131  */
132  memcpy(this->mat_ + 0, col1.data(), sizeof(Scalar) * 3);
133  this->mat_[3] = 0;
134  memcpy(this->mat_ + 4, col2.data(), sizeof(Scalar) * 3);
135  this->mat_[7] = 0;
136  memcpy(this->mat_ + 8, col3.data(), sizeof(Scalar) * 3);
137  for (int i = 11; i < 15; ++i) this->mat_[i] = 0;
138  this->mat_[15] = 1;
139  }
140 
141 
144 
145 
147  template<typename otherScalar>
149  { Matrix4x4T<Scalar>::operator=(_rhs); return *this; }
150 
152  template<typename otherScalar>
154  { Matrix4x4T<Scalar>::operator=(_rhs); return *this; }
155 
156 
157 
159  inline void scale( Scalar _x, Scalar _y, Scalar _z,
160  MultiplyFrom _mult_from = MULT_FROM_RIGHT );
162  inline void scale( const Vec3& _v,
163  MultiplyFrom _mult_from = MULT_FROM_RIGHT ) {
164  scale(_v[0], _v[1], _v[2], _mult_from);
165  }
166 
167 
169  inline void translate( Scalar _x, Scalar _y, Scalar _z,
170  MultiplyFrom _mult_from = MULT_FROM_RIGHT );
172  inline void translate( const Vec3& _v,
173  MultiplyFrom _mult_from = MULT_FROM_RIGHT ) {
174  translate(_v[0], _v[1], _v[2], _mult_from);
175  }
176 
177 
180  void rotate( Scalar angle, Scalar x, Scalar y, Scalar z,
181  MultiplyFrom _mult_from = MULT_FROM_RIGHT );
184  void rotate( Scalar _angle, const Vec3& _axis,
185  MultiplyFrom _mult_from = MULT_FROM_RIGHT ) {
186  rotate(_angle, _axis[0], _axis[1], _axis[2], _mult_from);
187  }
188 
189 
190 
192  inline void rotateX( Scalar _angle,
193  MultiplyFrom _mult_from = MULT_FROM_RIGHT ) {
194  rotateXYZ( X, _angle, _mult_from );
195  }
196 
198  inline void rotateY( Scalar _angle,
199  MultiplyFrom _mult_from = MULT_FROM_RIGHT ) {
200  rotateXYZ( Y, _angle, _mult_from );
201  }
202 
204  inline void rotateZ( Scalar _angle,
205  MultiplyFrom _mult_from = MULT_FROM_RIGHT ) {
206  rotateXYZ( Z, _angle, _mult_from );
207  }
208 
209 
210 
211 
215  void lookAt(const Vec3& eye,
216  const Vec3& center,
217  const Vec3& up);
218 
220  void inverse_lookAt(const Vec3& eye,
221  const Vec3& center,
222  const Vec3& up);
223 
224 
235  void perspective(Scalar fovY, Scalar aspect,
236  Scalar near_plane, Scalar far_plane);
237 
239  void inverse_perspective(Scalar fovY, Scalar aspect,
240  Scalar near_plane,Scalar far_plane);
241 
243  void frustum(Scalar left, Scalar right,
244  Scalar bottom, Scalar top,
245  Scalar near_plane, Scalar far_plane);
246 
248  void inverse_frustum(Scalar left,Scalar right,
249  Scalar bottom, Scalar top,
250  Scalar near_plane, Scalar far_plane);
251 
253  void ortho(Scalar left, Scalar right,
254  Scalar bottom, Scalar top,
255  Scalar near_plane, Scalar far_plane);
256 
258  void inverse_ortho(Scalar left, Scalar right,
259  Scalar bottom, Scalar top,
260  Scalar near_plane, Scalar far_plane);
261 
262 
263 
266 
269 
271  bool isPerspective() const;
272 
274  bool isOrtho() const;
275 
278 
279  //----------------------------------------------------- overloaded operators
280 
281  GLMatrixT& operator+= ( const Matrix4x4T<Scalar>& _rhs) {
282  Matrix4x4T<Scalar>::operator+=(_rhs); return *this;
283  }
284  GLMatrixT& operator-= ( const Matrix4x4T<Scalar>& _rhs) {
285  Matrix4x4T<Scalar>::operator-=(_rhs); return *this;
286  }
287  GLMatrixT& operator*= ( const Matrix4x4T<Scalar>& _rhs) {
288  Matrix4x4T<Scalar>::operator*=(_rhs); return *this;
289  }
290  GLMatrixT& leftMult(const Matrix4x4T<Scalar>& _rhs) {
291  Matrix4x4T<Scalar>::leftMult(_rhs); return *this;
292  }
293 
294  GLMatrixT operator+ (const Matrix4x4T<Scalar>& _rhs) const {
295  return GLMatrixT<Scalar>(*this) += _rhs;
296  }
297  GLMatrixT operator- (const Matrix4x4T<Scalar>& _rhs) const {
298  return GLMatrixT<Scalar>(*this) -= _rhs;
299  }
300  GLMatrixT operator*(const Matrix4x4T<Scalar>& _rhs) const {
301  return GLMatrixT<Scalar>(*this) *= _rhs;
302  }
303 
304  template <typename T>
305  inline VectorT<T,4> operator*(const VectorT<T,4>& _v) const {
307  }
308 
309 
310 
311 private:
312 
313  enum Axis { X, Y, Z };
314  void rotateXYZ( Axis _axis, Scalar _angle, MultiplyFrom _mult_from );
315 };
316 
317 
318 
319 
320 //=============================================================================
321 
322 
327 
328 
329 //=============================================================================
330 } // namespace ACG
331 //=============================================================================
332 #if defined(INCLUDE_TEMPLATES) && !defined(ACG_GLMATRIX_C)
333 #define ACG_GLMATRIX_TEMPLATES
334 #include "GLMatrixT_impl.hh"
335 #endif
336 //=============================================================================
337 #endif // ACG_GLMATRIX_HH defined
338 //=============================================================================
339 
void rotate(Scalar angle, Scalar x, Scalar y, Scalar z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
void perspective(Scalar fovY, Scalar aspect, Scalar near_plane, Scalar far_plane)
multiply self with a perspective projection matrix
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
void scale(const Vec3 &_v, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
multiply self with scaling matrix (x,y,z)
Definition: GLMatrixT.hh:162
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
GLMatrixT< Scalar > & operator=(const GLMatrixT< otherScalar > &_rhs)
assignement from other matrix type
Definition: GLMatrixT.hh:148
void rotateX(Scalar _angle, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
multiply self with a rotation matrix (angle in degree, x-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:172
Matrix4x4T & operator+=(const Matrix4x4T< Scalar > &_rhs)
self += _rhs
Definition: Matrix4x4T.hh:176
void ortho(Scalar left, Scalar right, Scalar bottom, Scalar top, Scalar near_plane, Scalar far_plane)
multiply self with orthographic projection matrix
Namespace providing different geometric functions concerning angles.
GLMatrixT< Scalar > & operator=(const Matrix4x4T< otherScalar > &_rhs)
assignement from other matrix type
Definition: GLMatrixT.hh:153
GLMatrixT(const Vec3 &col1, const Vec3 &col2, const Vec3 &col3)
Definition: GLMatrixT.hh:107
void translate(Scalar _x, Scalar _y, Scalar _z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
multiply self with translation matrix (x,y,z)
void rotateZ(Scalar _angle, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
multiply self with a rotation matrix (angle in degree, z-axis)
Definition: GLMatrixT.hh:204
GLMatrixT(const Matrix4x4T< OtherScalar > &_rhs)
construct from other matrix type
Definition: GLMatrixT.hh:91
void lookAt(const Vec3 &eye, const Vec3 &center, const Vec3 &up)
GLMatrixT(const Scalar _array[16])
Definition: GLMatrixT.hh:102
bool isOrtho() const
check if the matrix is an orthographic projection matrix
Scalar * data()
access to Scalar array
Definition: Vector11T.hh:200
void rotate(Scalar _angle, const Vec3 &_axis, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
Definition: GLMatrixT.hh:184
VectorT< Scalar, 2 > extract_planes_ortho() const
extract near and far clipping planes from an orthographic projection matrix
void rotateY(Scalar _angle, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
multiply self with a rotation matrix (angle in degree, y-axis)
Definition: GLMatrixT.hh:198
VectorT< Scalar, 2 > extract_planes_perspective() const
extract near and far clipping planes from a perspective projection matrix
GLMatrixT< double > GLMatrixd
typedef
Definition: GLMatrixT.hh:326
4x4 matrix implementing OpenGL commands.
Definition: GLMatrixT.hh:79
void scale(Scalar _x, Scalar _y, Scalar _z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
multiply self with scaling matrix (x,y,z)
bool isPerspective() const
check if the matrix is a perspective projection matrix
Matrix4x4T & operator-=(const Matrix4x4T< Scalar > &_rhs)
self -= _rhs
Definition: Matrix4x4T.hh:182
Matrix4x4T & operator*=(const Matrix4x4T< Scalar > &_rhs)
self *= _rhs
GLMatrixT< float > GLMatrixf
typedef
Definition: GLMatrixT.hh:324
void inverse_perspective(Scalar fovY, Scalar aspect, Scalar near_plane, Scalar far_plane)
multiply self from left with inverse of perspective projection matrix
Matrix4x4T< Scalar > & operator=(const Matrix4x4T< otherScalar > &_rhs)
assignment from other matrix type
Definition: Matrix4x4T.hh:117
void frustum(Scalar left, Scalar right, Scalar bottom, Scalar top, Scalar near_plane, Scalar far_plane)
multiply self with a perspective projection matrix
Matrix4x4T operator*(const Matrix4x4T< Scalar > &inst) const
self * _rhs
void inverse_lookAt(const Vec3 &eye, const Vec3 &center, const Vec3 &up)
multiply self from left with inverse lookAt matrix
GLMatrixT()
constructor: uninitialized values
Definition: GLMatrixT.hh:87
Matrix4x4T & leftMult(const Matrix4x4T< Scalar > &_rhs)
multiply from left: self = _rhs * self
GLMatrixT(const GLMatrixT< Scalar > &_rhs)
copy constructor
Definition: GLMatrixT.hh:96
~GLMatrixT()
destructor
Definition: GLMatrixT.hh:143
MultiplyFrom
Definition: GLMatrixT.hh:73
VectorT< Scalar, 2 > extract_planes() const
detect type of projection matrix and extract near and far clipping planes