From ceb485018f868670f51d055d65734ac852884a22 Mon Sep 17 00:00:00 2001 From: Nigel Barink Date: Sat, 13 May 2023 18:10:24 +0200 Subject: [PATCH] Rudimentary implementation of the translate,Rotate,Scale tools --- Editor/src/EditorLayer.h | 85 ++++++++++++++++++++++------ YoggieEngine/src/Application.cpp | 2 +- YoggieEngine/src/Graphics/Renderer.h | 4 +- YoggieEngine/src/Scene/Components.h | 70 +++++++++++++++++++++-- 4 files changed, 139 insertions(+), 22 deletions(-) diff --git a/Editor/src/EditorLayer.h b/Editor/src/EditorLayer.h index b62a37e..fff700c 100644 --- a/Editor/src/EditorLayer.h +++ b/Editor/src/EditorLayer.h @@ -118,6 +118,8 @@ public: } + ImGuizmo::OPERATION activeOperation = ImGuizmo::OPERATION::TRANSLATE; + void OnUI() override { ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, { ImGui::GetWindowWidth(), 7 }); ImGui::BeginMainMenuBar(); @@ -280,6 +282,49 @@ public: ImGui::Begin("RuntimeControls"); + + ImGui::BeginGroup(); + ImGui::SameLine(); + ImGui::Text(ICON_MD_TRANSFORM); + + auto spacing = 10; + float buttonWidth = 75.0f; + float buttonHeight = 30; + float ColorIntensity = 0.5f; + float ColorIntensityHovered = 0.75; + float ColorIntensityActive = 0.85; + float yPadding = 6.f; + + ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2{0, yPadding }); + ImGui::PushStyleColor(ImGuiCol_Button, ImVec4{ 0.0f,ColorIntensity ,0.0f ,1.0f }); + ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4{ 0.0f,ColorIntensityHovered ,0.0f ,1.0f }); + ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4{ 0.0f,ColorIntensityActive ,0.0f ,1.0f }); + ImGui::SameLine(0, spacing); + if (ImGui::Button("Translate", { buttonWidth,buttonHeight })) + activeOperation = ImGuizmo::OPERATION::TRANSLATE; + + ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2{ 0, yPadding }); + ImGui::PushStyleColor(ImGuiCol_Button, ImVec4{ ColorIntensity, 0.0f ,0.0f ,1.0f }); + ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4{ ColorIntensityHovered, 0.0f ,0.0f ,1.0f }); + ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4{ ColorIntensityActive, 0.0f ,0.0f ,1.0f }); + ImGui::SameLine(0, spacing); + if (ImGui::Button("Rotate", { buttonWidth,buttonHeight })) + activeOperation = ImGuizmo::OPERATION::ROTATE; + + ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2{ 0, yPadding }); + ImGui::PushStyleColor(ImGuiCol_Button, ImVec4{ 0.0f, 0.0f ,ColorIntensity ,1.0f }); + ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4{ 0.0f, 0.0f ,ColorIntensityHovered ,1.0f }); + ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4{ 0.0f, 0.0f ,ColorIntensityActive ,1.0f }); + ImGui::SameLine(0, spacing); + if (ImGui::Button("Scale", { buttonWidth,buttonHeight })) + activeOperation = ImGuizmo::OPERATION::SCALE; + + ImGui::PopStyleColor(9); + ImGui::PopStyleVar(3); + + + ImGui::EndGroup(); + ImGui::SameLine((ImGui::GetWindowContentRegionMax().x / 2) - (90)); auto color = ImVec4{ 0.001 * 12 , 0.001 * 201 , 0.001 * 69, 1.0f }; ImGui::PushStyleColor(ImGuiCol_Button, color); @@ -325,32 +370,40 @@ public: | ImGuiWindowFlags_NoCollapse; ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2{ 0,0 }); ImGui::Begin("SceneView",nullptr,viewportWindowFlags); - spdlog::info( "Editor Resolution {0}x{1}", (float)ImGui::GetWindowWidth(),(float)ImGui::GetWindowHeight()); + ImGui::Image((ImTextureID)(intptr_t)renderer.getCurrentFrameBuffer().GetColourAttachment(), ImVec2{(float)ImGui::GetWindowWidth(),(float)ImGui::GetWindowHeight()}); - ImGuizmo::Enable(true); - ImGuizmo::SetOrthographic(false); - ImGuizmo::SetDrawlist(); - - ImGuizmo::SetRect(ImGui::GetWindowPos().x, ImGui::GetWindowPos().y, ImGui::GetContentRegionMax().x, ImGui::GetContentRegionMax().y); - /* - const auto& projection = camera.projection; - glm::mat4& view = glm::mat4(1.0f); - ImGuizmo::DrawGrid(); - ImGuizmo::ViewManipulate(); + + + //ImGuizmo::ViewManipulate(glm::value_ptr(cameraView), 1, ImGui::GetWindowPos(), { 90,90 }, 0x22CCCCCCC); - if(selected != nullptr && selected->isValid()){ - auto& tc = selected->GetComponent(); - glm::mat4 transform = tc.GetTransform(); + if(Selected.isValid()){ + + + ImGuizmo::SetOrthographic(false); + ImGuizmo::SetDrawlist(); + + ImGuizmo::SetRect(ImGui::GetWindowPos().x, ImGui::GetWindowPos().y, ImGui::GetWindowWidth(), ImGui::GetWindowHeight()); + + const auto& ProjMatrix = camera->projection; + glm::mat4& cameraView = glm::inverse(((EditorCamera*)camera)->view); + glm::mat4 cameraDelta = glm::mat4(1.0f); + + + + + auto& tc = Selected.GetComponent(); + glm::mat4& transform = tc.GetTransform(); + ImGuizmo::Manipulate(glm::value_ptr(cameraView), glm::value_ptr(ProjMatrix), activeOperation, ImGuizmo::WORLD, glm::value_ptr(transform), nullptr, nullptr); + if(ImGuizmo::IsUsing()) + tc.Decompose(transform); - ImGuizmo::Manipulate(); } - */ ImGui::End(); ImGui::PopStyleVar(); diff --git a/YoggieEngine/src/Application.cpp b/YoggieEngine/src/Application.cpp index b121f01..f20d675 100644 --- a/YoggieEngine/src/Application.cpp +++ b/YoggieEngine/src/Application.cpp @@ -27,7 +27,7 @@ namespace YoggieEngine { config.MergeMode = true; config.GlyphMinAdvanceX = 18.0f; static const ImWchar icon_ranges[] = { ICON_MIN_MD , ICON_MAX_MD, 0 }; - io.Fonts->AddFontFromFileTTF("build/Debug/Fonts/MaterialIcons-Regular.ttf", 18, &config, icon_ranges); + io.Fonts->AddFontFromFileTTF("build/Debug/Fonts/MaterialIcons-Regular.ttf", 24, &config, icon_ranges); ImGui::StyleColorsDark(); /* diff --git a/YoggieEngine/src/Graphics/Renderer.h b/YoggieEngine/src/Graphics/Renderer.h index dc91567..52821c8 100644 --- a/YoggieEngine/src/Graphics/Renderer.h +++ b/YoggieEngine/src/Graphics/Renderer.h @@ -38,6 +38,8 @@ namespace YoggieEngine { void Render(Scene& scene, Camera MainCamera) { MainCamera.projection = glm::perspective(glm::radians(65.0f), ((float)width/(float)height), 0.0001f, 1000.0f); + int oldviewport[4]; + glGetIntegerv(GL_VIEWPORT, oldviewport); glViewport(0, 0, width, height); @@ -205,7 +207,7 @@ namespace YoggieEngine { glBindFramebuffer(GL_FRAMEBUFFER, 0); - + glViewport(oldviewport[0], oldviewport[1], oldviewport[2], oldviewport[3]); } diff --git a/YoggieEngine/src/Scene/Components.h b/YoggieEngine/src/Scene/Components.h index 6a3ac9d..4a42e03 100644 --- a/YoggieEngine/src/Scene/Components.h +++ b/YoggieEngine/src/Scene/Components.h @@ -1,6 +1,9 @@ #pragma once #include "Entity.h" + +#include #include + namespace YoggieEngine { struct IdentifierComponent { std::string name; @@ -8,15 +11,74 @@ namespace YoggieEngine { struct TransformComponent { - glm::vec3 Position = glm::vec3(0.0f); - glm::vec3 Rotation = glm::vec3(0.0f); + glm::vec3 Position = glm::vec3(0.0f, 0.0f, 0.0f); + glm::vec3 Rotation = glm::vec3(0.0f , 0.0f, 0.0f); glm::vec3 Scale = glm::vec3(1.0f); glm::mat4 LocalTransform = glm::mat4(1.0f); glm::mat4 GetTransform() const { - glm::mat4 rotation = glm::toMat4(glm::quat(Rotation)); - return glm::translate(glm::mat4(1.0f), Position) * rotation * glm::scale(glm::mat4(1.0f), Scale); + + + + return glm::translate(glm::mat4(1.0f), Position) * glm::toMat4(glm::quat(Rotation)) * glm::scale(glm::mat4(1.0f), Scale); + } + + void Decompose(glm::mat4 transformationMatrix) { + glm::mat4& tm = transformationMatrix; + + + auto& a = tm[0][0]; + auto& b = tm[1][0]; + auto& c = tm[2][0]; + auto& d = tm[3][0]; + + auto& e = tm[0][1]; + auto& f = tm[1][1]; + auto& g = tm[2][1]; + auto& h = tm[3][1]; + + auto& i = tm[0][2]; + auto& j = tm[1][2]; + auto& k = tm[2][2]; + auto& l = tm[3][2]; + + Position = glm::vec3(d, h, l); + d = 0; + h = 0; + l = 0; + + auto sx = glm::sqrt(a*a + e*e + i*i); + auto sy = glm::sqrt(b*b + f*f + j*j); + auto sz = glm::sqrt(c*c + g*g + k*k); + + Scale = glm::vec3(sx, sy, sz); + + a/= sx; + e /= sx; + i /= sx; + + b /= sy; + f /= sy; + j /= sy; + + c /= sz; + g /= sz; + k /= sz; + + + auto w = glm::sqrt(1 + tm[0][0] + tm[1][1] + tm[2][2]) / 2; + + + auto x = (tm[2][1] - tm[1][2]) / (4 * w); + auto y = (tm[0][2] - tm[2][0]) / (4 * w); + auto z = (tm[1][0] - tm[0][1]) / (4 * w); + + + auto rot = glm::quat(w, x, y, z); + Rotation = glm::eulerAngles(rot); + + }