From 4786986abdfc2a5ef1250e93044fde8cab2cd4f7 Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Sun, 17 Jan 2016 06:29:25 +0100 Subject: [PATCH] Rendering pipeline fixes across the board and base work on refactored editor --- include/Engine/Editor/EditorRenderSystem.h | 27 + include/Engine/Editor/EditorSystem.h | 96 +-- include/Engine/Editor/EditorSystemOld.h | 94 +++ include/Engine/Editor/EditorUI.h | 8 + .../Rendering/DebugCameraInputController.h | 7 +- include/Engine/Rendering/DrawFinalPass.h | 1 - include/Engine/Rendering/ESetCamera.h | 10 +- include/Engine/Rendering/IRenderer.h | 11 - include/Engine/Rendering/RenderJob.h | 1 - include/Engine/Rendering/RenderQueue.h | 3 +- include/Engine/Rendering/RenderState.h | 2 +- include/Engine/Rendering/RenderSystem.h | 21 +- resources/Schema/Components/Camera.xml | 1 - resources/Schema/Components/Camera.xsd | 1 - resources/Schema/Components/PointLight.xsd | 2 +- resources/Schema/Entities/EditorWidget.xml | 79 ++ resources/Schema/Entities/Test.xml | 2 +- src/Engine/Editor/EditorRenderSystem.cpp | 87 ++ src/Engine/Editor/EditorSystem.cpp | 760 +----------------- src/Engine/Editor/EditorSystemOld.cpp | 738 +++++++++++++++++ src/Engine/Editor/EditorUI.cpp | 0 src/Engine/Rendering/DrawFinalPass.cpp | 5 +- src/Engine/Rendering/DrawFinalPassState.cpp | 1 - src/Engine/Rendering/RenderState.cpp | 15 +- src/Engine/Rendering/RenderSystem.cpp | 145 +--- src/Engine/Rendering/Renderer.cpp | 15 +- src/Game/Game.cpp | 4 +- 27 files changed, 1134 insertions(+), 1002 deletions(-) create mode 100644 include/Engine/Editor/EditorRenderSystem.h create mode 100644 include/Engine/Editor/EditorSystemOld.h create mode 100644 include/Engine/Editor/EditorUI.h create mode 100755 resources/Schema/Entities/EditorWidget.xml create mode 100644 src/Engine/Editor/EditorRenderSystem.cpp create mode 100644 src/Engine/Editor/EditorSystemOld.cpp create mode 100644 src/Engine/Editor/EditorUI.cpp diff --git a/include/Engine/Editor/EditorRenderSystem.h b/include/Engine/Editor/EditorRenderSystem.h new file mode 100644 index 00000000..c593e29a --- /dev/null +++ b/include/Engine/Editor/EditorRenderSystem.h @@ -0,0 +1,27 @@ +#ifndef EditorRenderSystem_h__ +#define EditorRenderSystem_h__ + +#include "../Core/System.h" +#include "../Rendering/IRenderer.h" +#include "../Rendering/ModelJob.h" +#include "../Rendering/Camera.h" +#include "../Rendering/ESetCamera.h" + +class EditorRenderSystem : public ImpureSystem +{ +public: + EditorRenderSystem(EventBroker* eventBroker, IRenderer* renderer, RenderFrame* renderFrame); + + virtual void Update(World* world, double dt) override; + +private: + IRenderer* m_Renderer; + RenderFrame* m_RenderFrame; + Camera* m_EditorCamera; + EntityWrapper m_CurrentCamera = EntityWrapper::Invalid; + + EventRelay m_ESetCamera; + bool OnSetCamera(Events::SetCamera& e); +}; + +#endif \ No newline at end of file diff --git a/include/Engine/Editor/EditorSystem.h b/include/Engine/Editor/EditorSystem.h index 05e106d9..43022009 100644 --- a/include/Engine/Editor/EditorSystem.h +++ b/include/Engine/Editor/EditorSystem.h @@ -1,94 +1,30 @@ -#include -#include -#include -#include #include "../Core/System.h" -#include "../Core/EMousePress.h" -#include "../Core/EMouseRelease.h" -#include "../Core/EMouseMove.h" -#include "../Core/ConfigFile.h" -#include "../Input/EInputCommand.h" #include "../Rendering/IRenderer.h" -#include "../Core/Transform.h" -#include "../Core/EFileDropped.h" +#include "../Rendering/Camera.h" +#include "../Rendering/DebugCameraInputController.h" +#include "../Rendering/ESetCamera.h" +#include "../Core/World.h" +#include "../Core/SystemPipeline.h" +#include "../Core/ResourceManager.h" #include "../Core/EntityFilePreprocessor.h" #include "../Core/EntityFileParser.h" -#include "../Core/EntityFileWriter.h" class EditorSystem : public ImpureSystem { public: - EditorSystem(EventBroker* eventBroker, IRenderer* renderer); + EditorSystem(EventBroker* eventBroker, IRenderer* renderer, RenderFrame* renderFrame); + ~EditorSystem(); - virtual void Update(World* world, double dt) override; + void Update(World* world, double dt); private: IRenderer* m_Renderer; - World* m_World = nullptr; - Camera* m_Camera = nullptr; + RenderFrame* m_RenderFrame; + World* m_EditorWorld; + SystemPipeline* m_EditorWorldSystemPipeline; + Camera* m_EditorCamera; - bool m_Enabled; - bool m_Visible; - boost::filesystem::path m_DefaultEntityDir; - boost::filesystem::path m_CurrentFile; - std::vector m_PickingQueue; - - enum class WidgetMode - { - None, - Translate, - Rotate, - Scale - } m_WidgetMode = WidgetMode::None; - - enum class WidgetSpace - { - Local, - Global - } m_WidgetSpace = WidgetSpace::Global; - - EntityID m_Widget = EntityID_Invalid; - EntityID m_WidgetX = EntityID_Invalid; - EntityID m_WidgetPlaneX = EntityID_Invalid; - EntityID m_WidgetY = EntityID_Invalid; - EntityID m_WidgetPlaneY = EntityID_Invalid; - EntityID m_WidgetZ = EntityID_Invalid; - EntityID m_WidgetPlaneZ = EntityID_Invalid; - EntityID m_WidgetOrigin = EntityID_Invalid; - glm::vec3 m_WidgetCurrentAxis; - float m_WidgetPickingDepth = 0.f; - glm::vec3 m_WidgetPickingPosition = glm::vec3(0); - - EntityID m_Selection = EntityID_Invalid; - EntityID m_LastSelection = EntityID_Invalid; - EntityID m_UIDraggingEntity = EntityID_Invalid; - glm::vec3 m_Position; - std::string m_LastDroppedFile; - - static boost::filesystem::path openDialog(boost::filesystem::path defaultPath); - static boost::filesystem::path saveDialog(boost::filesystem::path defaultPath); - - EventRelay m_EInputCommand; - bool OnInputCommand(const Events::InputCommand& e); - EventRelay m_EMouseRelease; - bool OnMouseRelease(const Events::MouseRelease& e); - EventRelay m_EMousePress; - bool OnMousePress(const Events::MousePress& e); - EventRelay m_EMouseMove; - bool OnMouseMove(const Events::MouseMove& e); - EventRelay m_EFileDropped; - bool OnFileDropped(const Events::FileDropped& e); - - void Picking(); - void createWidget(); - void updateWidget(); - void setWidgetMode(WidgetMode newMode); - void setWidgetSpace(WidgetSpace space); - void drawUI(World* world, double dt); - bool createDeleteButton(std::string componentType); - bool createEntityNode(World* world, EntityID entity); - void changeParent(EntityID entity, EntityID newParent); - void fileImport(World* world); - void fileSave(World* world); - void fileSaveAs(World* world); + EntityWrapper m_Widget = EntityWrapper::Invalid; + EntityWrapper m_Camera = EntityWrapper::Invalid; + DebugCameraInputController* m_DebugCameraInputController; }; \ No newline at end of file diff --git a/include/Engine/Editor/EditorSystemOld.h b/include/Engine/Editor/EditorSystemOld.h new file mode 100644 index 00000000..b2d7df0c --- /dev/null +++ b/include/Engine/Editor/EditorSystemOld.h @@ -0,0 +1,94 @@ +#include +#include +#include +#include +#include "../Core/System.h" +#include "../Core/EMousePress.h" +#include "../Core/EMouseRelease.h" +#include "../Core/EMouseMove.h" +#include "../Core/ConfigFile.h" +#include "../Input/EInputCommand.h" +#include "../Rendering/IRenderer.h" +#include "../Core/Transform.h" +#include "../Core/EFileDropped.h" +#include "../Core/EntityFilePreprocessor.h" +#include "../Core/EntityFileParser.h" +#include "../Core/EntityFileWriter.h" + +class EditorSystemOld : public ImpureSystem +{ +public: + EditorSystemOld(EventBroker* eventBroker, IRenderer* renderer); + + virtual void Update(World* world, double dt) override; + +private: + IRenderer* m_Renderer; + World* m_World = nullptr; + Camera* m_Camera = nullptr; + + bool m_Enabled; + bool m_Visible; + boost::filesystem::path m_DefaultEntityDir; + boost::filesystem::path m_CurrentFile; + std::vector m_PickingQueue; + + enum class WidgetMode + { + None, + Translate, + Rotate, + Scale + } m_WidgetMode = WidgetMode::None; + + enum class WidgetSpace + { + Local, + Global + } m_WidgetSpace = WidgetSpace::Global; + + EntityID m_Widget = EntityID_Invalid; + EntityID m_WidgetX = EntityID_Invalid; + EntityID m_WidgetPlaneX = EntityID_Invalid; + EntityID m_WidgetY = EntityID_Invalid; + EntityID m_WidgetPlaneY = EntityID_Invalid; + EntityID m_WidgetZ = EntityID_Invalid; + EntityID m_WidgetPlaneZ = EntityID_Invalid; + EntityID m_WidgetOrigin = EntityID_Invalid; + glm::vec3 m_WidgetCurrentAxis; + float m_WidgetPickingDepth = 0.f; + glm::vec3 m_WidgetPickingPosition = glm::vec3(0); + + EntityID m_Selection = EntityID_Invalid; + EntityID m_LastSelection = EntityID_Invalid; + EntityID m_UIDraggingEntity = EntityID_Invalid; + glm::vec3 m_Position; + std::string m_LastDroppedFile; + + static boost::filesystem::path openDialog(boost::filesystem::path defaultPath); + static boost::filesystem::path saveDialog(boost::filesystem::path defaultPath); + + EventRelay m_EInputCommand; + bool OnInputCommand(const Events::InputCommand& e); + EventRelay m_EMouseRelease; + bool OnMouseRelease(const Events::MouseRelease& e); + EventRelay m_EMousePress; + bool OnMousePress(const Events::MousePress& e); + EventRelay m_EMouseMove; + bool OnMouseMove(const Events::MouseMove& e); + EventRelay m_EFileDropped; + bool OnFileDropped(const Events::FileDropped& e); + + void Picking(); + void createWidget(); + void updateWidget(); + void setWidgetMode(WidgetMode newMode); + void setWidgetSpace(WidgetSpace space); + void drawUI(World* world, double dt); + bool createDeleteButton(std::string componentType); + bool createEntityNode(World* world, EntityID entity); + void changeParent(EntityID entity, EntityID newParent); + void fileImport(World* world); + void fileSave(World* world); + void fileSaveAs(World* world); +}; \ No newline at end of file diff --git a/include/Engine/Editor/EditorUI.h b/include/Engine/Editor/EditorUI.h new file mode 100644 index 00000000..18299865 --- /dev/null +++ b/include/Engine/Editor/EditorUI.h @@ -0,0 +1,8 @@ +#include +#include + +class EditorUI +{ +public: + EditorUI(); +}; \ No newline at end of file diff --git a/include/Engine/Rendering/DebugCameraInputController.h b/include/Engine/Rendering/DebugCameraInputController.h index 4d74e288..1a43820c 100644 --- a/include/Engine/Rendering/DebugCameraInputController.h +++ b/include/Engine/Rendering/DebugCameraInputController.h @@ -1,3 +1,6 @@ +#ifndef DebugCameraInputController_h__ +#define DebugCameraInputController_h__ + #include #include "../Input/FirstPersonInputController.h" @@ -63,4 +66,6 @@ protected: glm::vec3 m_Velocity = glm::vec3(0, 0, 0); float m_BaseSpeed = 2.0f; float m_Speed = m_BaseSpeed; -}; \ No newline at end of file +}; + +#endif \ No newline at end of file diff --git a/include/Engine/Rendering/DrawFinalPass.h b/include/Engine/Rendering/DrawFinalPass.h index 52876592..20c7249d 100644 --- a/include/Engine/Rendering/DrawFinalPass.h +++ b/include/Engine/Rendering/DrawFinalPass.h @@ -32,7 +32,6 @@ private: const LightCullingPass* m_LightCullingPass; ShaderProgram* m_ForwardPlusProgram; - }; #endif \ No newline at end of file diff --git a/include/Engine/Rendering/ESetCamera.h b/include/Engine/Rendering/ESetCamera.h index 650f3b12..1b39e890 100644 --- a/include/Engine/Rendering/ESetCamera.h +++ b/include/Engine/Rendering/ESetCamera.h @@ -2,20 +2,14 @@ #define Events_SetCamera_h__ #include "../Core/EventBroker.h" -#include "../Core/Entity.h" -#include +#include "../Core/EntityWrapper.h" namespace Events { struct SetCamera : Event { -public: - SetCamera() { }; - std::string Name; - -private: - + EntityWrapper CameraEntity; }; } diff --git a/include/Engine/Rendering/IRenderer.h b/include/Engine/Rendering/IRenderer.h index 95053a85..6ef189f4 100644 --- a/include/Engine/Rendering/IRenderer.h +++ b/include/Engine/Rendering/IRenderer.h @@ -31,15 +31,6 @@ public: void SetFullscreen(bool fullscreen) { m_Fullscreen = fullscreen; } bool VSYNC() const { return m_VSYNC; } void SetVSYNC(bool vsync) { m_VSYNC = vsync; } - ::Camera* Camera() const { return m_Camera; } - void SetCamera(::Camera* camera) - { - if (camera == nullptr) { - m_Camera = m_DefaultCamera; - } else { - m_Camera = camera; - } - } virtual void Initialize() = 0; virtual void Update(double dt) = 0; virtual void Draw(RenderFrame& rq) = 0; @@ -54,8 +45,6 @@ protected: int m_GLVersion[2]; std::string m_GLVendor; GLFWwindow* m_Window = nullptr; - ::Camera* m_DefaultCamera; - ::Camera* m_Camera = nullptr; }; #endif // Renderer_h__ diff --git a/include/Engine/Rendering/RenderJob.h b/include/Engine/Rendering/RenderJob.h index 4afe0386..bcffc4a5 100644 --- a/include/Engine/Rendering/RenderJob.h +++ b/include/Engine/Rendering/RenderJob.h @@ -14,7 +14,6 @@ struct RenderJob friend class RenderQueue; public: - float Depth; protected: diff --git a/include/Engine/Rendering/RenderQueue.h b/include/Engine/Rendering/RenderQueue.h index 119ea57f..701d08b9 100644 --- a/include/Engine/Rendering/RenderQueue.h +++ b/include/Engine/Rendering/RenderQueue.h @@ -50,10 +50,11 @@ struct PointLightJob : RenderJob struct RenderScene { - ::Camera* Camera; + ::Camera* Camera = nullptr; std::list> ForwardJobs; std::list> PointLightJobs; Rectangle Viewport; + bool ClearDepth = false; void Clear() { diff --git a/include/Engine/Rendering/RenderState.h b/include/Engine/Rendering/RenderState.h index c6eb8775..688ef520 100644 --- a/include/Engine/Rendering/RenderState.h +++ b/include/Engine/Rendering/RenderState.h @@ -16,10 +16,10 @@ public: bool Disable(GLenum cap); bool CullFace(GLenum mode); bool ClearColor(glm::vec4 color); - bool Clear(GLbitfield mask); bool BindFramebuffer(GLint framebuffer); bool BlendEquation(GLenum mode); bool BlendFunc(GLenum sfactor, GLenum dfactor); + bool DepthMask(GLboolean flag); private: std::vector> m_ResetFunctions; diff --git a/include/Engine/Rendering/RenderSystem.h b/include/Engine/Rendering/RenderSystem.h index fe110276..39b35108 100644 --- a/include/Engine/Rendering/RenderSystem.h +++ b/include/Engine/Rendering/RenderSystem.h @@ -28,28 +28,17 @@ public: private: World* m_World = nullptr; const IRenderer* m_Renderer; - RenderFrame* m_RenderFrame; - bool m_SwitchCamera = false; Camera* m_Camera; - DebugCameraInputController* m_DebugCameraInputController; - - std::list m_CameraComponents; + EntityWrapper m_CurrentCamera = EntityWrapper::Invalid; EventRelay m_ESetCamera; - bool OnSetCamera(const Events::SetCamera &event); - EntityID m_CurrentCamera = EntityID_Invalid; - - void switchCamera(EntityID entity); - - void updateCamera(World* world, double dt); - void updateProjectionMatrix(ComponentWrapper& cameraComponent); - - void fillModels(std::list>& jobs, World* world); - void fillLight(std::list>& jobs, World* world); - + bool OnSetCamera(Events::SetCamera &event); EventRelay m_EInputCommand; bool OnInputCommand(const Events::InputCommand& e); + + void fillModels(std::list>& jobs, World* world); + void fillLight(std::list>& jobs, World* world); }; #endif \ No newline at end of file diff --git a/resources/Schema/Components/Camera.xml b/resources/Schema/Components/Camera.xml index ccb12f01..b9c28d53 100644 --- a/resources/Schema/Components/Camera.xml +++ b/resources/Schema/Components/Camera.xml @@ -1,6 +1,5 @@ - cam 45 0.01 5000 diff --git a/resources/Schema/Components/Camera.xsd b/resources/Schema/Components/Camera.xsd index 2b896c74..1bde2333 100644 --- a/resources/Schema/Components/Camera.xsd +++ b/resources/Schema/Components/Camera.xsd @@ -9,7 +9,6 @@ - Vertical Field of View in degrees diff --git a/resources/Schema/Components/PointLight.xsd b/resources/Schema/Components/PointLight.xsd index 9b802d8c..25d0aa07 100644 --- a/resources/Schema/Components/PointLight.xsd +++ b/resources/Schema/Components/PointLight.xsd @@ -12,7 +12,7 @@ - + diff --git a/resources/Schema/Entities/EditorWidget.xml b/resources/Schema/Entities/EditorWidget.xml new file mode 100755 index 00000000..e729af02 --- /dev/null +++ b/resources/Schema/Entities/EditorWidget.xml @@ -0,0 +1,79 @@ + + + + + + + + + + + + + + + + + + + + + + + + + Models/TranslationWidgetOrigin.obj + + + + + + + + Models/TranslationWidgetX.obj + + + + + + + + Models/TranslationWidgetY.obj + + + + + + + + Models/TranslationWidgetZ.obj + + + + + + + + Models/WidgetPlaneX.obj + + + + + + + + Models/WidgetPlaneY.obj + + + + + + + + Models/WidgetPlaneZ.obj + + + + + + diff --git a/resources/Schema/Entities/Test.xml b/resources/Schema/Entities/Test.xml index d2838ae9..5f57dbb7 100644 --- a/resources/Schema/Entities/Test.xml +++ b/resources/Schema/Entities/Test.xml @@ -9,7 +9,6 @@ Models/DummyScene.obj - @@ -25,3 +24,4 @@ + diff --git a/src/Engine/Editor/EditorRenderSystem.cpp b/src/Engine/Editor/EditorRenderSystem.cpp new file mode 100644 index 00000000..2d25338d --- /dev/null +++ b/src/Engine/Editor/EditorRenderSystem.cpp @@ -0,0 +1,87 @@ +#include "Editor/EditorRenderSystem.h" + +EditorRenderSystem::EditorRenderSystem(EventBroker* eventBroker, IRenderer* renderer, RenderFrame* renderFrame) + : System(eventBroker) + , m_Renderer(renderer) + , m_RenderFrame(renderFrame) +{ + EVENT_SUBSCRIBE_MEMBER(m_ESetCamera, &EditorRenderSystem::OnSetCamera); + auto resolution = Rectangle::Rectangle(1280, 720); + m_EditorCamera = new Camera((float)resolution.Width / resolution.Height, glm::radians(45.f), 0.01f, 5000.f); +} + +void EditorRenderSystem::Update(World* world, double dt) +{ + if (m_CurrentCamera) { + ComponentWrapper cameraTransform = m_CurrentCamera["Transform"]; + m_EditorCamera->SetPosition(cameraTransform["Position"]); + m_EditorCamera->SetOrientation(glm::quat((const glm::vec3&)cameraTransform["Orientation"])); + } + + RenderScene scene; + scene.ClearDepth = true; + scene.Camera = m_EditorCamera; + scene.Viewport = Rectangle(1920, 1080); + + auto models = world->GetComponents("Model"); + if (models != nullptr) { + for (auto& cModel : *models) { + if (!(bool)cModel["Visible"]) { + continue; + } + + const std::string& resource = cModel["Resource"]; + + Model* model; + try { + model = ResourceManager::Load<::Model, true>(resource); + } catch (const Resource::StillLoadingException&) { + continue; + } catch (const std::exception&) { + try { + model = ResourceManager::Load<::Model>("Models/Core/Error.obj"); + } catch (const std::exception&) { + continue; + } + } + + EntityWrapper entity(world, cModel.EntityID); + glm::mat4 modelMatrix = Transform::ModelMatrix(entity.ID, entity.World); + for (auto matGroup : model->MaterialGroups()) { + std::shared_ptr modelJob = std::make_shared(model, nullptr, modelMatrix, matGroup, cModel, entity.World); + scene.ForwardJobs.push_back(modelJob); + } + } + } + + auto pointLights = world->GetComponents("PointLight"); + if (pointLights != nullptr) { + for (auto& cPointLight : *pointLights) { + bool visible = cPointLight["Visible"]; + if (!visible) { + continue; + } + + EntityWrapper entity(world, cPointLight.EntityID); + ComponentWrapper& cTransform = entity["Transform"]; + std::shared_ptr pointLightJob = std::make_shared(cTransform, cPointLight, entity.World); + scene.PointLightJobs.push_back(pointLightJob); + } + } + + m_RenderFrame->Add(scene); +} + +bool EditorRenderSystem::OnSetCamera(Events::SetCamera& e) +{ + ComponentWrapper cTransform = e.CameraEntity["Transform"]; + ComponentWrapper cCamera = e.CameraEntity["Camera"]; + m_EditorCamera->SetFOV((double)cCamera["FOV"]); + m_EditorCamera->SetNearClip((double)cCamera["NearClip"]); + m_EditorCamera->SetFarClip((double)cCamera["FarClip"]); + m_EditorCamera->SetPosition(cTransform["Position"]); + m_EditorCamera->SetOrientation(glm::quat((const glm::vec3&)cTransform["Orientation"])); + m_CurrentCamera = e.CameraEntity; + return true; +} + diff --git a/src/Engine/Editor/EditorSystem.cpp b/src/Engine/Editor/EditorSystem.cpp index d5863814..ec385e9b 100644 --- a/src/Engine/Editor/EditorSystem.cpp +++ b/src/Engine/Editor/EditorSystem.cpp @@ -1,738 +1,46 @@ #include "Editor/EditorSystem.h" -#define IMGUI_DEFINE_MATH_OPERATORS -#include +#include "Core/UniformScaleSystem.h" +#include "Editor/EditorRenderSystem.h" -EditorSystem::EditorSystem(EventBroker* eventBroker, IRenderer* renderer) +EditorSystem::EditorSystem(EventBroker* eventBroker, IRenderer* renderer, RenderFrame* renderFrame) : System(eventBroker) - , ImpureSystem() , m_Renderer(renderer) + , m_RenderFrame(renderFrame) { - auto config = ResourceManager::Load("Config.ini"); - m_Enabled = config->Get("Debug.EditorEnabled", false); - m_Visible = m_Enabled; - m_DefaultEntityDir = boost::filesystem::path("Schema") / boost::filesystem::path("Entities"); + m_EditorWorld = new World(); + m_EditorWorldSystemPipeline = new SystemPipeline(eventBroker); + m_EditorWorldSystemPipeline->AddSystem(0); + m_EditorWorldSystemPipeline->AddSystem(1, m_Renderer, m_RenderFrame); + + auto widgetEntityFile = ResourceManager::Load("Schema/Entities/EditorWidget.xml"); + EntityFilePreprocessor fpp(widgetEntityFile); + fpp.RegisterComponents(m_EditorWorld); + EntityFileParser fp(widgetEntityFile); + EntityID widgetID = fp.MergeEntities(m_EditorWorld); + m_Widget = EntityWrapper(m_EditorWorld, widgetID); - if (!m_Enabled) { - return; - } + m_Camera = EntityWrapper(m_EditorWorld, m_EditorWorld->CreateEntity()); + m_EditorWorld->AttachComponent(m_Camera.ID, "Transform"); + m_EditorWorld->AttachComponent(m_Camera.ID, "Camera"); + m_DebugCameraInputController = new DebugCameraInputController(m_EventBroker, -1); - EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &EditorSystem::OnInputCommand); - EVENT_SUBSCRIBE_MEMBER(m_EMousePress, &EditorSystem::OnMousePress); - EVENT_SUBSCRIBE_MEMBER(m_EMouseRelease, &EditorSystem::OnMouseRelease); - EVENT_SUBSCRIBE_MEMBER(m_EMouseMove, &EditorSystem::OnMouseMove); - EVENT_SUBSCRIBE_MEMBER(m_EFileDropped, &EditorSystem::OnFileDropped); + Events::SetCamera e; + e.CameraEntity = m_Camera; + m_EventBroker->Publish(e); +} + +EditorSystem::~EditorSystem() +{ + delete m_DebugCameraInputController; + delete m_EditorWorldSystemPipeline; + delete m_EditorWorld; } void EditorSystem::Update(World* world, double dt) { - m_World = world; + m_EditorWorldSystemPipeline->Update(m_EditorWorld, dt); - if (!m_Enabled) { - return; - } - - if (!m_Visible) { - return; - } - Picking(); - updateWidget(); - - drawUI(world, dt); - - // Clear drop queue if it wasn't handled by any UI element - if (!m_LastDroppedFile.empty()) { - m_LastDroppedFile = ""; - } -} - - -boost::filesystem::path EditorSystem::openDialog(boost::filesystem::path defaultPath) -{ - namespace bfs = boost::filesystem; - auto absolutePath = bfs::absolute(defaultPath); - nfdchar_t* outPath = nullptr; - nfdresult_t result = NFD_OpenDialog(NULL, absolutePath.string().c_str(), &outPath); - if (result == NFD_ERROR) { - LOG_ERROR("NFD Error: %s", NFD_GetError()); - return bfs::path(); - } - - return bfs::absolute(outPath); -} - -boost::filesystem::path EditorSystem::saveDialog(boost::filesystem::path defaultPath) -{ - namespace bfs = boost::filesystem; - auto absolutePath = bfs::absolute(defaultPath); - nfdchar_t* outPath = nullptr; - nfdresult_t result = NFD_SaveDialog(NULL, absolutePath.string().c_str(), &outPath); - if (result == NFD_ERROR) { - LOG_ERROR("NFD Error: %s", NFD_GetError()); - return bfs::path(); - } - - return bfs::absolute(outPath); -} - -bool EditorSystem::OnInputCommand(const Events::InputCommand& e) -{ - if (e.Command == "ToggleEditor" && e.Value > 0) { - m_Visible = !m_Visible; - } - - if (e.Command == "EditorToolMove" && e.Value > 0) { - setWidgetMode(WidgetMode::Translate); - } - if (e.Command == "EditorToolRotate" && e.Value > 0) { - setWidgetMode(WidgetMode::Rotate); - } - if (e.Command == "EditorToolScale" && e.Value > 0) { - setWidgetMode(WidgetMode::Scale); - } - - if (e.Command == "EditorToggleTransformSpace" && e.Value > 0) { - if (m_WidgetSpace == WidgetSpace::Global) { - setWidgetSpace(WidgetSpace::Local); - } else if (m_WidgetSpace == WidgetSpace::Local) { - setWidgetSpace(WidgetSpace::Global); - } - } - - return true; -} - -bool EditorSystem::OnMousePress(const Events::MousePress& e) -{ - if (e.Button == GLFW_MOUSE_BUTTON_RIGHT) { - m_PickingQueue.push_back(glm::vec2((int)e.X, (int)e.Y)); - } - return true; -} - -bool EditorSystem::OnMouseMove(const Events::MouseMove& e) -{ - if (m_Widget == EntityID_Invalid) { - return false; - } - if (m_Selection == EntityID_Invalid) { - return false; - } - if (m_Selection == m_Widget) { - return false; - } - // TODO: No widgets for root entity until widgets reside in thier own world, - // or the widgets will move relative to the root entity being moved, which is WEEEIRD. - if (m_Selection == 0) { - return false; - } - if (m_Camera == nullptr) { - return false; - } - - auto widgetTransform = m_World->GetComponent(m_Widget, "Transform"); - glm::vec3 widgetOrientation = widgetTransform["Orientation"]; - glm::quat totalOrientation = m_Camera->Orientation() * glm::inverse(glm::quat(widgetOrientation)); - - int width; - int height; - glfwGetFramebufferSize(m_Renderer->Window(), &width, &height); - Rectangle res(width, height); - - glm::vec2 delta2(res.Width / 2.f + e.DeltaX, res.Height / 2.f + -e.DeltaY); - glm::vec3 deltaWorld = ScreenCoords::ToWorldPos( - delta2, - m_WidgetPickingDepth, - res, - m_Camera->ProjectionMatrix(), - glm::toMat4(glm::inverse(totalOrientation)) - ); - glm::vec3 origin = ScreenCoords::ToWorldPos( - glm::vec2(res.Width / 2.f, res.Height / 2.f), - m_WidgetPickingDepth, - res, - m_Camera->ProjectionMatrix(), - glm::toMat4(glm::inverse(totalOrientation)) - ); - deltaWorld = deltaWorld - origin; - glm::vec3 movement = deltaWorld * m_WidgetCurrentAxis; - - if (glm::length2(m_WidgetCurrentAxis) > 0.f) { - auto widgetTransform = m_World->GetComponent(m_Widget, "Transform"); - if (m_WidgetMode == WidgetMode::Translate) { - if (m_WidgetSpace == WidgetSpace::Global) { - EntityID parent = m_World->GetParent(m_Selection); - glm::quat inverseParentOrientation; - //if (parent != 0) { - inverseParentOrientation = glm::inverse(Transform::AbsoluteOrientation(m_World, parent)); - //} - (glm::vec3&)m_World->GetComponent(m_Selection, "Transform")["Position"] += inverseParentOrientation * movement; - } else if (m_WidgetSpace == WidgetSpace::Local) { - auto selectionTransform = m_World->GetComponent(m_Selection, "Transform"); - (glm::vec3&)selectionTransform["Position"] += glm::quat((glm::vec3)selectionTransform["Orientation"]) * movement; - } - } else if (m_WidgetMode == WidgetMode::Rotate) { - glm::vec3 finalMovement; - finalMovement.x = -deltaWorld.y * m_WidgetCurrentAxis.x; - finalMovement.y = deltaWorld.x * m_WidgetCurrentAxis.y; - finalMovement.z = deltaWorld.y * m_WidgetCurrentAxis.z; - if (m_WidgetSpace == WidgetSpace::Global) { - EntityID parent = m_World->GetParent(m_Selection); - glm::quat parentOrientation; - //if (parent != 0) { - // parentOrientation = RenderSystem::AbsoluteOrientation(m_World, parent); - //} - glm::vec3& selectionOrientation = m_World->GetComponent(m_Selection, "Transform")["Orientation"]; - glm::quat currentOrientation = Transform::AbsoluteOrientation(m_World, m_Selection); - //glm::quat currentOrientation = parentOrientation * glm::quat(selectionOrientation); - glm::quat deltaOrientation(finalMovement); - selectionOrientation = glm::eulerAngles(glm::inverse(parentOrientation) * (deltaOrientation * currentOrientation)); - } else if (m_WidgetSpace == WidgetSpace::Local) { - glm::vec3& selectionOrientation = m_World->GetComponent(m_Selection, "Transform")["Orientation"]; - glm::quat currentOrientation(selectionOrientation); - glm::quat deltaOrientation(finalMovement); - selectionOrientation = glm::eulerAngles(currentOrientation * deltaOrientation); - } - } else if (m_WidgetMode == WidgetMode::Scale) { - glm::vec3& scaleX = m_World->GetComponent(m_WidgetX, "Transform")["Scale"]; - glm::vec3& scaleY = m_World->GetComponent(m_WidgetY, "Transform")["Scale"]; - glm::vec3& scaleZ = m_World->GetComponent(m_WidgetZ, "Transform")["Scale"]; - - if (m_WidgetCurrentAxis.x > 0 && m_WidgetCurrentAxis.y > 0 && m_WidgetCurrentAxis.z > 0) { - float movementLength = glm::length(movement); - float dot = glm::dot((glm::vec3)widgetOrientation, movement); - movement = glm::vec3(movementLength) * glm::sign(dot); - (glm::vec3&)m_World->GetComponent(m_WidgetOrigin, "Transform")["Scale"] += movement; - } - if (m_WidgetCurrentAxis.x > 0) { - scaleX.x += movement.x; - } - if (m_WidgetCurrentAxis.y > 0) { - scaleY.y += movement.y; - } - if (m_WidgetCurrentAxis.z > 0) { - scaleZ.z += movement.z; - } - (glm::vec3&)m_World->GetComponent(m_Selection, "Transform")["Scale"] += movement; - } - } - - - /*LOG_DEBUG("DELTA %f", e.DeltaX); - if (e.X < 0) { - glfwSetCursorPos(m_Renderer->Window(), width - 1, e.Y); - } - if (e.X >= width) { - glfwSetCursorPos(m_Renderer->Window(), 0, e.Y); - }*/ - - return true; -} - -bool EditorSystem::OnMouseRelease(const Events::MouseRelease& e) -{ - if (glm::length2(m_WidgetCurrentAxis) > 0.f) { - m_WidgetCurrentAxis = glm::vec3(0.f); - //setWidgetMode(m_WidgetMode); - } - - return true; -} - -void EditorSystem::Picking() -{ - for (auto& pos : m_PickingQueue) { - auto result = m_Renderer->Pick(pos); - EntityID entity = result.Entity; - if (glm::length2(m_WidgetCurrentAxis) > 0.f) { - // ??? - } else { - LOG_INFO("Selected %i", entity); - if (entity != EntityID_Invalid) { - EntityID parent = m_World->GetParent(entity); - m_Camera = result.Camera; - if (parent == m_Widget) { - m_WidgetCurrentAxis = glm::vec3( - (entity == m_WidgetX) || (entity == m_WidgetOrigin) || (entity == m_WidgetPlaneY || entity == m_WidgetPlaneZ), - (entity == m_WidgetY) || (entity == m_WidgetOrigin) || (entity == m_WidgetPlaneX || entity == m_WidgetPlaneZ), - (entity == m_WidgetZ) || (entity == m_WidgetOrigin) || (entity == m_WidgetPlaneX || entity == m_WidgetPlaneY) - ); - m_WidgetPickingDepth = result.Depth; - //auto widgetTransform = m_World->GetComponent(m_Widget, "Transform"); - //auto selectionTransform = m_World->GetComponent(m_Selection, "Transform"); - //widgetTransform["Position"] = (glm::vec3)selectionTransform["Position"]; - } else { - ImGui::SetActiveID(0, nullptr); - if (m_WidgetMode == WidgetMode::None) { - m_WidgetMode = WidgetMode::Translate; - } - setWidgetMode(m_WidgetMode); - m_Selection = entity; - } - } - } - } - m_PickingQueue.clear(); -}; - -bool EditorSystem::OnFileDropped(const Events::FileDropped& e) -{ - m_LastDroppedFile = boost::filesystem::path(e.Path).lexically_relative(boost::filesystem::current_path()).string(); - std::replace(m_LastDroppedFile.begin(), m_LastDroppedFile.end(), '\\', '/'); - return true; -} - -void EditorSystem::createWidget() -{ - if (m_Widget == EntityID_Invalid) { - m_Widget = m_World->CreateEntity(); - m_World->AttachComponent(m_Widget, "Transform"); - m_WidgetX = m_World->CreateEntity(m_Widget); - m_World->AttachComponent(m_WidgetX, "Transform"); - m_World->AttachComponent(m_WidgetX, "Model"); - m_WidgetPlaneX = m_World->CreateEntity(m_Widget); - m_World->AttachComponent(m_WidgetPlaneX, "Transform"); - m_World->AttachComponent(m_WidgetPlaneX, "Model"); - m_World->GetComponent(m_WidgetPlaneX, "Model")["Resource"] = "Models/WidgetPlaneX.obj"; - m_WidgetY = m_World->CreateEntity(m_Widget); - m_World->AttachComponent(m_WidgetY, "Transform"); - m_World->AttachComponent(m_WidgetY, "Model"); - m_WidgetPlaneY = m_World->CreateEntity(m_Widget); - m_World->AttachComponent(m_WidgetPlaneY, "Transform"); - m_World->AttachComponent(m_WidgetPlaneY, "Model"); - m_World->GetComponent(m_WidgetPlaneY, "Model")["Resource"] = "Models/WidgetPlaneY.obj"; - m_WidgetZ = m_World->CreateEntity(m_Widget); - m_World->AttachComponent(m_WidgetZ, "Transform"); - m_World->AttachComponent(m_WidgetZ, "Model"); - m_WidgetPlaneZ = m_World->CreateEntity(m_Widget); - m_World->AttachComponent(m_WidgetPlaneZ, "Transform"); - m_World->AttachComponent(m_WidgetPlaneZ, "Model"); - m_World->GetComponent(m_WidgetPlaneZ, "Model")["Resource"] = "Models/WidgetPlaneZ.obj"; - m_WidgetOrigin = m_World->CreateEntity(m_Widget); - m_World->AttachComponent(m_WidgetOrigin, "Transform"); - m_World->AttachComponent(m_WidgetOrigin, "Model"); - setWidgetMode(WidgetMode::None); - } -} - -void EditorSystem::updateWidget() -{ - if (m_Widget == EntityID_Invalid) { - return; - } - if (m_Selection == m_Widget) { - return; - } - - if (m_Selection != EntityID_Invalid) { - auto widgetTransform = m_World->GetComponent(m_Widget, "Transform"); - glm::vec3 selectionPosition = Transform::AbsolutePosition(m_World, m_Selection); - widgetTransform["Position"] = selectionPosition; - if (m_WidgetSpace == WidgetSpace::Local) { - widgetTransform["Orientation"] = glm::eulerAngles(Transform::AbsoluteOrientation(m_World, m_Selection)); - } - } -} - -void EditorSystem::setWidgetMode(WidgetMode newMode) -{ - if (m_Widget == EntityID_Invalid) { - return; - } - - auto widgetTransform = m_World->GetComponent(m_Widget, "Transform"); - widgetTransform["Orientation"] = glm::vec3(0.f); - m_World->GetComponent(m_WidgetX, "Transform")["Scale"] = glm::vec3(1.f); - m_World->GetComponent(m_WidgetPlaneX, "Model")["Visible"] = false; - m_World->GetComponent(m_WidgetY, "Transform")["Scale"] = glm::vec3(1.f); - m_World->GetComponent(m_WidgetPlaneY, "Model")["Visible"] = false; - m_World->GetComponent(m_WidgetZ, "Transform")["Scale"] = glm::vec3(1.f); - m_World->GetComponent(m_WidgetPlaneZ, "Model")["Visible"] = false; - m_World->GetComponent(m_WidgetOrigin, "Transform")["Scale"] = glm::vec3(1.f); - m_World->GetComponent(m_WidgetOrigin, "Model")["Visible"] = false; - - if (newMode == WidgetMode::Translate) { - m_World->GetComponent(m_WidgetX, "Model")["Resource"] = "Models/TranslationWidgetX.obj"; - m_World->GetComponent(m_WidgetY, "Model")["Resource"] = "Models/TranslationWidgetY.obj"; - m_World->GetComponent(m_WidgetZ, "Model")["Resource"] = "Models/TranslationWidgetZ.obj"; - // Temporarily disabled for local space until I can figure out what's wrong with the math - if (m_WidgetSpace != WidgetSpace::Local) { - m_World->GetComponent(m_WidgetPlaneX, "Model")["Visible"] = true; - m_World->GetComponent(m_WidgetPlaneY, "Model")["Visible"] = true; - m_World->GetComponent(m_WidgetPlaneZ, "Model")["Visible"] = true; - } - if (m_Selection != EntityID_Invalid) { - if (m_WidgetSpace == WidgetSpace::Local) { - auto selectionTransform = m_World->GetComponent(m_Selection, "Transform"); - widgetTransform["Orientation"] = glm::eulerAngles(Transform::AbsoluteOrientation(m_World, m_Selection)); - } - } - } else if (newMode == WidgetMode::Scale) { - m_World->GetComponent(m_WidgetX, "Model")["Resource"] = "Models/ScaleWidgetX.obj"; - m_World->GetComponent(m_WidgetY, "Model")["Resource"] = "Models/ScaleWidgetY.obj"; - m_World->GetComponent(m_WidgetZ, "Model")["Resource"] = "Models/ScaleWidgetZ.obj"; - m_World->GetComponent(m_WidgetOrigin, "Model")["Visible"] = true; - m_World->GetComponent(m_WidgetOrigin, "Model")["Resource"] = "Models/ScaleWidgetOrigin.obj"; - if (m_Selection != EntityID_Invalid) { - auto selectionTransform = m_World->GetComponent(m_Selection, "Transform"); - widgetTransform["Orientation"] = glm::eulerAngles(Transform::AbsoluteOrientation(m_World, m_Selection)); - } - } else if (newMode == WidgetMode::Rotate) { - m_World->GetComponent(m_WidgetX, "Model")["Resource"] = "Models/RotationWidgetX.obj"; - m_World->GetComponent(m_WidgetY, "Model")["Resource"] = "Models/RotationWidgetY.obj"; - m_World->GetComponent(m_WidgetZ, "Model")["Resource"] = "Models/RotationWidgetZ.obj"; - if (m_Selection != EntityID_Invalid) { - auto selectionTransform = m_World->GetComponent(m_Selection, "Transform"); - if (m_WidgetSpace == WidgetSpace::Local) { - widgetTransform["Orientation"] = glm::eulerAngles(Transform::AbsoluteOrientation(m_World, m_Selection)); - } - } - } - m_WidgetMode = newMode; -} - -void EditorSystem::setWidgetSpace(WidgetSpace space) -{ - m_WidgetSpace = space; - setWidgetMode(m_WidgetMode); -} - -void EditorSystem::drawUI(World* world, double dt) -{ - namespace bfs = boost::filesystem; - - ImGui::ShowTestWindow(); - //ImGui::ShowStyleEditor(); - - if (ImGui::BeginMainMenuBar()) { - if (ImGui::BeginMenu("File")) { - //if (ImGui::MenuItem("New")) { } - if (ImGui::MenuItem("Import", "Ctrl+O")) { - fileImport(world); - } - if (ImGui::MenuItem("Save", "Ctrl+S")) { - fileSave(world); - } - if (ImGui::MenuItem("Save As...", "Ctrl+Shift+S")) { - fileSaveAs(world); - } - ImGui::Separator(); - if (ImGui::MenuItem("Close Editor", "F1")) { } - - ImGui::EndMenu(); - } - - ImGui::SameLine(); - if (ImGui::Button("Move")) { - setWidgetMode(WidgetMode::Translate); - } - ImGui::SameLine(); - if (ImGui::Button("Rotate")) { - setWidgetMode(WidgetMode::Rotate); - } - ImGui::SameLine(); - if (ImGui::Button("Scale")) { - setWidgetMode(WidgetMode::Scale); - } - ImGui::SameLine(); - if (m_WidgetSpace == WidgetSpace::Global) { - if (ImGui::Button("(Global)")) { - setWidgetSpace(WidgetSpace::Local); - } - } else if (m_WidgetSpace == WidgetSpace::Local) { - if (ImGui::Button("(Local)")) { - setWidgetSpace(WidgetSpace::Global); - } - } - - ImGui::EndMainMenuBar(); - } - - std::string title = std::string("Components #") + std::to_string(m_Selection) + std::string("###Components"); - if (ImGui::Begin(title.c_str())) { - if (m_Selection != EntityID_Invalid) { - auto& pools = world->GetComponentPools(); - - std::vector componentTypes; - for (auto& pair : pools) { - // Only add components the entity doesn't already have - if (!pair.second->KnowsEntity(m_Selection)) { - componentTypes.push_back(pair.first.c_str()); - } - } - int item = -1; - ImGui::PushItemWidth(ImGui::GetWindowContentRegionWidth() - 5.f); - if (ImGui::Combo("", &item, componentTypes.data(), componentTypes.size())) { - if (item != -1) { - std::string chosenType = std::string(componentTypes.at(item)); - world->AttachComponent(m_Selection, chosenType); - } - } - ImGui::PopItemWidth(); - - for (auto& pair : pools) { - const std::string& componentType = pair.first; - auto pool = pair.second; - if (!pool->KnowsEntity(m_Selection)) { - continue; - } - auto& ci = pool->ComponentInfo(); - - bool deletePressed = createDeleteButton(componentType); - if (deletePressed) { - world->DeleteComponent(m_Selection, componentType); - continue; - } - - if (ImGui::CollapsingHeader(componentType.c_str())) { - if (!ci.Meta->Annotation.empty()) { - ImGui::Text(ci.Meta->Annotation.c_str()); - } - - auto& component = world->GetComponent(m_Selection, componentType); - for (auto& kv : ci.Fields) { - const std::string& fieldName = kv.first; - auto& field = kv.second; - - std::string uniqueID = componentType + fieldName; - ImGui::PushID(uniqueID.c_str()); - if (field.Type == "Vector") { - auto& val = component.Field(fieldName); - if (fieldName == "Scale") { - ImGui::DragFloat3("", glm::value_ptr(val), 0.1f, 0.f, std::numeric_limits::max()); - } else if (fieldName == "Orientation") { - glm::vec3 tempVal = glm::fmod(val, glm::vec3(glm::two_pi())); - if (ImGui::SliderFloat3("", glm::value_ptr(tempVal), 0.f, glm::two_pi())) { - val = tempVal; - } - } else { - ImGui::DragFloat3("", glm::value_ptr(val), 0.1f, std::numeric_limits::lowest(), std::numeric_limits::max()); - } - } else if (field.Type == "Color") { - auto& val = component.Field(fieldName); - ImGui::ColorEdit4("", glm::value_ptr(val), true); - } else if (field.Type == "string") { - std::string& val = component.Field(fieldName); - char tempString[1024]; - memcpy(tempString, val.c_str(), std::min(val.length() + 1, sizeof(tempString))); - if (ImGui::InputText("", tempString, sizeof(tempString))) { - val = std::string(tempString); - LOG_DEBUG("%s::%s changed!", componentType.c_str(), fieldName.c_str()); - } - // DROP STUFF - if (ImGui::IsItemHovered() && !m_LastDroppedFile.empty()) { - val = m_LastDroppedFile; - m_LastDroppedFile = ""; - } - - } else if (field.Type == "double") { - float tempVal = static_cast(component.Field(fieldName)); - if (ImGui::InputFloat("", &tempVal, 0.01f, 1.f)) { - component.SetField(fieldName, static_cast(tempVal)); - } - } else if (field.Type == "int") { - int val = component.Field(fieldName); - ImGui::InputInt("", &val); - } else if (field.Type == "enum") { - int currentValue = component.Field(fieldName); - int item = -1; - std::stringstream enumKeys; - std::vector enumValues; - int i = 0; - for (auto& kv : ci.Meta->FieldEnumDefinitions.at(fieldName)) { - enumKeys << kv.first << " (" << kv.second << ")" << '\0'; - enumValues.push_back(kv.second); - if (currentValue == kv.second) { - item = i; - } - i++; - } - if (ImGui::Combo("", &item, enumKeys.str().c_str())) { - component.SetField(fieldName, enumValues.at(item)); - } - } else if (field.Type == "bool") { - auto& val = component.Field(fieldName); - ImGui::Checkbox("", &val); - } else { - ImGui::TextDisabled(field.Type.c_str()); - } - ImGui::PopID(); - - ImGui::SameLine(); - ImGui::Text(fieldName.c_str()); - if (ImGui::IsItemHovered()) { - ImGui::SetTooltip("field annotation goes here"); - } - } - } - } - } - - } - ImGui::End(); - - if (ImGui::Begin("Entities")) { - auto entityChildren = world->GetEntityChildren(); - std::function recurse = [&](EntityID parent) { - auto range = entityChildren.equal_range(parent); - for (auto it = range.first; it != range.second; it++) { - if (createEntityNode(world, it->second)) { - recurse(it->second); - ImGui::TreePop(); - } - } - }; - recurse(EntityID_Invalid); - } - ImGui::End(); -} - -bool EditorSystem::createEntityNode(World* world, EntityID entity) -{ - // HACK: Don't show the widget entities in the entity tree - if (entity == m_Widget) { - return false; - } - - ImVec2 pos = ImGui::GetCursorScreenPos(); - float width = ImGui::GetContentRegionAvailWidth(); - ImRect bb(pos + ImVec2(20, 0), pos + ImVec2(width, 13)); - auto window = ImGui::GetCurrentWindow(); - if (m_Selection == entity) { - const ImU32 col = window->Color(ImGuiCol_HeaderActive); - window->DrawList->AddRectFilled(bb.Min, bb.Max, col); - } - ImGuiID id = window->GetID((std::string("#SelectButton") + std::to_string(entity)).c_str()); - bool hovered = false; - bool held = false; - if (ImGui::ButtonBehavior(bb, id, &hovered, &held)) { - m_Selection = entity; - } - if (held) { - ImVec2 entityDragDelta = ImGui::GetMouseDragDelta(0); - if (std::abs(entityDragDelta.x) > 0 && std::abs(entityDragDelta.y) > 0) { - if (m_UIDraggingEntity == EntityID_Invalid) { - m_UIDraggingEntity = entity; - LOG_DEBUG("Started drag of entity %i", m_UIDraggingEntity); - } - ImGui::SetNextWindowPos(ImGui::GetIO().MousePos + ImVec2(20, 0)); - ImGui::Begin("Change parent", nullptr, ImVec2(0, 0), 0.3f, ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoResize|ImGuiWindowFlags_NoMove|ImGuiWindowFlags_NoSavedSettings); - ImGui::Text("#%i", m_UIDraggingEntity); - ImGui::End(); - } - } - - ImGui::SetNextTreeNodeOpened(true, ImGuiSetCond_Once); - std::string nodeTitle; - const std::string& entityName = world->GetName(entity); - if (!entityName.empty()) { - nodeTitle = entityName; - } else { - nodeTitle = std::string("#") + std::to_string(entity); - } - if (ImGui::TreeNode(nodeTitle.c_str())) { - if (m_UIDraggingEntity != EntityID_Invalid && ImGui::IsItemHoveredRect() && ImGui::IsMouseReleased(0)) { - LOG_DEBUG("Changed parent of %i to %i", m_UIDraggingEntity, entity); - changeParent(m_UIDraggingEntity, entity); - m_UIDraggingEntity = EntityID_Invalid; - } - - if (ImGui::BeginPopupContextItem("item context menu")) { - if (ImGui::Button("Add")) { - EntityID newEntity = world->CreateEntity(entity); - world->AttachComponent(newEntity, "Transform"); - } - ImGui::SameLine(); - if (ImGui::Button("Delete")) { - world->DeleteEntity(entity); - ImGui::CloseCurrentPopup(); - if (!world->ValidEntity(m_Selection)) { - m_Selection = EntityID_Invalid; - } - } - ImGui::EndPopup(); - } - return true; - } else { - return false; - } -} - -bool EditorSystem::createDeleteButton(std::string componentType) -{ - float width = ImGui::GetContentRegionAvailWidth(); - ImGuiWindow* window = ImGui::GetCurrentWindow(); - auto pos = ImGui::GetCursorScreenPos() + ImVec2(width - 14.f, 1); - ImRect bb = ImRect(pos, pos + ImVec2(14.f, 14.f)); - std::string idString = "#DELETE"; - idString += componentType; - ImGuiID id = window->GetID(idString.c_str()); - bool hovered; - bool held; - bool pressed = ImGui::ButtonBehavior(bb, id, &hovered, &held); - //ImU32 col = window->Color((held && hovered) ? ImGuiCol_CloseButtonActive : hovered ? ImGuiCol_CloseButtonHovered : ImGuiCol_CloseButton); - ImU32 col = window->Color((held && hovered) ? ImGuiCol_CloseButtonActive : hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button); - window->DrawList->AddCircleFilled(bb.GetCenter(), 7.f, col, 16); - return pressed; -} - -void EditorSystem::changeParent(EntityID entity, EntityID newParent) -{ - if (entity == newParent) { - return; - } - - // An entity can't be a child to one of its own children - auto children = m_World->GetEntityChildren().equal_range(entity); - for (auto it = children.first; it != children.second; it++) { - if (it->second == newParent) { - return; - } - } - - m_World->SetParent(entity, newParent); -} - -void EditorSystem::fileImport(World* world) -{ - m_CurrentFile = openDialog(m_DefaultEntityDir); - auto file = ResourceManager::Load(m_CurrentFile.string()); - EntityFilePreprocessor fpp(file); - fpp.RegisterComponents(world); - EntityFileParser fp(file); - fp.MergeEntities(world); - createWidget(); - updateWidget(); -} - -void EditorSystem::fileSave(World* world) -{ - if (boost::filesystem::exists(m_CurrentFile)) { - // HACK: Delete the widgets so they don't appear in the saved file - world->DeleteEntity(m_Widget); - m_Widget = EntityID_Invalid; - - EntityFileWriter writer(m_CurrentFile.string()); - writer.WriteWorld(world); - - createWidget(); - } else { - fileSaveAs(world); - } -} - -void EditorSystem::fileSaveAs(World* world) -{ - auto filePath = saveDialog(m_DefaultEntityDir); - if (filePath.empty()) { - return; - } - - // HACK: Delete the widgets so they don't appear in the saved file - world->DeleteEntity(m_Widget); - m_Widget = EntityID_Invalid; - - EntityFileWriter writer(filePath.string()); - writer.WriteWorld(world); - - createWidget(); -} + m_DebugCameraInputController->Update(dt); + m_Camera["Transform"]["Position"] = m_DebugCameraInputController->Position(); + m_Camera["Transform"]["Orientation"] = glm::eulerAngles(m_DebugCameraInputController->Orientation()); +} \ No newline at end of file diff --git a/src/Engine/Editor/EditorSystemOld.cpp b/src/Engine/Editor/EditorSystemOld.cpp new file mode 100644 index 00000000..675ea80b --- /dev/null +++ b/src/Engine/Editor/EditorSystemOld.cpp @@ -0,0 +1,738 @@ +#include "Editor/EditorSystemOld.h" +#define IMGUI_DEFINE_MATH_OPERATORS +#include + +EditorSystemOld::EditorSystemOld(EventBroker* eventBroker, IRenderer* renderer) + : System(eventBroker) + , ImpureSystem() + , m_Renderer(renderer) +{ + auto config = ResourceManager::Load("Config.ini"); + m_Enabled = config->Get("Debug.EditorEnabled", false); + m_Visible = m_Enabled; + m_DefaultEntityDir = boost::filesystem::path("Schema") / boost::filesystem::path("Entities"); + + if (!m_Enabled) { + return; + } + + EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &EditorSystemOld::OnInputCommand); + EVENT_SUBSCRIBE_MEMBER(m_EMousePress, &EditorSystemOld::OnMousePress); + EVENT_SUBSCRIBE_MEMBER(m_EMouseRelease, &EditorSystemOld::OnMouseRelease); + EVENT_SUBSCRIBE_MEMBER(m_EMouseMove, &EditorSystemOld::OnMouseMove); + EVENT_SUBSCRIBE_MEMBER(m_EFileDropped, &EditorSystemOld::OnFileDropped); +} + +void EditorSystemOld::Update(World* world, double dt) +{ + m_World = world; + + if (!m_Enabled) { + return; + } + + if (!m_Visible) { + return; + } + Picking(); + updateWidget(); + + drawUI(world, dt); + + // Clear drop queue if it wasn't handled by any UI element + if (!m_LastDroppedFile.empty()) { + m_LastDroppedFile = ""; + } +} + + +boost::filesystem::path EditorSystemOld::openDialog(boost::filesystem::path defaultPath) +{ + namespace bfs = boost::filesystem; + auto absolutePath = bfs::absolute(defaultPath); + nfdchar_t* outPath = nullptr; + nfdresult_t result = NFD_OpenDialog(NULL, absolutePath.string().c_str(), &outPath); + if (result == NFD_ERROR) { + LOG_ERROR("NFD Error: %s", NFD_GetError()); + return bfs::path(); + } + + return bfs::absolute(outPath); +} + +boost::filesystem::path EditorSystemOld::saveDialog(boost::filesystem::path defaultPath) +{ + namespace bfs = boost::filesystem; + auto absolutePath = bfs::absolute(defaultPath); + nfdchar_t* outPath = nullptr; + nfdresult_t result = NFD_SaveDialog(NULL, absolutePath.string().c_str(), &outPath); + if (result == NFD_ERROR) { + LOG_ERROR("NFD Error: %s", NFD_GetError()); + return bfs::path(); + } + + return bfs::absolute(outPath); +} + +bool EditorSystemOld::OnInputCommand(const Events::InputCommand& e) +{ + if (e.Command == "ToggleEditor" && e.Value > 0) { + m_Visible = !m_Visible; + } + + if (e.Command == "EditorToolMove" && e.Value > 0) { + setWidgetMode(WidgetMode::Translate); + } + if (e.Command == "EditorToolRotate" && e.Value > 0) { + setWidgetMode(WidgetMode::Rotate); + } + if (e.Command == "EditorToolScale" && e.Value > 0) { + setWidgetMode(WidgetMode::Scale); + } + + if (e.Command == "EditorToggleTransformSpace" && e.Value > 0) { + if (m_WidgetSpace == WidgetSpace::Global) { + setWidgetSpace(WidgetSpace::Local); + } else if (m_WidgetSpace == WidgetSpace::Local) { + setWidgetSpace(WidgetSpace::Global); + } + } + + return true; +} + +bool EditorSystemOld::OnMousePress(const Events::MousePress& e) +{ + if (e.Button == GLFW_MOUSE_BUTTON_RIGHT) { + m_PickingQueue.push_back(glm::vec2((int)e.X, (int)e.Y)); + } + return true; +} + +bool EditorSystemOld::OnMouseMove(const Events::MouseMove& e) +{ + if (m_Widget == EntityID_Invalid) { + return false; + } + if (m_Selection == EntityID_Invalid) { + return false; + } + if (m_Selection == m_Widget) { + return false; + } + // TODO: No widgets for root entity until widgets reside in thier own world, + // or the widgets will move relative to the root entity being moved, which is WEEEIRD. + if (m_Selection == 0) { + return false; + } + if (m_Camera == nullptr) { + return false; + } + + auto widgetTransform = m_World->GetComponent(m_Widget, "Transform"); + glm::vec3 widgetOrientation = widgetTransform["Orientation"]; + glm::quat totalOrientation = m_Camera->Orientation() * glm::inverse(glm::quat(widgetOrientation)); + + int width; + int height; + glfwGetFramebufferSize(m_Renderer->Window(), &width, &height); + Rectangle res(width, height); + + glm::vec2 delta2(res.Width / 2.f + e.DeltaX, res.Height / 2.f + -e.DeltaY); + glm::vec3 deltaWorld = ScreenCoords::ToWorldPos( + delta2, + m_WidgetPickingDepth, + res, + m_Camera->ProjectionMatrix(), + glm::toMat4(glm::inverse(totalOrientation)) + ); + glm::vec3 origin = ScreenCoords::ToWorldPos( + glm::vec2(res.Width / 2.f, res.Height / 2.f), + m_WidgetPickingDepth, + res, + m_Camera->ProjectionMatrix(), + glm::toMat4(glm::inverse(totalOrientation)) + ); + deltaWorld = deltaWorld - origin; + glm::vec3 movement = deltaWorld * m_WidgetCurrentAxis; + + if (glm::length2(m_WidgetCurrentAxis) > 0.f) { + auto widgetTransform = m_World->GetComponent(m_Widget, "Transform"); + if (m_WidgetMode == WidgetMode::Translate) { + if (m_WidgetSpace == WidgetSpace::Global) { + EntityID parent = m_World->GetParent(m_Selection); + glm::quat inverseParentOrientation; + //if (parent != 0) { + inverseParentOrientation = glm::inverse(Transform::AbsoluteOrientation(m_World, parent)); + //} + (glm::vec3&)m_World->GetComponent(m_Selection, "Transform")["Position"] += inverseParentOrientation * movement; + } else if (m_WidgetSpace == WidgetSpace::Local) { + auto selectionTransform = m_World->GetComponent(m_Selection, "Transform"); + (glm::vec3&)selectionTransform["Position"] += glm::quat((glm::vec3)selectionTransform["Orientation"]) * movement; + } + } else if (m_WidgetMode == WidgetMode::Rotate) { + glm::vec3 finalMovement; + finalMovement.x = -deltaWorld.y * m_WidgetCurrentAxis.x; + finalMovement.y = deltaWorld.x * m_WidgetCurrentAxis.y; + finalMovement.z = deltaWorld.y * m_WidgetCurrentAxis.z; + if (m_WidgetSpace == WidgetSpace::Global) { + EntityID parent = m_World->GetParent(m_Selection); + glm::quat parentOrientation; + //if (parent != 0) { + // parentOrientation = RenderSystem::AbsoluteOrientation(m_World, parent); + //} + glm::vec3& selectionOrientation = m_World->GetComponent(m_Selection, "Transform")["Orientation"]; + glm::quat currentOrientation = Transform::AbsoluteOrientation(m_World, m_Selection); + //glm::quat currentOrientation = parentOrientation * glm::quat(selectionOrientation); + glm::quat deltaOrientation(finalMovement); + selectionOrientation = glm::eulerAngles(glm::inverse(parentOrientation) * (deltaOrientation * currentOrientation)); + } else if (m_WidgetSpace == WidgetSpace::Local) { + glm::vec3& selectionOrientation = m_World->GetComponent(m_Selection, "Transform")["Orientation"]; + glm::quat currentOrientation(selectionOrientation); + glm::quat deltaOrientation(finalMovement); + selectionOrientation = glm::eulerAngles(currentOrientation * deltaOrientation); + } + } else if (m_WidgetMode == WidgetMode::Scale) { + glm::vec3& scaleX = m_World->GetComponent(m_WidgetX, "Transform")["Scale"]; + glm::vec3& scaleY = m_World->GetComponent(m_WidgetY, "Transform")["Scale"]; + glm::vec3& scaleZ = m_World->GetComponent(m_WidgetZ, "Transform")["Scale"]; + + if (m_WidgetCurrentAxis.x > 0 && m_WidgetCurrentAxis.y > 0 && m_WidgetCurrentAxis.z > 0) { + float movementLength = glm::length(movement); + float dot = glm::dot((glm::vec3)widgetOrientation, movement); + movement = glm::vec3(movementLength) * glm::sign(dot); + (glm::vec3&)m_World->GetComponent(m_WidgetOrigin, "Transform")["Scale"] += movement; + } + if (m_WidgetCurrentAxis.x > 0) { + scaleX.x += movement.x; + } + if (m_WidgetCurrentAxis.y > 0) { + scaleY.y += movement.y; + } + if (m_WidgetCurrentAxis.z > 0) { + scaleZ.z += movement.z; + } + (glm::vec3&)m_World->GetComponent(m_Selection, "Transform")["Scale"] += movement; + } + } + + + /*LOG_DEBUG("DELTA %f", e.DeltaX); + if (e.X < 0) { + glfwSetCursorPos(m_Renderer->Window(), width - 1, e.Y); + } + if (e.X >= width) { + glfwSetCursorPos(m_Renderer->Window(), 0, e.Y); + }*/ + + return true; +} + +bool EditorSystemOld::OnMouseRelease(const Events::MouseRelease& e) +{ + if (glm::length2(m_WidgetCurrentAxis) > 0.f) { + m_WidgetCurrentAxis = glm::vec3(0.f); + //setWidgetMode(m_WidgetMode); + } + + return true; +} + +void EditorSystemOld::Picking() +{ + for (auto& pos : m_PickingQueue) { + auto result = m_Renderer->Pick(pos); + EntityID entity = result.Entity; + if (glm::length2(m_WidgetCurrentAxis) > 0.f) { + // ??? + } else { + LOG_INFO("Selected %i", entity); + if (entity != EntityID_Invalid) { + EntityID parent = m_World->GetParent(entity); + m_Camera = result.Camera; + if (parent == m_Widget) { + m_WidgetCurrentAxis = glm::vec3( + (entity == m_WidgetX) || (entity == m_WidgetOrigin) || (entity == m_WidgetPlaneY || entity == m_WidgetPlaneZ), + (entity == m_WidgetY) || (entity == m_WidgetOrigin) || (entity == m_WidgetPlaneX || entity == m_WidgetPlaneZ), + (entity == m_WidgetZ) || (entity == m_WidgetOrigin) || (entity == m_WidgetPlaneX || entity == m_WidgetPlaneY) + ); + m_WidgetPickingDepth = result.Depth; + //auto widgetTransform = m_World->GetComponent(m_Widget, "Transform"); + //auto selectionTransform = m_World->GetComponent(m_Selection, "Transform"); + //widgetTransform["Position"] = (glm::vec3)selectionTransform["Position"]; + } else { + ImGui::SetActiveID(0, nullptr); + if (m_WidgetMode == WidgetMode::None) { + m_WidgetMode = WidgetMode::Translate; + } + setWidgetMode(m_WidgetMode); + m_Selection = entity; + } + } + } + } + m_PickingQueue.clear(); +}; + +bool EditorSystemOld::OnFileDropped(const Events::FileDropped& e) +{ + m_LastDroppedFile = boost::filesystem::path(e.Path).lexically_relative(boost::filesystem::current_path()).string(); + std::replace(m_LastDroppedFile.begin(), m_LastDroppedFile.end(), '\\', '/'); + return true; +} + +void EditorSystemOld::createWidget() +{ + if (m_Widget == EntityID_Invalid) { + m_Widget = m_World->CreateEntity(); + m_World->AttachComponent(m_Widget, "Transform"); + m_WidgetX = m_World->CreateEntity(m_Widget); + m_World->AttachComponent(m_WidgetX, "Transform"); + m_World->AttachComponent(m_WidgetX, "Model"); + m_WidgetPlaneX = m_World->CreateEntity(m_Widget); + m_World->AttachComponent(m_WidgetPlaneX, "Transform"); + m_World->AttachComponent(m_WidgetPlaneX, "Model"); + m_World->GetComponent(m_WidgetPlaneX, "Model")["Resource"] = "Models/WidgetPlaneX.obj"; + m_WidgetY = m_World->CreateEntity(m_Widget); + m_World->AttachComponent(m_WidgetY, "Transform"); + m_World->AttachComponent(m_WidgetY, "Model"); + m_WidgetPlaneY = m_World->CreateEntity(m_Widget); + m_World->AttachComponent(m_WidgetPlaneY, "Transform"); + m_World->AttachComponent(m_WidgetPlaneY, "Model"); + m_World->GetComponent(m_WidgetPlaneY, "Model")["Resource"] = "Models/WidgetPlaneY.obj"; + m_WidgetZ = m_World->CreateEntity(m_Widget); + m_World->AttachComponent(m_WidgetZ, "Transform"); + m_World->AttachComponent(m_WidgetZ, "Model"); + m_WidgetPlaneZ = m_World->CreateEntity(m_Widget); + m_World->AttachComponent(m_WidgetPlaneZ, "Transform"); + m_World->AttachComponent(m_WidgetPlaneZ, "Model"); + m_World->GetComponent(m_WidgetPlaneZ, "Model")["Resource"] = "Models/WidgetPlaneZ.obj"; + m_WidgetOrigin = m_World->CreateEntity(m_Widget); + m_World->AttachComponent(m_WidgetOrigin, "Transform"); + m_World->AttachComponent(m_WidgetOrigin, "Model"); + setWidgetMode(WidgetMode::None); + } +} + +void EditorSystemOld::updateWidget() +{ + if (m_Widget == EntityID_Invalid) { + return; + } + if (m_Selection == m_Widget) { + return; + } + + if (m_Selection != EntityID_Invalid) { + auto widgetTransform = m_World->GetComponent(m_Widget, "Transform"); + glm::vec3 selectionPosition = Transform::AbsolutePosition(m_World, m_Selection); + widgetTransform["Position"] = selectionPosition; + if (m_WidgetSpace == WidgetSpace::Local) { + widgetTransform["Orientation"] = glm::eulerAngles(Transform::AbsoluteOrientation(m_World, m_Selection)); + } + } +} + +void EditorSystemOld::setWidgetMode(WidgetMode newMode) +{ + if (m_Widget == EntityID_Invalid) { + return; + } + + auto widgetTransform = m_World->GetComponent(m_Widget, "Transform"); + widgetTransform["Orientation"] = glm::vec3(0.f); + m_World->GetComponent(m_WidgetX, "Transform")["Scale"] = glm::vec3(1.f); + m_World->GetComponent(m_WidgetPlaneX, "Model")["Visible"] = false; + m_World->GetComponent(m_WidgetY, "Transform")["Scale"] = glm::vec3(1.f); + m_World->GetComponent(m_WidgetPlaneY, "Model")["Visible"] = false; + m_World->GetComponent(m_WidgetZ, "Transform")["Scale"] = glm::vec3(1.f); + m_World->GetComponent(m_WidgetPlaneZ, "Model")["Visible"] = false; + m_World->GetComponent(m_WidgetOrigin, "Transform")["Scale"] = glm::vec3(1.f); + m_World->GetComponent(m_WidgetOrigin, "Model")["Visible"] = false; + + if (newMode == WidgetMode::Translate) { + m_World->GetComponent(m_WidgetX, "Model")["Resource"] = "Models/TranslationWidgetX.obj"; + m_World->GetComponent(m_WidgetY, "Model")["Resource"] = "Models/TranslationWidgetY.obj"; + m_World->GetComponent(m_WidgetZ, "Model")["Resource"] = "Models/TranslationWidgetZ.obj"; + // Temporarily disabled for local space until I can figure out what's wrong with the math + if (m_WidgetSpace != WidgetSpace::Local) { + m_World->GetComponent(m_WidgetPlaneX, "Model")["Visible"] = true; + m_World->GetComponent(m_WidgetPlaneY, "Model")["Visible"] = true; + m_World->GetComponent(m_WidgetPlaneZ, "Model")["Visible"] = true; + } + if (m_Selection != EntityID_Invalid) { + if (m_WidgetSpace == WidgetSpace::Local) { + auto selectionTransform = m_World->GetComponent(m_Selection, "Transform"); + widgetTransform["Orientation"] = glm::eulerAngles(Transform::AbsoluteOrientation(m_World, m_Selection)); + } + } + } else if (newMode == WidgetMode::Scale) { + m_World->GetComponent(m_WidgetX, "Model")["Resource"] = "Models/ScaleWidgetX.obj"; + m_World->GetComponent(m_WidgetY, "Model")["Resource"] = "Models/ScaleWidgetY.obj"; + m_World->GetComponent(m_WidgetZ, "Model")["Resource"] = "Models/ScaleWidgetZ.obj"; + m_World->GetComponent(m_WidgetOrigin, "Model")["Visible"] = true; + m_World->GetComponent(m_WidgetOrigin, "Model")["Resource"] = "Models/ScaleWidgetOrigin.obj"; + if (m_Selection != EntityID_Invalid) { + auto selectionTransform = m_World->GetComponent(m_Selection, "Transform"); + widgetTransform["Orientation"] = glm::eulerAngles(Transform::AbsoluteOrientation(m_World, m_Selection)); + } + } else if (newMode == WidgetMode::Rotate) { + m_World->GetComponent(m_WidgetX, "Model")["Resource"] = "Models/RotationWidgetX.obj"; + m_World->GetComponent(m_WidgetY, "Model")["Resource"] = "Models/RotationWidgetY.obj"; + m_World->GetComponent(m_WidgetZ, "Model")["Resource"] = "Models/RotationWidgetZ.obj"; + if (m_Selection != EntityID_Invalid) { + auto selectionTransform = m_World->GetComponent(m_Selection, "Transform"); + if (m_WidgetSpace == WidgetSpace::Local) { + widgetTransform["Orientation"] = glm::eulerAngles(Transform::AbsoluteOrientation(m_World, m_Selection)); + } + } + } + m_WidgetMode = newMode; +} + +void EditorSystemOld::setWidgetSpace(WidgetSpace space) +{ + m_WidgetSpace = space; + setWidgetMode(m_WidgetMode); +} + +void EditorSystemOld::drawUI(World* world, double dt) +{ + namespace bfs = boost::filesystem; + + ImGui::ShowTestWindow(); + //ImGui::ShowStyleEditor(); + + if (ImGui::BeginMainMenuBar()) { + if (ImGui::BeginMenu("File")) { + //if (ImGui::MenuItem("New")) { } + if (ImGui::MenuItem("Import", "Ctrl+O")) { + fileImport(world); + } + if (ImGui::MenuItem("Save", "Ctrl+S")) { + fileSave(world); + } + if (ImGui::MenuItem("Save As...", "Ctrl+Shift+S")) { + fileSaveAs(world); + } + ImGui::Separator(); + if (ImGui::MenuItem("Close Editor", "F1")) { } + + ImGui::EndMenu(); + } + + ImGui::SameLine(); + if (ImGui::Button("Move")) { + setWidgetMode(WidgetMode::Translate); + } + ImGui::SameLine(); + if (ImGui::Button("Rotate")) { + setWidgetMode(WidgetMode::Rotate); + } + ImGui::SameLine(); + if (ImGui::Button("Scale")) { + setWidgetMode(WidgetMode::Scale); + } + ImGui::SameLine(); + if (m_WidgetSpace == WidgetSpace::Global) { + if (ImGui::Button("(Global)")) { + setWidgetSpace(WidgetSpace::Local); + } + } else if (m_WidgetSpace == WidgetSpace::Local) { + if (ImGui::Button("(Local)")) { + setWidgetSpace(WidgetSpace::Global); + } + } + + ImGui::EndMainMenuBar(); + } + + std::string title = std::string("Components #") + std::to_string(m_Selection) + std::string("###Components"); + if (ImGui::Begin(title.c_str())) { + if (m_Selection != EntityID_Invalid) { + auto& pools = world->GetComponentPools(); + + std::vector componentTypes; + for (auto& pair : pools) { + // Only add components the entity doesn't already have + if (!pair.second->KnowsEntity(m_Selection)) { + componentTypes.push_back(pair.first.c_str()); + } + } + int item = -1; + ImGui::PushItemWidth(ImGui::GetWindowContentRegionWidth() - 5.f); + if (ImGui::Combo("", &item, componentTypes.data(), componentTypes.size())) { + if (item != -1) { + std::string chosenType = std::string(componentTypes.at(item)); + world->AttachComponent(m_Selection, chosenType); + } + } + ImGui::PopItemWidth(); + + for (auto& pair : pools) { + const std::string& componentType = pair.first; + auto pool = pair.second; + if (!pool->KnowsEntity(m_Selection)) { + continue; + } + auto& ci = pool->ComponentInfo(); + + bool deletePressed = createDeleteButton(componentType); + if (deletePressed) { + world->DeleteComponent(m_Selection, componentType); + continue; + } + + if (ImGui::CollapsingHeader(componentType.c_str())) { + if (!ci.Meta->Annotation.empty()) { + ImGui::Text(ci.Meta->Annotation.c_str()); + } + + auto& component = world->GetComponent(m_Selection, componentType); + for (auto& kv : ci.Fields) { + const std::string& fieldName = kv.first; + auto& field = kv.second; + + std::string uniqueID = componentType + fieldName; + ImGui::PushID(uniqueID.c_str()); + if (field.Type == "Vector") { + auto& val = component.Field(fieldName); + if (fieldName == "Scale") { + ImGui::DragFloat3("", glm::value_ptr(val), 0.1f, 0.f, std::numeric_limits::max()); + } else if (fieldName == "Orientation") { + glm::vec3 tempVal = glm::fmod(val, glm::vec3(glm::two_pi())); + if (ImGui::SliderFloat3("", glm::value_ptr(tempVal), 0.f, glm::two_pi())) { + val = tempVal; + } + } else { + ImGui::DragFloat3("", glm::value_ptr(val), 0.1f, std::numeric_limits::lowest(), std::numeric_limits::max()); + } + } else if (field.Type == "Color") { + auto& val = component.Field(fieldName); + ImGui::ColorEdit4("", glm::value_ptr(val), true); + } else if (field.Type == "string") { + std::string& val = component.Field(fieldName); + char tempString[1024]; + memcpy(tempString, val.c_str(), std::min(val.length() + 1, sizeof(tempString))); + if (ImGui::InputText("", tempString, sizeof(tempString))) { + val = std::string(tempString); + LOG_DEBUG("%s::%s changed!", componentType.c_str(), fieldName.c_str()); + } + // DROP STUFF + if (ImGui::IsItemHovered() && !m_LastDroppedFile.empty()) { + val = m_LastDroppedFile; + m_LastDroppedFile = ""; + } + + } else if (field.Type == "double") { + float tempVal = static_cast(component.Field(fieldName)); + if (ImGui::InputFloat("", &tempVal, 0.01f, 1.f)) { + component.SetField(fieldName, static_cast(tempVal)); + } + } else if (field.Type == "int") { + int val = component.Field(fieldName); + ImGui::InputInt("", &val); + } else if (field.Type == "enum") { + int currentValue = component.Field(fieldName); + int item = -1; + std::stringstream enumKeys; + std::vector enumValues; + int i = 0; + for (auto& kv : ci.Meta->FieldEnumDefinitions.at(fieldName)) { + enumKeys << kv.first << " (" << kv.second << ")" << '\0'; + enumValues.push_back(kv.second); + if (currentValue == kv.second) { + item = i; + } + i++; + } + if (ImGui::Combo("", &item, enumKeys.str().c_str())) { + component.SetField(fieldName, enumValues.at(item)); + } + } else if (field.Type == "bool") { + auto& val = component.Field(fieldName); + ImGui::Checkbox("", &val); + } else { + ImGui::TextDisabled(field.Type.c_str()); + } + ImGui::PopID(); + + ImGui::SameLine(); + ImGui::Text(fieldName.c_str()); + if (ImGui::IsItemHovered()) { + ImGui::SetTooltip("field annotation goes here"); + } + } + } + } + } + + } + ImGui::End(); + + if (ImGui::Begin("Entities")) { + auto entityChildren = world->GetEntityChildren(); + std::function recurse = [&](EntityID parent) { + auto range = entityChildren.equal_range(parent); + for (auto it = range.first; it != range.second; it++) { + if (createEntityNode(world, it->second)) { + recurse(it->second); + ImGui::TreePop(); + } + } + }; + recurse(EntityID_Invalid); + } + ImGui::End(); +} + +bool EditorSystemOld::createEntityNode(World* world, EntityID entity) +{ + // HACK: Don't show the widget entities in the entity tree + if (entity == m_Widget) { + return false; + } + + ImVec2 pos = ImGui::GetCursorScreenPos(); + float width = ImGui::GetContentRegionAvailWidth(); + ImRect bb(pos + ImVec2(20, 0), pos + ImVec2(width, 13)); + auto window = ImGui::GetCurrentWindow(); + if (m_Selection == entity) { + const ImU32 col = window->Color(ImGuiCol_HeaderActive); + window->DrawList->AddRectFilled(bb.Min, bb.Max, col); + } + ImGuiID id = window->GetID((std::string("#SelectButton") + std::to_string(entity)).c_str()); + bool hovered = false; + bool held = false; + if (ImGui::ButtonBehavior(bb, id, &hovered, &held)) { + m_Selection = entity; + } + if (held) { + ImVec2 entityDragDelta = ImGui::GetMouseDragDelta(0); + if (std::abs(entityDragDelta.x) > 0 && std::abs(entityDragDelta.y) > 0) { + if (m_UIDraggingEntity == EntityID_Invalid) { + m_UIDraggingEntity = entity; + LOG_DEBUG("Started drag of entity %i", m_UIDraggingEntity); + } + ImGui::SetNextWindowPos(ImGui::GetIO().MousePos + ImVec2(20, 0)); + ImGui::Begin("Change parent", nullptr, ImVec2(0, 0), 0.3f, ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoResize|ImGuiWindowFlags_NoMove|ImGuiWindowFlags_NoSavedSettings); + ImGui::Text("#%i", m_UIDraggingEntity); + ImGui::End(); + } + } + + ImGui::SetNextTreeNodeOpened(true, ImGuiSetCond_Once); + std::string nodeTitle; + const std::string& entityName = world->GetName(entity); + if (!entityName.empty()) { + nodeTitle = entityName; + } else { + nodeTitle = std::string("#") + std::to_string(entity); + } + if (ImGui::TreeNode(nodeTitle.c_str())) { + if (m_UIDraggingEntity != EntityID_Invalid && ImGui::IsItemHoveredRect() && ImGui::IsMouseReleased(0)) { + LOG_DEBUG("Changed parent of %i to %i", m_UIDraggingEntity, entity); + changeParent(m_UIDraggingEntity, entity); + m_UIDraggingEntity = EntityID_Invalid; + } + + if (ImGui::BeginPopupContextItem("item context menu")) { + if (ImGui::Button("Add")) { + EntityID newEntity = world->CreateEntity(entity); + world->AttachComponent(newEntity, "Transform"); + } + ImGui::SameLine(); + if (ImGui::Button("Delete")) { + world->DeleteEntity(entity); + ImGui::CloseCurrentPopup(); + if (!world->ValidEntity(m_Selection)) { + m_Selection = EntityID_Invalid; + } + } + ImGui::EndPopup(); + } + return true; + } else { + return false; + } +} + +bool EditorSystemOld::createDeleteButton(std::string componentType) +{ + float width = ImGui::GetContentRegionAvailWidth(); + ImGuiWindow* window = ImGui::GetCurrentWindow(); + auto pos = ImGui::GetCursorScreenPos() + ImVec2(width - 14.f, 1); + ImRect bb = ImRect(pos, pos + ImVec2(14.f, 14.f)); + std::string idString = "#DELETE"; + idString += componentType; + ImGuiID id = window->GetID(idString.c_str()); + bool hovered; + bool held; + bool pressed = ImGui::ButtonBehavior(bb, id, &hovered, &held); + //ImU32 col = window->Color((held && hovered) ? ImGuiCol_CloseButtonActive : hovered ? ImGuiCol_CloseButtonHovered : ImGuiCol_CloseButton); + ImU32 col = window->Color((held && hovered) ? ImGuiCol_CloseButtonActive : hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button); + window->DrawList->AddCircleFilled(bb.GetCenter(), 7.f, col, 16); + return pressed; +} + +void EditorSystemOld::changeParent(EntityID entity, EntityID newParent) +{ + if (entity == newParent) { + return; + } + + // An entity can't be a child to one of its own children + auto children = m_World->GetEntityChildren().equal_range(entity); + for (auto it = children.first; it != children.second; it++) { + if (it->second == newParent) { + return; + } + } + + m_World->SetParent(entity, newParent); +} + +void EditorSystemOld::fileImport(World* world) +{ + m_CurrentFile = openDialog(m_DefaultEntityDir); + auto file = ResourceManager::Load(m_CurrentFile.string()); + EntityFilePreprocessor fpp(file); + fpp.RegisterComponents(world); + EntityFileParser fp(file); + fp.MergeEntities(world); + createWidget(); + updateWidget(); +} + +void EditorSystemOld::fileSave(World* world) +{ + if (boost::filesystem::exists(m_CurrentFile)) { + // HACK: Delete the widgets so they don't appear in the saved file + world->DeleteEntity(m_Widget); + m_Widget = EntityID_Invalid; + + EntityFileWriter writer(m_CurrentFile.string()); + writer.WriteWorld(world); + + createWidget(); + } else { + fileSaveAs(world); + } +} + +void EditorSystemOld::fileSaveAs(World* world) +{ + auto filePath = saveDialog(m_DefaultEntityDir); + if (filePath.empty()) { + return; + } + + // HACK: Delete the widgets so they don't appear in the saved file + world->DeleteEntity(m_Widget); + m_Widget = EntityID_Invalid; + + EntityFileWriter writer(filePath.string()); + writer.WriteWorld(world); + + createWidget(); +} diff --git a/src/Engine/Editor/EditorUI.cpp b/src/Engine/Editor/EditorUI.cpp new file mode 100644 index 00000000..e69de29b diff --git a/src/Engine/Rendering/DrawFinalPass.cpp b/src/Engine/Rendering/DrawFinalPass.cpp index b0ca4afe..45dafa71 100644 --- a/src/Engine/Rendering/DrawFinalPass.cpp +++ b/src/Engine/Rendering/DrawFinalPass.cpp @@ -27,6 +27,7 @@ void DrawFinalPass::Draw(RenderScene& scene) GLERROR("DrawFinalPass::Draw: Pre"); DrawFinalPassState state; + m_ForwardPlusProgram->Bind(); GLuint shaderHandle = m_ForwardPlusProgram->GetHandle(); @@ -34,8 +35,8 @@ void DrawFinalPass::Draw(RenderScene& scene) glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, m_LightCullingPass->LightGridSSBO()); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 4, m_LightCullingPass->LightIndexSSBO()); - glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "V"), 1, GL_FALSE, glm::value_ptr(m_Renderer->Camera()->ViewMatrix())); - glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "P"), 1, GL_FALSE, glm::value_ptr(m_Renderer->Camera()->ProjectionMatrix())); + glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "V"), 1, GL_FALSE, glm::value_ptr(scene.Camera->ViewMatrix())); + glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "P"), 1, GL_FALSE, glm::value_ptr(scene.Camera->ProjectionMatrix())); glUniform2f(glGetUniformLocation(shaderHandle, "ScreenDimensions"), m_Renderer->Resolution().Width, m_Renderer->Resolution().Height); //TODO: Render: Add code for more jobs than modeljobs. diff --git a/src/Engine/Rendering/DrawFinalPassState.cpp b/src/Engine/Rendering/DrawFinalPassState.cpp index 1cb9d7da..2cda4069 100644 --- a/src/Engine/Rendering/DrawFinalPassState.cpp +++ b/src/Engine/Rendering/DrawFinalPassState.cpp @@ -9,7 +9,6 @@ DrawFinalPassState::DrawFinalPassState() Enable(GL_DEPTH_TEST); Enable(GL_CULL_FACE); ClearColor(glm::vec4(200.f / 255, 0.f / 255, 200.f / 255, 0.f)); - Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); } DrawFinalPassState::~DrawFinalPassState() diff --git a/src/Engine/Rendering/RenderState.cpp b/src/Engine/Rendering/RenderState.cpp index 4c47a8a2..1da2f0b1 100644 --- a/src/Engine/Rendering/RenderState.cpp +++ b/src/Engine/Rendering/RenderState.cpp @@ -44,12 +44,6 @@ bool RenderState::ClearColor(glm::vec4 color) return !GLERROR("RenderState::ClearColor"); } -bool RenderState::Clear(GLbitfield mask) -{ - glClear(mask); - return !GLERROR("RenderState::Clear"); -} - bool RenderState::BindFramebuffer(GLint framebuffer) { GLint originalRead; @@ -91,6 +85,15 @@ bool RenderState::BlendFunc(GLenum sfactor, GLenum dfactor) return !GLERROR("RenderState::BlendFunc"); } +bool RenderState::DepthMask(GLboolean flag) +{ + GLboolean original; + glGetBooleanv(GL_DEPTH_WRITEMASK, &original); + m_ResetFunctions.push_back(std::bind(glDepthMask, original)); + glDepthMask(flag); + return !GLERROR("RenderState::DepthMask"); +} + RenderState::~RenderState() { for (auto& f : m_ResetFunctions) { diff --git a/src/Engine/Rendering/RenderSystem.cpp b/src/Engine/Rendering/RenderSystem.cpp index 38b80ea2..2af782e3 100644 --- a/src/Engine/Rendering/RenderSystem.cpp +++ b/src/Engine/Rendering/RenderSystem.cpp @@ -9,71 +9,26 @@ RenderSystem::RenderSystem(EventBroker* eventBroker, const IRenderer* renderer, EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &RenderSystem::OnInputCommand); m_Camera = new Camera((float)m_Renderer->Resolution().Width / m_Renderer->Resolution().Height, glm::radians(45.f), 0.01f, 5000.f); - m_DebugCameraInputController = new DebugCameraInputController(eventBroker, -1); } RenderSystem::~RenderSystem() { delete m_Camera; - delete m_DebugCameraInputController; } -bool RenderSystem::OnSetCamera(const Events::SetCamera &event) +bool RenderSystem::OnSetCamera(Events::SetCamera& e) { - auto cameras = m_World->GetComponents("Camera"); - - if (cameras != nullptr) { - for (auto it = cameras->begin(); it != cameras->end(); it++) { - if ((std::string)(*it)["Name"] == event.Name) { - switchCamera((*it).EntityID); - } - } - } + ComponentWrapper cTransform = e.CameraEntity["Transform"]; + ComponentWrapper cCamera = e.CameraEntity["Camera"]; + m_Camera->SetFOV((double)cCamera["FOV"]); + m_Camera->SetNearClip((double)cCamera["NearClip"]); + m_Camera->SetFarClip((double)cCamera["FarClip"]); + m_Camera->SetPosition(cTransform["Position"]); + m_Camera->SetOrientation(glm::quat((const glm::vec3&)cTransform["Orientation"])); + m_CurrentCamera = e.CameraEntity; return true; } -void RenderSystem::switchCamera(EntityID entity) -{ - if(m_World->HasComponent(entity, "Camera")) { - - if (m_CurrentCamera != EntityID_Invalid) { - if (m_World->HasComponent(m_CurrentCamera, "Model")) { - m_World->GetComponent(m_CurrentCamera, "Model")["Visible"] = true; - } - if (m_World->HasComponent(m_CurrentCamera, "Listener")) { - m_World->DeleteComponent(m_CurrentCamera, "Listener"); - } - } - - if (m_World->HasComponent(entity, "Model")) { - m_World->GetComponent(entity, "Model")["Visible"] = false; - } - if (!m_World->HasComponent(entity, "Listener")) { - m_World->AttachComponent(entity, "Listener"); - } - m_CurrentCamera = entity; - m_SwitchCamera = false; - - } else { - LOG_ERROR("Entity %i does not have a CameraComponent", entity); - m_SwitchCamera = false; - } -} - -void RenderSystem::updateProjectionMatrix(ComponentWrapper& cameraComponent) -{ - double fov = cameraComponent["FOV"]; - double aspectRatio = (float)m_Renderer->Resolution().Width / m_Renderer->Resolution().Height; - double nearClip = cameraComponent["NearClip"]; - double farClip = cameraComponent["FarClip"]; - - m_Camera->SetFOV(glm::radians(fov)); - m_Camera->SetAspectRatio(aspectRatio); - m_Camera->SetNearClip(nearClip); - m_Camera->SetFarClip(farClip); - m_Camera->UpdateProjectionMatrix(); -} - void RenderSystem::fillModels(std::list>& jobs, World* world) { auto models = world->GetComponents("Model"); @@ -113,7 +68,6 @@ void RenderSystem::fillModels(std::list>& jobs, World } } - void RenderSystem::fillLight(std::list>& jobs, World* world) { auto pointLights = world->GetComponents("PointLight"); @@ -138,12 +92,7 @@ void RenderSystem::fillLight(std::list>& jobs, World* bool RenderSystem::OnInputCommand(const Events::InputCommand& e) { - if (e.Command == "SwitchCamera" && e.Value > 0) { - m_SwitchCamera = true; - return true; - } else { - return false; - } + return false; } void RenderSystem::Update(World* world, double dt) @@ -151,10 +100,12 @@ void RenderSystem::Update(World* world, double dt) m_World = world; m_EventBroker->Process(); - updateCamera(world, dt); - + if (m_CurrentCamera) { + ComponentWrapper cameraTransform = m_CurrentCamera["Transform"]; + m_Camera->SetPosition(cameraTransform["Position"]); + m_Camera->SetOrientation(glm::quat((const glm::vec3&)cameraTransform["Orientation"])); + } //Only supports opaque geometry atm - m_RenderFrame->Clear(); RenderScene scene; scene.Camera = m_Camera; @@ -163,70 +114,4 @@ void RenderSystem::Update(World* world, double dt) fillLight(scene.PointLightJobs, world); m_RenderFrame->Add(scene); -} - -void RenderSystem::updateCamera(World* world, double dt) -{ - if (m_SwitchCamera) { - auto cameras = world->GetComponents("Camera"); - if (cameras == nullptr) { - return; - } - for (auto it = cameras->begin(); it != cameras->end(); it++) { - if ((*it).EntityID == m_CurrentCamera) { - it++; - if (it != cameras->end()) { - switchCamera((*it).EntityID); - } else { - switchCamera((*cameras->begin()).EntityID); - } - break; - } - } - if (m_World->HasComponent(m_CurrentCamera, "Camera")) { - ComponentWrapper& cameraComponent = world->GetComponent(m_CurrentCamera, "Camera"); - ComponentWrapper& cameraTransform = world->GetComponent(m_CurrentCamera, "Transform"); - - m_DebugCameraInputController->SetOrientation(glm::quat((glm::vec3)cameraTransform["Orientation"])); - m_DebugCameraInputController->SetPosition(cameraTransform["Position"]); - } - } - - if (m_World->ValidEntity(m_CurrentCamera)) { - if (world->HasComponent(m_CurrentCamera, "Camera") && world->HasComponent(m_CurrentCamera, "Transform")) { - ComponentWrapper& cameraComponent = world->GetComponent(m_CurrentCamera, "Camera"); - ComponentWrapper& cameraTransform = world->GetComponent(m_CurrentCamera, "Transform"); - - m_DebugCameraInputController->Update(dt); - (glm::vec3&)cameraTransform["Orientation"] = glm::eulerAngles(m_DebugCameraInputController->Orientation()); - (glm::vec3&)cameraTransform["Position"] = m_DebugCameraInputController->Position(); - - glm::vec3 position = Transform::AbsolutePosition(world, m_CurrentCamera); - glm::quat orientation = Transform::AbsoluteOrientation(world, m_CurrentCamera); - - m_Camera->SetPosition(position); - m_Camera->SetOrientation(orientation); - - updateProjectionMatrix(cameraComponent); - - } - } else { - m_Camera = m_Camera; - - auto cameras = world->GetComponents("Camera"); - if (cameras != nullptr) { - if (cameras->begin() != cameras->end()) { - ComponentWrapper& cameraC = *cameras->begin(); - switchCamera(cameraC.EntityID); - - ComponentWrapper& cameraComponent = world->GetComponent(m_CurrentCamera, "Camera"); - ComponentWrapper& cameraTransform = world->GetComponent(m_CurrentCamera, "Transform"); - - m_DebugCameraInputController->SetOrientation(glm::quat((glm::vec3)cameraTransform["Orientation"])); - m_DebugCameraInputController->SetPosition(cameraTransform["Position"]); - } - } - } - - m_Camera->UpdateViewMatrix(); } \ No newline at end of file diff --git a/src/Engine/Rendering/Renderer.cpp b/src/Engine/Rendering/Renderer.cpp index 2f4c0ba2..e8ed1d9a 100644 --- a/src/Engine/Rendering/Renderer.cpp +++ b/src/Engine/Rendering/Renderer.cpp @@ -15,15 +15,6 @@ void Renderer::Initialize() m_UnitSphere = ResourceManager::Load("Models/Core/UnitSphere.obj"); m_ImGuiRenderPass = new ImGuiRenderPass(this, m_EventBroker); - - - // Create default camera - m_DefaultCamera = new ::Camera((float)m_Resolution.Width / m_Resolution.Height, glm::radians(45.f), 0.01f, 5000.f); - m_DefaultCamera->SetPosition(glm::vec3(0, 0, 10)); - if (m_Camera == nullptr) { - m_Camera = m_DefaultCamera; - } - } void Renderer::InitializeWindow() @@ -92,14 +83,14 @@ void Renderer::Update(double dt) void Renderer::Draw(RenderFrame& frame) { glClearColor(255.f / 255, 163.f / 255, 176.f / 255, 0.f); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - m_PickingPass->ClearPicking(); for (auto scene : frame.RenderScenes){ + if (scene->ClearDepth) { + glClear(GL_DEPTH_BUFFER_BIT); + } - m_Camera = scene->Camera; // remove renderer camera when Editor uses the render scene cameras. FillDepth(*scene); m_PickingPass->Draw(*scene); m_LightCullingPass->GenerateNewFrustum(*scene); diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index ff1e28fd..5be95802 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -77,7 +77,6 @@ Game::Game(int argc, char* argv[]) unsigned int updateOrderLevel = 0; m_SystemPipeline->AddSystem(updateOrderLevel); m_SystemPipeline->AddSystem(updateOrderLevel); - m_SystemPipeline->AddSystem(updateOrderLevel, m_Renderer); m_SystemPipeline->AddSystem(updateOrderLevel); m_SystemPipeline->AddSystem(updateOrderLevel); m_SystemPipeline->AddSystem(updateOrderLevel); @@ -91,6 +90,8 @@ Game::Game(int argc, char* argv[]) m_SystemPipeline->AddSystem(updateOrderLevel, m_OctreeCollision); ++updateOrderLevel; m_SystemPipeline->AddSystem(updateOrderLevel, m_Renderer, m_RenderFrame); + ++updateOrderLevel; + m_SystemPipeline->AddSystem(updateOrderLevel, m_Renderer, m_RenderFrame); // Invoke network if (m_Config->Get("Networking.StartNetwork", false)) { @@ -149,6 +150,7 @@ void Game::Tick() m_SoundSystem->Update(dt); GLERROR("Game::Tick m_RenderQueueFactory->Update"); m_Renderer->Draw(*m_RenderFrame); + m_RenderFrame->Clear(); GLERROR("Game::Tick m_Renderer->Draw"); m_EventBroker->Swap(); m_EventBroker->Clear();