diff --git a/Makefile b/Makefile index 311c56d..f9b3c44 100644 --- a/Makefile +++ b/Makefile @@ -56,7 +56,7 @@ endif # Directories # ------------------------------------------------------------------------------ SRC_DIR := src -VENDOR_DIRS := vendor/imgui-docking vendor/stb +VENDOR_DIRS := vendor/imgui-docking vendor/stb vendor/ImGuizmo BUILD_DIR := build # ------------------------------------------------------------------------------ @@ -69,6 +69,7 @@ INCLUDE_DIRS := \ $(GLEW_INCLUDE) \ vendor/stb/include \ vendor/lua \ + vendor/ImGuizmo \ vendor/gcml # Add them to compiler flags: diff --git a/imgui.ini b/imgui.ini index 0309894..b2668ba 100644 --- a/imgui.ini +++ b/imgui.ini @@ -1,6 +1,6 @@ [Window][DockSpace] Pos=0,0 -Size=1280,720 +Size=1920,1177 Collapsed=0 [Window][Debug##Default] @@ -80,8 +80,8 @@ Collapsed=0 DockId=0x0000001F,0 [Window][Performance##performance] -Pos=8,360 -Size=335,352 +Pos=8,581 +Size=335,588 Collapsed=0 DockId=0x0000001C,0 @@ -105,7 +105,7 @@ DockId=0x0000000F,0 [Window][Scene Window##SceneWindow] Pos=8,28 -Size=335,330 +Size=335,551 Collapsed=0 DockId=0x0000001B,0 @@ -134,26 +134,26 @@ Collapsed=0 DockId=0x0000001E,0 [Window][ Logger##logger] -Pos=345,389 -Size=530,323 +Pos=345,846 +Size=1170,323 Collapsed=0 DockId=0x00000025,0 [Window][ Editor##EditorWindow] Pos=345,28 -Size=530,359 +Size=1170,816 Collapsed=0 DockId=0x0000001F,0 [Window][ Inspector##InspectorWindow] -Pos=877,28 -Size=395,684 +Pos=1517,28 +Size=395,1141 Collapsed=0 DockId=0x00000022,0 [Window][ Profiler] -Pos=345,389 -Size=530,323 +Pos=345,846 +Size=1170,323 Collapsed=0 DockId=0x00000025,1 @@ -170,7 +170,7 @@ Column 2 Weight=0.9665 Column 3 Weight=0.6950 [Docking][Data] -DockSpace ID=0x14621557 Window=0x3DA2F1DE Pos=302,345 Size=1264,684 Split=X Selected=0xF7365A5A +DockSpace ID=0x14621557 Window=0x3DA2F1DE Pos=8,51 Size=1904,1141 Split=X Selected=0xF7365A5A DockNode ID=0x00000020 Parent=0x14621557 SizeRef=884,684 Split=X DockNode ID=0x00000013 Parent=0x00000020 SizeRef=335,1142 Split=Y Selected=0x818D04BB DockNode ID=0x0000001B Parent=0x00000013 SizeRef=264,551 HiddenTabBar=1 Selected=0x1D5D92B6 @@ -193,7 +193,7 @@ DockSpace ID=0x14621557 Window=0x3DA2F1DE Pos=302,345 Si DockNode ID=0x00000017 Parent=0x0000000D SizeRef=1202,776 Split=Y Selected=0xDFF75B3F DockNode ID=0x0000001D Parent=0x00000017 SizeRef=518,720 Split=Y Selected=0x9A7B23B9 DockNode ID=0x0000001F Parent=0x0000001D SizeRef=549,359 CentralNode=1 HiddenTabBar=1 Selected=0x9A7B23B9 - DockNode ID=0x00000025 Parent=0x0000001D SizeRef=549,323 Selected=0x1F29F1F5 + DockNode ID=0x00000025 Parent=0x0000001D SizeRef=549,323 Selected=0x7A66B86B DockNode ID=0x0000001E Parent=0x00000017 SizeRef=518,417 Selected=0xC74E1AEE DockNode ID=0x00000018 Parent=0x0000000D SizeRef=1202,364 Split=X Selected=0x1C0788A1 DockNode ID=0x00000019 Parent=0x00000018 SizeRef=601,364 Selected=0x1C0788A1 diff --git a/src/Componenets/CameraComponent.cpp b/src/Components/CameraComponent.cpp similarity index 100% rename from src/Componenets/CameraComponent.cpp rename to src/Components/CameraComponent.cpp diff --git a/src/Componenets/CameraComponent.h b/src/Components/CameraComponent.h similarity index 100% rename from src/Componenets/CameraComponent.h rename to src/Components/CameraComponent.h diff --git a/src/Componenets/Component.h b/src/Components/Component.h similarity index 100% rename from src/Componenets/Component.h rename to src/Components/Component.h diff --git a/src/Componenets/GameObject.cpp b/src/Components/GameObject.cpp similarity index 100% rename from src/Componenets/GameObject.cpp rename to src/Components/GameObject.cpp diff --git a/src/Componenets/GameObject.h b/src/Components/GameObject.h similarity index 100% rename from src/Componenets/GameObject.h rename to src/Components/GameObject.h diff --git a/src/Componenets/Mesh.cpp b/src/Components/Mesh.cpp similarity index 100% rename from src/Componenets/Mesh.cpp rename to src/Components/Mesh.cpp diff --git a/src/Componenets/Mesh.h b/src/Components/Mesh.h similarity index 100% rename from src/Componenets/Mesh.h rename to src/Components/Mesh.h diff --git a/src/Componenets/ScriptComponent.cpp b/src/Components/ScriptComponent.cpp similarity index 100% rename from src/Componenets/ScriptComponent.cpp rename to src/Components/ScriptComponent.cpp diff --git a/src/Componenets/ScriptComponent.h b/src/Components/ScriptComponent.h similarity index 100% rename from src/Componenets/ScriptComponent.h rename to src/Components/ScriptComponent.h diff --git a/src/Componenets/Transform.cpp b/src/Components/Transform.cpp similarity index 53% rename from src/Componenets/Transform.cpp rename to src/Components/Transform.cpp index f1aea19..5a75e5c 100644 --- a/src/Componenets/Transform.cpp +++ b/src/Components/Transform.cpp @@ -1,5 +1,10 @@ // TransformComponent.cpp #include "Transform.h" +#define GLM_ENABLE_EXPERIMENTAL +#include +#include +#include +#include const std::string TransformComponent::name = "Transform"; @@ -8,16 +13,15 @@ TransformComponent::TransformComponent() { position = glm::vec3(0.0f, 0.0f, 0.0f); rotation = glm::vec3(0.0f, 0.0f, 0.0f); - scale = glm::vec3(1.0f, 1.0f, 1.0f); - + scale = glm::vec3(1.0f, 1.0f, 1.0f); } -const std::string& TransformComponent::GetName() const +const std::string &TransformComponent::GetName() const { return name; } -const std::string& TransformComponent::GetStaticName() +const std::string &TransformComponent::GetStaticName() { return name; } @@ -28,6 +32,43 @@ void TransformComponent::Update(float _deltaTime) return; } +// New Methods +glm::mat4 TransformComponent::GetTransformMatrix() const +{ + glm::mat4 translation = glm::translate(glm::mat4(1.0f), position); + + // Convert Euler angles (in degrees) to a quaternion and then to a rotation matrix + glm::quat quatRotation = glm::quat(glm::radians(rotation)); + glm::mat4 rotationMatrix = glm::toMat4(quatRotation); + + glm::mat4 scaling = glm::scale(glm::mat4(1.0f), scale); + + // Note: The order is Scale -> Rotate -> Translate + return translation * rotationMatrix * scaling; +} + +void TransformComponent::SetTransformMatrix(const glm::mat4 &transform) +{ + // Extract translation + position = glm::vec3(transform[3]); + + // Extract scale factors + scale.x = glm::length(glm::vec3(transform[0])); + scale.y = glm::length(glm::vec3(transform[1])); + scale.z = glm::length(glm::vec3(transform[2])); + + // Remove scale from rotation matrix + glm::mat4 rotationMatrix = transform; + rotationMatrix[0] /= scale.x; + rotationMatrix[1] /= scale.y; + rotationMatrix[2] /= scale.z; + + // Convert rotation matrix to quaternion and then to Euler angles (in degrees) + glm::quat quatRotation = glm::quat_cast(rotationMatrix); + glm::vec3 euler = glm::degrees(glm::eulerAngles(quatRotation)); + rotation = euler; +} + YAML::Node TransformComponent::Serialize() { YAML::Node node; @@ -65,7 +106,7 @@ YAML::Node TransformComponent::Serialize() return node; } -void TransformComponent::Deserialize(const YAML::Node& node) +void TransformComponent::Deserialize(const YAML::Node &node) { if (node["Position"]) { diff --git a/src/Componenets/Transform.h b/src/Components/Transform.h similarity index 90% rename from src/Componenets/Transform.h rename to src/Components/Transform.h index c93e2d1..f8a1eac 100644 --- a/src/Componenets/Transform.h +++ b/src/Components/Transform.h @@ -41,6 +41,9 @@ public: // Serialization methods virtual YAML::Node Serialize() override; virtual void Deserialize(const YAML::Node &node) override; + glm::mat4 GetTransformMatrix() const; + void SetTransformMatrix(const glm::mat4& transform); + private: static const std::string name; diff --git a/src/Engine.h b/src/Engine.h index da0dc81..2434951 100644 --- a/src/Engine.h +++ b/src/Engine.h @@ -12,9 +12,9 @@ #include "Windows/LuaEditorWindow.h" #include "Windows/ProfilerWindow.h" -#include "Componenets/GameObject.h" -#include "Componenets/Mesh.h" -#include "Componenets/Transform.h" +#include "Components/GameObject.h" +#include "Components/Mesh.h" +#include "Components/Transform.h" #include "Engine/AssetManager.h" #include "Engine/ThemeManager.h" diff --git a/src/Engine/LuaAPI.cpp b/src/Engine/LuaAPI.cpp index 112876a..0bb3ade 100644 --- a/src/Engine/LuaAPI.cpp +++ b/src/Engine/LuaAPI.cpp @@ -3,11 +3,11 @@ #include "LuaAPI.h" #include "LuaMacros.h" // Include the macros for binding #include "gcml.h" // Include gcml.h for DEBUG_PRINT macros -#include "Componenets/Component.h" -#include "Componenets/Transform.h" -#include "Componenets/Mesh.h" -#include "Componenets/ScriptComponent.h" -#include "Componenets/GameObject.h" +#include "Components/Component.h" +#include "Components/Transform.h" +#include "Components/Mesh.h" +#include "Components/ScriptComponent.h" +#include "Components/GameObject.h" #include "Windows/LoggerWindow.h" #include "Engine/InputManager.h" diff --git a/src/Engine/SceneManager.cpp b/src/Engine/SceneManager.cpp index eecd487..a1ef8c3 100644 --- a/src/Engine/SceneManager.cpp +++ b/src/Engine/SceneManager.cpp @@ -1,9 +1,9 @@ #include "SceneManager.h" -#include "./Componenets/Component.h" -#include "./Componenets/Transform.h" -#include "./Componenets/Mesh.h" -#include "./Componenets/GameObject.h" +#include "./Components/Component.h" +#include "./Components/Transform.h" +#include "./Components/Mesh.h" +#include "./Components/GameObject.h" #include "imgui.h" diff --git a/src/Engine/SceneManager.h b/src/Engine/SceneManager.h index b3f63da..dd747cd 100644 --- a/src/Engine/SceneManager.h +++ b/src/Engine/SceneManager.h @@ -1,6 +1,6 @@ #pragma once #include -#include "Componenets/GameObject.h" +#include "Components/GameObject.h" class SceneManager diff --git a/src/Windows/InspectorWindow.h b/src/Windows/InspectorWindow.h index c8e6e79..e01eda6 100644 --- a/src/Windows/InspectorWindow.h +++ b/src/Windows/InspectorWindow.h @@ -5,11 +5,11 @@ #include "imgui.h" #include "gcml.h" -#include "Componenets/GameObject.h" -#include "Componenets/Mesh.h" -#include "Componenets/Transform.h" -#include "Componenets/ScriptComponent.h" -#include "Componenets/CameraComponent.h" +#include "Components/GameObject.h" +#include "Components/Mesh.h" +#include "Components/Transform.h" +#include "Components/ScriptComponent.h" +#include "Components/CameraComponent.h" diff --git a/src/Windows/RenderWindow.cpp b/src/Windows/RenderWindow.cpp index de3773f..35c56c1 100644 --- a/src/Windows/RenderWindow.cpp +++ b/src/Windows/RenderWindow.cpp @@ -1,44 +1,43 @@ -// RenderWindow.cpp - #include "RenderWindow.h" -#include // Add this line -#include -#include -#include #include "imgui.h" +#include +#include +#include +#include + +#include "Components/GameObject.h" +#include "Components/mesh.h" +#include "Components/transform.h" +#include "Engine/AssetManager.h" #include "Engine/Settings.h" +#include "Rendering/Shader.h" +#include "ImGuizmo.h" #include "gcml.h" - -#include "Componenets/GameObject.h" -#include "Componenets/mesh.h" -#include "Componenets/transform.h" - -extern std::vector> g_GameObjects; +#include "Icons.h" #define CAM_FOV 45.0f #define CAM_NEAR_PLAIN 0.1f #define CAM_FAR_PLAIN 2048.0f -// Include your AssetManager & Shader headers -#include "Engine/AssetManager.h" -#include "Rendering/Shader.h" - -#include "Icons.h" - -// Extern reference to our global (or extern) asset manager +// Managers extern AssetManager g_AssetManager; extern Settings g_SettingsManager; -extern std::shared_ptr g_RuntimeCameraObject; +// Settings +extern bool DrawBBBO; +// GameObjects +extern std::vector> g_GameObjects; +extern std::shared_ptr g_RuntimeCameraObject; +extern GameObject *g_SelectedObject; + +// Profilers extern int g_GPU_Triangles_drawn_to_screen; extern int g_GPU_Draw_Calls; -extern bool DrawBBBO; - // Simple container for six planes struct FrustumPlanes { @@ -212,23 +211,43 @@ bool PlayPauseButton(const char *label, bool *isPlaying, ImVec2 Size) return false; // No toggle occurred } +// Enum for gizmo operations +enum GizmoOperation +{ + GIZMO_TRANSLATE, + GIZMO_ROTATE, + GIZMO_SCALE +}; + +// Initialize with a default operation +GizmoOperation currentOperation = GIZMO_TRANSLATE; + void RenderWindow::Show(bool *GameRunning) { + // Begin the ImGui window with an icon and label ImGui::Begin(ICON_FA_GAMEPAD " Editor##EditorWindow"); + ImGuizmo::BeginFrame(); + + ImGuizmo::SetOrthographic(false); + ImGuizmo::SetDrawlist(); + + // Initialize OpenGL resources if not already done if (!m_Initialized) { InitGLResources(); m_Initialized = true; } + // Get the available size for rendering within the window ImVec2 size = ImGui::GetContentRegionAvail(); int w = static_cast(size.x); int h = static_cast(size.y); - // If there's space, render to the FBO, then show it as an ImGui image + // Check if there's space to render if (w > 0 && h > 0) { + // Resize the FBO if the window size has changed if (w != m_LastWidth || h != m_LastHeight) { m_FBO.Create(w, h); @@ -236,33 +255,126 @@ void RenderWindow::Show(bool *GameRunning) m_LastHeight = h; } + // Render the 3D scene to the FBO RenderSceneToFBO(GameRunning); - // Render the image first + // Display the rendered scene as an image in ImGui + // Correctly cast the texture ID for OpenGL ImGui::Image(m_FBO.GetTextureID(), size, ImVec2(0, 0), ImVec2(1, 1)); // Calculate button position to place it slightly right and down from the top-left of the image ImVec2 imagePos = ImGui::GetItemRectMin(); // Add an offset to position the button - ImVec2 buttonOffset(10.0f, 10.0f); // Adjust these values as needed for the desired offset + ImVec2 buttonOffset(10.0f, 10.0f); // 10 pixels right and 10 pixels down ImVec2 buttonPos = ImVec2(imagePos.x + buttonOffset.x, imagePos.y + buttonOffset.y); // Set cursor position for the button ImGui::SetCursorScreenPos(buttonPos); // Dynamically calculate button size based on window size - float buttonWidth = size.x * 0.03f; // 5% of the window width + float buttonWidth = size.x * 0.05f; // 5% of the window width ImVec2 buttonSize = ImVec2(buttonWidth, buttonWidth); // Render the Play/Pause button with the calculated size PlayPauseButton("##PlayPauseButton", GameRunning, buttonSize); + + // --------------------------------------------------- + // *** Gizmo Operation Selection UI *** + // --------------------------------------------------- + + // --------------------------------------------------- + // *** Integrate ImGuizmo for Manipulating Objects *** + // --------------------------------------------------- + + // Ensure a GameObject is selected + if (g_SelectedObject) + { + // Retrieve the TransformComponent from the selected object + auto transform = g_SelectedObject->GetComponent(); + if (transform) + { + // Get the current transformation matrix + glm::mat4 modelMatrix = transform->GetTransformMatrix(); + + // Obtain view and projection matrices from the active camera + glm::mat4 viewMatrix; + glm::mat4 projectionMatrix; + + if (m_ActiveCamera) // Ensure m_ActiveCamera is correctly initialized + { + viewMatrix = m_ActiveCamera->GetViewMatrix(); + projectionMatrix = m_ActiveCamera->GetProjectionMatrix(); + } + else + { + // Fallback view matrix + viewMatrix = glm::lookAt(glm::vec3(0.0f, 0.0f, 5.0f), + glm::vec3(0.0f, 0.0f, 0.0f), + glm::vec3(0.0f, 1.0f, 0.0f)); + + // Fallback projection matrix + float aspect = (h != 0) ? static_cast(w) / static_cast(h) : 1.0f; + projectionMatrix = glm::perspective(glm::radians(45.0f), aspect, 0.1f, 100.0f); + } + + static ImGuizmo::OPERATION currentOperation = ImGuizmo::TRANSLATE; + + if (ImGui::IsWindowFocused() && ImGui::IsWindowHovered()) + { + + if (ImGui::IsKeyPressed(ImGuiKey_T)) + { + currentOperation = ImGuizmo::TRANSLATE; + } + if (ImGui::IsKeyPressed(ImGuiKey_R)) + { + currentOperation = ImGuizmo::ROTATE; + } + + if (ImGui::IsKeyPressed(ImGuiKey_S)) + { + currentOperation = ImGuizmo::SCALE; + } + } + + // Define snap settings + bool snap = false; // Enable snapping if needed + float snapValue = 0.1f; // Snap increment + + // Set the ImGuizmo rectangle to the window's position and size + ImVec2 windowPos = ImGui::GetWindowPos(); + ImVec2 windowSize = ImGui::GetWindowSize(); + ImGuizmo::SetRect(windowPos.x, windowPos.y, windowSize.x, windowSize.y); + + // Render the gizmo and handle user interaction + projectionMatrix[1][1] *= -1.0f; // Flip Image Internaly + ImGuizmo::Manipulate( + glm::value_ptr(viewMatrix), + glm::value_ptr(projectionMatrix), + currentOperation, + ImGuizmo::LOCAL, + glm::value_ptr(modelMatrix), + nullptr, // Optional delta matrix + snap ? &snapValue : nullptr // Optional snap values + ); + + // Check if the gizmo is being used (i.e., if the user is interacting with it) + if (ImGuizmo::IsUsing()) + { + // Update the TransformComponent with the modified matrix + transform->SetTransformMatrix(modelMatrix); + } + } + } } else { + // Display message if there's insufficient space to render the scene ImGui::Text("No space to render."); } + // End the ImGui window ImGui::End(); } @@ -272,6 +384,9 @@ void RenderWindow::InitGLResources() // 1) Load SHADER from the asset manager // ---------------------------------------------------- + // throw this in here cus we dont have a constructor + m_ActiveCamera = nullptr; + { std::shared_ptr shaderAsset = g_AssetManager.loadAsset(AssetType::SHADER, "assets/shaders/UnlitMaterial"); if (!shaderAsset) @@ -370,8 +485,10 @@ void RenderWindow::RenderSceneToFBO(bool *GameRunning) if (activeCamera) { + // TODO: Add camera Movement in editor view = activeCamera->GetViewMatrix(); proj = activeCamera->GetProjectionMatrix(); + m_ActiveCamera = activeCamera; } else { @@ -379,6 +496,7 @@ void RenderWindow::RenderSceneToFBO(bool *GameRunning) view = glm::translate(glm::mat4(1.f), glm::vec3(0.f, 0.f, -5.f)); float aspect = (m_LastHeight != 0) ? (float)m_LastWidth / (float)m_LastHeight : 1.0f; proj = glm::perspective(glm::radians(CAM_FOV), aspect, CAM_NEAR_PLAIN, CAM_FAR_PLAIN); + m_ActiveCamera = nullptr; } // 4) Extract frustum planes for culling diff --git a/src/Windows/RenderWindow.h b/src/Windows/RenderWindow.h index 45b947e..1553cb0 100644 --- a/src/Windows/RenderWindow.h +++ b/src/Windows/RenderWindow.h @@ -2,8 +2,9 @@ #include "../Rendering/FBO.h" #include - +#include #include "Rendering/Shader.h" // +#include "Components/CameraComponent.h" class RenderWindow { @@ -14,6 +15,8 @@ private: void InitGLResources(); void RenderSceneToFBO(bool *GameRunning); + std::shared_ptr m_ActiveCamera; + // Offscreen render target FBO m_FBO; diff --git a/src/Windows/SceneWindow.h b/src/Windows/SceneWindow.h index 94fd9e2..54b3710 100644 --- a/src/Windows/SceneWindow.h +++ b/src/Windows/SceneWindow.h @@ -3,7 +3,7 @@ #include #include -#include "Componenets/GameObject.h" +#include "Components/GameObject.h" // SceneWindow class declaration