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

@@ -7,6 +7,7 @@
#pragma once #pragma once
#include "Base/ICloseable.hpp" #include "Base/ICloseable.hpp"
#include "Scene/IRayIntersectable.hpp"
#include "DrawEncoder.hpp" #include "DrawEncoder.hpp"
#include <memory> #include <memory>
#include <vector> #include <vector>
@@ -26,13 +27,14 @@ namespace OpenVulkano::Scene
BACKGROUND = 0, MAIN, TRANSPARENT, POST BACKGROUND = 0, MAIN, TRANSPARENT, POST
}; };
class Drawable : public ICloseable class Drawable : public ICloseable, public IRayIntersectable
{ {
std::vector<Node*> m_nodes; std::vector<Node*> m_nodes;
Scene* m_scene = nullptr; Scene* m_scene = nullptr;
Shader* m_shader = nullptr; Shader* m_shader = nullptr;
const DrawEncoder m_encoder; const DrawEncoder m_encoder;
const DrawPhase m_drawPhase; const DrawPhase m_drawPhase;
bool m_isHittable = false;
public: public:
explicit Drawable(const DrawEncoder& encoder, explicit Drawable(const DrawEncoder& encoder,
@@ -45,6 +47,12 @@ namespace OpenVulkano::Scene
void SetShader(Shader* shader) { m_shader = shader; } void SetShader(Shader* shader) { m_shader = shader; }
std::optional<RayHit> Intersect(const Ray& ray) const override { return {}; }
void SetIsHittable(bool hittable) { m_isHittable = hittable; }
bool IsHittable() const { return m_isHittable; }
[[nodiscard]] Scene* GetScene() const { return m_scene; } [[nodiscard]] Scene* GetScene() const { return m_scene; }
[[nodiscard]] const auto& GetNodes() const { return m_nodes; } [[nodiscard]] const auto& GetNodes() const { return m_nodes; }

View File

@@ -0,0 +1,18 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#pragma once
#include "Scene/Ray.hpp"
namespace OpenVulkano::Scene
{
class IRayIntersectable
{
public:
virtual std::optional<RayHit> Intersect(const Ray& ray) const = 0;
};
}

View File

@@ -80,6 +80,11 @@ namespace OpenVulkano::Scene
m_billboardSettings = settings; m_billboardSettings = settings;
} }
std::optional<RayHit> LabelDrawable::Intersect(const Ray& ray) const
{
return ray.IntersectAABB(m_bbox);
}
void LabelDrawable::RecalculateBbox(const Math::AABB& other) void LabelDrawable::RecalculateBbox(const Math::AABB& other)
{ {
if (m_bbox.IsEmpty()) if (m_bbox.IsEmpty())

View File

@@ -61,6 +61,7 @@ namespace OpenVulkano::Scene
Math::Vector3f& GetPosition() { return m_position; } Math::Vector3f& GetPosition() { return m_position; }
bool IsBillboard() const { return m_isBillboard; } bool IsBillboard() const { return m_isBillboard; }
const Math::AABB& GetBoundingBox() const { return m_bbox; } const Math::AABB& GetBoundingBox() const { return m_bbox; }
std::optional<RayHit> Intersect(const Ray& ray) const override;
private: private:
void RecalculateBbox(const Math::AABB& other); void RecalculateBbox(const Math::AABB& other);
void SetupShaders(); void SetupShaders();

View File

@@ -5,6 +5,9 @@
*/ */
#include "SimpleDrawable.hpp" #include "SimpleDrawable.hpp"
#include "Scene/Geometry.hpp"
#include "Scene/Shader/Shader.hpp"
#include "Base/Logger.hpp"
#include <stdexcept> #include <stdexcept>
namespace OpenVulkano::Scene namespace OpenVulkano::Scene
@@ -26,4 +29,61 @@ namespace OpenVulkano::Scene
m_uniBuffer = drawable->m_uniBuffer; m_uniBuffer = drawable->m_uniBuffer;
SetShader(drawable->GetShader()); 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 {};
}
} }

View File

@@ -44,6 +44,8 @@ namespace OpenVulkano::Scene
void Init(SimpleDrawable* drawable); void Init(SimpleDrawable* drawable);
std::optional<RayHit> Intersect(const Ray& ray) const override;
[[nodiscard]] Geometry* GetMesh() const { return m_mesh; } [[nodiscard]] Geometry* GetMesh() const { return m_mesh; }
[[nodiscard]] Material* GetMaterial() const { return m_material; } [[nodiscard]] Material* GetMaterial() const { return m_material; }