From e79a629257c7993fd486e2ee94e2ac09ab5b8fc9 Mon Sep 17 00:00:00 2001 From: SweetId <2630750+SweetId@users.noreply.github.com> Date: Tue, 21 Nov 2023 20:06:04 +0530 Subject: [PATCH] WIP: add 3d render inside imguiwindow --- .../Core/Application/BaseApplication.hpp | 5 ++ .../Core/Components/CameraComponent.hpp | 14 +++++- .../Core/Application/BaseApplication.cpp | 50 ++++++++++++++++--- src/NazaraEditor/Editor/Application.cpp | 6 ++- src/NazaraEditor/Editor/UI/MainWindow.cpp | 5 +- src/NazaraEditor/Editor/main.cpp | 8 +-- 6 files changed, 69 insertions(+), 19 deletions(-) diff --git a/include/NazaraEditor/Core/Application/BaseApplication.hpp b/include/NazaraEditor/Core/Application/BaseApplication.hpp index e9efb22..ca5f82d 100644 --- a/include/NazaraEditor/Core/Application/BaseApplication.hpp +++ b/include/NazaraEditor/Core/Application/BaseApplication.hpp @@ -80,11 +80,16 @@ namespace Nz inline Nz::Window* GetWindow() { return m_window; } inline const Nz::Window* GetWindow() const { return m_window; } + inline Nz::Texture* GetEngineTexture() { return m_engineTexture.get(); } + inline const Nz::Texture* GetEngineTexture() const { return m_engineTexture.get(); } + private: static EditorBaseApplication* s_instance; Nz::Window* m_window; std::unique_ptr m_windowSwapchain; + std::unique_ptr m_editorCamera; + std::shared_ptr m_engineTexture; std::vector> m_windows; std::filesystem::path m_resourceFolder; diff --git a/include/NazaraEditor/Core/Components/CameraComponent.hpp b/include/NazaraEditor/Core/Components/CameraComponent.hpp index 6a14166..7bd9c47 100644 --- a/include/NazaraEditor/Core/Components/CameraComponent.hpp +++ b/include/NazaraEditor/Core/Components/CameraComponent.hpp @@ -2,17 +2,21 @@ #include +#include #include +#include namespace Nz { + class Camera; + class DebugDrawer; class NodeComponent; class WindowEventHandler; class NAZARAEDITOR_CORE_API EditorCameraComponent { public: - EditorCameraComponent(); + EditorCameraComponent(Nz::Camera& camera, Nz::DebugDrawer& debug); EditorCameraComponent(const EditorCameraComponent&) = delete; EditorCameraComponent(EditorCameraComponent&&); @@ -27,13 +31,21 @@ namespace Nz inline const Nz::EulerAnglesf& GetOrientation() const { return m_targetAngles; } private: + void RaycastSelection(int x, int y); + + NazaraSlot(Nz::WindowEventHandler, OnMouseButtonReleased, m_onMouseClicked); NazaraSlot(Nz::WindowEventHandler, OnMouseMoved, m_onMouseMoved); + Nz::Camera& m_camera; Nz::EulerAnglesf m_targetAngles; Nz::Vector3f m_targetPosition; Nz::Vector3f m_currentVelocity; float m_moveSpeed; float m_smoothSpeed; + + Nz::Rayf m_lastRay; + Nz::DebugDrawer& m_debugDrawer; + Nz::MillisecondClock m_debugClock; }; } \ No newline at end of file diff --git a/src/NazaraEditor/Core/Application/BaseApplication.cpp b/src/NazaraEditor/Core/Application/BaseApplication.cpp index bbb4b45..b4131df 100644 --- a/src/NazaraEditor/Core/Application/BaseApplication.cpp +++ b/src/NazaraEditor/Core/Application/BaseApplication.cpp @@ -13,6 +13,12 @@ namespace Nz NazaraAssert(s_instance == nullptr, "EditorBaseApplication already exists"); s_instance = this; + + std::filesystem::path resourceDir = "assets/editor"; + if (!std::filesystem::is_directory(resourceDir) && std::filesystem::is_directory("../.." / resourceDir)) + resourceDir = "../.." / resourceDir; + SetResourceFolder(resourceDir); + auto& windowing = AddComponent(); std::shared_ptr device = Nz::Graphics::Instance()->GetRenderDevice(); @@ -22,6 +28,23 @@ namespace Nz m_windowSwapchain = std::make_unique(device, window); m_window = &window; + // Allocate texture for engine rendering + { + Nz::TextureInfo screenTextureInfo = { + .pixelFormat = Nz::PixelFormat::RGBA8, + .type = Nz::ImageType::E2D, + .usageFlags = Nz::TextureUsage::ColorAttachment | Nz::TextureUsage::ShaderSampling | Nz::TextureUsage::TransferDestination, + .levelCount = 1, + .height = 720, + .width = 1280 + }; + + std::size_t size = Nz::PixelFormatInfo::ComputeSize(screenTextureInfo.pixelFormat, screenTextureInfo.width, screenTextureInfo.height, screenTextureInfo.depth); + + std::vector defaultScreen(size, 0xFF); + m_engineTexture = device->InstantiateTexture(screenTextureInfo, defaultScreen.data(), false); + } + // connect basic handler window.GetEventHandler().OnQuit.Connect([&window](const auto* handler) { NazaraUnused(handler); @@ -33,6 +56,10 @@ namespace Nz Nz::Imgui::Instance()->Init(window); ImGui::EnsureContextOnThisThread(); + // load the passes after Imgui is init + auto passList = Nz::PipelinePassList::LoadFromFile(m_resourceFolder / "editor.passlist"); + m_editorCamera = std::make_unique(std::make_shared(*m_windowSwapchain), passList); + AddUpdaterFunc(Interval{ Nz::Time::Milliseconds(16) }, [&](Nz::Time elapsed) { if (!window.IsOpen()) return; @@ -41,15 +68,16 @@ namespace Nz m_popupManager.Update(); - Nz::RenderFrame frame = m_windowSwapchain->AcquireFrame(); - if (!frame) - return; + Nz::Imgui::Instance()->Update(elapsed.AsSeconds()); + Nz::Imgui::Instance()->Render(); - Nz::Imgui::Instance()->Update(window, elapsed.AsSeconds()); + //Nz::RenderFrame frame = m_windowSwapchain->AcquireFrame(); + //if (!frame) + // return; - Nz::Imgui::Instance()->Render(m_windowSwapchain.get(), frame); + //Nz::Imgui::Instance()->Render(m_windowSwapchain->GetSwapchain(), frame); - frame.Present(); + //frame.Present(); }); } @@ -86,18 +114,24 @@ namespace Nz bool bRes = m_level.CreateNewLevel(); if (bRes) { + RenderSystem& system = m_level.GetEnttWorld()->AddSystem(); m_level.GetEnttWorld()->AddSystem(); + system.AttachExternalSwapchain(*m_windowSwapchain); + system.GetFramePipeline().RegisterViewer(m_editorCamera.get(), 2); + // configure camera m_mainCamera = CreateEntity("MainCamera"); auto& cmp = m_mainCamera.get(); cmp.SetFlags(EditorEntityFlags_Hidden); + + auto passList = Nz::PipelinePassList::LoadFromFile(m_resourceFolder / "engine.passlist"); - auto& cameraComponent = camera.emplace(m_windowSwapchain.get(), Nz::ProjectionType::Perspective); + auto& cameraComponent = m_mainCamera.emplace(std::make_shared(m_engineTexture), passList, Nz::ProjectionType::Perspective); cameraComponent.UpdateFOV(70.f); cameraComponent.UpdateClearColor(Nz::Color(0.46f, 0.48f, 0.84f, 1.f)); - auto& editorcam = m_mainCamera.emplace(); + m_mainCamera.emplace(cameraComponent, system.GetFramePipeline().GetDebugDrawer()); OnLevelChanged(m_level); } diff --git a/src/NazaraEditor/Editor/Application.cpp b/src/NazaraEditor/Editor/Application.cpp index cb03542..e0607e8 100644 --- a/src/NazaraEditor/Editor/Application.cpp +++ b/src/NazaraEditor/Editor/Application.cpp @@ -45,9 +45,11 @@ namespace NzEditor light.UpdateRotation(Nz::Quaternionf::LookAt(Nz::Vector3f(-1, 0, -1), Nz::Vector3f::Up())); light.EnableShadowCasting(true); } + + for (int i = 0; i < 1; ++i) { - auto cube = CreateEntity("Cube"); - cube.get().SetPosition(Nz::Vector3f(0, 0, 0)); + auto cube = CreateEntity(std::format("Cube_{}", i + 1)); + cube.get().SetPosition(Nz::Vector3f(i * 2, 0, 0)); Nz::GraphicsComponent& graphicsComponent = cube.emplace(); std::shared_ptr boxMesh = Nz::GraphicalMesh::Build(Nz::Primitive::Box(Nz::Vector3f(1.f), Nz::Vector3ui::Zero(), Nz::Matrix4f::Scale(Nz::Vector3f(1.f)), Nz::Rectf(0.f, 0.f, 2.f, 2.f))); diff --git a/src/NazaraEditor/Editor/UI/MainWindow.cpp b/src/NazaraEditor/Editor/UI/MainWindow.cpp index 7a58b10..e4f8c9b 100644 --- a/src/NazaraEditor/Editor/UI/MainWindow.cpp +++ b/src/NazaraEditor/Editor/UI/MainWindow.cpp @@ -14,8 +14,7 @@ namespace NzEditor { Nz::EditorMainWindow::OnRenderImgui(); - auto flags = ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoDecoration; - if (ImGui::Begin("MainWindow", nullptr, flags)) + if (ImGui::Begin("MainWindow")) { auto cam = GetApplication()->GetMainCamera(); auto& camcomponent = cam.get(); @@ -25,6 +24,8 @@ namespace NzEditor auto rotation = transform.GetRotation().ToEulerAngles(); ImGui::Text("Camera position: %.2f %.2f %.2f", transform.GetPosition().x, transform.GetPosition().y, transform.GetPosition().z); ImGui::Text("Camera rotation: %.2f %.2f %.2f", rotation.roll.value, rotation.pitch.value, rotation.yaw.value); + + ImGui::Image(GetApplication()->GetEngineTexture()); ImGui::End(); } } diff --git a/src/NazaraEditor/Editor/main.cpp b/src/NazaraEditor/Editor/main.cpp index d690ee0..a094109 100644 --- a/src/NazaraEditor/Editor/main.cpp +++ b/src/NazaraEditor/Editor/main.cpp @@ -23,17 +23,12 @@ int WinMain(int argc, char* argv[]) Nz::EditorLogger logger; - std::filesystem::path resourceDir = "assets/editor"; - if (!std::filesystem::is_directory(resourceDir) && std::filesystem::is_directory("../.." / resourceDir)) - resourceDir = "../.." / resourceDir; - NzEditor::Application app; - app.SetResourceFolder(resourceDir); app.SetLogger(logger); ImGui::EnsureContextOnThisThread(); - Nz::Localization::Instance()->LoadLocalizationFile(resourceDir / "localization.csv"); + Nz::Localization::Instance()->LoadLocalizationFile(app.GetResourceFolder() / "localization.csv"); Nz::Localization::Instance()->SetLocale("en-US"); entt::meta() @@ -52,5 +47,6 @@ int WinMain(int argc, char* argv[]) .type(entt::type_hash::value()) .func<&Nz::ReflectComponent, Nz::EditorNameComponent>>(entt::hashed_string("Reflect")); + app.NewLevel(); return app.Run(); } \ No newline at end of file