code refactoring

This commit is contained in:
ohyzha
2024-11-04 22:48:35 +02:00
parent 3d96889778
commit e2df88ca22
7 changed files with 71 additions and 45 deletions

View File

@@ -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);

View File

@@ -52,17 +52,17 @@ namespace OpenVulkano::Scene
}
}
}
if (res)
{
if (SimpleDrawable* sd = dynamic_cast<SimpleDrawable*>(res->drawable))
{
Logger::APP->info("Ray intersects object {}", sd->GetMesh()->name);
}
else if (LabelDrawable* sd = dynamic_cast<LabelDrawable*>(res->drawable))
{
Logger::APP->info("Ray intersects label {}", sd->GetTexts().front().GetText());
}
}
//if (res)
//{
// if (SimpleDrawable* sd = dynamic_cast<SimpleDrawable*>(res->drawable))
// {
// Logger::APP->info("Ray intersects object {}", sd->GetMesh()->name);
// }
// else if (LabelDrawable* sd = dynamic_cast<LabelDrawable*>(res->drawable))
// {
// Logger::APP->info("Ray intersects label {}", sd->GetTexts().front().GetText());
// }
//}
return res;
}
}

View File

@@ -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;

View File

@@ -67,6 +67,8 @@ namespace OpenVulkano::Scene
void SetMatrix(const Math::Matrix4f& mat);
[[nodiscard]] Node* GetRoot();
[[nodiscard]] Math::Matrix3f GetRotationMatrix() const { return static_cast<const Math::Matrix3f>(localMat); }
[[nodiscard]] const Math::Matrix4f& GetMatrix() const { return localMat; }

View File

@@ -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

View File

@@ -8,7 +8,9 @@
#include "Scene/Geometry.hpp"
#include "Scene/Shader/Shader.hpp"
#include "Base/Logger.hpp"
#include <magic_enum.hpp>
#include <stdexcept>
#include <cassert>
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 {};
}

View File

@@ -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<AtlasData> GetAtlasData() { return m_atlasData; }
private:
Geometry m_geometry;