Added Gismos and Changed includes

This commit is contained in:
OusmBlueNinja 2025-01-04 22:04:21 -06:00
parent f90e601396
commit d375f8cd98
21 changed files with 231 additions and 65 deletions

View File

@ -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:

View File

@ -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

View File

@ -1,5 +1,10 @@
// TransformComponent.cpp
#include "Transform.h"
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/quaternion.hpp>
#include <glm/gtx/quaternion.hpp>
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"])
{

View File

@ -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;

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -1,6 +1,6 @@
#pragma once
#include <vector>
#include "Componenets/GameObject.h"
#include "Components/GameObject.h"
class SceneManager

View File

@ -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"

View File

@ -1,44 +1,43 @@
// RenderWindow.cpp
#include "RenderWindow.h"
#include <vector> // Add this line
#include <GL/glew.h>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "imgui.h"
#include <GL/glew.h>
#include <vector>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#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<std::shared_ptr<GameObject>> 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<CameraComponent> g_RuntimeCameraObject;
// Settings
extern bool DrawBBBO;
// GameObjects
extern std::vector<std::shared_ptr<GameObject>> g_GameObjects;
extern std::shared_ptr<CameraComponent> 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<int>(size.x);
int h = static_cast<int>(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<TransformComponent>();
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<float>(w) / static_cast<float>(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<Shader> shaderAsset = g_AssetManager.loadAsset<Shader>(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

View File

@ -2,8 +2,9 @@
#include "../Rendering/FBO.h"
#include <glm/glm.hpp>
#include <memory>
#include "Rendering/Shader.h" //
#include "Components/CameraComponent.h"
class RenderWindow
{
@ -14,6 +15,8 @@ private:
void InitGLResources();
void RenderSceneToFBO(bool *GameRunning);
std::shared_ptr<CameraComponent> m_ActiveCamera;
// Offscreen render target
FBO m_FBO;

View File

@ -3,7 +3,7 @@
#include <string>
#include <glm/glm.hpp>
#include "Componenets/GameObject.h"
#include "Components/GameObject.h"
// SceneWindow class declaration