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

@@ -3,6 +3,12 @@
#include <open_engine.hpp>
#include "open_engine/core.hpp"
#include "open_engine/events/event.hpp"
#include "open_engine/events/key_event.hpp"
#include "open_engine/logging.hpp"
#include "open_engine/math/math.hpp"
#include "open_engine/scene/components.hpp"
#include "panels/scene_hierarchy.hpp"
//#include <imfilebrowser.h>
@@ -14,6 +20,7 @@
#include <GLFW/glfw3.h>
#include <cstdint>
#include <imgui.h>
#include <ImGuizmo.h>
namespace OpenEngine {
@@ -68,16 +75,6 @@ namespace OpenEngine {
scene = CreateRef<Scene>();
//sq_entity = scene->CreateEntity("square");
//sq_entity.AddComponents<TransformComponent>();
//sq_entity.AddComponents<SpriteRendererComponent>(glm::vec4(1.0f, 0.0f, 0.0f, 1.0f));
//camera_bis = scene->CreateEntity("Main camera");
//camera_bis.AddComponents<TransformComponent>();
//camera_bis.AddComponents<CameraComponent>();
//camera_bis.AddComponents<NativeScriptComponent>().Bind<CameraController>();
scene_hierarchy.SetContext(scene);
}
@@ -92,12 +89,10 @@ namespace OpenEngine {
(spec.width != viewport_size.x || spec.height != viewport_size.y))
{
framebuffer->Resize((uint32_t)viewport_size.x, (uint32_t)viewport_size.y);
//camera.OnResize(viewport_size.x, viewport_size.y);
}
{
OE_PROFILE_SCOPE("Setting up Rendering");
//camera.OnUpdate();
framebuffer->Bind();
scene->OnViewportResize((uint32_t)viewport_size.x, (uint32_t)viewport_size.y);
@@ -107,36 +102,49 @@ namespace OpenEngine {
RenderCommand::SetClearColor({0.11f, 0.11f, 0.15f, 1.0f});
RenderCommand::Clear();
//Renderer2D::BeginScene(camera.GetCamera());
/*
for (float y = -5.0f; y < 5.0f; y += 0.5f) {
for (float x = -5.0f; x < 5.0f; x += 0.5f) {
glm::vec4 gradient_color = {(x + 5.0f) / 10.0f, 0.3f, (y + 5.0f) / 10.0f, 1.0f};
Renderer2D::DrawQuad({{x, y, 0.0f}, glm::vec3(0.45f, 0.45f, 0.0f)}, gradient_color);
}
}
Transform tr5 = {
glm::vec3(world_pos_cursor.x, world_pos_cursor.y, 0.9f),
glm::vec3(0.1f, 0.1f, 0.0f), 0.0f};
Renderer2D::DrawQuad(tr5, {1, 1, 1, 1});
*/
scene->OnUpdate();
//Renderer2D::EndScene();
framebuffer->Unbind();
}
bool EditorKeyBinds(KeyPressedEvent& event)
{
if (event.GetRepeatCount() > 0)
return false;
bool control = Input::IsKeyPressed(Key::LeftControl) || Input::IsKeyPressed(Key::RightControl);
bool shift = Input::IsKeyPressed(Key::LeftShift) || Input::IsKeyPressed(Key::RightShift);
switch(event.GetKeyCode()) {
case KeyCode::Q: {
guizmo_operation = -1;
break;
}
case KeyCode::W: {
guizmo_operation = ImGuizmo::OPERATION::TRANSLATE;
break;
}
case KeyCode::E: {
guizmo_operation = ImGuizmo::OPERATION::ROTATE;
break;
}
case KeyCode::R: {
guizmo_operation = ImGuizmo::OPERATION::SCALE;
break;
}
default:
break;
}
return true;
};
void OnEvent(Event& event) override
{
OE_PROFILE_FUNCTION();
//camera.OnEvent(event);
}
EventDispatcher dispatcher(event);
dispatcher.Dispatch<KeyPressedEvent>(BIND_EVENT_FN(EditorLayer::EditorKeyBinds));
};
float remap(float value, float minInput, float maxInput, float minOutput, float maxOutput) {
// 1. Normalize the input to a 0.0 - 1.0 range
@@ -144,7 +152,7 @@ namespace OpenEngine {
// 2. Use glm::mix to interpolate between the output range
return glm::mix(minOutput, maxOutput, t);
}
};
glm::vec3 ScreenToWorld(
float screenX,
@@ -188,7 +196,7 @@ namespace OpenEngine {
ImGui::Begin("Viewport");
bool focused = ImGui::IsWindowFocused();
bool hovered = ImGui::IsWindowHovered();
Application::Get().GetImGuiLayer()->SetBlockEvents(!focused || !hovered);
Application::Get().GetImGuiLayer()->SetBlockEvents((!focused && !hovered) || ImGui::IsAnyItemActive());
ImVec2 viewport_panel_size = ImGui::GetContentRegionAvail();
@@ -218,6 +226,8 @@ namespace OpenEngine {
uint32_t texture_id = framebuffer->GetColorAttachmentRendererID();
ImGui::Image((void*)(uint64_t)texture_id, ImVec2{ viewport_size.x, viewport_size.y }, ImVec2{ 0, 1 }, ImVec2{ 1, 0 });
DrawGuizmos();
ImGui::End();
ImGui::PopStyleVar();
};
@@ -266,6 +276,49 @@ namespace OpenEngine {
};
*/
void DrawGuizmos()
{
Entity selected_entity = scene_hierarchy.GetSelectedEntity();
if (selected_entity && selected_entity.HasComponent<TransformComponent>() && guizmo_operation != -1) {
ImGuizmo::SetOrthographic(false);
ImGuizmo::SetDrawlist();
auto window_position = ImGui::GetWindowPos();
auto window_size = ImGui::GetWindowSize();
ImGuizmo::SetRect(window_position.x, window_position.y, window_size.x, window_size.y);
auto camera_entity = scene->GetPrimaryCamera();
auto camera = camera_entity.GetComponents<CameraComponent>().camera;
const glm::mat4& projection = camera.GetProjection();
glm::mat4 camera_view = glm::inverse(camera_entity.GetComponents<TransformComponent>().GetTransform());
auto& transform_comp = selected_entity.GetComponents<TransformComponent>();
glm::mat4 transform = transform_comp.GetTransform();
bool snap = Input::IsKeyPressed(KeyCode::LeftControl);
float snap_value = 0.1f;
if (guizmo_operation == ImGuizmo::OPERATION::ROTATE)
snap_value = 10.0f;
float snap_values[] = { snap_value, snap_value, snap_value };
ImGuizmo::Manipulate(glm::value_ptr(camera_view), glm::value_ptr(projection),
(ImGuizmo::OPERATION)guizmo_operation, ImGuizmo::LOCAL,
glm::value_ptr(transform), nullptr,
snap ? snap_values : nullptr);
if (ImGuizmo::IsUsing()) {
glm::vec3 translation, rotation, scale;
Math::DecomposeTransform(transform, translation, rotation, scale);
glm::vec3 delta_rotation = rotation - transform_comp.rotation;
transform_comp.translation = translation;
transform_comp.rotation += delta_rotation;
transform_comp.scale = scale;
}
}
};
void OnImGuiRender() override
{
OE_PROFILE_FUNCTION();
@@ -274,7 +327,7 @@ namespace OpenEngine {
// Testing file pickers
if (ImGui::BeginMainMenuBar()) {
if (ImGui::BeginMenu("File")) {
if (ImGui::MenuItem("Save As", "Ctrl+S")) {
if (ImGui::MenuItem("Save Scene As", "Ctrl+S")) {
std::string file = FileDialogs::SaveFile("useless");
OE_TRACE("saving to filename: {}", file);
if (!file.empty()) {
@@ -282,7 +335,7 @@ namespace OpenEngine {
serializer.Serialize(file);
}
}
if (ImGui::MenuItem("Load", "Ctrl+O")) {
if (ImGui::MenuItem("Open Scene", "Ctrl+O")) {
std::string file = FileDialogs::OpenFile("useless");
OE_DEBUG("loading filename: {}", file);
if (!file.empty()) {
@@ -334,10 +387,13 @@ namespace OpenEngine {
glm::vec2 viewport_size = { 0.0f, 0.0f };
Ref<OpenEngine::FrameBuffer> framebuffer;
SceneHierarchy scene_hierarchy;
Entity camera_bis;
int guizmo_operation = -1;
Ref<Scene> scene;
Entity sq_entity;
//ImGui::FileBrowser fileDialog;