From f37175a01e5853f36b7735a3e6ee9c51fccd9480 Mon Sep 17 00:00:00 2001 From: Nigel Barink Date: Thu, 3 Nov 2022 20:33:14 +0100 Subject: [PATCH] Additions to editor Entities can be selected using the scene-explorer, Components can be viewed and edited through the inspector , empty Entities can be added through the mainmenu bar --- BarinkEngine/src/Graphics/Renderer.cpp | 15 ++- BarinkEngine/src/Scene/Components.h | 11 +- BarinkEngine/src/Scene/Entity.h | 15 ++- BarinkEngine/src/Scene/Scene.cpp | 3 +- Editor/src/main.cpp | 65 +++++++----- Editor/src/widgets.cpp | 135 +++++++++++++++++++++++++ Editor/src/widgets.h | 23 +++++ Editor/src/widgets/widgets.h | 79 --------------- 8 files changed, 234 insertions(+), 112 deletions(-) create mode 100644 Editor/src/widgets.cpp create mode 100644 Editor/src/widgets.h delete mode 100644 Editor/src/widgets/widgets.h diff --git a/BarinkEngine/src/Graphics/Renderer.cpp b/BarinkEngine/src/Graphics/Renderer.cpp index d520809..89d7bb6 100644 --- a/BarinkEngine/src/Graphics/Renderer.cpp +++ b/BarinkEngine/src/Graphics/Renderer.cpp @@ -3,7 +3,7 @@ #include "../Graphics/Memory/VertexArray.h" #include "../Graphics/Memory/Buffer.h" #include - +#include float Angle = 0.0; Camera cam = Camera(glm::vec3(12.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), 90.0f); glm::mat4 projection = glm::perspective(glm::radians(cam.Zoom), (800.0f / 600.0f), 0.001f, 100.0f); @@ -63,8 +63,17 @@ void BarinkEngine::Renderer::Render(Scene& scene) renderComponent.shader.setUniformFloat("lighting.strength", light.Strength); }); + + glm::mat4 rotation = glm::rotate(glm::mat4(1.0f), trans.Rotation.x, glm::vec3(1.0f, 0.0f, 0.0f)); + rotation *= glm::rotate(glm::mat4(1.0f), trans.Rotation.y, glm::vec3(0.0f, 1.0f, 0.0f)); + rotation *= glm::rotate(glm::mat4(1.0f), trans.Rotation.z, glm::vec3(0.0f, 0.0f, 1.0f)); + + + glm::mat4 modelMatrix = glm::translate(glm::mat4(1.0f), trans.Position) * glm::scale(glm::mat4(1.0f), trans.Scale) *rotation; + + renderComponent.shader.setUniformVec3("Color", renderComponent.color); - renderComponent.shader.setUniformMat4("M", trans.transform); + renderComponent.shader.setUniformMat4("M", modelMatrix); renderComponent.shader.setUniformMat4("V", cam.GetViewMatrix()); renderComponent.shader.setUniformMat4("P", projection); @@ -82,7 +91,7 @@ void BarinkEngine::Renderer::Render(Framebuffer& framebuffer, Scene& scene) glBindFramebuffer(GL_FRAMEBUFFER, framebuffer.GetId()); - glClearColor(.5f, .0f, .5f, 1.0f); + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); Render(scene); diff --git a/BarinkEngine/src/Scene/Components.h b/BarinkEngine/src/Scene/Components.h index dc61317..6f8da69 100644 --- a/BarinkEngine/src/Scene/Components.h +++ b/BarinkEngine/src/Scene/Components.h @@ -8,14 +8,14 @@ namespace BarinkEngine { }; struct TransformComponent { - glm::mat4 transform = glm::mat4(1.0f); - + glm::vec3 Position = glm::vec3(0.0f); + glm::vec3 Rotation = glm::vec3(0.0f); + glm::vec3 Scale = glm::vec3(1.0f); }; struct LightComponent { float Strength = 1.0f; glm::vec3 Color = glm::vec3(1.0f, 1.0f, 1.0f); - }; @@ -24,6 +24,11 @@ namespace BarinkEngine { }; + + struct ScriptComponent { + std::string file; // TODO : replace with proper properties + }; + struct Render3DComponent { unsigned int VAO = 0; unsigned int IBO = 0; diff --git a/BarinkEngine/src/Scene/Entity.h b/BarinkEngine/src/Scene/Entity.h index 4b34657..82e4ccc 100644 --- a/BarinkEngine/src/Scene/Entity.h +++ b/BarinkEngine/src/Scene/Entity.h @@ -2,13 +2,13 @@ #include class Scene; + class Entity { public: Entity() = default; Entity(entt::entity e, Scene* scene); Entity(const Entity& other) = default; - template T& AddComponent() { return m_scene->m_registry.emplace(m_entity); @@ -19,6 +19,19 @@ public: return m_scene->m_registry.get(m_entity); } + + template + bool HasComponent() { + return m_scene->getReg().all_of(m_entity); + } + + + + // NOTE: Not Scene context aware!! + bool operator== (Entity& other) { + return m_entity == other.m_entity; + } + private: entt::entity m_entity; Scene* m_scene; diff --git a/BarinkEngine/src/Scene/Scene.cpp b/BarinkEngine/src/Scene/Scene.cpp index 80d57c0..43f4e2e 100644 --- a/BarinkEngine/src/Scene/Scene.cpp +++ b/BarinkEngine/src/Scene/Scene.cpp @@ -3,7 +3,7 @@ #include "Components.h" Scene::Scene() { - m_registry = entt::basic_registry(); + //m_registry = entt::basic_registry(); } Scene::~Scene() @@ -21,3 +21,4 @@ Entity Scene::AddEntity(std::string name) return entity; } + diff --git a/Editor/src/main.cpp b/Editor/src/main.cpp index c652765..2893fe2 100644 --- a/Editor/src/main.cpp +++ b/Editor/src/main.cpp @@ -1,14 +1,18 @@ +#include +#include + +#include + +#include "stb_image.h" +#include "../../libs/guizmo/ImGuizmo.h" + #include "../../BarinkEngine/src/BarinkEngine.h" #include "../../BarinkEngine/src/AssetManager/ModelImporter.h" #include "../../BarinkEngine/src/Graphics/Memory/Framebuffer.h" -#include #include "../../BarinkEngine/src/PerfCounter.cpp" #include "../../BarinkEngine/src/Scene/Entity.h" -#include "stb_image.h" -#include "../../libs/guizmo/ImGuizmo.h" -#include "../../libs/glm/glm/gtc/type_ptr.hpp" -#include -#include "widgets/widgets.h" +#include "Widgets.h" + /* * Define globals */ @@ -18,18 +22,17 @@ Scene Level1; BarinkEngine::SceneObject* Model; Entity cube; +entt::entity Selected; /* * Runs once at startup * - USe to initialize the game/sandbox/demo */ void Start() { - - auto io = ImGui::GetIO(); io.Fonts->AddFontFromFileTTF("build/Debug/Fonts/Roboto-Regular.ttf", 18); + framebuffer = new Framebuffer(); - // Build a basic test scene - // NOTE: This will later be done through an editor + // Create a level and load it as the current level auto importer = BarinkEngine::ModelImporter(); @@ -37,22 +40,27 @@ void Start() { // Create a cube Model = importer.Import("build/Debug/Models/Cube.obj"); cube = Level1.AddEntity("cube"); + auto& render3DComponent = cube.AddComponent(); render3DComponent.mesh = *(Model->renderable->mesh); - cube.GetComponent().transform = glm::translate(glm::mat4(1.0f), glm::vec3(1.0f, 0.0f, 5.0f)); - renderer.Prepare(Level1); + cube.GetComponent().Position = glm::vec3(1.0f, 0.0f, 5.0f); + auto cube2 = Level1.AddEntity("Cube1"); + auto& rendercube2 = cube2.AddComponent(); + rendercube2.mesh = *(Model->renderable->mesh); + // create an ambient light source auto AmbientLight = Level1.AddEntity("AmbientLight"); - AmbientLight.AddComponent(); + auto light = AmbientLight.AddComponent(); + light.Color = glm::vec3(1.0f); + light.Strength = 1.0f; - std::cout << "Colour attachment id; " << framebuffer->GetColourAttachment() << std::endl; + Selected = (entt::entity) -1; renderer.Prepare(Level1); - } /* * Runs every frame @@ -77,21 +85,30 @@ void ImmediateGraphicsDraw() ImGui::EndMenu(); } + if (ImGui::BeginMenu("Scene")) { + + + if (ImGui::MenuItem("Add Entity")) { + Level1.AddEntity("New entity"); + } + + + ImGui::EndMenu(); + } + + ImGui::EndMainMenuBar(); - - // Show internal BarinkEngine stats //ShowStats(); - Viewport(*framebuffer , Level1); - Inspector(); - SceneExplorer(Level1); + Viewport(*framebuffer, Level1); + SceneExplorer(Selected, Level1); + Inspector(Selected, Level1 ); + Settings(); AssetsFinder(); Console(); - - - + ImGui::ShowDemoWindow(); ImGui::ShowMetricsWindow(); @@ -102,14 +119,12 @@ void Render() renderer.Render( *framebuffer, Level1); } - /* * Runs every frame * - Meant for game logic ( non-physics related) */ void Update() { - } /* diff --git a/Editor/src/widgets.cpp b/Editor/src/widgets.cpp new file mode 100644 index 0000000..c517b2f --- /dev/null +++ b/Editor/src/widgets.cpp @@ -0,0 +1,135 @@ +#include "widgets.h" +#include +#include "../../BarinkEngine/src/Scene/Components.h" +#include "../../BarinkEngine/src/Scene/Entity.h" +class Editor; + +void ComponentView(const std::string& componentName, voidFunction func) +{ + ImGuiWindowFlags_ window_flags = ImGuiWindowFlags_None; + ImGui::PushStyleVar(ImGuiStyleVar_ChildRounding, 5.0f); + ImGui::BeginChild(componentName.c_str()); + + func(); + + ImGui::EndChild(); + ImGui::PopStyleVar(); +} + +void Inspector(entt::entity ent , Scene& scene) { + ImGui::Begin("Inspector"); + static float Zoom = 90; + static glm::vec3 Position = glm::vec3(0.0f, 0.0f, 0.0f); + static glm::vec3 Rotation = glm::vec3(0.0f, 0.0f, 0.0f); + if (scene.getReg().valid(ent)) { + + Entity entity = Entity(ent, &scene); + + auto component = entity.GetComponent(); + ImGui::LabelText("## Name:", component.name.c_str() ); + + if (entity.HasComponent()) { + auto& transform = entity.GetComponent(); + ImGui::DragFloat3("Position", glm::value_ptr(transform.Position) , 0.01); + ImGui::DragFloat3("Rotation", glm::value_ptr(transform.Rotation), 0.01); + ImGui::DragFloat3("Scale", glm::value_ptr(transform.Scale), 0.01, 0); + + } + + if (entity.HasComponent()) { + auto& light = entity.GetComponent(); + ImGui::DragFloat("Strength", &light.Strength, 0.001f); + ImGui::ColorEdit3("Colour", glm::value_ptr(light.Color)); + + } + + if (entity.HasComponent ()) { + auto& camera = entity.GetComponent(); + ComponentView("Camera", [] { + ImGui::SliderFloat("Zoom", &Zoom, 10, 190); + ImGui::InputFloat3("Position:", &Position[0]); + ImGui::InputFloat3("Rotation:", &Rotation[0]); + }); + } + + if (entity.HasComponent()) { + ComponentView("Scripting", [] { + ImGui::LabelText("##--", "Hello scripting"); + }); + } + + } + + + ImGui::End(); + +} + +void SceneExplorer(entt::entity& selected, Scene& scene ) +{ + ImGui::Begin("Scene Explorer"); + + scene.getReg().each([&](entt::entity enttNumber) { + Entity entity = Entity(enttNumber, &scene); + auto id = entity.GetComponent(); + + if (ImGui::Selectable(id.name.c_str(), enttNumber == selected )) { + selected = enttNumber; + } + + }); + + ImGui::End(); +} + +void Viewport(Framebuffer& framebuffer, Scene& scene) { + + unsigned int viewportWindowFlags = ImGuiWindowFlags_NoTitleBar + | ImGuiWindowFlags_NoDecoration + | ImGuiWindowFlags_NoScrollbar + | ImGuiWindowFlags_NoMove + | ImGuiWindowFlags_NoCollapse; + + ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2{ 0,0 }); + ImGui::Begin("Viewport", false, viewportWindowFlags); + ImGui::Image((void*)(intptr_t)framebuffer.GetColourAttachment(), ImVec2{ (float)800,(float)600 }); + + ImGuizmo::SetDrawlist(); + ImGuizmo::SetRect(ImGui::GetWindowPos().x, ImGui::GetWindowPos().y, ImGui::GetWindowWidth(), ImGui::GetWindowHeight()); + ImGuizmo::Enable(true); + auto cam = glm::mat4(1.0f); + auto eye = glm::vec3(0.0f); + auto center = glm::vec3(0.0f); + auto up = glm::vec3(0.0f, 1.0f, 0.0f); + auto view = glm::lookAt(eye, center, up); + + glm::mat4 projection = glm::perspective(glm::radians(90.0f), (800.0f / 600.0f), 0.001f, 100.0f); + auto transformMatrix = glm::mat4(1.0f); + + ImGuizmo::Manipulate(glm::value_ptr(view), glm::value_ptr(projection), ImGuizmo::TRANSLATE, ImGuizmo::WORLD, glm::value_ptr(transformMatrix)); + + + //ImGuizmo::Manipulate(glm::value_ptr(static_cam), glm::value_ptr(static_projection), ImGuizmo::TRANSLATE, ImGuizmo::WORLD, glm::value_ptr(trans)); + ImGuizmo::ViewManipulate(glm::value_ptr(cam), 8.0f, ImVec2{ 0.0f,0.0f }, ImVec2{ 128.0f,128.0f }, 0x10101010); + ImGui::End(); + ImGui::PopStyleVar(); + +} + +void Settings() { + ImGui::Begin("Settings"); + ImGui::LabelText("##title-settings", "Fine grain control over your engine... "); + ImGui::End(); +} + +void Console() { + ImGui::Begin("Console", false); + ImGui::Dummy(ImVec2{ 128, 128 }); + ImGui::End(); +} + +void AssetsFinder() { + ImGui::Begin("Asset-Finder", false); + ImGui::Dummy(ImVec2{ 128, 128 }); + ImGui::End(); +} \ No newline at end of file diff --git a/Editor/src/widgets.h b/Editor/src/widgets.h new file mode 100644 index 0000000..a12b0bc --- /dev/null +++ b/Editor/src/widgets.h @@ -0,0 +1,23 @@ +#pragma once +#include +#include +#include +#include "../../libs/guizmo/ImGuizmo.h" +#include "../../BarinkEngine/src/BarinkEngine.h" +#include +#include +typedef void ( *voidFunction ) (void); + +void ComponentView(const std::string& componentName, voidFunction func); + +void Inspector(entt::entity entity, Scene& scene); + +void SceneExplorer(entt::entity& selected, Scene& scene); + +void Viewport(Framebuffer& framebuffer, Scene& scene); + +void Settings(); + +void Console(); + +void AssetsFinder(); \ No newline at end of file diff --git a/Editor/src/widgets/widgets.h b/Editor/src/widgets/widgets.h deleted file mode 100644 index 2156948..0000000 --- a/Editor/src/widgets/widgets.h +++ /dev/null @@ -1,79 +0,0 @@ -#pragma once -#include -#include - -inline void Inspector () { - ImGui::Begin("Inspector"); - static float Zoom = 90; - static glm::vec3 Position = glm::vec3(0.0f, 0.0f, 0.0f); - static glm::vec3 Rotation = glm::vec3(0.0f, 0.0f, 0.0f); - - ImGui::BeginChild("Camera"); - - ImGui::SliderFloat("Zoom", &Zoom, 10, 190); - ImGui::InputFloat3("Position:", &Position[0]); - - ImGui::InputFloat3("Rotation:", &Rotation[0]); - ImGui::EndChild(); - - - ImGui::BeginChild("Scripting"); - ImGui::Text("Hello world!"); - ImGui::EndChild(); - ImGui::End(); -} - -inline void SceneExplorer(Scene& scene) -{ - - ImGui::Begin("Scene Explorer"); - - scene.getReg().each([&](auto entity) { - auto id = scene.getReg().get(entity); - ImGui::LabelText("##myObject", "%s", id.name.c_str()); - }); - - - ImGui::End(); -} - -inline void Viewport(Framebuffer& framebuffer , Scene& scene ) { - - unsigned int viewportWindowFlags = ImGuiWindowFlags_NoTitleBar - | ImGuiWindowFlags_NoDecoration - | ImGuiWindowFlags_NoScrollbar - | ImGuiWindowFlags_NoMove - | ImGuiWindowFlags_NoCollapse; - - ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2{ 0,0 }); - ImGui::Begin("Viewport", false, viewportWindowFlags ); - ImGui::Image((void*)(intptr_t)framebuffer.GetColourAttachment(), ImVec2{ (float)800,(float)600 }); - - ImGuizmo::SetDrawlist(); - ImGuizmo::SetRect(ImGui::GetWindowPos().x, ImGui::GetWindowPos().y, ImGui::GetWindowWidth(), ImGui::GetWindowHeight()); - ImGuizmo::Enable(true); - auto cam = glm::mat4(1.0f); - //ImGuizmo::Manipulate(glm::value_ptr(static_cam), glm::value_ptr(static_projection), ImGuizmo::TRANSLATE, ImGuizmo::WORLD, glm::value_ptr(trans)); - ImGuizmo::ViewManipulate(glm::value_ptr(cam), 8.0f, ImVec2{ 0.0f,0.0f }, ImVec2{ 128.0f,128.0f }, 0x10101010); - ImGui::End(); - ImGui::PopStyleVar(); - -} - -inline void Settings() { - ImGui::Begin("Settings"); - ImGui::LabelText("##title-settings", "Fine grain control over your engine... "); - ImGui::End(); -} - -inline void Console() { - ImGui::Begin("Console", false ); - ImGui::Dummy(ImVec2{ 128, 128 }); - ImGui::End(); -} - -inline void AssetsFinder() { - ImGui::Begin("Asset-Finder", false ); - ImGui::Dummy(ImVec2{ 128, 128 }); - ImGui::End(); -} \ No newline at end of file