/* * 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 "Math/Math.hpp" #include "Math/AABB.hpp" #include namespace OpenVulkano::Scene { class Drawable; class Node; struct RayHit { Math::Vector3f point; Math::Vector3f normal; float distance2; [[nodiscard]] float GetDistance() const { if (distance == -1) { distance = std::sqrt(distance2); } return distance; } void SetDistance(float d) { this->distance = d; this->distance2 = d * d; } bool operator==(const RayHit& other) const; bool operator!=(const RayHit& other) const; private: mutable float distance = -1; }; struct DrawableRayHit : RayHit { DrawableRayHit() = default; DrawableRayHit(const RayHit& hit) : RayHit(hit) {}; Drawable* drawable = nullptr; Node* node = nullptr; }; class Ray { public: Ray() = default; Ray(const Math::Vector3f& origin, const Math::Vector3f& dir) : m_origin(origin), m_dir(Math::Utils::normalize(dir)) { } void SetPosition(const Math::Vector3f& origin) { m_origin = origin; } void SetDir(const Math::Vector3f& dir) { m_dir = dir; } [[nodiscard]] std::optional IntersectSphere(const Math::Vector3f& center, float radius) const; int IntersectSphere(const Math::Vector3f& center, float radius, RayHit& p1, RayHit& p2) const; [[nodiscard]] std::optional IntersectTriangle(const Math::Vector3f& v0, const Math::Vector3f& v1, const Math::Vector3f& v2) const; [[nodiscard]] std::optional IntersectTriangle(const Math::Vector3f& v0, const Math::Vector3f& v1, const Math::Vector3f& v2, const Math::Vector3f& n0, const Math::Vector3f& n1, const Math::Vector3f& n2) const; [[nodiscard]] std::optional IntersectQuad(const Math::Vector3f& v0, const Math::Vector3f& v1, const Math::Vector3f& v2, const Math::Vector3f& v3) const; [[nodiscard]] std::optional IntersectAABB(const Math::AABB& bbox) const; int IntersectAABB(const Math::AABB& bbox, RayHit& p1, RayHit& p2) const; [[nodiscard]] std::optional IntersectPlane(const Math::Vector3f& pOrigin, const Math::Vector3f& pNorm) const; [[nodiscard]] Math::Vector3f& GetOrigin() { return m_origin; } [[nodiscard]] Math::Vector3f& GetDir() { return m_dir; } [[nodiscard]] const Math::Vector3f& GetOrigin() const { return m_origin; } [[nodiscard]] const Math::Vector3f& GetDir() const { return m_dir; } private: Math::Vector3f m_origin; Math::Vector3f m_dir; mutable Math::Vector2f m_baryPos; }; }