WIP: add 3d render inside imguiwindow

This commit is contained in:
SweetId 2023-11-21 20:06:04 +05:30
parent 2b9b4f6df8
commit e79a629257
6 changed files with 69 additions and 19 deletions

View File

@ -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<Nz::WindowSwapchain> m_windowSwapchain;
std::unique_ptr<Nz::Camera> m_editorCamera;
std::shared_ptr<Nz::Texture> m_engineTexture;
std::vector<std::unique_ptr<Nz::EditorWindow>> m_windows;
std::filesystem::path m_resourceFolder;

View File

@ -2,17 +2,21 @@
#include <NazaraEditor/Core/Core.hpp>
#include <Nazara/Core/Clock.hpp>
#include <Nazara/Platform/WindowEventHandler.hpp>
#include <Nazara/Math/Ray.hpp>
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;
};
}

View File

@ -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<Nz::AppWindowingComponent>();
std::shared_ptr<Nz::RenderDevice> device = Nz::Graphics::Instance()->GetRenderDevice();
@ -22,6 +28,23 @@ namespace Nz
m_windowSwapchain = std::make_unique<Nz::WindowSwapchain>(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<std::uint8_t> 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<Nz::Camera>(std::make_shared<RenderWindow>(*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<RenderSystem>();
m_level.GetEnttWorld()->AddSystem<EditorCameraSystem>();
system.AttachExternalSwapchain(*m_windowSwapchain);
system.GetFramePipeline().RegisterViewer(m_editorCamera.get(), 2);
// configure camera
m_mainCamera = CreateEntity("MainCamera");
auto& cmp = m_mainCamera.get<Nz::EditorNameComponent>();
cmp.SetFlags(EditorEntityFlags_Hidden);
auto passList = Nz::PipelinePassList::LoadFromFile(m_resourceFolder / "engine.passlist");
auto& cameraComponent = camera.emplace<Nz::CameraComponent>(m_windowSwapchain.get(), Nz::ProjectionType::Perspective);
auto& cameraComponent = m_mainCamera.emplace<Nz::CameraComponent>(std::make_shared<Nz::RenderTexture>(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<Nz::EditorCameraComponent>();
m_mainCamera.emplace<Nz::EditorCameraComponent>(cameraComponent, system.GetFramePipeline().GetDebugDrawer());
OnLevelChanged(m_level);
}

View File

@ -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<Nz::NodeComponent>().SetPosition(Nz::Vector3f(0, 0, 0));
auto cube = CreateEntity(std::format("Cube_{}", i + 1));
cube.get<Nz::NodeComponent>().SetPosition(Nz::Vector3f(i * 2, 0, 0));
Nz::GraphicsComponent& graphicsComponent = cube.emplace<Nz::GraphicsComponent>();
std::shared_ptr<Nz::GraphicalMesh> 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)));

View File

@ -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<Nz::EditorCameraComponent>();
@ -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();
}
}

View File

@ -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<Nz::NodeComponent>()
@ -52,5 +47,6 @@ int WinMain(int argc, char* argv[])
.type(entt::type_hash<Nz::EditorNameComponent>::value())
.func<&Nz::ReflectComponent<Nz::EditorPropertyInspector<Nz::EditorRenderer>, Nz::EditorNameComponent>>(entt::hashed_string("Reflect"));
app.NewLevel();
return app.Run();
}