From 82e0e4df5ce8ec81619bd1463e3ec94dce216c71 Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Wed, 2 Nov 2022 10:33:17 +0800 Subject: [PATCH] Added editor gridlines and cardinal axes --- .../src/Application/SBApplication.cpp | 10 +- SHADE_Engine/src/Editor/SHEditor.cpp | 104 +++++++++++++++++- SHADE_Engine/src/Editor/SHEditor.h | 9 +- .../MiddleEnd/Interface/SHDebugDrawSystem.cpp | 2 +- 4 files changed, 114 insertions(+), 11 deletions(-) diff --git a/SHADE_Application/src/Application/SBApplication.cpp b/SHADE_Application/src/Application/SBApplication.cpp index 8b437584..10cc09b3 100644 --- a/SHADE_Application/src/Application/SBApplication.cpp +++ b/SHADE_Application/src/Application/SBApplication.cpp @@ -72,6 +72,9 @@ namespace Sandbox SHSystemManager::CreateSystem(); SHSystemManager::CreateSystem(); SHSystemManager::CreateSystem(); + + // Link up SHDebugDraw + SHDebugDraw::Init(SHSystemManager::GetSystem()); #ifdef SHEDITOR SDL_Init(SDL_INIT_VIDEO); @@ -127,9 +130,6 @@ namespace Sandbox SHSceneManager::InitSceneManager("TestScene"); SHFrameRateController::UpdateFRC(); - - // Link up SHDebugDraw - SHDebugDraw::Init(SHSystemManager::GetSystem()); } void SBApplication::Update(void) @@ -146,10 +146,6 @@ namespace Sandbox if(editor->editorState == SHEditor::State::PLAY) SHSceneManager::SceneUpdate(0.016f); #endif - SHDebugDraw::Line(SHVec4(1.0f, 1.0f, 1.0f, 1.0f), SHVec3(-5.0f, 0.0f, 0.0f), SHVec3(5.0f, 0.0f, 0.0f)); - SHDebugDraw::Line(SHVec4(1.0f, 1.0f, 1.0f, 1.0f), SHVec3(-5.0f, 1.0f, 0.0f), SHVec3(5.0f, 1.0f, 0.0f)); - SHDebugDraw::Cube(SHVec4(1.0f, 1.0f, 1.0f, 1.0f), {}, SHVec3(1.0f, 1.0f, 1.0f)); - SHDebugDraw::Sphere(SHVec4(1.0f, 1.0f, 1.0f, 1.0f), {}, 3.0f); SHSystemManager::RunRoutines(editor->editorState != SHEditor::State::PLAY, 0.016f); editor->PollPicking(); } diff --git a/SHADE_Engine/src/Editor/SHEditor.cpp b/SHADE_Engine/src/Editor/SHEditor.cpp index cf5056a5..6259a7c6 100644 --- a/SHADE_Engine/src/Editor/SHEditor.cpp +++ b/SHADE_Engine/src/Editor/SHEditor.cpp @@ -46,6 +46,7 @@ #include #include "Graphics/MiddleEnd/Interface/SHMousePickSystem.h" +#include "Tools/SHDebugDraw.h" RTTR_REGISTRATION { @@ -77,7 +78,6 @@ namespace SHADE //#==============================================================# void SHEditor::Init() { - IMGUI_CHECKVERSION(); if(auto context = ImGui::CreateContext()) { @@ -120,6 +120,17 @@ namespace SHADE window->Init(); } + /* Editor View Gridlines */ + SetUpGridLines(true, true); + // Handle state changes so that we only show in edit mode + std::shared_ptr> stateChangeEventReceiver + { + std::make_shared>(this, &SHEditor::onEditorStateChanged) + }; + SHEventManager::SubscribeTo(SH_EDITOR_ON_PLAY_EVENT, std::dynamic_pointer_cast(stateChangeEventReceiver)); + SHEventManager::SubscribeTo(SH_EDITOR_ON_PAUSE_EVENT, std::dynamic_pointer_cast(stateChangeEventReceiver)); + SHEventManager::SubscribeTo(SH_EDITOR_ON_STOP_EVENT, std::dynamic_pointer_cast(stateChangeEventReceiver)); + SHLOG_INFO("Successfully initialised SHADE Engine Editor") } @@ -180,6 +191,97 @@ namespace SHADE io->Fonts->Build(); } + void SHEditor::SetUpGridLines(bool drawGrid, bool drawAxes) + { + // Clear existing lines + SHDebugDraw::ClearPersistentDraws(); + + static constexpr float DELTA = 1.0f; + static constexpr int EXTENT_COUNT = static_cast(500.0f /* TODO: Remove hard code */ / DELTA); + static constexpr float LINE_HALF_LENGTH = (DELTA * static_cast(EXTENT_COUNT)) * 0.5f; + + // Render Grid + static const SHColour GRID_COL = { 0.2f, 0.2f, 0.2f, 1.0f }; + if (drawGrid) + { + for (int i = 1; i < EXTENT_COUNT; ++i) + { + // X-Axis Lines + SHDebugDraw::PersistentLine + ( + GRID_COL, + SHVec3 { -LINE_HALF_LENGTH, 0.0f, i * DELTA }, + SHVec3 { LINE_HALF_LENGTH, 0.0f, i * DELTA } + ); + SHDebugDraw::PersistentLine + ( + GRID_COL, + SHVec3 { -LINE_HALF_LENGTH, 0.0f, i * -DELTA }, + SHVec3 { LINE_HALF_LENGTH, 0.0f, i * -DELTA } + ); + // Y-Axis Lines + SHDebugDraw::PersistentLine + ( + GRID_COL, + SHVec3 { i * DELTA, 0.0f, -LINE_HALF_LENGTH }, + SHVec3 { i * DELTA, 0.0f, LINE_HALF_LENGTH } + ); + SHDebugDraw::PersistentLine + ( + GRID_COL, + SHVec3 { -i * DELTA, 0.0f, -LINE_HALF_LENGTH }, + SHVec3 { -i * DELTA, 0.0f, LINE_HALF_LENGTH } + ); + } + } + + // Render World Axes + if (drawAxes || drawGrid) + { + const SHColour X_AXIS_COL = drawAxes ? SHColour::RED : GRID_COL; + const SHColour Y_AXIS_COL = drawAxes ? SHColour::GREEN : GRID_COL; + const SHColour Z_AXIS_COL = drawAxes ? SHColour::BLUE : GRID_COL; + // X + SHDebugDraw::PersistentLine + ( + X_AXIS_COL, + SHVec3 { -LINE_HALF_LENGTH, 0.0f, 0.0f }, + SHVec3 { LINE_HALF_LENGTH, 0.0f, 0.0f } + ); + // Y + SHDebugDraw::PersistentLine + ( + Y_AXIS_COL, + SHVec3 { 0.0f, -LINE_HALF_LENGTH, 0.0f }, + SHVec3 { 0.0f, LINE_HALF_LENGTH, 0.0f } + ); + // Z + SHDebugDraw::PersistentLine + ( + Z_AXIS_COL, + SHVec3 { 0.0f, 0.0f, -LINE_HALF_LENGTH }, + SHVec3 { 0.0f, 0.0f, LINE_HALF_LENGTH } + ); + } + } + + SHEventHandle SHEditor::onEditorStateChanged(SHEventPtr eventPtr) + { + auto eventData = reinterpret_cast*>(eventPtr.get()); + switch (editorState) + { + case State::PAUSE: + case State::STOP: + SetUpGridLines(true, true); + break; + case State::PLAY: + default: + SHDebugDraw::ClearPersistentDraws(); + break; + } + return eventData->handle; + } + void SHEditor::Exit() { for (const auto& window : SHEditorWindowManager::editorWindows | std::views::values) diff --git a/SHADE_Engine/src/Editor/SHEditor.h b/SHADE_Engine/src/Editor/SHEditor.h index 624069db..6e0ef5ae 100644 --- a/SHADE_Engine/src/Editor/SHEditor.h +++ b/SHADE_Engine/src/Editor/SHEditor.h @@ -16,8 +16,9 @@ #include "Resource/SHHandle.h" #include "EditorWindow/SHEditorWindow.h" #include "Tools/SHLog.h" -#include "Gizmos/SHTransformGizmo.h" - +#include "Gizmos/SHTransformGizmo.h"` +#include "Events/SHEventDefines.h" +#include "Events/SHEvent.h" //#==============================================================# //|| Library Includes || @@ -194,6 +195,10 @@ namespace SHADE void InitFonts() noexcept; + void SetUpGridLines(bool drawGrid, bool drawAxes); + + SHEventHandle onEditorStateChanged(SHEventPtr eventPtr); + // Handle to command pool used for ImGui Vulkan Backend Handle imguiCommandPool; // Handle to command buffer used for ImGui Vulkan Backend diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp index 16069564..90fe51d5 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp @@ -95,7 +95,7 @@ namespace SHADE // Get Current frame index const uint32_t FRAME_IDX = GFX_SYSTEM->GetCurrentFrameIndex(); - // Set Pipeline + // Set Pipelin cmdBuffer->BindPipeline(GFX_SYSTEM->GetDebugDrawPipeline()); cmdBuffer->SetLineWidth(LineWidth);