From aea9ceca71b59358248be94fdf272fd4791ca990 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Tue, 4 Jun 2019 14:11:45 +0300 Subject: [PATCH] Add more public methods to Transform --- source/transform.cpp | 42 ++++++++++++++++++++++++++++++++---------- source/transform.h | 4 ++++ 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/source/transform.cpp b/source/transform.cpp index 9660c2c4..4b67bf91 100644 --- a/source/transform.cpp +++ b/source/transform.cpp @@ -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(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(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(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(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(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(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 diff --git a/source/transform.h b/source/transform.h index ee821ce1..01c783bd 100644 --- a/source/transform.h +++ b/source/transform.h @@ -39,8 +39,12 @@ private: public: Transform(); + static Transform from_matrix(const Matrix &); + void set_position(const Vector3 &); void set_euler(const AngleVector3 &); + void set_rotation(const Angle &, const Vector3 &); + void set_scale(float); void set_scale(const Vector3 &); const Vector3 &get_position() const { return position; } const AngleVector3 &get_euler() const { return euler; } -- 2.43.0