introduce intersectable interface

This commit is contained in:
ohyzha
2024-11-04 18:20:32 +02:00
parent 4f3336014a
commit 4956884d5f
6 changed files with 95 additions and 1 deletions

View File

@@ -5,6 +5,9 @@
*/
#include "SimpleDrawable.hpp"
#include "Scene/Geometry.hpp"
#include "Scene/Shader/Shader.hpp"
#include "Base/Logger.hpp"
#include <stdexcept>
namespace OpenVulkano::Scene
@@ -26,4 +29,61 @@ namespace OpenVulkano::Scene
m_uniBuffer = drawable->m_uniBuffer;
SetShader(drawable->GetShader());
}
std::optional<RayHit> SimpleDrawable::Intersect(const Ray& ray) const
{
if (!m_mesh || !GetShader())
{
return {};
}
if (m_mesh->aabb.IsEmpty())
{
m_mesh->CalculateAABB();
}
if (ray.IntersectAABB(m_mesh->aabb))
{
if (GetShader()->topology == Topology::TRIANGLE_LIST)
{
if (m_mesh->indexCount != 0)
{
for (int i = 0; i < m_mesh->indexCount / 3; i++)
{
if (m_mesh->indexType == VertexIndexType::UINT16)
{
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;
}
}
}
}
else
{
for (int i = 0; i < m_mesh->vertexCount / 3; i++)
{
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))
{
return hit;
}
}
}
}
}
return {};
}
}