From 3c38e2a988af37e1be5f074fc18b05c85ab9f05c Mon Sep 17 00:00:00 2001 From: Nigel Barink Date: Mon, 8 May 2023 22:07:29 +0200 Subject: [PATCH] Editor Layer + Updating Camera System Started updating the camera system, Moving editor logic to an Editor layer --- Editor/src/EditorCamera.h | 32 +++ Editor/src/EditorLayer.h | 247 ++++++++++++++++++ Editor/src/Views/Viewport.cpp | 25 +- Editor/src/Views/Viewport.h | 14 +- Editor/src/app.cpp | 182 ++----------- .../src/Graphics/Primitives/Camera.cpp | 46 ---- YoggieEngine/src/Graphics/Primitives/Camera.h | 24 +- .../src/Graphics/Primitives/Texture.cpp | 4 +- YoggieEngine/src/Graphics/Renderer.cpp | 18 +- 9 files changed, 330 insertions(+), 262 deletions(-) create mode 100644 Editor/src/EditorCamera.h create mode 100644 Editor/src/EditorLayer.h delete mode 100644 YoggieEngine/src/Graphics/Primitives/Camera.cpp diff --git a/Editor/src/EditorCamera.h b/Editor/src/EditorCamera.h new file mode 100644 index 0000000..3259c45 --- /dev/null +++ b/Editor/src/EditorCamera.h @@ -0,0 +1,32 @@ +#pragma once +#include "../../YoggieEngine/src/YoggieEngine.h" + +class EditorCamera : public YoggieEngine::Camera { +public: + EditorCamera () : Camera(){ + Front = glm::vec3(0.0f, 0.0f, 1.0f); + Right = glm::vec3(-1.0f, 0.0f, 0.0f); + Up = glm::vec3(0.0f, 1.0f, 0.0f); + + projection = glm::perspective(glm::radians(65.0f), (800.0f / 600.0f), 0.001f, 100.0f); + + + view = glm::translate(glm::mat4(1.0f), Position) * glm::toMat4(glm::quat(Rotation)); + } + + void Update() { + + view = glm::translate(glm::mat4(1.0f), Position) * glm::toMat4(glm::quat(Rotation)); + } + + glm::vec3 Position; + glm::vec3 Rotation; + +private: + + + + glm::vec3 Front; + glm::vec3 Right; + glm::vec3 Up; +}; \ No newline at end of file diff --git a/Editor/src/EditorLayer.h b/Editor/src/EditorLayer.h new file mode 100644 index 0000000..1fabb0e --- /dev/null +++ b/Editor/src/EditorLayer.h @@ -0,0 +1,247 @@ +#pragma once +#include +#include +#include +#include +#include +#include "AssetManagement/SceneSerializer.h" +#include "AssetManagement/AssetRegistry.h" +#include "Views/Viewport.h" +#include "PropertyPanels/SceneExplorer.h" +#include "AssetManagement/AssetFinder.h" +#include "MainMenuBar.h" +#include "PropertyPanels/Inspector.h" +#include "Project/ProjectInfo.h" +#include "Runtime/RuntimeControls.h" +#include "AssetManagement/uuid.h" +#include "Project/Settings.h" +#include "Console.h" +#include "AssetManagement/AssetLoaders/ModelLoader.h" + +using namespace YoggieEngine; +class EditorLayer : public Layer { + +public: + EditorLayer(): + Layer(), + rc(), + sceneview(scene, Selected), + explorer(Selected, scene), + inspector (Selected) + { + } + + void OnStartup() override { + std::string path = (std::filesystem::current_path()).string(); + project.setProjectDirectory(path); + assetsView = AssetFinder(project.GetProjectDirectory()); + LoadLastOrEmptyProject(); + + AssetRegistry assetManager = AssetRegistry(); + ModelLoader modelLoader = ModelLoader(); + + std::cout << project.GetProjectDirectory() << std::endl; + + auto latern = modelLoader.LoadAsset(std::filesystem::path("build/debug/Models/Latern.gltf")); + std::cout << "Loaded mesh: " << latern.GetName() << std::endl; + + //ProjectInfo projectInfo(project); + //Settings settings = Settings(); + //Console console = Console(); + + + + Selected = YoggieEngine::Entity((entt::entity)-1, &scene); + + } + + void OnUpdate() override { + scene.Update(); + + if (sceneview.isFocused) { + UpdateSceneCamera(sceneview); + + std::cout << "Scene view in Focus!\r"; + } + + } + + + void OnUI() override { + { + MainMenuBar menuBar = MainMenuBar(); + menuBar.ApplicationMenu(project); + menuBar.SceneMenu(project, scene); + menuBar.SelectMenu(); + menuBar.WindowMenu(); + menuBar.DebugMenu(); + menuBar.Help(); + + } + + //projectInfo.Update(); + sceneview.Update(); + rc.Update(); + explorer.Update(); + //settings.Update(); + inspector.Update(); + //console.Update(); + + assetsView.Update(); + + ImGui::ShowDemoWindow(); + //ImGui::ShowMetricsWindow(); + } + + + void OnCreate() override { + std::cout << " Layer Create!" << std::endl; + + } + + void OnDestroy() override { + std::cout << " Layer Destroy!" << std::endl; + + } + + +private: + + RuntimeControls rc; + Viewport sceneview ; + SceneExplorer explorer; + Inspector inspector; + AssetFinder assetsView; + + bool SimulatePhysics = true; + YoggieEngine::Entity Selected; + Project project; + Scene scene; + + void LoadLastOrEmptyProject() { + // Check if there is a last known loaded project and + // load that one . + + // Otherwise load no project.. + // OR + // Load an empty project. + mINI::INIStructure ini; + + if (std::filesystem::exists("build\\Debug\\Editor.ini")) + { + mINI::INIFile file("build\\Debug\\Editor.ini"); + file.read(ini); + } + else + { + spdlog::debug("Could not find an `Editor.ini` file."); + } + + if (ini["editor"]["openlastproject"] == "TRUE") + { + + Project::LoadProject(ini["cache"]["project"], project); + LoadScene(ini["cache"]["scene"], scene); + + } + else + { + spdlog::debug("Starting without a project. Please create one."); + } + } + + + void UpdateSceneCamera(Viewport& sceneview) { + const float movement_speed = 0.01f; + static float lastX = 400, lastY = 300; + const float sensitivity = 0.1; + static bool firstMouse = true; + + /* + + if (MouseButtonPressed(YOGGIE_MOUSE_BUTTON_RIGHT)) { + + glfwSetInputMode((GLFWwindow*)appWindow->GetHandle(), GLFW_CURSOR, GLFW_CURSOR_HIDDEN); + auto newX = getCursorPosX(appWindow); + auto newY = getCursorPosY(appWindow); + + if (firstMouse) + { + lastX = newX; + lastY = newY; + firstMouse = false; + } + + + float xoffset = newX - lastX; + float yoffset = newY - lastY; + + lastX = newX; + lastY = newY; + + xoffset *= sensitivity; + yoffset *= sensitivity; + + sceneview.cam.Rotation.x += (xoffset / 2); + sceneview.cam.Rotation.y += (xoffset /2); + sceneview.cam.Rotation.z += yoffset; + + + if (sceneview.cam.pitch > 89.0f) + sceneview.cam.pitch = 89.0f; + if (sceneview.cam.pitch < -89.0f) + sceneview.cam.pitch = -89.0f; + + + } + else if (firstMouse == false) + { + glfwSetInputMode((GLFWwindow*)appWindow->GetHandle(), GLFW_CURSOR, GLFW_CURSOR_NORMAL); + firstMouse = true; + } + + + + */ + + EditorCamera& cam = sceneview.GetCamera(); + + + + if (keyIsPressed(YOGGIE_KEY_UP)) + cam.Rotation.x += movement_speed; + + if (keyIsPressed(YOGGIE_KEY_DOWN)) + cam.Rotation.x -= movement_speed; + + if (keyIsPressed(YOGGIE_KEY_LEFT)) + cam.Rotation.y += movement_speed; + + if (keyIsPressed(YOGGIE_KEY_RIGHT)) + cam.Rotation.y -= movement_speed; + + + cam.Update(); + + + + /* + + // Check for Camara movement input here! + if (keyIsPressed(YOGGIE_KEY_W)) + sceneview.cam.Position -= sceneview.cam.Front * movement_speed; + + if (keyIsPressed(YOGGIE_KEY_A)) + sceneview.cam.Position += sceneview.cam.Right * movement_speed; + + if (keyIsPressed(YOGGIE_KEY_S)) + sceneview.cam.Position += sceneview.cam.Front * movement_speed; + + if (keyIsPressed(YOGGIE_KEY_D)) + sceneview.cam.Position -= sceneview.cam.Right * movement_speed; + */ + + + } + +}; \ No newline at end of file diff --git a/Editor/src/Views/Viewport.cpp b/Editor/src/Views/Viewport.cpp index 6fb0d7b..220056a 100644 --- a/Editor/src/Views/Viewport.cpp +++ b/Editor/src/Views/Viewport.cpp @@ -1,32 +1,23 @@ #include "Viewport.h" -Viewport::Viewport(YoggieEngine::Scene& scene ): +Viewport::Viewport(YoggieEngine::Scene& scene, YoggieEngine::Entity& selected) : EditorWindow("SceneView"), - renderer(YoggieEngine::RendererConfig{ 1200, 700, glm::vec3(0), true }), - cam(glm::vec3(14.0f, 1.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), 90), - scene(scene) + renderer(YoggieEngine::RendererConfig{ 1200, 700, glm::vec3(0), true }) { - renderer.SetMainCamera(cam); - + CurrentScene = &scene; + this->selected = &selected; } void Viewport::Draw() { - auto group = scene.getReg().view(); + auto group = CurrentScene->getReg().view(); group.each([&](auto enity, YoggieEngine::TransformComponent& t, YoggieEngine::Render3DComponent& renderComponent) { renderer.Submit(renderComponent, t); }); + + isFocused = ImGui::IsWindowFocused(); - cam.Update(); - renderer.SetMainCamera(cam); - renderer.Render(scene); - - ImVec2 WinPos = ImGui::GetWindowPos(); - ImVec2 ContentRegionMin = ImGui::GetWindowContentRegionMin(); - ImVec2 ContentRegionMax = ImGui::GetWindowContentRegionMax(); - ImVec2 ScreenSpaceMin = { ContentRegionMin.x + WinPos.x, ContentRegionMin.y + WinPos.y }; - ImVec2 ScreenSpaceMax = { ContentRegionMax.x + WinPos.x,ContentRegionMax.y + WinPos.y }; - + renderer.Render(*CurrentScene); ImGui::Image( (void*)(intptr_t)renderer.getCurrentFrameBuffer().GetColourAttachment(), diff --git a/Editor/src/Views/Viewport.h b/Editor/src/Views/Viewport.h index e41c4fb..1679bd5 100644 --- a/Editor/src/Views/Viewport.h +++ b/Editor/src/Views/Viewport.h @@ -6,19 +6,23 @@ #include #include "../../libs/guizmo/ImGuizmo.h" +#include "../EditorCamera.h" class Viewport : public EditorWindow { public: bool isFocused = false; - YoggieEngine::Camera cam; - - Viewport(YoggieEngine::Scene& scene); - + Viewport(YoggieEngine::Scene& scene, YoggieEngine::Entity& selected); + Viewport() = default; void Draw() override; + EditorCamera& GetCamera(){ return camera; } + private: YoggieEngine::Renderer renderer; - YoggieEngine::Scene& scene; + YoggieEngine::Scene* CurrentScene; + YoggieEngine::Entity* selected; + glm::mat4 cameraDelta = glm::mat4(1.0); + EditorCamera camera; }; \ No newline at end of file diff --git a/Editor/src/app.cpp b/Editor/src/app.cpp index e5dabde..3ecb8c7 100644 --- a/Editor/src/app.cpp +++ b/Editor/src/app.cpp @@ -1,20 +1,7 @@ #include "../../YoggieEngine/src/EntryPoint.h" -#include -#include -#include -#include -#include "AssetManagement/SceneSerializer.h" -#include "AssetManagement/AssetManager.h" -#include "Views/Viewport.h" -#include "PropertyPanels/SceneExplorer.h" -#include "AssetManagement/AssetFinder.h" -#include "MainMenuBar.h" -#include "PropertyPanels/Inspector.h" -#include "Project/ProjectInfo.h" -#include "Runtime/RuntimeControls.h" -#include "AssetManagement/uuid.h" -#include "Project/Settings.h" -#include "Console.h" +#include +#include "EditorLayer.h" + using namespace YoggieEngine; @@ -24,175 +11,42 @@ public: void Run() override { - std::string path = (std::filesystem::current_path()).string(); - project.setProjectDirectory(path); - LoadLastOrEmptyProject(); - - //ProjectInfo projectInfo(project); - RuntimeControls rc = RuntimeControls(); - - Viewport sceneview = Viewport(scene); - SceneExplorer explorer(Selected, scene); - Inspector inspector = Inspector(Selected); - //Settings settings = Settings(); - AssetFinder assetsView = AssetFinder(); - //Console console = Console(); + // Create EditorLayer + EditorLayer* firstLayer = new EditorLayer(); + + firstLayer->OnStartup(); - Selected = YoggieEngine::Entity((entt::entity) -1, &scene); double previous = glfwGetTime(); double lag = 0.0; - while (!appWindow->WindowShouldClose()) - { + while (!appWindow->WindowShouldClose()) { PollEvents(); double now = glfwGetTime(); - double elapsed = now - previous ; + double elapsed = now - previous; previous = now; lag += elapsed; - - scene.Update(); - - if (sceneview.isFocused) { - UpdateSceneCamera(sceneview); - - std::cout << "Scene view in Focus!\r" ; - } - GuiBegin(); - - - { - MainMenuBar menuBar = MainMenuBar(); - - // Show a menu bar - menuBar.ApplicationMenu(project); - menuBar.SceneMenu(project, scene); - menuBar.SelectMenu(); - menuBar.WindowMenu(); - menuBar.DebugMenu(); - menuBar.Help(); - - } - - //projectInfo.Update(); - sceneview.Update(); - rc.Update(); - explorer.Update(); - //settings.Update(); - inspector.Update(); - //console.Update(); - - assetsView.Update(); - - ImGui::ShowDemoWindow(); - //ImGui::ShowMetricsWindow(); - - + firstLayer->OnUpdate(); + firstLayer->OnUI(); GuiEnd(); + SwapBuffers(); } + + firstLayer->OnDestroy(); + + } - void LoadLastOrEmptyProject() { - // Check if there is a last known loaded project and - // load that one . - - // Otherwise load no project.. - // OR - // Load an empty project. - mINI::INIStructure ini; - - if (std::filesystem::exists("build\\Debug\\Editor.ini")) - { - mINI::INIFile file("build\\Debug\\Editor.ini"); - file.read(ini); - } - else - { - spdlog::debug("Could not find an `Editor.ini` file."); - } - - if (ini["editor"]["openlastproject"] == "TRUE") - { - Project::LoadProject(ini["cache"]["project"], project); - LoadScene(ini["cache"]["scene"], scene); - - } - else - { - spdlog::debug("Starting without a project. Please create one."); - } - } private: - bool SimulatePhysics = true; - YoggieEngine::Entity Selected; - Project project; - Scene scene; - void UpdateSceneCamera(Viewport& sceneview) { - const float movement_speed = 0.1f; - static float lastX = 400, lastY = 300; - const float sensitivity = 0.1; - static bool firstMouse = true; - - if (MouseButtonPressed(YOGGIE_MOUSE_BUTTON_RIGHT)) { - - glfwSetInputMode((GLFWwindow*)appWindow->GetHandle(), GLFW_CURSOR, GLFW_CURSOR_HIDDEN); - auto newX = getCursorPosX(appWindow); - auto newY = getCursorPosY(appWindow); - - if (firstMouse) - { - lastX = newX; - lastY = newY; - firstMouse = false; - } - - - float xoffset = newX - lastX; - float yoffset = newY - lastY; - - lastX = newX; - lastY = newY; - - xoffset *= sensitivity; - yoffset *= sensitivity; - - sceneview.cam.yaw += xoffset; - sceneview.cam.pitch += yoffset; - - if (sceneview.cam.pitch > 89.0f) - sceneview.cam.pitch = 89.0f; - if (sceneview.cam.pitch < -89.0f) - sceneview.cam.pitch = -89.0f; - - } - else if (firstMouse == false) - { - glfwSetInputMode((GLFWwindow*)appWindow->GetHandle(), GLFW_CURSOR, GLFW_CURSOR_NORMAL); - firstMouse = true; - } - - // Check for Camara movement input here! - if (keyIsPressed(YOGGIE_KEY_W)) { - sceneview.cam.Position += sceneview.cam.Front * movement_speed; - std::cout << "Pressed W !" << std::endl; - } - - if (keyIsPressed(YOGGIE_KEY_A)) - sceneview.cam.Position -= sceneview.cam.Right * movement_speed; - - if (keyIsPressed(YOGGIE_KEY_S)) - sceneview.cam.Position -= sceneview.cam.Front * movement_speed; - - if (keyIsPressed(YOGGIE_KEY_D)) - sceneview.cam.Position += sceneview.cam.Right * movement_speed; - } + std::vector layers = std::vector(); + }; diff --git a/YoggieEngine/src/Graphics/Primitives/Camera.cpp b/YoggieEngine/src/Graphics/Primitives/Camera.cpp deleted file mode 100644 index 698578d..0000000 --- a/YoggieEngine/src/Graphics/Primitives/Camera.cpp +++ /dev/null @@ -1,46 +0,0 @@ -#include -#include "Camera.h" -namespace YoggieEngine { - Camera::Camera(const Camera& other) - : Position(other.Position), Rotation(other.Rotation), Zoom(other.Zoom) - { - - } - Camera::Camera(glm::vec3 position, glm::vec3 rotation, float zoom) - : Position(position), Rotation( rotation) - { - Front = glm::vec3(0.0f, 0.0f, 1.0f); - Right = glm::vec3(-1.0f, 0.0f, 0.0f); - Up = glm::vec3(0.0f, 1.0f, 0.0f); - Zoom = zoom; - - ProjectionMatrix = glm::perspective(glm::radians(Zoom), (800.0f / 600.0f), 0.001f, 100.0f); - Update(); - - } - - Camera::~Camera() { - - } - - void Camera::Update() { - glm::vec3 WorldUp = glm::vec3(0.0f, 1.0f, 0.0f); - - glm::vec3(direction); - - - direction.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch)); - direction.z = sin(glm::radians(yaw)); - direction.y = sin(glm::radians(pitch)) * cos(glm::radians(pitch)); - Front = glm::normalize(direction); - Right = glm::normalize(glm::cross(Front, WorldUp)); - Up = glm::normalize(glm::cross(Right, Front)); - - - ViewMatrix = glm::lookAt( - Position, - Position + Front, - Up); - } - -} diff --git a/YoggieEngine/src/Graphics/Primitives/Camera.h b/YoggieEngine/src/Graphics/Primitives/Camera.h index 438bf81..e84c950 100644 --- a/YoggieEngine/src/Graphics/Primitives/Camera.h +++ b/YoggieEngine/src/Graphics/Primitives/Camera.h @@ -2,28 +2,14 @@ namespace YoggieEngine { class Camera { public: + Camera() { - Camera() = default; - Camera(const Camera& other); - Camera(glm::vec3 position, glm::vec3 rotation, float zoom); - ~Camera(); - - void Update(); + } + + glm::mat4 view; + glm::mat4 projection; - float yaw = 180; - float pitch = 0; - float Zoom; - - glm::mat4 ViewMatrix; - glm::mat4 ProjectionMatrix; - - glm::vec3 Position; - glm::vec3 Rotation; - - glm::vec3 Front; - glm::vec3 Right; - glm::vec3 Up; }; diff --git a/YoggieEngine/src/Graphics/Primitives/Texture.cpp b/YoggieEngine/src/Graphics/Primitives/Texture.cpp index 8eccaba..1676590 100644 --- a/YoggieEngine/src/Graphics/Primitives/Texture.cpp +++ b/YoggieEngine/src/Graphics/Primitives/Texture.cpp @@ -8,7 +8,6 @@ namespace YoggieEngine { if (data) { glGenTextures(1, &Id); - glBindTexture(GL_TEXTURE_2D, Id); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); glGenerateMipmap(GL_TEXTURE_2D); @@ -22,11 +21,12 @@ namespace YoggieEngine { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); } - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); } else { + std::cout << "Failed to load image!" << std::endl; spdlog::error("Failed to load image ({0})", texturePath); } stbi_image_free(data); diff --git a/YoggieEngine/src/Graphics/Renderer.cpp b/YoggieEngine/src/Graphics/Renderer.cpp index 456bc4a..7f5067e 100644 --- a/YoggieEngine/src/Graphics/Renderer.cpp +++ b/YoggieEngine/src/Graphics/Renderer.cpp @@ -245,8 +245,8 @@ void Renderer::GeometryPass() { gBufferShader.setUniformVec3("Color", command.color); gBufferShader.setUniformMat4("Model", command.transform.LocalTransform); - gBufferShader.setUniformMat4("View", MainCamera.ViewMatrix); - gBufferShader.setUniformMat4("Projection", MainCamera.ProjectionMatrix); + gBufferShader.setUniformMat4("View", MainCamera.view); + gBufferShader.setUniformMat4("Projection", MainCamera.projection); glDrawElements(GL_TRIANGLES, static_cast(command.num_elements), GL_UNSIGNED_INT, NULL); @@ -271,8 +271,8 @@ void Renderer::ForwardGeometryPass() forwardShader.setUniformVec3("Color", command.color); forwardShader.setUniformMat4("M", command.transform.LocalTransform); - forwardShader.setUniformMat4("V", MainCamera.ViewMatrix); - forwardShader.setUniformMat4("P", MainCamera.ProjectionMatrix); + forwardShader.setUniformMat4("V", MainCamera.view); + forwardShader.setUniformMat4("P", MainCamera.projection); glDrawElements(GL_TRIANGLES, static_cast(command.num_elements), GL_UNSIGNED_INT, NULL); @@ -287,8 +287,8 @@ void Renderer::SkyboxPass() { // Render skybox glDepthMask(GL_FALSE); SkyboxShader.Use(); - SkyboxShader.setUniformMat4("projection", MainCamera.ProjectionMatrix); - SkyboxShader.setUniformMat4("view", glm::mat4(glm::mat3(MainCamera.ViewMatrix))); // remove rotation from the view matrix + SkyboxShader.setUniformMat4("projection", MainCamera.projection); + SkyboxShader.setUniformMat4("view", glm::mat4(glm::mat3(MainCamera.view))); // remove rotation from the view matrix glBindVertexArray(skyboxVAO); glBindTexture(GL_TEXTURE_CUBE_MAP, sky.getID()); glDrawArrays(GL_TRIANGLES, 0, 36); @@ -334,7 +334,7 @@ void Renderer::lightingPass(Scene& scene){ lightnr++; }); - lightingPassShader.setUniformVec3("viewPos", MainCamera.Position); + //lightingPassShader.setUniformVec3("viewPos", MainCamera.Position); // render to quad if (quadVAO == 0) @@ -373,8 +373,8 @@ void Renderer::BlendingPass() { glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, grassTexture.GetID()); - BlendingShader.setUniformMat4("V", MainCamera.ViewMatrix); - BlendingShader.setUniformMat4("P", MainCamera.ProjectionMatrix); + BlendingShader.setUniformMat4("V", MainCamera.view); + BlendingShader.setUniformMat4("P", MainCamera.projection); for (unsigned int i = 0; i < vegetation.size(); i++) {