NGL  6.5
The NCCA Graphics Library
Transformation.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 Jon Macey
3 
4  This program is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17 #include "ShaderLib.h"
18 #include "Transformation.h"
19 //----------------------------------------------------------------------------------------------------------------------
22 //----------------------------------------------------------------------------------------------------------------------
23 namespace ngl
24 {
25 
26 // Constructor -------------------------------------------------------------------------------------------------------------------
28 {
29  m_position = Vec3(0.0f,0.0f,0.0f);
30  m_scale = Vec3(1.0f,1.0f,1.0f);
31  m_rotation = Vec3(0.0f,0.0f,0.0f);
32  m_isMatrixComputed = false;
33  m_matrix=1.0f;
34  m_transposeMatrix=1.0f;
35  m_inverseMatrix=1.0f;
37 
38 }
39 
41 {
42  //m_isMatrixComputed=false;
43 
44  this->m_position=_t.m_position;
45  this->m_scale = _t.m_scale;
46  this->m_rotation = _t.m_rotation;
47  this->m_isMatrixComputed = true;
48  this->m_matrix=_t.m_matrix;
49  this->m_transposeMatrix=_t.m_transposeMatrix;
50  this->m_inverseMatrix=_t.m_inverseMatrix;
51 }
52 
54 {
55  //m_isMatrixComputed=false;
56 
57  this->m_position=_t.m_position;
58  this->m_scale = _t.m_scale;
59  this->m_rotation = _t.m_rotation;
60  this->m_isMatrixComputed = true;
61  this->m_matrix=_t.m_matrix;
62  this->m_transposeMatrix=_t.m_transposeMatrix;
63  this->m_inverseMatrix=_t.m_inverseMatrix;
64  return *this;
65 }
66 
67 
68 
69 
70 void Transformation::setMatrix( const Mat4 &_m ) noexcept
71 {
72  m_matrix=_m;
75  m_isMatrixComputed = true;
76 }
77 
78 // Set scale ---------------------------------------------------------------------------------------------------------------------
79 void Transformation::setScale( const Vec3& _scale ) noexcept
80 {
81  m_scale = _scale;
82  m_isMatrixComputed = false;
83 }
84 
85 void Transformation::setScale( const Vec4& _scale ) noexcept
86 {
87  m_scale = _scale;
88  m_isMatrixComputed = false;
89 }
90 
91 void Transformation::setScale(Real _x, Real _y, Real _z ) noexcept
92 {
93  m_scale.set(_x,_y,_z);
94  m_isMatrixComputed = false;
95 }
96 
97 // add scale ---------------------------------------------------------------------------------------------------------------------
98 void Transformation::addScale( const Vec3& _scale ) noexcept
99 {
100  m_scale += _scale;
101  m_isMatrixComputed = false;
102 }
103 
104 
105 void Transformation::addScale(Real _x, Real _y, Real _z ) noexcept
106 {
107  m_scale.m_x+=_x;
108  m_scale.m_y+=_y;
109  m_scale.m_z+=_z;
110 
111  m_isMatrixComputed = false;
112 }
113 
114 // Set position --------------------------------------------------------------------------------------------------------------------
115 void Transformation::setPosition(const Vec4 &_position ) noexcept
116 {
117  m_position = _position;
118  m_isMatrixComputed = false;
119 }
120 void Transformation::setPosition(const Vec3 &_position) noexcept
121 {
122  m_position = _position;
123  m_isMatrixComputed = false;
124 }
125 void Transformation::setPosition(Real _x, Real _y, Real _z ) noexcept
126 {
127  m_position.set(_x,_y,_z);
128  m_isMatrixComputed = false;
129 }
130 
131 // Set position --------------------------------------------------------------------------------------------------------------------
132 void Transformation::addPosition( const Vec3& _position) noexcept
133 {
134  m_position+= _position;
135  m_isMatrixComputed = false;
136 }
137 void Transformation::addPosition( Real _x, Real _y, Real _z ) noexcept
138 {
139  m_position.m_x+=_x;
140  m_position.m_y+=_y;
141  m_position.m_z+=_z;
142 
143  m_isMatrixComputed = false;
144 }
145 
146 
147 // set rotation -------------------------------------------------------------------------------------------------------------------
148 void Transformation::setRotation( const Vec3 &_rotation ) noexcept
149 {
150  m_rotation = _rotation;
151  m_isMatrixComputed = false;
152 }
153 void Transformation::setRotation( const Vec4 &_rotation ) noexcept
154 {
155  m_rotation = _rotation;
156  m_isMatrixComputed = false;
157 }
158 
159 
160 void Transformation::setRotation(Real _x, Real _y, Real _z ) noexcept
161 {
162  m_rotation.set(_x,_y,_z);
163 
164  m_isMatrixComputed = false;
165 }
166 
167 
168 // set rotation -------------------------------------------------------------------------------------------------------------------
169 void Transformation::addRotation(const Vec3 &_rotation ) noexcept
170 {
171  m_rotation+= _rotation;
172  m_isMatrixComputed = false;
173 }
174 void Transformation::addRotation(Real _x, Real _y, Real _z) noexcept
175 {
176  m_rotation.m_x+=_x;
177  m_rotation.m_y+=_y;
178  m_rotation.m_z+=_z;
179  m_isMatrixComputed = false;
180 }
181 
182 
183 // reset matrix ---------------------------------------------------------------------------------------------------------------------
184 void Transformation::reset() noexcept
185 {
186  m_position = Vec3(0.0f,0.0f,0.0f);
187  m_scale = Vec3(1.0f,1.0f,1.0f);
188  m_rotation = Vec3(0.0f,0.0f,0.0f);
189  m_isMatrixComputed = false;
190  computeMatrices();
191 }
192 
193 // comptue matrix ---------------------------------------------------------------------------------------------------------------------
195 {
196  if (!m_isMatrixComputed) // need to recalculate
197  {
198  Mat4 scale;
199  Mat4 rX;
200  Mat4 rY;
201  Mat4 rZ;
202  Mat4 trans;
203 
204  // rotation/scale matrix
205  Mat4 rotationScale;
207 
208  rX.rotateX(m_rotation.m_x);
209  rY.rotateY(m_rotation.m_y);
210  rZ.rotateZ(m_rotation.m_z);
211  rotationScale = scale * rX * rY * rZ;
212 
213  // transform matrix
214  m_matrix = rotationScale;
215  m_matrix.m_m[3][0] = m_position.m_x;
216  m_matrix.m_m[3][1] = m_position.m_y;
217  m_matrix.m_m[3][2] = m_position.m_z;
218  m_matrix.m_m[3][3] = 1;
219 
220 
221 
222  // tranpose matrix
223  m_transposeMatrix = rotationScale;
228  m_transposeMatrix.m_m[3][3] = 1;
229 
230  // inverse matrix
232  scale.scale(1.0f / m_scale.m_x, 1.0f / m_scale.m_y, 1.0f / m_scale.m_z);
233  rX.rotateX(-m_rotation.m_x);
234  rY.rotateY(-m_rotation.m_y);
235  rZ.rotateZ(-m_rotation.m_z);
236  m_inverseMatrix = trans * rZ * rY * rX * scale ;
237 
238  m_isMatrixComputed = true;
239  }
240 }
241 
243 {
244  m_isMatrixComputed=false;
245 
246  computeMatrices();
247  m_matrix*=_m.m_matrix;
248 
250  m_transposeMatrix*=_m.m_transposeMatrix;
251 
253  m_inverseMatrix*=_m.m_inverseMatrix;
254 }
255 
257 {
258  m_isMatrixComputed=false;
259  computeMatrices();
261  t.m_matrix=m_matrix*_m.m_matrix;
262  t.m_transposeMatrix=m_transposeMatrix*_m.m_transposeMatrix;
263  t.m_inverseMatrix=m_inverseMatrix*_m.m_inverseMatrix;
264 
265  return t;
266 }
267 void Transformation::loadMatrixToShader(const std::string &_param, const ActiveMatrix &_which) noexcept
268 {
269  computeMatrices();
271  switch (_which)
272  {
273  case ActiveMatrix::NORMAL :
274  {
275  shader->setShaderParamFromMat4(_param,m_matrix);
276  }
277  break;
279  {
281  }
282  break;
283  case ActiveMatrix::INVERSE :
284  {
285  shader->setShaderParamFromMat4(_param,m_inverseMatrix);
286  }
287  break;
288 
289  }
290 
291 
292 }
293 
295 {
296  computeMatrices();
298  switch (_which)
299  {
300  case ActiveMatrix::NORMAL :
301  {
302  Mat4 tx=_global.getMatrix()*this->getMatrix();
303  shader->setShaderParamFromMat4(_param,tx);
304  }
305  break;
307  {
308  Mat4 tx=_global.getTransposeMatrix()*this->getTransposeMatrix();
309 
310  shader->setShaderParamFromMat4(_param,tx);
311  }
312  break;
313  case ActiveMatrix::INVERSE :
314  {
315  Mat4 tx=_global.getInverseMatrix()*this->getInverseMatrix();
316  shader->setShaderParamFromMat4(_param,tx);
317  }
318  break;
319 
320  }
321 
322 }
323 
324 
325 } // end ngl
void loadMatrixToShader(const std::string &_param, const ActiveMatrix &_which=ActiveMatrix::NORMAL) noexcept
load the current transform matrix to the shader
GLenum GLenum GLenum GLenum GLenum scale
Definition: glew.h:14161
simple Vector class for OpenGL graphics, contains overloaded operators for most math functions...
Definition: Vec4.h:57
Mat4 m_transposeMatrix
transpose matrix transformation
const Mat4 & transpose() noexcept
method to transpose the matrix
Definition: Mat4.cpp:389
bool m_isMatrixComputed
boolean defines if the matrix is dirty or not
Singleton Class to init and Use GLSL Shaders the class stores the shaders as a map of shader objects ...
Definition: ShaderLib.h:55
Transformation() noexcept
Constructor.
Mat4 m_matrix
matrix transformation
void computeMatrices() noexcept
method to compute the matrix, transpose and inverse matrix. set the m_bIsMatrixComputed variable to t...
Mat4 getInverseMatrix() noexcept
function to get the inverse matrix. It computes the inverse matrix if it&#39;s dirty
void addScale(const Vec3 &_scale) noexcept
method to add to the existing the scale value in the transform
Real m_m[4][4]
matrix element m_m as a 4x4 array mapped by union to m_nn elements and m_openGL
Definition: Mat4.h:295
Transformation operator*(const Transformation &_m) noexcept
operator for Transform multiplication will do a matrix multiplication on each of the matrices ...
GLdouble GLdouble t
Definition: glew.h:1401
simple Vec3 encapsulates a 3 float object like glsl vec3 but not maths use the Vec3 class for maths a...
Definition: Vec3.h:51
main shader loader / manager class for GLSL shaders
implementation files for RibExport class
Definition: AABB.cpp:22
void operator*=(const Transformation &_m) noexcept
*= operator
void rotateZ(const Real _deg) noexcept
set this matrix to a rotation matrix in the Z axis for value _deg note the matrix should be set to id...
Definition: Mat4.cpp:430
void setMatrix(const Mat4 &_m) noexcept
PRECISION Real
create a variable called Real which is the main data type we use (GLfloat for most cases) ...
Definition: Types.h:127
GLuint shader
Definition: glew.h:1816
void loadGlobalAndCurrentMatrixToShader(const std::string &_param, Transformation &_global, const ActiveMatrix &_which=ActiveMatrix::NORMAL) noexcept
load the current * global transform matrix to the shader
void scale(const Real _x, const Real _y, const Real _z) noexcept
set the matrix scale values
Definition: Mat4.cpp:450
Real m_y
y component
Definition: Vec3.h:311
void rotateY(const Real _deg) noexcept
set this matrix to a rotation matrix in the Y axis for value _deg note the matrix should be set to id...
Definition: Mat4.cpp:418
Mat4 getMatrix() noexcept
function to get the matrix. It computes the matrix if it&#39;s dirty
void set(Real _x, Real _y, Real _z) noexcept
sets the Vec3 component from 3 values
Definition: Vec3.cpp:33
void rotateX(const Real _deg) noexcept
set this matrix to a rotation matrix in the X axis for value _deg note the matrix should be set to id...
Definition: Mat4.cpp:406
a simple transformation object containing rot / tx / scale and final matrix
Real m_x
x component
Definition: Vec3.h:310
void setShaderParamFromMat4(const std::string &_paramName, Mat4 _p1) noexcept
set a shader param by name for 1 int param note that the shader must be the currently active shader o...
Definition: ShaderLib.cpp:39
void setPosition(const Vec4 &_position) noexcept
method to set the position
Mat4 m_inverseMatrix
inverse matrix transformation
void translate(const Real _x, const Real _y, const Real _z) noexcept
set the matrix as a translation matrix
Definition: Mat4.cpp:442
static ShaderLib * instance()
Get the instance.
void addPosition(const Vec4 &_position) noexcept
method add to the existing set the position
Vec3 m_rotation
rotation
Vec3 m_position
position
Transformation describes a transformation (translate, scale, rotation) modifed by j macey and include...
ActiveMatrix
Matrix Class to do simple matrix operations included operator overloaded functions for maths and matr...
Definition: Mat4.h:58
Transformation & operator=(const Transformation &_t) noexcept
assignment operator
void reset() noexcept
a method to set all the transforms to the identity
void addRotation(const Vec3 &_rotation) noexcept
method to add to the existing rotation
Mat4 getTransposeMatrix() noexcept
function to get the transpose matrix. It computes the transpose matrix if it&#39;s dirty ...
void setRotation(const Vec3 &_rotation) noexcept
method to set the rotation
GLsizei const GLchar *const * string
Definition: glew.h:1847
void setScale(const Vec3 &_scale) noexcept
method to set the scale value in the transform
Real m_z
z component
Definition: Vec3.h:312
GLclampf f
Definition: glew.h:3511