From b8cb96b81fef69bcc6f6b2b6f48c7198f42b03d2 Mon Sep 17 00:00:00 2001 From: Benau Date: Thu, 16 Jun 2022 12:00:15 +0800 Subject: [PATCH] Add GEVulkanCameraSceneNode --- lib/graphics_engine/CMakeLists.txt | 1 + .../include/ge_vulkan_scene_manager.hpp | 5 ++ .../src/ge_vulkan_camera_scene_node.cpp | 48 +++++++++++++++++++ .../src/ge_vulkan_camera_scene_node.hpp | 43 +++++++++++++++++ .../src/ge_vulkan_scene_manager.cpp | 21 ++++++++ 5 files changed, 118 insertions(+) create mode 100644 lib/graphics_engine/src/ge_vulkan_camera_scene_node.cpp create mode 100644 lib/graphics_engine/src/ge_vulkan_camera_scene_node.hpp diff --git a/lib/graphics_engine/CMakeLists.txt b/lib/graphics_engine/CMakeLists.txt index 9d02bc8fe..614103e0a 100644 --- a/lib/graphics_engine/CMakeLists.txt +++ b/lib/graphics_engine/CMakeLists.txt @@ -27,6 +27,7 @@ set(GE_SOURCES src/ge_texture.cpp src/ge_dx9_texture.cpp src/ge_vulkan_2d_renderer.cpp + src/ge_vulkan_camera_scene_node.cpp src/ge_vulkan_command_loader.cpp src/ge_vulkan_driver.cpp src/ge_vulkan_dynamic_buffer.cpp diff --git a/lib/graphics_engine/include/ge_vulkan_scene_manager.hpp b/lib/graphics_engine/include/ge_vulkan_scene_manager.hpp index 81856ffd6..438087602 100644 --- a/lib/graphics_engine/include/ge_vulkan_scene_manager.hpp +++ b/lib/graphics_engine/include/ge_vulkan_scene_manager.hpp @@ -18,6 +18,11 @@ public: // ------------------------------------------------------------------------ ~GEVulkanSceneManager(); // ------------------------------------------------------------------------ + virtual irr::scene::ICameraSceneNode* addCameraSceneNode( + irr::scene::ISceneNode* parent = 0, + const irr::core::vector3df& position = irr::core::vector3df(0, 0, 0), + const irr::core::vector3df& lookat = irr::core::vector3df(0, 0, 100), + irr::s32 id = -1, bool make_active = true); }; // GEVulkanSceneManager } diff --git a/lib/graphics_engine/src/ge_vulkan_camera_scene_node.cpp b/lib/graphics_engine/src/ge_vulkan_camera_scene_node.cpp new file mode 100644 index 000000000..73dfffc2d --- /dev/null +++ b/lib/graphics_engine/src/ge_vulkan_camera_scene_node.cpp @@ -0,0 +1,48 @@ +#include "ge_vulkan_camera_scene_node.hpp" + +#include "ge_vulkan_dynamic_buffer.hpp" + +namespace GE +{ +// ---------------------------------------------------------------------------- +GEVulkanCameraSceneNode::GEVulkanCameraSceneNode(irr::scene::ISceneNode* parent, + irr::scene::ISceneManager* mgr, + irr::s32 id, + const irr::core::vector3df& position, + const irr::core::vector3df& lookat) + : CCameraSceneNode(parent, mgr, id, position, lookat) +{ + m_buffer = new GEVulkanDynamicBuffer(GVDBT_GPU_RAM, + VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, sizeof(GEVulkanCameraUBO)); +} // GEVulkanCameraSceneNode + +// ---------------------------------------------------------------------------- +GEVulkanCameraSceneNode::~GEVulkanCameraSceneNode() +{ + delete m_buffer; +} // ~GEVulkanCameraSceneNode + +// ---------------------------------------------------------------------------- +void GEVulkanCameraSceneNode::render() +{ + irr::scene::CCameraSceneNode::render(); + memcpy(m_ubo_data.m_view_matrix.data(), + ViewArea.getTransform(irr::video::ETS_VIEW).pointer(), + 16 * sizeof(float)); + memcpy(m_ubo_data.m_projection_matrix.data(), + ViewArea.getTransform(irr::video::ETS_PROJECTION).pointer(), + 16 * sizeof(float)); + irr::core::matrix4 mat; + ViewArea.getTransform(irr::video::ETS_VIEW).getInverse(mat); + memcpy(m_ubo_data.m_inverse_view_matrix.data(), mat.pointer(), + 16 * sizeof(float)); + ViewArea.getTransform(irr::video::ETS_PROJECTION).getInverse(mat); + memcpy(m_ubo_data.m_inverse_projection_matrix.data(), mat.pointer(), + 16 * sizeof(float)); + mat = ViewArea.getTransform(irr::video::ETS_PROJECTION) * + ViewArea.getTransform(irr::video::ETS_VIEW); + memcpy(m_ubo_data.m_projection_view_matrix.data(), mat.pointer(), + 16 * sizeof(float)); +} // render + +} diff --git a/lib/graphics_engine/src/ge_vulkan_camera_scene_node.hpp b/lib/graphics_engine/src/ge_vulkan_camera_scene_node.hpp new file mode 100644 index 000000000..e3ea9cc17 --- /dev/null +++ b/lib/graphics_engine/src/ge_vulkan_camera_scene_node.hpp @@ -0,0 +1,43 @@ +#ifndef HEADER_GE_VULKAN_CAMERA_SCENE_NODE_HPP +#define HEADER_GE_VULKAN_CAMERA_SCENE_NODE_HPP + +#include "../source/Irrlicht/CCameraSceneNode.h" + +#include + +namespace GE +{ +struct GEVulkanCameraUBO +{ +std::array m_view_matrix; +std::array m_projection_matrix; +std::array m_inverse_view_matrix; +std::array m_inverse_projection_matrix; +std::array m_projection_view_matrix; +}; + +class GEVulkanDynamicBuffer; + +class GEVulkanCameraSceneNode : public irr::scene::CCameraSceneNode +{ +private: + GEVulkanDynamicBuffer* m_buffer; + + GEVulkanCameraUBO m_ubo_data; +public: + // ------------------------------------------------------------------------ + GEVulkanCameraSceneNode(irr::scene::ISceneNode* parent, + irr::scene::ISceneManager* mgr, irr::s32 id, + const irr::core::vector3df& position = irr::core::vector3df(0, 0, 0), + const irr::core::vector3df& lookat = irr::core::vector3df(0, 0, 100)); + // ------------------------------------------------------------------------ + ~GEVulkanCameraSceneNode(); + // ------------------------------------------------------------------------ + virtual void render(); + // ------------------------------------------------------------------------ + GEVulkanDynamicBuffer* getBuffer() const { return m_buffer; } +}; // GEVulkanCameraSceneNode + +} + +#endif diff --git a/lib/graphics_engine/src/ge_vulkan_scene_manager.cpp b/lib/graphics_engine/src/ge_vulkan_scene_manager.cpp index de7b0e562..72b8629bc 100644 --- a/lib/graphics_engine/src/ge_vulkan_scene_manager.cpp +++ b/lib/graphics_engine/src/ge_vulkan_scene_manager.cpp @@ -1,5 +1,6 @@ #include "ge_vulkan_scene_manager.hpp" +#include "ge_vulkan_camera_scene_node.hpp" #include "ge_vulkan_mesh_cache.hpp" namespace GE @@ -21,4 +22,24 @@ GEVulkanSceneManager::~GEVulkanSceneManager() { } // ~GEVulkanSceneManager +// ---------------------------------------------------------------------------- +irr::scene::ICameraSceneNode* GEVulkanSceneManager::addCameraSceneNode( + irr::scene::ISceneNode* parent, + const irr::core::vector3df& position, + const irr::core::vector3df& lookat, + irr::s32 id, bool make_active) +{ + if (!parent) + parent = this; + + irr::scene::ICameraSceneNode* node = new GEVulkanCameraSceneNode(parent, + this, id, position, lookat); + + if (make_active) + setActiveCamera(node); + node->drop(); + + return node; +} // addCameraSceneNode + }