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) = default;
97 
100  inline GLMatrixT(const Scalar _array[16]) : Matrix4x4T<Scalar>(_array) {}
101 
105  inline GLMatrixT(const Vec3 &col1, const Vec3 &col2, const Vec3 &col3) {
106  /*
107  * Don't try to optimize anything by hand, here. gcc -O2 does the right thing:
108  *
109  * mov %rax,-0x70(%rsp)
110  * mov %rdx,-0x68(%rsp)
111  * mov %rsi,-0x50(%rsp)
112  * lea -0x68(%rsp),%rdx
113  * mov %rax,0x8(%rsp)
114  * mov %rcx,-0x60(%rsp)
115  * lea 0x10(%rsp),%rsi
116  * movq $0x0,-0x58(%rsp)
117  * mov %rdi,-0x48(%rsp)
118  * xor %eax,%eax
119  * mov %r8,-0x40(%rsp)
120  * movq $0x0,-0x38(%rsp)
121  * mov %r9,-0x30(%rsp)
122  * mov %r10,-0x28(%rsp)
123  * mov %r11,-0x20(%rsp)
124  * movq $0x0,-0x18(%rsp)
125  * movq $0x0,-0x10(%rsp)
126  * movq $0x0,-0x8(%rsp)
127  * movq $0x0,(%rsp)
128  *
129  */
130  memcpy(this->mat_ + 0, col1.data(), sizeof(Scalar) * 3);
131  this->mat_[3] = 0;
132  memcpy(this->mat_ + 4, col2.data(), sizeof(Scalar) * 3);
133  this->mat_[7] = 0;
134  memcpy(this->mat_ + 8, col3.data(), sizeof(Scalar) * 3);
135  for (int i = 11; i < 15; ++i) this->mat_[i] = 0;
136  this->mat_[15] = 1;
137  }
138 
139 
142 
143 
145  template<typename otherScalar>
147  { Matrix4x4T<Scalar>::operator=(_rhs); return *this; }
148 
150  template<typename otherScalar>
152  { Matrix4x4T<Scalar>::operator=(_rhs); return *this; }
153 
154 
155 
157  inline void scale( Scalar _x, Scalar _y, Scalar _z,
158  MultiplyFrom _mult_from = MULT_FROM_RIGHT );
160  inline void scale( const Vec3& _v,
161  MultiplyFrom _mult_from = MULT_FROM_RIGHT ) {
162  scale(_v[0], _v[1], _v[2], _mult_from);
163  }
164 
165 
167  inline void translate( Scalar _x, Scalar _y, Scalar _z,
168  MultiplyFrom _mult_from = MULT_FROM_RIGHT );
170  inline void translate( const Vec3& _v,
171  MultiplyFrom _mult_from = MULT_FROM_RIGHT ) {
172  translate(_v[0], _v[1], _v[2], _mult_from);
173  }
174 
175 
178  void rotate( Scalar angle, Scalar x, Scalar y, Scalar z,
179  MultiplyFrom _mult_from = MULT_FROM_RIGHT );
182  void rotate( Scalar _angle, const Vec3& _axis,
183  MultiplyFrom _mult_from = MULT_FROM_RIGHT ) {
184  rotate(_angle, _axis[0], _axis[1], _axis[2], _mult_from);
185  }
186 
187 
188 
190  inline void rotateX( Scalar _angle,
191  MultiplyFrom _mult_from = MULT_FROM_RIGHT ) {
192  rotateXYZ( X, _angle, _mult_from );
193  }
194 
196  inline void rotateY( Scalar _angle,
197  MultiplyFrom _mult_from = MULT_FROM_RIGHT ) {
198  rotateXYZ( Y, _angle, _mult_from );
199  }
200 
202  inline void rotateZ( Scalar _angle,
203  MultiplyFrom _mult_from = MULT_FROM_RIGHT ) {
204  rotateXYZ( Z, _angle, _mult_from );
205  }
206 
207 
208 
209 
213  void lookAt(const Vec3& eye,
214  const Vec3& center,
215  const Vec3& up);
216 
218  void inverse_lookAt(const Vec3& eye,
219  const Vec3& center,
220  const Vec3& up);
221 
222 
233  void perspective(Scalar fovY, Scalar aspect,
234  Scalar near_plane, Scalar far_plane);
235 
237  void inverse_perspective(Scalar fovY, Scalar aspect,
238  Scalar near_plane,Scalar far_plane);
239 
241  void frustum(Scalar left, Scalar right,
242  Scalar bottom, Scalar top,
243  Scalar near_plane, Scalar far_plane);
244 
246  void inverse_frustum(Scalar left,Scalar right,
247  Scalar bottom, Scalar top,
248  Scalar near_plane, Scalar far_plane);
249 
251  void ortho(Scalar left, Scalar right,
252  Scalar bottom, Scalar top,
253  Scalar near_plane, Scalar far_plane);
254 
256  void inverse_ortho(Scalar left, Scalar right,
257  Scalar bottom, Scalar top,
258  Scalar near_plane, Scalar far_plane);
259 
260 
261 
264 
267 
269  bool isPerspective() const;
270 
272  bool isOrtho() const;
273 
276 
277  //----------------------------------------------------- overloaded operators
278 
279  GLMatrixT& operator+= ( const Matrix4x4T<Scalar>& _rhs) {
280  Matrix4x4T<Scalar>::operator+=(_rhs); return *this;
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& leftMult(const Matrix4x4T<Scalar>& _rhs) {
289  Matrix4x4T<Scalar>::leftMult(_rhs); return *this;
290  }
291 
292  GLMatrixT operator+ (const Matrix4x4T<Scalar>& _rhs) const {
293  return GLMatrixT<Scalar>(*this) += _rhs;
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 
302  template <typename T>
303  inline VectorT<T,4> operator*(const VectorT<T,4>& _v) const {
305  }
306 
307 
308 
309 private:
310 
311  enum Axis { X, Y, Z };
312  void rotateXYZ( Axis _axis, Scalar _angle, MultiplyFrom _mult_from );
313 };
314 
315 
316 
317 
318 //=============================================================================
319 
320 
325 
326 
327 //=============================================================================
328 } // namespace ACG
329 //=============================================================================
330 #if defined(INCLUDE_TEMPLATES) && !defined(ACG_GLMATRIX_C)
331 #define ACG_GLMATRIX_TEMPLATES
332 #include "GLMatrixT_impl.hh"
333 #endif
334 //=============================================================================
335 #endif // ACG_GLMATRIX_HH defined
336 //=============================================================================
337 
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:160
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:146
void rotateX(Scalar _angle, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
multiply self with a rotation matrix (angle in degree, x-axis)
Definition: GLMatrixT.hh:190
void translate(const Vec3 &_v, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
multiply self with translation matrix (x,y,z)
Definition: GLMatrixT.hh:170
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:151
GLMatrixT(const Vec3 &col1, const Vec3 &col2, const Vec3 &col3)
Definition: GLMatrixT.hh:105
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:202
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:100
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:182
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:196
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:324
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:322
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()
destructor
Definition: GLMatrixT.hh:141
MultiplyFrom
Definition: GLMatrixT.hh:73
VectorT< Scalar, 2 > extract_planes() const
detect type of projection matrix and extract near and far clipping planes