From f401b3694ac931c9578fed8f9ca598c7eb46c780 Mon Sep 17 00:00:00 2001 From: Kbz-8 Date: Thu, 14 Nov 2024 11:44:52 +0100 Subject: [PATCH] fixing rotation --- runtime/Includes/Core/Graphics.inl | 3 +-- runtime/Includes/Graphics/Drawable.h | 2 +- runtime/Includes/Maths/Mat4.h | 4 ++-- runtime/Includes/Maths/Quaternions.h | 9 +++++---- runtime/Sources/Renderer/RenderPasses/2DPass.cpp | 8 ++++---- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/runtime/Includes/Core/Graphics.inl b/runtime/Includes/Core/Graphics.inl index a3fd304..b07350e 100644 --- a/runtime/Includes/Core/Graphics.inl +++ b/runtime/Includes/Core/Graphics.inl @@ -54,7 +54,6 @@ namespace mlx Text& new_text = p_scene->CreateText(str); new_text.SetPosition(Vec2f{ static_cast(x), static_cast(y) }); new_text.SetColor(std::move(vec_color)); - new_text.SetCenter(Vec2f{ 0.0f, 0.0f }); } else if(!p_scene->IsTextAtGivenDrawLayer(str, m_draw_layer)) p_scene->BringToDrawLayer(text.Get(), m_draw_layer); @@ -72,10 +71,10 @@ namespace mlx m_pixelput_called = false; } Sprite& new_sprite = p_scene->CreateSprite(texture); + new_sprite.SetCenter(Vec2f{ texture->GetWidth() / 2.0f, texture->GetHeight() / 2.0f }); new_sprite.SetPosition(Vec2f{ static_cast(x), static_cast(y) }); new_sprite.SetScale(Vec2f{ scale, scale }); new_sprite.SetRotation(angle); - new_sprite.SetCenter(Vec2f{ texture->GetWidth() / 2.0f, texture->GetHeight() / 2.0f }); } else if(!p_scene->IsTextureAtGivenDrawLayer(texture, m_draw_layer)) p_scene->BringToDrawLayer(sprite.Get(), m_draw_layer); diff --git a/runtime/Includes/Graphics/Drawable.h b/runtime/Includes/Graphics/Drawable.h index 021700d..803fd5f 100644 --- a/runtime/Includes/Graphics/Drawable.h +++ b/runtime/Includes/Graphics/Drawable.h @@ -50,7 +50,7 @@ namespace mlx Vec4f m_color = Vec4f{ 1.0f, 1.0f, 1.0f, 1.0f }; Vec2f m_position = Vec2f{ 0.0f, 0.0f }; Vec2f m_scale = Vec2f{ 1.0f, 1.0f }; - Vec2f m_center; + Vec2f m_center = Vec2f{ 0.0f, 0.0f }; DrawableType m_type; }; } diff --git a/runtime/Includes/Maths/Mat4.h b/runtime/Includes/Maths/Mat4.h index 722f5cd..bc8adf9 100644 --- a/runtime/Includes/Maths/Mat4.h +++ b/runtime/Includes/Maths/Mat4.h @@ -95,8 +95,8 @@ namespace mlx static constexpr Mat4 ConcatenateTransform(const Mat4& left, const Mat4& right); static constexpr Mat4 Identity(); static constexpr Mat4 LookAt(const Vec3& eye, const Vec3& target, const Vec3& up = Vec3::Up()); - static constexpr Mat4 Ortho(T left, T right, T top, T bottom, T zNear = -1.0, T zFar = 1.0); - static Mat4 Perspective(RadianAngle angle, T ratio, T zNear, T zFar); + static constexpr Mat4 Ortho(T left, T right, T top, T bottom, T z_near = -1.0, T zFar = 1.0); + static Mat4 Perspective(RadianAngle angle, T ratio, T z_near, T z_far); static constexpr Mat4 Rotate(const Quat& rotation); static constexpr Mat4 Scale(const Vec3& scale); static constexpr Mat4 Translate(const Vec3& translation); diff --git a/runtime/Includes/Maths/Quaternions.h b/runtime/Includes/Maths/Quaternions.h index dfa1809..77b7b4e 100644 --- a/runtime/Includes/Maths/Quaternions.h +++ b/runtime/Includes/Maths/Quaternions.h @@ -20,10 +20,9 @@ namespace mlx template constexpr explicit Quat(const Quat& quat); constexpr Quat(const Quat&) = default; constexpr Quat(Quat&&) = default; - ~Quat() = default; RadianAngle AngleBetween(const Quat& vec) const; - constexpr bool ApproxEqual(const Quat& quat, T maxDifference = std::numeric_limits::epsilon()) const; + constexpr bool ApproxEqual(const Quat& quat, T max_difference = std::numeric_limits::epsilon()) const; Quat& ComputeW(); constexpr Quat& Conjugate(); @@ -68,16 +67,18 @@ namespace mlx constexpr bool operator>=(const Quat& quat) const; static RadianAngle AngleBetween(const Quat& lhs, const Quat& rhs); - static constexpr bool ApproxEqual(const Quat& lhs, const Quat& rhs, T maxDifference = std::numeric_limits::epsilon()); + static constexpr bool ApproxEqual(const Quat& lhs, const Quat& rhs, T max_difference = std::numeric_limits::epsilon()); static constexpr Quat Identity(); static constexpr Quat Lerp(const Quat& from, const Quat& to, T interpolation); static Quat LookAt(const Vec3& forward, const Vec3& up); static Quat Normalize(const Quat& quat, T* length = nullptr); static Quat RotationBetween(const Vec3& from, const Vec3& to); - static Quat RotateTowards(const Quat& from, const Quat& to, RadianAngle maxRotation); + static Quat RotateTowards(const Quat& from, const Quat& to, RadianAngle max_rotation); static Quat Mirror(Quat quat, const Vec3& axis); static Quat Slerp(const Quat& from, const Quat& to, T interpolation); static constexpr Quat Zero(); + + ~Quat() = default; }; using Quatd = Quat; diff --git a/runtime/Sources/Renderer/RenderPasses/2DPass.cpp b/runtime/Sources/Renderer/RenderPasses/2DPass.cpp index a6a9e4e..4a767ee 100644 --- a/runtime/Sources/Renderer/RenderPasses/2DPass.cpp +++ b/runtime/Sources/Renderer/RenderPasses/2DPass.cpp @@ -108,10 +108,10 @@ namespace mlx DrawableData drawable_data; drawable_data.color = drawable->GetColor(); drawable_data.model_matrix = Mat4f::Identity(); - drawable_data.model_matrix.SetTranslation(Vec3f{ drawable->GetPosition(), 0.0f }); - drawable_data.model_matrix.SetScale(Vec3f{ drawable->GetScale(), 1.0f }); - drawable_data.model_matrix.SetRotation(drawable->GetRotation()); - //drawable_data.model_matrix = Mat4f::Translate(-Vec3f{ drawable->GetCenter(), 0.0f }) * Mat4f::Rotate(drawable->GetRotation()) * drawable_data.model_matrix; + drawable_data.model_matrix.ApplyTranslation(Vec3f{ -drawable->GetCenter() / 2.0f, 0.0f }); + drawable_data.model_matrix.ApplyRotation(drawable->GetRotation()); + drawable_data.model_matrix.ApplyTranslation(Vec3f{ drawable->GetPosition() + drawable->GetCenter(), 0.0f }); + drawable_data.model_matrix.ApplyScale(Vec3f{ drawable->GetScale(), 1.0f }); drawable->Bind(frame_index, cmd);