]> git.tdb.fi Git - libs/game.git/commitdiff
Add factory function from a matrix to TransformValues
authorMikko Rasa <tdb@tdb.fi>
Sun, 26 Jan 2025 15:14:15 +0000 (17:14 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sun, 26 Jan 2025 16:25:54 +0000 (18:25 +0200)
source/game/transform.cpp
source/game/transform.h

index 5b55ab2dc4f05c0833196d549f77405e7f43d26b..60225f15f4f979acc73e11c5ef39c4e3031dc13e 100644 (file)
@@ -3,6 +3,21 @@
 
 namespace Msp::Game {
 
+TransformValues TransformValues::from_matrix(const LinAl::Matrix<float, 4, 4> &m)
+{
+       LinAl::Vector<float, 3> p = m.column(3).slice<3>(0);
+
+       LinAl::Vector<float, 3> x = m.column(0).slice<3>(0);
+       LinAl::Vector<float, 3> y = m.column(1).slice<3>(0);
+       LinAl::Vector<float, 3> z = m.column(2).slice<3>(0);
+       LinAl::Vector<float, 3> s(x.norm(), y.norm(), z.norm());
+
+       LinAl::Vector<float, 3> ul_cols[3] = { x/s.x, y/s.y, z/s.z };
+       auto r = Geometry::Quaternion<float>::from_matrix(LinAl::Matrix<float, 3, 3>::from_columns(ul_cols));
+
+       return { p, r, s };
+}
+
 Transform::Transform(Handle<Entity> e):
        BufferedComponent<TransformData>(e)
 { }
index fc63f29d4dbfa0d082bfa014269c78e20797a0bb..b8632674bf2723f75b64daf68413a81007a48844 100644 (file)
@@ -21,6 +21,8 @@ struct TransformValues
                const LinAl::Vector<float, 3> &s = LinAl::Vector<float, 3>(1.0f, 1.0f, 1.0f)):
                position(p), rotation(r), scale(s)
        { }
+
+       static TransformValues from_matrix(const LinAl::Matrix<float, 4, 4> &);
 };
 
 struct TransformData