Add docking of main viewport

- Change ImGui branch to docking
- Rework code to handle viewport resizes instead of window resize
This commit is contained in:
Thraix
2023-07-13 23:41:20 +02:00
parent f9fb74ba6a
commit 65a86bd5a2
21 changed files with 143 additions and 89 deletions
+2
View File
@@ -198,6 +198,7 @@
<ClCompile Include="src\copium\event\MousePressEvent.cpp" />
<ClCompile Include="src\copium\event\MouseReleaseEvent.cpp" />
<ClCompile Include="src\copium\event\MouseScrollEvent.cpp" />
<ClCompile Include="src\copium\event\ViewportResize.cpp" />
<ClCompile Include="src\copium\event\WindowFocusEvent.cpp" />
<ClCompile Include="src\copium\event\WindowResizeEvent.cpp" />
<ClCompile Include="src\copium\mesh\Mesh.cpp" />
@@ -259,6 +260,7 @@
<ClInclude Include="src\copium\ecs\ECSManager.h" />
<ClInclude Include="src\copium\ecs\Entity.h" />
<ClInclude Include="src\copium\ecs\EntitySet.h" />
<ClInclude Include="src\copium\event\ViewportResize.h" />
<ClInclude Include="src\copium\example\AnimationSystem.h" />
<ClInclude Include="src\copium\example\DebugSystem.h" />
<ClInclude Include="src\copium\example\LevelGeneratorComponentListener.h" />
@@ -225,6 +225,9 @@
<ClCompile Include="src\copium\core\ImGuiInstance.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\copium\event\ViewportResize.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\copium\sampler\DepthAttachment.h">
@@ -530,5 +533,8 @@
<ClInclude Include="src\copium\core\ImGuiInstance.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\copium\event\ViewportResize.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
-5
View File
@@ -1,5 +0,0 @@
[Pipeline]
vert-filepath=res/shaders/passthrough.vert
frag-filepath=res/shaders/passthrough.frag
type=Passthrough
uuid=8fdcfe12-5c69-cf29-2f31-e177d6267e4e
-12
View File
@@ -1,12 +0,0 @@
#version 450
layout(set = 0, binding = 0) uniform sampler2D texSampler;
layout(location = 0) in vec2 inTexCoord;
layout(location = 0) out vec4 outColor;
void main()
{
outColor = texture(texSampler, inTexCoord);
}
-12
View File
@@ -1,12 +0,0 @@
#version 450
layout(location = 0) in vec2 inPosition;
layout(location = 0) out vec2 outTexCoord;
void main()
{
gl_Position = vec4(inPosition, 0.0, 1.0);
outTexCoord = inPosition * 0.5 + 0.5;
outTexCoord.y = 1.0 - outTexCoord.y;
}
+35 -42
View File
@@ -3,14 +3,14 @@
#include "copium/asset/AssetManager.h"
#include "copium/core/Vulkan.h"
#include "copium/event/EventDispatcher.h"
#include "copium/event/Input.h"
#include "copium/event/KeyPressEvent.h"
#include "copium/event/MouseMoveEvent.h"
#include "copium/event/MousePressEvent.h"
#include "copium/event/MouseScrollEvent.h"
#include "copium/event/ViewportResize.h"
#include "copium/event/WindowFocusEvent.h"
#include "copium/event/WindowResizeEvent.h"
#include "copium/mesh/Vertex.h"
#include "copium/mesh/VertexPassthrough.h"
#include "copium/sampler/Font.h"
#include <glm/gtc/matrix_transform.hpp>
@@ -35,17 +35,6 @@ namespace Copium
4, 5, 6, 6, 7, 4
};
const std::vector<VertexPassthrough> verticesPassthrough = {
VertexPassthrough{{-1.0f, -1.0f}},
VertexPassthrough{{-1.0f, 1.0f}},
VertexPassthrough{{ 1.0f, 1.0f}},
VertexPassthrough{{ 1.0f, -1.0f}},
};
const std::vector<uint16_t> indicesPassthrough = {
0, 1, 2, 2, 3, 0,
};
Application::Application()
{
EventDispatcher::AddEventHandler(this);
@@ -63,7 +52,6 @@ namespace Copium
vkDeviceWaitIdle(Vulkan::GetDevice());
AssetManager::UnloadAsset(texture2D);
AssetManager::UnloadAsset(graphicsPipeline);
AssetManager::UnloadAsset(graphicsPipelinePassthrough);
AssetManager::UnloadAsset(framebuffer);
EventDispatcher::RemoveEventHandler(this);
}
@@ -88,11 +76,10 @@ namespace Copium
scene->OnEvent(event);
switch (event.GetType())
{
case EventType::WindowResize:
case EventType::ViewportResize:
{
const WindowResizeEvent& windowResizeEvent = static_cast<const WindowResizeEvent&>(event);
AssetManager::GetAsset<Framebuffer>(framebuffer).Resize(windowResizeEvent.GetWidth(), windowResizeEvent.GetHeight());
descriptorSetPassthrough->SetSampler(AssetManager::GetAsset<Framebuffer>(framebuffer).GetColorAttachment(), 0);
const ViewportResize& viewportResizeEvent = static_cast<const ViewportResize&>(event);
AssetManager::GetAsset<Framebuffer>(framebuffer).Resize(viewportResizeEvent.GetViewport().GetSize().x, viewportResizeEvent.GetViewport().GetSize().y);
descriptorSetImGui->SetSampler(AssetManager::GetAsset<Framebuffer>(framebuffer).GetColorAttachment(), 0);
return EventResult::Continue;
@@ -145,9 +132,6 @@ namespace Copium
descriptorSet = AssetManager::GetAsset<Pipeline>(graphicsPipeline).CreateDescriptorSet(*descriptorPool, 0);
descriptorSet->SetSampler(AssetManager::GetAsset<Texture2D>(texture2D), 1);
descriptorSetPassthrough = AssetManager::GetAsset<Pipeline>(graphicsPipelinePassthrough).CreateDescriptorSet(*descriptorPool, 0);
descriptorSetPassthrough->SetSampler(AssetManager::GetAsset<Framebuffer>(framebuffer).GetColorAttachment(), 0);
descriptorSetImGui = Vulkan::GetImGuiInstance().CreateDescriptorSet();
descriptorSetImGui->SetSampler(AssetManager::GetAsset<Framebuffer>(framebuffer).GetColorAttachment(), 0);
}
@@ -155,13 +139,11 @@ namespace Copium
void Application::InitializeGraphicsPipeline()
{
graphicsPipeline = AssetManager::LoadAsset<Pipeline>("pipeline.meta");
graphicsPipelinePassthrough = AssetManager::LoadAsset<Pipeline>("passthrough.meta");
}
void Application::InitializeMesh()
{
mesh = std::make_unique<Mesh>(vertices, indices);
meshPassthrough = std::make_unique<Mesh>(verticesPassthrough, indicesPassthrough);
}
void Application::InitializeCommandBuffer()
@@ -171,13 +153,31 @@ namespace Copium
void Application::RecordCommandBuffer()
{
Framebuffer& fb = AssetManager::GetAsset<Framebuffer>(framebuffer);
// TODO: Move this logic elsewhere
Vulkan::GetImGuiInstance().Begin();
commandBuffer->Begin();
ImGui::SetNextWindowPos(ImVec2{0, 0});
ImGui::SetNextWindowSize(ImVec2{(float)Vulkan::GetWindow().GetWidth(), (float)Vulkan::GetWindow().GetHeight()});
ImGui::Begin("Docker", nullptr, ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoBringToFrontOnFocus);
ImGui::DockSpace(ImGui::GetID("Dockspace"));
ImGui::End();
ImGui::Begin("Viewport");
ImGui::Image(*descriptorSetImGui, ImVec2{480, 270}, ImVec2{0, 1}, ImVec2{1, 0});
BoundingBox viewport{ImGui::GetCursorScreenPos().x,
ImGui::GetCursorScreenPos().y + ImGui::GetContentRegionAvail().y,
ImGui::GetCursorScreenPos().x + ImGui::GetContentRegionAvail().x,
ImGui::GetCursorScreenPos().y};
if (viewport.GetSize() != glm::vec2{fb.GetWidth(), fb.GetHeight()})
{
OnEvent(ViewportResize(viewport));
CP_INFO("Viewport resize");
}
ImGui::Image(*descriptorSetImGui, ImGui::GetContentRegionAvail(), ImVec2{0, 1}, ImVec2{1, 0});
ImGui::End();
Framebuffer& fb = AssetManager::GetAsset<Framebuffer>(framebuffer);
commandBuffer->Begin();
Pipeline& pl = AssetManager::GetAsset<Pipeline>(graphicsPipeline);
fb.Bind(*commandBuffer);
pl.Bind(*commandBuffer);
@@ -190,20 +190,15 @@ namespace Copium
mesh->Bind(*commandBuffer);
mesh->Render(*commandBuffer);
// TODO: Move this logic elsewhere and only have the Rendering part here
Input::PushViewport(viewport);
scene->Update();
Input::PopViewport();
fb.Unbind(*commandBuffer);
Vulkan::GetSwapChain().BeginFrameBuffer(*commandBuffer);
Pipeline& plPassthrough = AssetManager::GetAsset<Pipeline>(graphicsPipelinePassthrough);
plPassthrough.Bind(*commandBuffer);
plPassthrough.SetDescriptorSet(*descriptorSetPassthrough);
plPassthrough.BindDescriptorSets(*commandBuffer);
meshPassthrough->Bind(*commandBuffer);
meshPassthrough->Render(*commandBuffer);
Vulkan::GetImGuiInstance().End();
Vulkan::GetImGuiInstance().Render(*commandBuffer);
@@ -219,13 +214,11 @@ namespace Copium
Framebuffer& fb = AssetManager::GetAsset<Framebuffer>(framebuffer);
float aspect = fb.GetWidth() / (float)fb.GetHeight();
{
UniformBuffer& uniformBuffer = descriptorSet->GetUniformBuffer("ubo");
uniformBuffer.Set("projection", glm::perspective(glm::radians(45.0f), aspect, 0.1f, 10.0f));
uniformBuffer.Set("view", glm::lookAt(glm::vec3(2.0f, 2.0f, 2.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f)));
uniformBuffer.Set("model", glm::rotate(glm::mat4(1.0f), time * glm::radians(90.0f), glm::vec3(0.0f, 1.0f, 0.0f)));
uniformBuffer.Set("lightPos", (glm::vec3)(glm::rotate(glm::mat4{1.0f}, time * glm::radians(45.0f), glm::vec3(0, 1, 0)) * glm::vec4{0.3, 0.1, 0, 1}));
uniformBuffer.Update();
}
UniformBuffer& uniformBuffer = descriptorSet->GetUniformBuffer("ubo");
uniformBuffer.Set("projection", glm::perspective(glm::radians(45.0f), aspect, 0.1f, 10.0f));
uniformBuffer.Set("view", glm::lookAt(glm::vec3(2.0f, 2.0f, 2.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f)));
uniformBuffer.Set("model", glm::rotate(glm::mat4(1.0f), time * glm::radians(90.0f), glm::vec3(0.0f, 1.0f, 0.0f)));
uniformBuffer.Set("lightPos", (glm::vec3)(glm::rotate(glm::mat4{1.0f}, time * glm::radians(45.0f), glm::vec3(0, 1, 0)) * glm::vec4{0.3, 0.1, 0, 1}));
uniformBuffer.Update();
}
}
@@ -21,14 +21,11 @@ namespace Copium
AssetHandle texture2D2;
AssetHandle font;
AssetHandle graphicsPipeline;
AssetHandle graphicsPipelinePassthrough;
std::unique_ptr<DescriptorPool> descriptorPool;
std::unique_ptr<DescriptorSet> descriptorSet;
std::unique_ptr<DescriptorSet> descriptorSetPassthrough;
std::unique_ptr<DescriptorSet> descriptorSetImGui;
std::unique_ptr<Scene> scene;
std::unique_ptr<Mesh> mesh;
std::unique_ptr<Mesh> meshPassthrough;
std::unique_ptr<CommandBuffer> commandBuffer;
public:
@@ -52,9 +52,10 @@ namespace Copium
{
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
ImGui::StyleColorsDark();
+11 -1
View File
@@ -24,6 +24,7 @@
#include "copium/example/PlayerControllerSystem.h"
#include "copium/example/RenderSystem.h"
#include "copium/example/UiRenderSystem.h"
#include "copium/event/ViewportResize.h"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
@@ -49,7 +50,7 @@ namespace Copium
ecs->AddSystem<CameraUpdateSystem>(&viewMatrix, &projectionMatrix, &invPvMatrix, &uiProjectionMatrix);
ecs->AddSystem<MouseFollowSystem>(&invPvMatrix);
ecs->AddSystem<FrameCountSystem>();
ecs->AddSystem<DebugSystem>();
ecs->AddSystem<DebugSystem>(&viewport);
ecs->AddSystem<AnimationSystem>();
ecs->AddSystem<RenderSystem>(renderer.get(), descriptorSetRenderer.get(), &commandBuffer, &viewMatrix, &projectionMatrix); // better way to store the RenderSystem data?
ecs->AddSystem<UiRenderSystem>(uiRenderer.get(), uiDescriptorSetRenderer.get(), &commandBuffer, &uiProjectionMatrix);
@@ -66,7 +67,16 @@ namespace Copium
EventResult Scene::OnEvent(const Event& event)
{
switch (event.GetType())
{
case EventType::ViewportResize:
const ViewportResize& viewportResizeEvent = static_cast<const ViewportResize&>(event);
viewport = viewportResizeEvent.GetViewport();
break;
}
Input::PushViewport(viewport);
ecs->UpdateSystems(EventSignal{event});
Input::PopViewport();
return EventResult::Continue;
}
+1
View File
@@ -23,6 +23,7 @@ namespace Copium
glm::mat4 viewMatrix;
glm::mat4 invPvMatrix;
glm::mat4 uiProjectionMatrix;
BoundingBox viewport;
public:
Scene(CommandBuffer& commandBuffer, DescriptorPool& descriptorPool);
void Update();
+2 -1
View File
@@ -5,6 +5,7 @@
#define CP_EVENT_TYPE_ENUMS \
MouseMove, MousePress, MouseRelease, MouseScroll, \
KeyPress, KeyRelease, \
WindowResize, WindowFocus
WindowResize, WindowFocus, \
ViewportResize
CP_ENUM_CREATOR(Copium, EventType, CP_EVENT_TYPE_ENUMS);
+19
View File
@@ -12,6 +12,7 @@ namespace Copium
bool Input::mouseDownList[MAX_NUM_MOUSE_BUTTONS];
bool Input::mouseEventList[MAX_NUM_MOUSE_BUTTONS];
glm::vec2 Input::mousePos{0.0f};
glm::vec2 Input::mousePosViewport{0.0f};
bool Input::IsKeyPressed(int keyCode)
{
@@ -66,6 +67,11 @@ namespace Copium
return mousePos;
}
glm::vec2 Input::GetMousePosViewport()
{
return mousePosViewport;
}
glm::vec2 Input::GetMouseWindowPos()
{
return glm::vec2{(mousePos.x + 1.0f) * 0.5f * Vulkan::GetWindow().GetWidth(), (1.0f - mousePos.y) * 0.5f * Vulkan::GetWindow().GetHeight()};
@@ -88,6 +94,7 @@ namespace Copium
void Input::OnMouseMove(glm::vec2 mousePos)
{
Input::mousePos = mousePos;
Input::mousePosViewport = mousePos;
}
void Input::Update()
@@ -95,4 +102,16 @@ namespace Copium
memset(keyEventList, false, sizeof(keyEventList));
memset(mouseEventList, false, sizeof(mouseEventList));
}
void Input::PushViewport(const BoundingBox& viewport)
{
mousePosViewport = GetMouseWindowPos();
mousePosViewport.x = (mousePosViewport.x - viewport.l) / viewport.GetSize().x * 2 - 1;
mousePosViewport.y = 1 - (mousePosViewport.y - viewport.t) / viewport.GetSize().y * 2;
}
void Input::PopViewport()
{
mousePosViewport = mousePos;
}
}
+5
View File
@@ -3,6 +3,7 @@
#include <vector>
#include "copium/event/InputCode.h"
#include "copium/util/BoundingBox.h"
#include <glm/glm.hpp>
@@ -19,6 +20,7 @@ namespace Copium
static bool mouseDownList[MAX_NUM_MOUSE_BUTTONS];
static bool mouseEventList[MAX_NUM_MOUSE_BUTTONS];
static glm::vec2 mousePos;
static glm::vec2 mousePosViewport;
public:
// Will only be true for a single frame after the KeyPressEvent/KeyReleaseEvent
@@ -37,11 +39,14 @@ namespace Copium
static glm::vec2 GetMouseWindowPos();
static glm::vec2 GetMousePos();
static glm::vec2 GetMousePosViewport();
static void OnKey(int keyCode, bool pressed);
static void OnMouse(int buttion, bool pressed);
static void OnMouseMove(glm::vec2 mousePos);
static void Update();
static void PushViewport(const BoundingBox& viewport);
static void PopViewport();
};
}
@@ -0,0 +1,13 @@
#include "copium/event/ViewportResize.h"
namespace Copium
{
ViewportResize::ViewportResize(const BoundingBox& viewport)
: Event(EventType::ViewportResize), viewport{viewport}
{}
const BoundingBox& ViewportResize::GetViewport() const
{
return viewport;
}
}
@@ -0,0 +1,17 @@
#pragma once
#include "copium/event/Event.h"
#include "copium/event/EventType.h"
#include "copium/util/BoundingBox.h"
namespace Copium
{
class ViewportResize : public Event
{
BoundingBox viewport;
public:
ViewportResize(const BoundingBox& viewport);
const BoundingBox& GetViewport() const;
};
}
@@ -3,7 +3,7 @@
#include "copium/ecs/System.h"
#include "copium/example/Components.h"
#include "copium/event/EventSignal.h"
#include "copium/event/WindowResizeEvent.h"
#include "copium/event/ViewportResize.h"
#include <glm/gtc/matrix_transform.hpp>
@@ -46,19 +46,19 @@ namespace Copium
const EventSignal& eventSignal = static_cast<const EventSignal&>(signal);
switch (eventSignal.GetEvent().GetType())
{
case EventType::WindowResize:
case EventType::ViewportResize:
{
const WindowResizeEvent& windowResizeEvent = static_cast<const WindowResizeEvent&>(eventSignal.GetEvent());
const ViewportResize& viewportResize = static_cast<const ViewportResize&>(eventSignal.GetEvent());
if (camera.uiCamera)
{
camera.projection.r = windowResizeEvent.GetWidth();
camera.projection.t = windowResizeEvent.GetHeight();
camera.projection.r = viewportResize.GetViewport().GetSize().x;
camera.projection.t = viewportResize.GetViewport().GetSize().y;
camera.projection.l = 0.0f;
camera.projection.b = 0.0f;
}
else
{
float aspect = windowResizeEvent.GetWidth() / (float)windowResizeEvent.GetHeight();
float aspect = viewportResize.GetViewport().GetSize().x / viewportResize.GetViewport().GetSize().y;
camera.projection.r = aspect;
camera.projection.l = -aspect;
}
@@ -9,7 +9,12 @@ namespace Copium
{
class DebugSystem : public System<DebugC, TextC, TransformC>
{
BoundingBox* viewport;
public:
DebugSystem(BoundingBox* viewport)
: viewport{viewport}
{}
void RunEntity(Entity entity, DebugC& debug, TextC& text, TransformC& transform) override
{
const PlayerC& player = debug.playerEntity.GetComponent<PlayerC>();
@@ -23,7 +28,7 @@ namespace Copium
text.text += String::Format("Grounded: %s", player.grounded ? "true" : "false");
const Font& font = AssetManager::GetAsset<Font>(text.font);
transform.position.y = Vulkan::GetSwapChain().GetExtent().height - 10.0f - font.GetBaseHeight() * text.fontSize;
transform.position.y = viewport->GetSize().y - 10.0f - font.GetBaseHeight() * text.fontSize;
}
};
}
@@ -18,7 +18,7 @@ namespace Copium
void RunEntity(Entity entity, MouseFollowC& mouseFollow, TransformC& transform)
{
transform.position = (*invPvMatrix) * glm::vec4{Input::GetMousePos().x, Input::GetMousePos().y, 0.0f, 1.0f};
transform.position = (*invPvMatrix) * glm::vec4{Input::GetMousePosViewport().x, Input::GetMousePosViewport().y, 0.0f, 1.0f};
transform.position -= transform.size * glm::vec2{0.5f};
}
};
+11 -1
View File
@@ -20,6 +20,16 @@ namespace Copium
glm::vec2 BoundingBox::GetSize() const
{
return rt - lb;
return glm::abs(rt - lb);
}
bool BoundingBox::operator==(const BoundingBox& boundingBox) const
{
return l == boundingBox.l && b == boundingBox.b && r == boundingBox.r && t == boundingBox.t;
}
bool BoundingBox::operator!=(const BoundingBox& boundingBox) const
{
return !(*this == boundingBox);
}
}
@@ -18,5 +18,8 @@ namespace Copium
BoundingBox(glm::vec2 lb, glm::vec2 rt);
glm::vec2 GetSize() const;
bool operator==(const BoundingBox& boundingBox) const;
bool operator!=(const BoundingBox& boundingBox) const;
};
}