From e2df88ca226b24554ac14b938b9fb1e920ac80ab Mon Sep 17 00:00:00 2001 From: ohyzha Date: Mon, 4 Nov 2024 22:48:35 +0200 Subject: [PATCH] code refactoring --- .../Controller/CameraController.cpp | 7 +- openVulkanoCpp/Scene/Camera.cpp | 22 +++--- openVulkanoCpp/Scene/Node.cpp | 9 +++ openVulkanoCpp/Scene/Node.hpp | 2 + openVulkanoCpp/Scene/Scene.hpp | 5 +- openVulkanoCpp/Scene/SimpleDrawable.cpp | 69 +++++++++++-------- openVulkanoCpp/Scene/TextDrawable.hpp | 2 +- 7 files changed, 71 insertions(+), 45 deletions(-) diff --git a/openVulkanoCpp/Controller/CameraController.cpp b/openVulkanoCpp/Controller/CameraController.cpp index 81ba2e1..1a8d195 100644 --- a/openVulkanoCpp/Controller/CameraController.cpp +++ b/openVulkanoCpp/Controller/CameraController.cpp @@ -14,9 +14,8 @@ namespace OpenVulkano m_camera = camera; m_actionCastRay = m_inputManager->GetAction("cast ray"); m_actionCastRay->BindKey(Input::InputKey::Mouse::BUTTON_1); - // BIND FOR TOUCH - m_actionCastRay->BindKey(Input::InputKey::Touch::AXIS_TAP_X); m_actionCastRay->BindKey(Input::InputKey::Touch::AXIS_TAP_X); + m_actionCastRay->BindKey(Input::InputKey::Touch::AXIS_TAP_Y); } void CameraController::Tick() @@ -30,7 +29,9 @@ namespace OpenVulkano Math::Vector2i pos = mouse->GetMousePosition(); mouse->onLeftButtonClick.NotifyAll(pos); } - else if (touch) + // not sure about second condition here, but here should be something + else if (touch && (touch->GetAxis(Input::InputKey::Touch::AXIS_TAP_X) != 0 + || touch->GetAxis(Input::InputKey::Touch::AXIS_TAP_Y) != 0)) { Math::Vector2i pos = touch->GetTapPosition(); touch->OnTap.NotifyAll(pos); diff --git a/openVulkanoCpp/Scene/Camera.cpp b/openVulkanoCpp/Scene/Camera.cpp index 6253937..34d4533 100644 --- a/openVulkanoCpp/Scene/Camera.cpp +++ b/openVulkanoCpp/Scene/Camera.cpp @@ -52,17 +52,17 @@ namespace OpenVulkano::Scene } } } - if (res) - { - if (SimpleDrawable* sd = dynamic_cast(res->drawable)) - { - Logger::APP->info("Ray intersects object {}", sd->GetMesh()->name); - } - else if (LabelDrawable* sd = dynamic_cast(res->drawable)) - { - Logger::APP->info("Ray intersects label {}", sd->GetTexts().front().GetText()); - } - } + //if (res) + //{ + // if (SimpleDrawable* sd = dynamic_cast(res->drawable)) + // { + // Logger::APP->info("Ray intersects object {}", sd->GetMesh()->name); + // } + // else if (LabelDrawable* sd = dynamic_cast(res->drawable)) + // { + // Logger::APP->info("Ray intersects label {}", sd->GetTexts().front().GetText()); + // } + //} return res; } } diff --git a/openVulkanoCpp/Scene/Node.cpp b/openVulkanoCpp/Scene/Node.cpp index c362f43..344d669 100644 --- a/openVulkanoCpp/Scene/Node.cpp +++ b/openVulkanoCpp/Scene/Node.cpp @@ -110,6 +110,15 @@ namespace OpenVulkano::Scene UpdateWorldMatrix(parent ? parent->GetWorldMatrix() : Math::Matrix4f(1)); } + Node* Node::GetRoot() + { + if (IsRoot() || !parent) + { + return this; + } + return parent->GetRoot(); + } + void Node::UpdateWorldMatrix(const Math::Matrix4f& parentWorldMat) { worldMat = parentWorldMat * localMat; diff --git a/openVulkanoCpp/Scene/Node.hpp b/openVulkanoCpp/Scene/Node.hpp index 70bb07f..abcdcff 100644 --- a/openVulkanoCpp/Scene/Node.hpp +++ b/openVulkanoCpp/Scene/Node.hpp @@ -67,6 +67,8 @@ namespace OpenVulkano::Scene void SetMatrix(const Math::Matrix4f& mat); + [[nodiscard]] Node* GetRoot(); + [[nodiscard]] Math::Matrix3f GetRotationMatrix() const { return static_cast(localMat); } [[nodiscard]] const Math::Matrix4f& GetMatrix() const { return localMat; } diff --git a/openVulkanoCpp/Scene/Scene.hpp b/openVulkanoCpp/Scene/Scene.hpp index c4babf5..a3dc720 100644 --- a/openVulkanoCpp/Scene/Scene.hpp +++ b/openVulkanoCpp/Scene/Scene.hpp @@ -88,7 +88,10 @@ namespace OpenVulkano virtual void SetCamera(Camera* camera) { this->camera = camera; - camera->scene = this; + if (!camera->scene) + { + this->GetRoot()->AddChild(camera->GetRoot()); + } } Camera* GetCamera() const diff --git a/openVulkanoCpp/Scene/SimpleDrawable.cpp b/openVulkanoCpp/Scene/SimpleDrawable.cpp index cc2de23..2d8aa99 100644 --- a/openVulkanoCpp/Scene/SimpleDrawable.cpp +++ b/openVulkanoCpp/Scene/SimpleDrawable.cpp @@ -8,7 +8,9 @@ #include "Scene/Geometry.hpp" #include "Scene/Shader/Shader.hpp" #include "Base/Logger.hpp" +#include #include +#include namespace OpenVulkano::Scene { @@ -40,49 +42,58 @@ namespace OpenVulkano::Scene { m_mesh->CalculateAABB(); } - if (ray.IntersectAABB(m_mesh->aabb)) + auto bboxHit = ray.IntersectAABB(m_mesh->aabb); + if (!bboxHit) { - if (GetShader()->topology == Topology::TRIANGLE_LIST) + return {}; + } + if (GetShader()->topology == Topology::TRIANGLE_LIST) + { + if (m_mesh->indexCount != 0) { - if (m_mesh->indexCount != 0) + assert(m_mesh->indexCount % 3 == 0 && "Topology is TRIANGLE_LIST but index count is not divisible by 3"); + for (int i = 0; i < m_mesh->indexCount; i += 3) { - for (int i = 0; i < m_mesh->indexCount / 3; i++) + if (m_mesh->indexType == VertexIndexType::UINT16) { - if (m_mesh->indexType == VertexIndexType::UINT16) + uint16_t* indices = m_mesh->GetIndices16(); + if (auto hit = ray.IntersectTriangle(m_mesh->vertices[indices[i]].position, + m_mesh->vertices[indices[i + 1]].position, + m_mesh->vertices[indices[i + 2]].position)) { - uint16_t* indices = m_mesh->GetIndices16(); - if (auto hit = ray.IntersectTriangle(m_mesh->vertices[indices[i * 3]].position, - m_mesh->vertices[indices[i * 3 + 1]].position, - m_mesh->vertices[indices[i * 3 + 2]].position)) - { - return hit; - } - } - else - { - uint32_t* indices = m_mesh->GetIndices32(); - if (auto hit = ray.IntersectTriangle(m_mesh->vertices[indices[i * 3]].position, - m_mesh->vertices[indices[i * 3 + 1]].position, - m_mesh->vertices[indices[i * 3 + 2]].position)) - { - return hit; - } + return hit; } } - } - else - { - for (int i = 0; i < m_mesh->vertexCount / 3; i++) + else { - if (auto hit = ray.IntersectTriangle(m_mesh->vertices[i * 3].position, - m_mesh->vertices[i * 3 + 1].position, - m_mesh->vertices[i * 3 + 2].position)) + uint32_t* indices = m_mesh->GetIndices32(); + if (auto hit = ray.IntersectTriangle(m_mesh->vertices[indices[i]].position, + m_mesh->vertices[indices[i + 1]].position, + m_mesh->vertices[indices[i + 2]].position)) { return hit; } } } } + else + { + assert(m_mesh->indexCount % 3 == 0 && "Topology is TRIANGLE_LIST but vertex count is not divisible by 3"); + for (int i = 0; i < m_mesh->vertexCount; i += 3) + { + if (auto hit = ray.IntersectTriangle(m_mesh->vertices[i].position, + m_mesh->vertices[i + 1].position, + m_mesh->vertices[i + 2].position)) + { + return hit; + } + } + } + } + else + { + Logger::APP->debug("Bbox is hit, but intersection check for topology {} is not implemented", magic_enum::enum_name(GetShader()->topology)); + return bboxHit; } return {}; } diff --git a/openVulkanoCpp/Scene/TextDrawable.hpp b/openVulkanoCpp/Scene/TextDrawable.hpp index ae7f8b5..3896651 100644 --- a/openVulkanoCpp/Scene/TextDrawable.hpp +++ b/openVulkanoCpp/Scene/TextDrawable.hpp @@ -51,7 +51,7 @@ namespace OpenVulkano::Scene Math::AABB& GetBoundingBox() { return m_bbox; } TextConfig& GetConfig() { return m_cfg; } Shader* GetShader() { return m_shader; } - std::string& GetText() { return m_text; } + [[nodiscard]] const std::string& GetText() const { return m_text; } std::shared_ptr GetAtlasData() { return m_atlasData; } private: Geometry m_geometry;