From f0984b611786cfeb23422d7c4a7cbf1a626ee319 Mon Sep 17 00:00:00 2001 From: nigelbarink Date: Sun, 23 Oct 2022 17:33:49 +0200 Subject: [PATCH] Adding a really basic ambient light component --- BarinkEngine/src/GUI/GUIManager.cpp | 2 - BarinkEngine/src/Graphics/Renderer.cpp | 10 +++-- BarinkEngine/src/PerfCounter.h | 2 +- BarinkEngine/src/Scene/Components.h | 7 +++- BarinkEngine/src/Scene/Scene.cpp | 5 ++- BarinkEngine/src/Scene/Scene.h | 2 +- SandboxApp/src/Sandbox.cpp | 58 ++++++++++++++++++++++---- 7 files changed, 67 insertions(+), 19 deletions(-) diff --git a/BarinkEngine/src/GUI/GUIManager.cpp b/BarinkEngine/src/GUI/GUIManager.cpp index 956ac54..5308d66 100644 --- a/BarinkEngine/src/GUI/GUIManager.cpp +++ b/BarinkEngine/src/GUI/GUIManager.cpp @@ -35,10 +35,8 @@ void GUIManager::Render() ImGui::NewFrame(); - ImGui::Begin("##App"); ImmediateGraphicsDraw(); - ImGui::End(); ImGui::Render(); ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); diff --git a/BarinkEngine/src/Graphics/Renderer.cpp b/BarinkEngine/src/Graphics/Renderer.cpp index 3ac0f54..b50dfd8 100644 --- a/BarinkEngine/src/Graphics/Renderer.cpp +++ b/BarinkEngine/src/Graphics/Renderer.cpp @@ -48,16 +48,20 @@ void BarinkEngine::Renderer::Prepare(Scene& scene ) { void BarinkEngine::Renderer::Render(Scene& scene) { auto group = scene.getReg().view(); - group.each([](auto entity , TransformComponent& trans, Render3DComponent& renderComponent) + group.each([&](auto entity , TransformComponent& trans, Render3DComponent& renderComponent) { - glBindVertexArray(renderComponent.VAO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, renderComponent.IBO); renderComponent.shader.Use(); - renderComponent.shader.setUniformVec3("Color", renderComponent.color); + auto lights = scene.getReg().view(); + lights.each([&](auto entity, LightComponent& light) { + renderComponent.shader.setUniformVec3("lighting.color", light.Color); + renderComponent.shader.setUniformFloat("lighting.strength", light.Strength); + }); + renderComponent.shader.setUniformVec3("Color", renderComponent.color); renderComponent.shader.setUniformMat4("M", trans.transform); renderComponent.shader.setUniformMat4("V", cam.GetViewMatrix()); renderComponent.shader.setUniformMat4("P", projection); diff --git a/BarinkEngine/src/PerfCounter.h b/BarinkEngine/src/PerfCounter.h index e8cca8b..40d670b 100644 --- a/BarinkEngine/src/PerfCounter.h +++ b/BarinkEngine/src/PerfCounter.h @@ -39,7 +39,7 @@ inline void SamplePerformance(void) { inline void ShowStats() { - ImGui::Begin("Statistics", false, ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove); + ImGui::Begin("Statistics", false, ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize ); ImGui::Text("FPS: %i", ES.FPS); ImGui::Text("Frame Time: %f", ES.frameTime); diff --git a/BarinkEngine/src/Scene/Components.h b/BarinkEngine/src/Scene/Components.h index f098382..dc61317 100644 --- a/BarinkEngine/src/Scene/Components.h +++ b/BarinkEngine/src/Scene/Components.h @@ -12,6 +12,12 @@ namespace BarinkEngine { }; + struct LightComponent { + float Strength = 1.0f; + glm::vec3 Color = glm::vec3(1.0f, 1.0f, 1.0f); + + }; + struct CameraComponent { glm::mat4 view; @@ -22,7 +28,6 @@ namespace BarinkEngine { unsigned int VAO = 0; unsigned int IBO = 0; Mesh mesh; - // TODO: becomes a material glm::vec3 color; Shader shader; diff --git a/BarinkEngine/src/Scene/Scene.cpp b/BarinkEngine/src/Scene/Scene.cpp index 98be8b8..66832c4 100644 --- a/BarinkEngine/src/Scene/Scene.cpp +++ b/BarinkEngine/src/Scene/Scene.cpp @@ -8,11 +8,12 @@ Scene::Scene() Scene::~Scene() {} -Entity Scene::AddEntity(std::string& name) +Entity Scene::AddEntity(std::string name) { Entity entity = { m_registry.create(), this }; - entity.AddComponent(); + auto& ident = entity.AddComponent(); + ident.name = name; entity.AddComponent(); diff --git a/BarinkEngine/src/Scene/Scene.h b/BarinkEngine/src/Scene/Scene.h index b706258..dc085a3 100644 --- a/BarinkEngine/src/Scene/Scene.h +++ b/BarinkEngine/src/Scene/Scene.h @@ -9,7 +9,7 @@ public: Scene(); ~Scene(); - Entity AddEntity(std::string& name); + Entity AddEntity(std::string name); entt::registry& getReg() { return m_registry; } diff --git a/SandboxApp/src/Sandbox.cpp b/SandboxApp/src/Sandbox.cpp index f53c1f7..0c3f627 100644 --- a/SandboxApp/src/Sandbox.cpp +++ b/SandboxApp/src/Sandbox.cpp @@ -21,28 +21,36 @@ Entity cube; * - USe to initialize the game/sandbox/demo */ void Start() { - - cube = scene.AddEntity((std::string&)"cube"); - auto& render3DComponent = cube.AddComponent(); auto importer = BarinkEngine::ModelImporter(); + // Load in asset(S) object = importer.Import("build/Debug/Models/Cube.obj"); renderable = object->renderable; + + // Add Entities to the scene + cube = scene.AddEntity("cube"); + auto& render3DComponent = cube.AddComponent(); render3DComponent.mesh = *renderable->mesh; cube.GetComponent() .transform = glm::rotate(glm::mat4(1.0f), 32.0f, glm::vec3(0.5f,1.0f,0.0f)); + // Create a second cube - auto cube2 = scene.AddEntity((std::string&)"Cube2"); + auto cube2 = scene.AddEntity("Cube2"); auto& cube2Render = cube2.AddComponent(); cube2Render.mesh = *renderable->mesh; cube2Render.color = glm::vec3(0.0f, 1.0f, 0.0f); auto& cube2Trans = cube2.GetComponent(); cube2Trans.transform = glm::translate( glm::mat4(1.0f), glm::vec3(1.0f,0.0f, 5.0f)); - + // Create a light + auto AmbientLight = scene.AddEntity("AmbientLight"); + AmbientLight.AddComponent(); + + + renderer.Prepare(scene); @@ -53,6 +61,7 @@ void Start() { +bool showImGuiMetrics = false; /* * Runs every frame * - Use to draw Immediate mode graphics (Not meant for HUD's ) @@ -62,16 +71,47 @@ void ImmediateGraphicsDraw() // Show internal BarinkEngine stats ShowStats(); - ImGui::Begin("Render edit"); + ImGui::Begin("Scene view"); + auto group = scene.getReg().view(); + group.each([](auto entity, BarinkEngine::IdentifierComponent& identifier) { - auto& a = cube.GetComponent(); + ImGui::Text("%s", identifier.name.c_str()); + + }); + ImGui::End(); - ImGui::DragFloat3("Color", &a.color[0], 0.01f, 0.0f, 1.0f); + + ImGui::Begin("Settings"); + + + if (ImGui::Button("ImGui Debug")) + { + std::cout << "Click!" << std::endl; + showImGuiMetrics = true; + } + ImGui::ShowMetricsWindow(&showImGuiMetrics); + + auto& a = cube.GetComponent(); + + auto& b = cube.GetComponent(); + + ImGui::DragFloat3("Color", &a.color[0], 0.01f, 0.0f, 1.0f); + + ImGui::DragFloat3("Position", &b.transform[3][0], 0.01f, 0.0f, 16.0f); + + auto l = scene.getReg().view(); + l.each([](auto entity, BarinkEngine::LightComponent& light) { + ImGui::Text("Lighting"); + ImGui::SliderFloat("Intensity", &light.Strength, 0.0f, 1.0f); + ImGui::SliderFloat3("l-Color", &light.Color[0], 0.0f, 1.0f); + + }); + ImGui::End(); - + }