TDME2 1.9.121
Transformations.cpp
Go to the documentation of this file.
2
3#include <string>
4
5#include <tdme/tdme.h>
11#include <tdme/math/Vector3.h>
12
13using std::to_string;
14
21
22Transformations::Transformations()
23{
25 scale.set(1.0f, 1.0f, 1.0f);
27}
28
30}
31
33{
34 if (this == &transformations) return;
35 *this = transformations;
36}
37
38void Transformations::fromMatrix(const Matrix4x4& matrix, RotationOrder* rotationOrder) {
39 matrix.getScale(scale);
41 auto eulerAngles = matrix.computeEulerAngles();
42 rotations.clear();
43 rotations.push_back(Rotation(rotationOrder->getAxis0(), eulerAngles[rotationOrder->getAxis0VectorIndex()]));
44 rotations.push_back(Rotation(rotationOrder->getAxis1(), eulerAngles[rotationOrder->getAxis1VectorIndex()]));
45 rotations.push_back(Rotation(rotationOrder->getAxis2(), eulerAngles[rotationOrder->getAxis2VectorIndex()]));
46 update();
47}
48
50{
51 // matrices
52 Matrix4x4 translationMatrix;
53 Matrix4x4 scaleMatrix;
54 Matrix4x4 rotationsMatrix;
55 Matrix4x4 rotationsTranslationsMatrix;
56
57 // transformation matrix identity
59 // set up translation matrix
60 translationMatrix.identity().translate(translation);
61 // set up scale matrix
62 scaleMatrix.identity().scale(scale);
63 // create and multiply rotations
65 for (auto& rotation: rotations) {
66 rotation.update();
67 rotationsQuaternion.multiply(rotation.getQuaternion());
68 }
70 // apply rotations
71 rotationsMatrix.identity();
72 // pivot
73 rotationsMatrix.translate(pivot.clone().scale(-1.0f));
74 // rotations
76 // pivot
77 rotationsTranslationsMatrix.identity().translate(pivot);
78 rotationsMatrix.multiply(rotationsTranslationsMatrix);
79 // apply to transformation matrix
81 transformationsMatrix.multiply(rotationsMatrix);
82 transformationsMatrix.multiply(translationMatrix);
83}
84
87}
88
90 translation.scale(-1.0f);
91 scale.setX(1.0f / scale.getX());
92 scale.setY(1.0f / scale.getY());
93 scale.setZ(1.0f / scale.getZ());
94 for (auto& rotation: rotations) {
95 rotation.setAngle(rotation.getAngle() - 180.0f);
96 }
98}
Rotation representation.
Definition: Rotation.h:18
Transformations which contain scale, rotations and translation.
virtual void fromMatrix(const Matrix4x4 &matrix, RotationOrder *rotationOrder)
Set up this transformations from given matrix and rotation order.
const Matrix4x4 & getTransformationsMatrix() const
virtual void invert()
Invert this transformations.
virtual void applyParentTransformations(const Transformations &parentTransformations)
Apply parent transformations.
vector< Rotation > rotations
virtual void fromTransformations(const Transformations &transformations)
Set up this transformations from given transformations.
virtual void update()
Computes transformation matrix.
virtual ~Transformations()
Destructor.
Represents rotation orders of a model.
Definition: RotationOrder.h:23
const Vector3 & getAxis0() const
Definition: RotationOrder.h:60
const Vector3 & getAxis2() const
Definition: RotationOrder.h:74
const Vector3 & getAxis1() const
Definition: RotationOrder.h:67
4x4 3D Matrix class
Definition: Matrix4x4.h:24
Vector3 computeEulerAngles() const
Compute Euler angles (rotation around x, y, z axes)
Definition: Matrix4x4.h:661
Matrix4x4 & identity()
Setup identity matrix.
Definition: Matrix4x4.h:326
void getScale(Vector3 &scale) const
Get scale.
Definition: Matrix4x4.h:282
Matrix4x4 & translate(const Vector3 &v)
Sets up a translation matrix.
Definition: Matrix4x4.h:460
Matrix4x4 & invert()
Inverts the matrix.
Definition: Matrix4x4.h:536
void getTranslation(Vector3 &translation) const
Get translation.
Definition: Matrix4x4.h:261
Matrix4x4 & scale(float s)
Scales this matrix.
Definition: Matrix4x4.h:418
Vector3 multiply(const Vector3 &v) const
Multiplies a vector3 with this matrix into destination vector.
Definition: Matrix4x4.h:351
Quaternion class.
Definition: Quaternion.h:22
Quaternion & multiply(const Quaternion q)
Multiplies this quaternion with quaternion q.
Definition: Quaternion.h:227
Matrix4x4 computeMatrix() const
Computes a matrix from given.
Definition: Quaternion.h:299
Quaternion & identity()
Set up quaternion identity.
Definition: Quaternion.h:171
Quaternion & normalize()
Normalize quaternion.
Definition: Quaternion.h:213
3D vector 3 class
Definition: Vector3.h:22
float getY() const
Definition: Vector3.h:119
float getX() const
Definition: Vector3.h:103
float getZ() const
Definition: Vector3.h:136
Vector3 & setZ(float z)
Set Z.
Definition: Vector3.h:145
Vector3 & set(float x, float y, float z)
Set up vector.
Definition: Vector3.h:73
Vector3 & setX(float x)
Set X.
Definition: Vector3.h:111
Vector3 clone() const
Clones the vector.
Definition: Vector3.h:372
Vector3 & scale(float scale)
Scale this vector.
Definition: Vector3.h:349
Vector3 & setY(float y)
Set Y.
Definition: Vector3.h:128