Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 50 additions & 4 deletions include/omath/3d_primitives/box.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <array>

namespace omath::primitives
{
template<class BoxMeshType>
[[nodiscard]]
std::array<Triangle<Vector3<float>>, 12> create_box(const Vector3<float>& top, const Vector3<float>& bottom,
const Vector3<float>& dir_forward, const Vector3<float>& dir_right,
float ratio = 4.f) noexcept;
}
BoxMeshType create_box(const Vector3<float>& top, const Vector3<float>& bottom, const Vector3<float>& dir_forward,
const Vector3<float>& 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<Vector3<float>, 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<Vector3<std::uint32_t>, 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
13 changes: 8 additions & 5 deletions include/omath/3d_primitives/mesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,17 @@ namespace omath::primitives
template<typename T> concept HasNormal = requires(T vertex) { vertex.normal; };
template<typename T> concept HasUv = requires(T vertex) { vertex.uv; };

template<class Mat4X4, class RotationAngles, class MeshTypeTrait, class VertType = Vertex<>>
template<class Mat4X4, class RotationAngles, class MeshTypeTrait, class VertType = Vertex<>,
class VboType = std::vector<VertType>, class EboType = std::vector<Vector3<std::uint32_t>>>
class Mesh final
{
public:
using VectorType = VertType::VectorType;
using VertexType = VertType;
using VertexType = VboType::value_type;

private:
using Vbo = std::vector<VertexType>;
using Ebo = std::vector<Vector3<std::uint32_t>>;
using Vbo = VboType;
using Ebo = EboType;

public:
Vbo m_vertex_buffer;
Expand Down Expand Up @@ -102,7 +103,9 @@ namespace omath::primitives
VectorType vertex_position_to_world_space(const Vector3<float>& vertex_position) const
requires HasPosition<VertexType>
{
auto abs_vec = get_to_world_matrix() * mat_column_from_vector<typename Mat4X4::ContainedType, Mat4X4::get_store_ordering()>(vertex_position);
auto abs_vec = get_to_world_matrix()
* mat_column_from_vector<typename Mat4X4::ContainedType, Mat4X4::get_store_ordering()>(
vertex_position);

return {abs_vec.at(0, 0), abs_vec.at(1, 0), abs_vec.at(2, 0)};
}
Expand Down
24 changes: 20 additions & 4 deletions include/omath/3d_primitives/plane.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <array>

namespace omath::primitives
{
template<class PlaneMeshType>
[[nodiscard]]
std::array<Triangle<Vector3<float>>, 2> create_plane(const Vector3<float>& vertex_a,
const Vector3<float>& vertex_b,
const Vector3<float>& direction, float size) noexcept;
}
PlaneMeshType create_plane(const Vector3<float>& vertex_a, const Vector3<float>& vertex_b,
const Vector3<float>& 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<Vector3<float>, 4> grid = {vertex_a, vertex_b, second_vertex_a, second_vertex_b};

std::array<Vector3<std::uint32_t>, 2> poly;
poly[0] = {1, 1, 2};
poly[1] = {0, 1, 3};

return PlaneMeshType(std::move(grid), std::move(poly));
}
} // namespace omath::primitives
17 changes: 17 additions & 0 deletions include/omath/engines/frostbite_engine/primitives.hpp
Original file line number Diff line number Diff line change
@@ -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 <array>
namespace omath::frostbite_engine
{
using BoxMesh = primitives::Mesh<Mat4X4, ViewAngles, MeshTrait, primitives::Vertex<>, std::array<Vector3<float>, 8>,
std::array<Vector3<std::uint32_t>, 12>>;

using PlaneMesh = primitives::Mesh<Mat4X4, ViewAngles, MeshTrait, primitives::Vertex<>,
std::array<Vector3<float>, 4>, std::array<Vector3<std::uint32_t>, 2>>;
} // namespace omath::frostbite_engine
17 changes: 17 additions & 0 deletions include/omath/engines/iw_engine/primitives.hpp
Original file line number Diff line number Diff line change
@@ -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 <array>
namespace omath::iw_engine
{
using BoxMesh = primitives::Mesh<Mat4X4, ViewAngles, MeshTrait, primitives::Vertex<>, std::array<Vector3<float>, 8>,
std::array<Vector3<std::uint32_t>, 12>>;

using PlaneMesh = primitives::Mesh<Mat4X4, ViewAngles, MeshTrait, primitives::Vertex<>,
std::array<Vector3<float>, 4>, std::array<Vector3<std::uint32_t>, 2>>;
} // namespace omath::iw_engine
17 changes: 17 additions & 0 deletions include/omath/engines/opengl_engine/primitives.hpp
Original file line number Diff line number Diff line change
@@ -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 <array>
namespace omath::opengl_engine
{
using BoxMesh = primitives::Mesh<Mat4X4, ViewAngles, MeshTrait, primitives::Vertex<>, std::array<Vector3<float>, 8>,
std::array<Vector3<std::uint32_t>, 12>>;

using PlaneMesh = primitives::Mesh<Mat4X4, ViewAngles, MeshTrait, primitives::Vertex<>,
std::array<Vector3<float>, 4>, std::array<Vector3<std::uint32_t>, 2>>;
} // namespace omath::opengl_engine
17 changes: 17 additions & 0 deletions include/omath/engines/source_engine/primitives.hpp
Original file line number Diff line number Diff line change
@@ -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 <array>
namespace omath::source_engine
{
using BoxMesh = primitives::Mesh<Mat4X4, ViewAngles, MeshTrait, primitives::Vertex<>, std::array<Vector3<float>, 8>,
std::array<Vector3<std::uint32_t>, 12>>;

using PlaneMesh = primitives::Mesh<Mat4X4, ViewAngles, MeshTrait, primitives::Vertex<>,
std::array<Vector3<float>, 4>, std::array<Vector3<std::uint32_t>, 2>>;
} // namespace omath::source_engine
2 changes: 1 addition & 1 deletion include/omath/engines/unity_engine/camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@

namespace omath::unity_engine
{
using Camera = projection::Camera<Mat4X4, ViewAngles, CameraTrait>;
using Camera = projection::Camera<Mat4X4, ViewAngles, CameraTrait>;
} // namespace omath::unity_engine
17 changes: 17 additions & 0 deletions include/omath/engines/unity_engine/primitives.hpp
Original file line number Diff line number Diff line change
@@ -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 <array>
namespace omath::unity_engine
{
using BoxMesh = primitives::Mesh<Mat4X4, ViewAngles, MeshTrait, primitives::Vertex<>, std::array<Vector3<float>, 8>,
std::array<Vector3<std::uint32_t>, 12>>;

using PlaneMesh = primitives::Mesh<Mat4X4, ViewAngles, MeshTrait, primitives::Vertex<>,
std::array<Vector3<float>, 4>, std::array<Vector3<std::uint32_t>, 2>>;
} // namespace omath::unity_engine
17 changes: 17 additions & 0 deletions include/omath/engines/unreal_engine/primitives.hpp
Original file line number Diff line number Diff line change
@@ -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 <array>
namespace omath::unreal_engine
{
using BoxMesh = primitives::Mesh<Mat4X4, ViewAngles, MeshTrait, primitives::Vertex<>, std::array<Vector3<float>, 8>,
std::array<Vector3<std::uint32_t>, 12>>;

using PlaneMesh = primitives::Mesh<Mat4X4, ViewAngles, MeshTrait, primitives::Vertex<>,
std::array<Vector3<float>, 4>, std::array<Vector3<std::uint32_t>, 2>>;
} // namespace omath::unreal_engine
54 changes: 0 additions & 54 deletions source/3d_primitives/box.cpp

This file was deleted.

19 changes: 0 additions & 19 deletions source/3d_primitives/plane.cpp

This file was deleted.

12 changes: 12 additions & 0 deletions tests/general/unit_test_primitive_box.cpp
Original file line number Diff line number Diff line change
@@ -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 <gtest/gtest.h>

TEST(test, test)
{
auto result = omath::primitives::create_box<omath::opengl_engine::BoxMesh>({0.f, 30.f, 0.f}, {}, omath::opengl_engine::k_abs_forward,
omath::opengl_engine::k_abs_right);
}