transform guizmos, math and bugfixing file open/save

This commit is contained in:
Erris
2026-02-20 22:59:30 +01:00
parent a897d5c798
commit 02430073ec
12 changed files with 210 additions and 44 deletions

View File

@@ -31,4 +31,5 @@
#include "open_engine/renderer/shader.hpp"
#include "open_engine/scene/entity.hpp"
#include "open_engine/scene/scene.hpp"
#endif // OPEN_ENGINE_HPP

View File

@@ -0,0 +1,13 @@
#ifndef MATH_HPP
#define MATH_HPP
#include <glm/fwd.hpp>
namespace OpenEngine::Math {
bool DecomposeTransform(const glm::mat4& transform,
glm::vec3& out_translation, glm::vec3& out_rotation,
glm::vec3& out_scale);
}
#endif // MATH_HPP

View File

@@ -9,6 +9,10 @@
#include <glm/gtc/type_ptr.hpp>
#include <glm/fwd.hpp>
#include <glm/glm.hpp>
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/quaternion.hpp>
#include <imgui_internal.h>
#include <imgui.h>
#include <string>
@@ -55,10 +59,7 @@ namespace OpenEngine {
{
glm::mat4 transform = glm::translate(glm::mat4(1.0f), translation);
transform *= glm::rotate(glm::mat4(1.0f), rotation.x, { 1, 0, 0 })
* glm::rotate(glm::mat4(1.0f), rotation.y, { 0, 1, 0 })
* glm::rotate(glm::mat4(1.0f), rotation.z, { 0, 0, 1 });
transform *= glm::toMat4(glm::quat(rotation));
transform *= glm::scale(glm::mat4(1.0f), scale);
return transform;

View File

@@ -22,6 +22,8 @@ namespace OpenEngine {
entt::registry& GetRegistry() { return registry; };
Entity GetPrimaryCamera();
private:
entt::registry registry;

View File

@@ -41,7 +41,6 @@ namespace OpenEngine {
}
}
// TODO: Debug Make work consistently
std::string FileDialogs::SaveFile(const char* filters)
{
NFD_Init();

View File

@@ -8,6 +8,7 @@
#include <application.hpp>
#include <layer.hpp>
#include <ImGuizmo.h>
#include "imgui.h"
#include <glad/glad.h>
#include <GLFW/glfw3.h>
@@ -309,6 +310,7 @@ namespace OpenEngine {
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
ImGuizmo::BeginFrame();
}
void ImGuiLayer::End()

View File

@@ -0,0 +1,43 @@
#include <pch.hpp>
#include <math/math.hpp>
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/matrix_decompose.hpp>
namespace OpenEngine::Math {
bool DecomposeTransform(const glm::mat4& transform,
glm::vec3& out_translation, glm::vec3& out_rotation,
glm::vec3& out_scale)
{
// 1. Extract Translation
out_translation = glm::vec3(transform[3]);
// 2. Extract Scale and handle Negative Scaling (Reflection)
glm::vec3 vX = glm::vec3(transform[0]);
glm::vec3 vY = glm::vec3(transform[1]);
glm::vec3 vZ = glm::vec3(transform[2]);
out_scale.x = glm::length(vX);
out_scale.y = glm::length(vY);
out_scale.z = glm::length(vZ);
// If the determinant is negative, one axis is mirrored
if (glm::determinant(transform) < 0) {
out_scale.x *= -1.0f;
}
// 3. Extract Rotation (Orthonormalize)
// Avoid division by zero if scale is near-zero
glm::mat3 rotation_matrix;
rotation_matrix[0] = (out_scale.x != 0.0f) ? vX / out_scale.x : glm::vec3(1, 0, 0);
rotation_matrix[1] = (out_scale.y != 0.0f) ? vY / out_scale.y : glm::vec3(0, 1, 0);
rotation_matrix[2] = (out_scale.z != 0.0f) ? vZ / out_scale.z : glm::vec3(0, 0, 1);
out_rotation = glm::eulerAngles(glm::quat_cast(rotation_matrix));
return true;
}
}

View File

@@ -80,4 +80,17 @@ namespace OpenEngine {
camera_comp.camera.SetViewportSize(width, height);
}
}
Entity Scene::GetPrimaryCamera()
{
auto view = registry.view<CameraComponent>();
for (auto entity : view) {
const auto& camera = view.get<CameraComponent>(entity);
if (camera.primary)
return Entity { entity, this };
}
return {};
}
}