diff --git a/include/omath/3d_primitives/box.hpp b/include/omath/3d_primitives/box.hpp index b039825f..0e1a7e14 100644 --- a/include/omath/3d_primitives/box.hpp +++ b/include/omath/3d_primitives/box.hpp @@ -3,14 +3,60 @@ // #pragma once +#include "mesh.hpp" +#include "omath/engines/opengl_engine/camera.hpp" +#include "omath/engines/opengl_engine/traits/mesh_trait.hpp" #include "omath/linear_algebra/triangle.hpp" #include "omath/linear_algebra/vector3.hpp" #include namespace omath::primitives { + template [[nodiscard]] - std::array>, 12> create_box(const Vector3& top, const Vector3& bottom, - const Vector3& dir_forward, const Vector3& dir_right, - float ratio = 4.f) noexcept; -} + BoxMeshType create_box(const Vector3& top, const Vector3& bottom, const Vector3& dir_forward, + const Vector3& dir_right, const float ratio = 4.f) noexcept + { + const auto height = top.distance_to(bottom); + const auto side_size = height / ratio; + + // corner layout (0‑3 bottom, 4‑7 top) + std::array, 8> p; + p[0] = bottom + (dir_forward + dir_right) * side_size; // front‑right‑bottom + p[1] = bottom + (dir_forward - dir_right) * side_size; // front‑left‑bottom + p[2] = bottom + (-dir_forward + dir_right) * side_size; // back‑right‑bottom + p[3] = bottom + (-dir_forward - dir_right) * side_size; // back‑left‑bottom + p[4] = top + (dir_forward + dir_right) * side_size; // front‑right‑top + p[5] = top + (dir_forward - dir_right) * side_size; // front‑left‑top + p[6] = top + (-dir_forward + dir_right) * side_size; // back‑right‑top + p[7] = top + (-dir_forward - dir_right) * side_size; // back‑left‑top + + std::array, 12> poly; + + // bottom face (+Y up ⇒ wind CW when viewed from above) + poly[0] = {0, 2, 3}; + poly[1] = {0, 3, 1}; + + // top face + poly[2] = {4, 7, 6}; + poly[3] = {4, 5, 7}; + + // front face + poly[4] = {0, 5, 1}; + poly[5] = {0, 4, 5}; + + // right face + poly[6] = {0, 6, 2}; + poly[7] = {0, 4, 6}; + + // back face + poly[8] = {2, 7, 3}; + poly[9] = {2, 6, 7}; + + // left face + poly[10] = {1, 7, 5}; + poly[11] = {1, 3, 7}; + + return BoxMeshType{std::move(p), std::move(poly)}; + } +} // namespace omath::primitives diff --git a/include/omath/3d_primitives/mesh.hpp b/include/omath/3d_primitives/mesh.hpp index bac55130..c0ac6e47 100644 --- a/include/omath/3d_primitives/mesh.hpp +++ b/include/omath/3d_primitives/mesh.hpp @@ -25,16 +25,17 @@ namespace omath::primitives template concept HasNormal = requires(T vertex) { vertex.normal; }; template concept HasUv = requires(T vertex) { vertex.uv; }; - template> + template, + class VboType = std::vector, class EboType = std::vector>> class Mesh final { public: using VectorType = VertType::VectorType; - using VertexType = VertType; + using VertexType = VboType::value_type; private: - using Vbo = std::vector; - using Ebo = std::vector>; + using Vbo = VboType; + using Ebo = EboType; public: Vbo m_vertex_buffer; @@ -102,7 +103,9 @@ namespace omath::primitives VectorType vertex_position_to_world_space(const Vector3& vertex_position) const requires HasPosition { - auto abs_vec = get_to_world_matrix() * mat_column_from_vector(vertex_position); + auto abs_vec = get_to_world_matrix() + * mat_column_from_vector( + vertex_position); return {abs_vec.at(0, 0), abs_vec.at(1, 0), abs_vec.at(2, 0)}; } diff --git a/include/omath/3d_primitives/plane.hpp b/include/omath/3d_primitives/plane.hpp index 26f5871d..d5c83326 100644 --- a/include/omath/3d_primitives/plane.hpp +++ b/include/omath/3d_primitives/plane.hpp @@ -3,14 +3,30 @@ // #pragma once +#include "mesh.hpp" +#include "omath/engines/opengl_engine/camera.hpp" +#include "omath/engines/opengl_engine/mesh.hpp" +#include "omath/engines/opengl_engine/traits/mesh_trait.hpp" #include "omath/linear_algebra/triangle.hpp" #include "omath/linear_algebra/vector3.hpp" #include namespace omath::primitives { + template [[nodiscard]] - std::array>, 2> create_plane(const Vector3& vertex_a, - const Vector3& vertex_b, - const Vector3& direction, float size) noexcept; -} + PlaneMeshType create_plane(const Vector3& vertex_a, const Vector3& vertex_b, + const Vector3& direction, const float size) noexcept + { + const auto second_vertex_a = vertex_a + direction * size; + const auto second_vertex_b = vertex_b + direction * size; + + std::array, 4> grid = {vertex_a, vertex_b, second_vertex_a, second_vertex_b}; + + std::array, 2> poly; + poly[0] = {1, 1, 2}; + poly[1] = {0, 1, 3}; + + return PlaneMeshType(std::move(grid), std::move(poly)); + } +} // namespace omath::primitives diff --git a/include/omath/engines/frostbite_engine/primitives.hpp b/include/omath/engines/frostbite_engine/primitives.hpp new file mode 100644 index 00000000..3398d39b --- /dev/null +++ b/include/omath/engines/frostbite_engine/primitives.hpp @@ -0,0 +1,17 @@ +// +// Created by Vladislav on 27.01.2026. +// + +#pragma once +#include "mesh.hpp" +#include "omath/engines/frostbite_engine/traits/mesh_trait.hpp" +#include "omath/linear_algebra/vector3.hpp" +#include +namespace omath::frostbite_engine +{ + using BoxMesh = primitives::Mesh, std::array, 8>, + std::array, 12>>; + + using PlaneMesh = primitives::Mesh, + std::array, 4>, std::array, 2>>; +} // namespace omath::frostbite_engine \ No newline at end of file diff --git a/include/omath/engines/iw_engine/primitives.hpp b/include/omath/engines/iw_engine/primitives.hpp new file mode 100644 index 00000000..f95d46c6 --- /dev/null +++ b/include/omath/engines/iw_engine/primitives.hpp @@ -0,0 +1,17 @@ +// +// Created by Vladislav on 27.01.2026. +// + +#pragma once +#include "mesh.hpp" +#include "omath/engines/iw_engine/traits/mesh_trait.hpp" +#include "omath/linear_algebra/vector3.hpp" +#include +namespace omath::iw_engine +{ + using BoxMesh = primitives::Mesh, std::array, 8>, + std::array, 12>>; + + using PlaneMesh = primitives::Mesh, + std::array, 4>, std::array, 2>>; +} // namespace omath::iw_engine \ No newline at end of file diff --git a/include/omath/engines/opengl_engine/primitives.hpp b/include/omath/engines/opengl_engine/primitives.hpp new file mode 100644 index 00000000..9390237b --- /dev/null +++ b/include/omath/engines/opengl_engine/primitives.hpp @@ -0,0 +1,17 @@ +// +// Created by Vladislav on 27.01.2026. +// + +#pragma once +#include "mesh.hpp" +#include "omath/engines/opengl_engine/traits/mesh_trait.hpp" +#include "omath/linear_algebra/vector3.hpp" +#include +namespace omath::opengl_engine +{ + using BoxMesh = primitives::Mesh, std::array, 8>, + std::array, 12>>; + + using PlaneMesh = primitives::Mesh, + std::array, 4>, std::array, 2>>; +} // namespace omath::opengl_engine \ No newline at end of file diff --git a/include/omath/engines/source_engine/primitives.hpp b/include/omath/engines/source_engine/primitives.hpp new file mode 100644 index 00000000..f8a9d264 --- /dev/null +++ b/include/omath/engines/source_engine/primitives.hpp @@ -0,0 +1,17 @@ +// +// Created by Vladislav on 27.01.2026. +// + +#pragma once +#include "mesh.hpp" +#include "omath/engines/source_engine/traits/mesh_trait.hpp" +#include "omath/linear_algebra/vector3.hpp" +#include +namespace omath::source_engine +{ + using BoxMesh = primitives::Mesh, std::array, 8>, + std::array, 12>>; + + using PlaneMesh = primitives::Mesh, + std::array, 4>, std::array, 2>>; +} // namespace omath::source_engine \ No newline at end of file diff --git a/include/omath/engines/unity_engine/camera.hpp b/include/omath/engines/unity_engine/camera.hpp index 642290a3..bc3d6d2b 100644 --- a/include/omath/engines/unity_engine/camera.hpp +++ b/include/omath/engines/unity_engine/camera.hpp @@ -9,5 +9,5 @@ namespace omath::unity_engine { - using Camera = projection::Camera; + using Camera = projection::Camera; } // namespace omath::unity_engine \ No newline at end of file diff --git a/include/omath/engines/unity_engine/primitives.hpp b/include/omath/engines/unity_engine/primitives.hpp new file mode 100644 index 00000000..7e4c55d8 --- /dev/null +++ b/include/omath/engines/unity_engine/primitives.hpp @@ -0,0 +1,17 @@ +// +// Created by Vladislav on 27.01.2026. +// + +#pragma once +#include "mesh.hpp" +#include "omath/engines/unity_engine/traits/mesh_trait.hpp" +#include "omath/linear_algebra/vector3.hpp" +#include +namespace omath::unity_engine +{ + using BoxMesh = primitives::Mesh, std::array, 8>, + std::array, 12>>; + + using PlaneMesh = primitives::Mesh, + std::array, 4>, std::array, 2>>; +} // namespace omath::unity_engine \ No newline at end of file diff --git a/include/omath/engines/unreal_engine/primitives.hpp b/include/omath/engines/unreal_engine/primitives.hpp new file mode 100644 index 00000000..2a71e764 --- /dev/null +++ b/include/omath/engines/unreal_engine/primitives.hpp @@ -0,0 +1,17 @@ +// +// Created by Vladislav on 27.01.2026. +// + +#pragma once +#include "mesh.hpp" +#include "omath/engines/unreal_engine/traits/mesh_trait.hpp" +#include "omath/linear_algebra/vector3.hpp" +#include +namespace omath::unreal_engine +{ + using BoxMesh = primitives::Mesh, std::array, 8>, + std::array, 12>>; + + using PlaneMesh = primitives::Mesh, + std::array, 4>, std::array, 2>>; +} // namespace omath::unreal_engine \ No newline at end of file diff --git a/source/3d_primitives/box.cpp b/source/3d_primitives/box.cpp deleted file mode 100644 index 9c251b98..00000000 --- a/source/3d_primitives/box.cpp +++ /dev/null @@ -1,54 +0,0 @@ -// -// Created by Vlad on 4/18/2025. -// -#include "omath/3d_primitives/box.hpp" - -namespace omath::primitives -{ - std::array>, 12> create_box(const Vector3& top, const Vector3& bottom, - const Vector3& dir_forward, - const Vector3& dir_right, const float ratio) noexcept - { - const auto height = top.distance_to(bottom); - const auto side_size = height / ratio; - - // corner layout (0‑3 bottom, 4‑7 top) - std::array, 8> p; - p[0] = bottom + (dir_forward + dir_right) * side_size; // front‑right‑bottom - p[1] = bottom + (dir_forward - dir_right) * side_size; // front‑left‑bottom - p[2] = bottom + (-dir_forward + dir_right) * side_size; // back‑right‑bottom - p[3] = bottom + (-dir_forward - dir_right) * side_size; // back‑left‑bottom - p[4] = top + (dir_forward + dir_right) * side_size; // front‑right‑top - p[5] = top + (dir_forward - dir_right) * side_size; // front‑left‑top - p[6] = top + (-dir_forward + dir_right) * side_size; // back‑right‑top - p[7] = top + (-dir_forward - dir_right) * side_size; // back‑left‑top - - std::array>, 12> poly; - - // bottom face (+Y up ⇒ wind CW when viewed from above) - poly[0] = {p[0], p[2], p[3]}; - poly[1] = {p[0], p[3], p[1]}; - - // top face - poly[2] = {p[4], p[7], p[6]}; - poly[3] = {p[4], p[5], p[7]}; - - // front face - poly[4] = {p[0], p[5], p[1]}; - poly[5] = {p[0], p[4], p[5]}; - - // right face - poly[6] = {p[0], p[6], p[2]}; - poly[7] = {p[0], p[4], p[6]}; - - // back face - poly[8] = {p[2], p[7], p[3]}; - poly[9] = {p[2], p[6], p[7]}; - - // left face - poly[10] = {p[1], p[7], p[5]}; - poly[11] = {p[1], p[3], p[7]}; - - return poly; - } -} // namespace omath::primitives diff --git a/source/3d_primitives/plane.cpp b/source/3d_primitives/plane.cpp deleted file mode 100644 index c5418109..00000000 --- a/source/3d_primitives/plane.cpp +++ /dev/null @@ -1,19 +0,0 @@ -// -// Created by Vlad on 8/28/2025. -// -#include "omath/3d_primitives/plane.hpp" - -namespace omath::primitives -{ - std::array>, 2> create_plane(const Vector3& vertex_a, - const Vector3& vertex_b, - const Vector3& direction, const float size) noexcept - { - const auto second_vertex_a = vertex_a + direction * size; - return std::array - { - Triangle{second_vertex_a, vertex_a, vertex_b}, - Triangle{second_vertex_a, vertex_b + direction * size, vertex_b} - }; - } -} // namespace omath::primitives \ No newline at end of file diff --git a/tests/general/unit_test_primitive_box.cpp b/tests/general/unit_test_primitive_box.cpp new file mode 100644 index 00000000..3d7345fb --- /dev/null +++ b/tests/general/unit_test_primitive_box.cpp @@ -0,0 +1,12 @@ +// +// Created by Vladislav on 11.01.2026. +// +#include "omath/3d_primitives/box.hpp" +#include "omath/engines/opengl_engine/primitives.hpp" +#include + +TEST(test, test) +{ + auto result = omath::primitives::create_box({0.f, 30.f, 0.f}, {}, omath::opengl_engine::k_abs_forward, + omath::opengl_engine::k_abs_right); +} \ No newline at end of file