code refactoring
This commit is contained in:
@@ -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 {};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user