]> git.tdb.fi Git - libs/gl.git/blobdiff - source/transform.cpp
Use bezier splines in Animation
[libs/gl.git] / source / transform.cpp
index 9660c2c43fd15a1ea0af01ba953462e36fc6cc67..4b67bf910e453bf0d9a677dfed47eb0a1d229933 100644 (file)
@@ -9,6 +9,23 @@ Transform::Transform():
        scale(1.0f, 1.0f, 1.0f)
 { }
 
+Transform Transform::from_matrix(const Matrix &matrix)
+{
+       Transform trn;
+       trn.position = matrix.column(3).slice<3>(0);
+
+       trn.euler.z = Geometry::atan2<float>(matrix(1, 0), matrix(0, 0));
+       Matrix m = Matrix::rotation(-trn.euler.z, Vector3(0.0f, 0.0f, 1.0f))*matrix;
+       trn.euler.y = Geometry::atan2<float>(m(2, 0), m(0, 0));
+       m = Matrix::rotation(-trn.euler.y, Vector3(0.0f, 1.0f, 0.0f))*m;
+       trn.euler.x = Geometry::atan2<float>(m(2, 1), m(1, 1));
+       m = Matrix::rotation(-trn.euler.x, Vector3(1.0f, 0.0f, 0.0f))*m;
+
+       trn.scale = Vector3(m(0, 0), m(1, 1), m(2, 2));
+
+       return trn;
+}
+
 void Transform::set_position(const Vector3 &p)
 {
        position = p;
@@ -19,6 +36,16 @@ void Transform::set_euler(const AngleVector3 &e)
        euler = e;
 }
 
+void Transform::set_rotation(const Angle &angle, const Vector3 &axis)
+{
+       euler = from_matrix(Matrix::rotation(angle, axis)).euler;
+}
+
+void Transform::set_scale(float s)
+{
+       set_scale(Vector3(s, s, s));
+}
+
 void Transform::set_scale(const Vector3 &s)
 {
        scale = s;
@@ -48,32 +75,27 @@ Transform::Loader::Loader(Transform &t):
 
 void Transform::Loader::position(float x, float y, float z)
 {
-       obj.position = Vector3(x, y, z);
+       obj.set_position(Vector3(x, y, z));
 }
 
 void Transform::Loader::euler(float x, float y, float z)
 {
-       obj.euler = AngleVector3(Angle::from_degrees(x), Angle::from_degrees(y), Angle::from_degrees(z));
+       obj.set_euler(AngleVector3(Angle::from_degrees(x), Angle::from_degrees(y), Angle::from_degrees(z)));
 }
 
 void Transform::Loader::rotation(float a, float x, float y, float z)
 {
-       Matrix matrix = Matrix::rotation(Angle::from_degrees(a), Vector3(x, y, z));
-       obj.euler.z = Geometry::atan2<float>(matrix(1, 0), matrix(0, 0));
-       matrix = Matrix::rotation(-obj.euler.z, Vector3(0.0f, 0.0f, 1.0f))*matrix;
-       obj.euler.y = Geometry::atan2<float>(matrix(2, 0), matrix(0, 0));
-       matrix = Matrix::rotation(-obj.euler.y, Vector3(0.0f, 1.0f, 0.0f))*matrix;
-       obj.euler.x = Geometry::atan2<float>(matrix(2, 1), matrix(1, 1));
+       obj.set_rotation(Angle::from_degrees(a), Vector3(x, y, z));
 }
 
 void Transform::Loader::scale_uniform(float s)
 {
-       obj.scale = Vector3(s, s, s);
+       obj.set_scale(s);
 }
 
 void Transform::Loader::scale(float x, float y, float z)
 {
-       obj.scale = Vector3(x, y, z);
+       obj.set_scale(Vector3(x, y, z));
 }
 
 } // namespace GL