From 1b7fe0736431eab0e68eece98d67e1bf085eaaa2 Mon Sep 17 00:00:00 2001 From: kj16609 Date: Tue, 30 Jul 2024 02:21:23 -0400 Subject: [PATCH] Cleanup GUI systems --- shaders/textured-gbuffer.frag | 3 +- src/engine/EngineContext.cpp | 19 ++-- src/engine/camera/Camera.cpp | 15 ++++ src/engine/camera/Camera.hpp | 9 +- src/engine/camera/CameraManager.cpp | 2 + src/engine/gui/camera.cpp | 13 ++- src/engine/gui/core.cpp | 24 +++-- src/engine/gui/dockspace.hpp | 55 ------------ src/engine/gui/gui_window_names.hpp | 16 ++++ src/engine/gui/preview.cpp | 130 ++++++++++++++++----------- src/engine/gui/safe_include.hpp | 28 ++++++ src/engine/systems/CullingSystem.cpp | 1 - 12 files changed, 178 insertions(+), 137 deletions(-) delete mode 100644 src/engine/gui/dockspace.hpp create mode 100644 src/engine/gui/gui_window_names.hpp diff --git a/shaders/textured-gbuffer.frag b/shaders/textured-gbuffer.frag index db17028..67e725d 100644 --- a/shaders/textured-gbuffer.frag +++ b/shaders/textured-gbuffer.frag @@ -15,7 +15,7 @@ layout (set = 1, binding = 0) uniform CameraInfo { mat4 projection; mat4 view; mat4 inverse_view; -} ubo; +} camera_info; layout (set = 2, binding = 0) uniform sampler2D tex[]; @@ -43,7 +43,6 @@ void main() discard; } - out_albedo = tex_value; out_position.a = linearDepth(out_position.z); diff --git a/src/engine/EngineContext.cpp b/src/engine/EngineContext.cpp index 1eed1ba..3d8930b 100644 --- a/src/engine/EngineContext.cpp +++ b/src/engine/EngineContext.cpp @@ -22,7 +22,6 @@ #include "engine/debug/drawers.hpp" #include "engine/literals/size.hpp" #include "engine/model/prebuilt/terrainModel.hpp" -#include "engine/systems/EntityRendererSystem.hpp" #include "gui/core.hpp" #include "model/builders/SceneBuilder.hpp" @@ -108,19 +107,19 @@ namespace fgl::engine //camera.setOrthographicProjection( -aspect, aspect, -1, 1, -1, 1 ); const float aspect { m_renderer.getAspectRatio() }; - auto& primary_camera { camera_manager.getPrimary() }; - auto secondary_camera { camera_manager.createCamera( { 1920, 1080 } ) }; + auto& editor_camera { camera_manager.getPrimary() }; + // auto secondary_camera { camera_manager.createCamera( { 1920, 1080 } ) }; - primary_camera + editor_camera .setPerspectiveProjection( glm::radians( 90.0f ), aspect, constants::NEAR_PLANE, constants::FAR_PLANE ); - secondary_camera - ->setPerspectiveProjection( glm::radians( 90.0f ), aspect, constants::NEAR_PLANE, constants::FAR_PLANE ); + // secondary_camera + // ->setPerspectiveProjection( glm::radians( 90.0f ), aspect, constants::NEAR_PLANE, constants::FAR_PLANE ); const auto old_aspect_ratio { m_renderer.getAspectRatio() }; camera_controller.moveInPlaneXZ( m_window.window(), 0.0, viewer ); - primary_camera.setView( viewer.getPosition(), viewer.getRotation() ); - secondary_camera->setView( viewer.getPosition(), viewer.getRotation() ); + editor_camera.setView( viewer.getPosition(), viewer.getRotation() ); + // secondary_camera->setView( viewer.getPosition(), viewer.getRotation() ); TracyCZoneEnd( TRACY_PrepareEngine ); @@ -149,12 +148,12 @@ namespace fgl::engine if ( old_aspect_ratio != m_renderer.getAspectRatio() ) { - primary_camera.setPerspectiveProjection( + editor_camera.setPerspectiveProjection( glm::radians( 90.0f ), m_renderer.getAspectRatio(), constants::NEAR_PLANE, constants::FAR_PLANE ); } camera_controller.moveInPlaneXZ( m_window.window(), delta_time, viewer ); - primary_camera.setView( viewer.getPosition(), viewer.getRotation() ); + editor_camera.setView( viewer.getPosition(), viewer.getRotation() ); if ( auto& command_buffer = m_renderer.beginFrame(); *command_buffer ) { diff --git a/src/engine/camera/Camera.cpp b/src/engine/camera/Camera.cpp index c042c44..94be067 100644 --- a/src/engine/camera/Camera.cpp +++ b/src/engine/camera/Camera.cpp @@ -123,6 +123,11 @@ namespace fgl::engine command_buffer.endRenderPass(); } + void Camera::setName( const std::string_view str ) + { + name = str; + } + void Camera:: copyOutput( const vk::raii::CommandBuffer& command_buffer, const FrameIndex frame_index, Image& target ) { @@ -278,6 +283,11 @@ namespace fgl::engine frustum = translation_matrix * base_frustum; } + const std::string& Camera::getName() const + { + return name; + } + void Camera::initCameraRenderer() { assert( !m_renderer ); @@ -376,4 +386,9 @@ namespace fgl::engine Camera::~Camera() {} + CameraIDX Camera::getIDX() const + { + return camera_idx; + } + } // namespace fgl::engine diff --git a/src/engine/camera/Camera.hpp b/src/engine/camera/Camera.hpp index 5d07c7c..a779612 100644 --- a/src/engine/camera/Camera.hpp +++ b/src/engine/camera/Camera.hpp @@ -40,6 +40,7 @@ namespace fgl::engine using CameraIDX = std::uint8_t; + class Camera { inline static CameraIDX camera_counter { 0 }; @@ -68,6 +69,8 @@ namespace fgl::engine inline static std::unique_ptr< CameraRenderer > m_renderer; std::shared_ptr< CameraSwapchain > m_swapchain; + std::string name; + Matrix< MatrixType::ModelToWorld > frustumTranslationMatrix() const; void updateFrustum(); @@ -82,7 +85,9 @@ namespace fgl::engine ~Camera(); - CameraIDX getIDX() { return camera_idx; } + CameraIDX getIDX() const; + + const std::string& getName() const; static void initCameraRenderer(); @@ -153,6 +158,8 @@ namespace fgl::engine void beginRenderpass( const vk::raii::CommandBuffer& command_buffer, const FrameInfo& info ); void endRenderpass( const vk::raii::CommandBuffer& command_buffer ); + void setName( std::string_view str ); + void copyOutput( const vk::raii::CommandBuffer& command_buffer, FrameIndex frame_index, Image& target ); }; diff --git a/src/engine/camera/CameraManager.cpp b/src/engine/camera/CameraManager.cpp index 69e11d0..d3aae89 100644 --- a/src/engine/camera/CameraManager.cpp +++ b/src/engine/camera/CameraManager.cpp @@ -6,6 +6,7 @@ #include "Camera.hpp" #include "engine/debug/drawers.hpp" +#include "engine/gui/gui_window_names.hpp" #include "engine/literals/size.hpp" namespace fgl::engine @@ -30,6 +31,7 @@ namespace fgl::engine debug::setDebugDrawingCamera( getPrimary() ); m_primary_camera = createCamera( { 1920, 1080 } ); + m_primary_camera->setName( CAMERA_EDITOR_NAME ); } std::shared_ptr< Camera > CameraManager::createCamera( const vk::Extent2D extent ) diff --git a/src/engine/gui/camera.cpp b/src/engine/gui/camera.cpp index 209e7b8..2e763b9 100644 --- a/src/engine/gui/camera.cpp +++ b/src/engine/gui/camera.cpp @@ -30,8 +30,17 @@ namespace fgl::engine::gui Camera& camera { *camera_ptr }; - const auto name { std::format( "Camera: {}", camera.getIDX() ) }; - ImGui::Begin( name.c_str() ); + std::string name { "" }; + + if ( camera.getName() == "" ) + name = std::format( "Camera: ID {}", camera.getIDX() ); + else + name = camera.getName(); + + ImGui::Begin( + name.c_str(), + nullptr, + ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_MenuBar ); drawRenderingOutputs( info, camera ); diff --git a/src/engine/gui/core.cpp b/src/engine/gui/core.cpp index ff238ff..c517f7b 100644 --- a/src/engine/gui/core.cpp +++ b/src/engine/gui/core.cpp @@ -10,19 +10,18 @@ #pragma GCC diagnostic ignored "-Wconversion" #include #include - -#include #pragma GCC diagnostic pop #include -#include "dockspace.hpp" #include "engine/descriptors/DescriptorPool.hpp" #include "engine/filesystem/FileBrowser.hpp" #include "engine/model/Model.hpp" #include "engine/rendering/Device.hpp" #include "engine/rendering/Renderer.hpp" #include "engine/tree/octtree/OctTreeNode.hpp" +#include "gui_window_names.hpp" +#include "safe_include.hpp" namespace fgl::engine::gui { @@ -75,14 +74,11 @@ namespace fgl::engine::gui ImGui_ImplVulkan_NewFrame(); ImGui_ImplGlfw_NewFrame(); ImGui::NewFrame(); - - ImGui::Begin( "Main" ); } void endImGui( vk::raii::CommandBuffer& command_buffer ) { ZoneScoped; - ImGui::End(); ImGui::Render(); ImDrawData* data { ImGui::GetDrawData() }; @@ -118,15 +114,15 @@ namespace fgl::engine::gui ImGuiID mv_node { primary_id }; //ImGuiID mv_node { ImGui::DockBuilderSplitNode( primary_id, ImGuiDir_Up, 1.0f - 0.3f, &primary_id, nullptr ) }; - ImGui::DockBuilderDockWindow( "Scene", lb_node ); + ImGui::DockBuilderDockWindow( OBJECT_TREE_VIEW_NAME, lb_node ); //ImGui::DockBuilderDockWindow( "Main", mv_node ); - ImGui::DockBuilderDockWindow( "Camera: 0", mv_node ); + ImGui::DockBuilderDockWindow( CAMERA_EDITOR_NAME, mv_node ); - ImGui::DockBuilderDockWindow( "Entity info", rb_node ); + ImGui::DockBuilderDockWindow( ENTITY_INFO_NAME, rb_node ); - ImGui::DockBuilderDockWindow( "File Picker", bb_node ); + ImGui::DockBuilderDockWindow( FILE_PICKER_NAME, bb_node ); ImGui::DockBuilderFinish( primary_id ); } @@ -139,7 +135,7 @@ namespace fgl::engine::gui // Docks seem utterly broken. ImGuiID primary_id { - ImGui::DockSpaceOverViewport( ImGui::GetID( "MainWindowDockspace" ), ImGui::GetMainViewport() ) + ImGui::DockSpaceOverViewport( ImGui::GetID( GUI_DOCKSPACE_NAME ), ImGui::GetMainViewport() ) }; // +--------------------------------------------------------------------+ // | | | | @@ -187,7 +183,7 @@ namespace fgl::engine::gui void drawEntityGUI( FrameInfo& info ) { ZoneScoped; - ImGui::Begin( "Scene" ); + ImGui::Begin( OBJECT_TREE_VIEW_NAME ); for ( OctTreeNodeLeaf* leaf : info.game_objects.getAllLeafs() ) { @@ -210,7 +206,7 @@ namespace fgl::engine::gui void drawEntityInfo( [[maybe_unused]] FrameInfo& info ) { ZoneScoped; - ImGui::Begin( "Entity info" ); + ImGui::Begin( ENTITY_INFO_NAME ); if ( selected_object ) selected_object->drawImGui(); @@ -220,7 +216,7 @@ namespace fgl::engine::gui void drawFilesystemGUI( FrameInfo& info ) { ZoneScoped; - ImGui::Begin( "File Picker", nullptr, ImGuiWindowFlags_MenuBar ); + ImGui::Begin( FILE_PICKER_NAME, nullptr, ImGuiWindowFlags_MenuBar ); filesystem::FileBrowser::drawGui( info ); diff --git a/src/engine/gui/dockspace.hpp b/src/engine/gui/dockspace.hpp deleted file mode 100644 index 3c79d72..0000000 --- a/src/engine/gui/dockspace.hpp +++ /dev/null @@ -1,55 +0,0 @@ -// -// Created by kj16609 on 7/28/24. -// - -#pragma once - - -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Weffc++" -#pragma GCC diagnostic ignored "-Wold-style-cast" -#pragma GCC diagnostic ignored "-Wconversion" -#include -#pragma GCC diagnostic pop - -namespace ImGui -{ - ImGuiID DockSpaceOverViewportEx(const ImGuiViewport* viewport, ImGuiDockNodeFlags dockspace_flags, const ImGuiWindowClass* window_class, auto DockBuilder) - { - if (viewport == NULL) - viewport = GetMainViewport(); - - SetNextWindowPos(viewport->WorkPos); - SetNextWindowSize(viewport->WorkSize); - SetNextWindowViewport(viewport->ID); - - ImGuiWindowFlags host_window_flags = 0; - host_window_flags |= ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoDocking; - host_window_flags |= ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoNavFocus; - if (dockspace_flags & ImGuiDockNodeFlags_PassthruCentralNode) - host_window_flags |= ImGuiWindowFlags_NoBackground; - - char label[32]; - ImFormatString(label, IM_ARRAYSIZE(label), "DockSpaceViewport_%08X", viewport->ID); - - PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f); - PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f); - PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f)); - Begin(label, NULL, host_window_flags); - PopStyleVar(3); - - ImGuiID dockspace_id = GetID("DockSpace"); - - /// vvvv ---- call DockBuilder callback if no docking exists --- vvvv - if(DockBuilderGetNode(dockspace_id) == nullptr) - { - DockBuilder(dockspace_id); - } - /// ^^^^ ---------------------------------------------------- ^^^^ - - DockSpace(dockspace_id, ImVec2(0.0f, 0.0f), dockspace_flags, window_class); - End(); - - return dockspace_id; - } -} diff --git a/src/engine/gui/gui_window_names.hpp b/src/engine/gui/gui_window_names.hpp new file mode 100644 index 0000000..26e4ed0 --- /dev/null +++ b/src/engine/gui/gui_window_names.hpp @@ -0,0 +1,16 @@ +// +// Created by kj16609 on 7/29/24. +// + +#pragma once + +#include + +namespace fgl::engine +{ + constexpr std::string_view GUI_DOCKSPACE_NAME { "MainWindowDockspace" }; + constexpr std::string_view CAMERA_EDITOR_NAME { "Scene" }; + constexpr std::string_view FILE_PICKER_NAME { "File Browser" }; + constexpr std::string_view OBJECT_TREE_VIEW_NAME { "Object List" }; + constexpr std::string_view ENTITY_INFO_NAME { "Entiy Info" }; +} // namespace fgl::engine \ No newline at end of file diff --git a/src/engine/gui/preview.cpp b/src/engine/gui/preview.cpp index 7db1ce6..7953bc0 100644 --- a/src/engine/gui/preview.cpp +++ b/src/engine/gui/preview.cpp @@ -106,83 +106,109 @@ namespace fgl::engine::gui float height_size { static_cast< float >( max_extent.height ) }; float width_size { ratio * static_cast< float >( max_extent.height ) }; - // If height is larger then the size then we need to compute the width from the height max + // If the width is higher then the max extent, Then we wanna use the width instead if ( width_size > max_extent.width ) { width_size = static_cast< float >( max_extent.width ); height_size = static_cast< float >( max_extent.width ) / ratio; } + assert( width_size <= max_extent.width ); + assert( height_size <= max_extent.height ); + return { static_cast< std::uint32_t >( width_size ), static_cast< std::uint32_t >( height_size ) }; } - FGL_FORCE_INLINE_FLATTEN vk::Extent2D calculateTargetSize( const float ratio, const ImVec2 max_extent ) + FGL_FORCE_INLINE_FLATTEN inline vk::Extent2D calculateTargetSize( const float ratio, const ImVec2 max_extent ) { return calculateTargetSize( ratio, vk::Extent2D( max_extent.x, max_extent.y ) ); } + enum RenderingOutputSelection : std::uint_fast8_t + { + Composite = 0, + Albedo = 1, + Normal = 2, + Position = 3 + }; + + inline static bool hotkeys_enabled { true }; + + inline void drawConfigBar( + [[maybe_unused]] const FrameInfo& info, + [[maybe_unused]] const Camera& camera, + [[maybe_unused]] const FrameIndex frame_index, + std::uint_fast8_t& current ) + { + static constexpr char* const options[] = { "Composite", "Albedo", "Normal", "Position" }; + if ( ImGui::BeginMenuBar() ) + { + if ( ImGui::BeginMenu( "Output" ) ) + { + if ( ImGui::MenuItem( options[ Composite ], "Alt+1", current == Composite, true ) ) + { + current = Composite; + } + + if ( ImGui::MenuItem( options[ Albedo ], "Alt+2", current == Albedo, true ) ) + { + current = Albedo; + } + + if ( ImGui::MenuItem( options[ Normal ], "Alt+3", current == Normal, true ) ) + { + current = Normal; + } + + if ( ImGui::MenuItem( options[ Position ], "Alt+4", current == Position, true ) ) + { + current = Position; + } + + if ( ImGui::MenuItem( "Enable Hotkeys" ), nullptr, hotkeys_enabled ) + { + hotkeys_enabled = !hotkeys_enabled; + } + + ImGui::EndMenu(); + } + + ImGui::EndMenuBar(); + } + } + void drawRenderingOutputs( FrameInfo& info, const Camera& camera ) { ZoneScoped; const auto frame_index { info.frame_idx }; - enum RenderingOutputSelection : std::uint_fast8_t - { - Composite = 0, - Albedo = 1, - Normal = 2, - Position = 3 - }; - - static const char* const options[] = { "Composite", "Albedo", "Normal", "Position" }; static std::uint_fast8_t current { Composite }; - if ( ImGui::BeginCombo( "Rendering Output", options[ current ] ) ) - { - constexpr vk::Extent2D desired_size { 64, 64 }; - //Calculate size - const float ratio { info.swap_chain.extentAspectRatio() }; - const auto size { calculateTargetSize( ratio, desired_size ) }; - - //Composite - if ( ImGui::Selectable( options[ Composite ], current == Composite ) ) - { - log::debug( "Changing output to Compositite" ); - current = Composite; - } - - camera.getSwapchain().g_buffer_albedo_img[ frame_index ]->drawImGui( size ); - ImGui::SameLine(); - if ( ImGui::Selectable( options[ Albedo ], current == Albedo ) ) - { - log::debug( "Changing output to Albedo" ); - current = Albedo; - } - - camera.getSwapchain().g_buffer_normal_img[ frame_index ]->drawImGui( size ); - ImGui::SameLine(); - if ( ImGui::Selectable( options[ Normal ], current == Normal ) ) - { - log::debug( "Changing output to Normal" ); - current = Normal; - } - - camera.getSwapchain().g_buffer_position_img[ frame_index ]->drawImGui( size ); - ImGui::SameLine(); - if ( ImGui::Selectable( options[ Position ], current == Position ) ) - { - log::debug( "Changing output to Position" ); - current = Position; - } - - ImGui::EndCombo(); - } + drawConfigBar( info, camera, frame_index, current ); const float ratio { info.swap_chain.extentAspectRatio() }; const auto imgui_size { ImGui::GetWindowSize() }; const auto target_size { calculateTargetSize( ratio, imgui_size ) }; - //Compute optimal size using aspect ratio + if ( hotkeys_enabled && ImGui::IsWindowFocused() && ImGui::IsKeyDown( ImGuiKey_LeftAlt ) ) + { + if ( ImGui::IsKeyPressed( ImGuiKey_1 ) ) + { + current = Composite; + } + else if ( ImGui::IsKeyPressed( ImGuiKey_2 ) ) + { + current = Albedo; + } + else if ( ImGui::IsKeyPressed( ImGuiKey_3 ) ) + { + current = Normal; + } + else if ( ImGui::IsKeyPressed( ImGuiKey_4 ) ) + { + current = Position; + } + } switch ( current ) { diff --git a/src/engine/gui/safe_include.hpp b/src/engine/gui/safe_include.hpp index ab4b6b2..639dfa6 100644 --- a/src/engine/gui/safe_include.hpp +++ b/src/engine/gui/safe_include.hpp @@ -8,6 +8,34 @@ #pragma GCC diagnostic ignored "-Weffc++" #pragma GCC diagnostic ignored "-Wold-style-cast" #pragma GCC diagnostic ignored "-Wconversion" +// clang-format off #include #include +// clang-format on #pragma GCC diagnostic pop + +#include + +namespace ImGui +{ + + //void ImGui::DockBuilderDockWindow(const char* window_name, ImGuiID node_id) + FGL_FORCE_INLINE inline void DockBuilderDockWindow( const std::string_view str, ImGuiID node_id ) + { + ::ImGui::DockBuilderDockWindow( str.data(), node_id ); + } + + // IMGUI_API ImGuiID GetID(const char* str_id); // calculate unique ID (hash of whole ID stack + given parameter). e.g. if you want to query into ImGuiStorage yourself + FGL_FORCE_INLINE inline ImGuiID GetID( const std::string_view str ) + { + return ::ImGui::GetID( str.data() ); + } + + // IMGUI_API bool Begin(const char* name, bool* p_open = NULL, ImGuiWindowFlags flags = 0); + FGL_FORCE_INLINE inline bool + Begin( const std::string_view name, bool* p_open = nullptr, ImGuiWindowFlags flags = 0 ) + { + return ::ImGui::Begin( name.data(), p_open, flags ); + } + +} // namespace ImGui diff --git a/src/engine/systems/CullingSystem.cpp b/src/engine/systems/CullingSystem.cpp index fbfe0f7..6f4b8a7 100644 --- a/src/engine/systems/CullingSystem.cpp +++ b/src/engine/systems/CullingSystem.cpp @@ -8,7 +8,6 @@ #include "engine/FrameInfo.hpp" #include "engine/camera/Camera.hpp" -#include "engine/model/Model.hpp" #include "engine/tree/octtree/OctTreeNode.hpp" namespace fgl::engine