mirror of
https://github.com/orange-cpp/omath.git
synced 2026-02-13 07:03:25 +00:00
added files
This commit is contained in:
@@ -3,14 +3,64 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
#pragma once
|
#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/triangle.hpp"
|
||||||
#include "omath/linear_algebra/vector3.hpp"
|
#include "omath/linear_algebra/vector3.hpp"
|
||||||
#include <array>
|
#include <array>
|
||||||
|
|
||||||
namespace omath::primitives
|
namespace omath::primitives
|
||||||
{
|
{
|
||||||
|
using BoxMesh = Mesh<opengl_engine::Mat4X4, opengl_engine::ViewAngles, opengl_engine::MeshTrait, Vertex<>,
|
||||||
|
std::array<Vertex<>, 8>, std::array<Vector3<std::uint32_t>, 12>>;
|
||||||
|
|
||||||
|
template<class BoxMeshType = BoxMesh>
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
std::array<Triangle<Vector3<float>>, 12> create_box(const Vector3<float>& top, const Vector3<float>& bottom,
|
BoxMeshType
|
||||||
const Vector3<float>& dir_forward, const Vector3<float>& dir_right,
|
create_box(const Vector3<float>& top, const Vector3<float>& bottom, const Vector3<float>& dir_forward,
|
||||||
float ratio = 4.f) noexcept;
|
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
|
||||||
|
|||||||
@@ -25,7 +25,8 @@ namespace omath::primitives
|
|||||||
template<typename T> concept HasNormal = requires(T vertex) { vertex.normal; };
|
template<typename T> concept HasNormal = requires(T vertex) { vertex.normal; };
|
||||||
template<typename T> concept HasUv = requires(T vertex) { vertex.uv; };
|
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
|
class Mesh final
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@@ -33,8 +34,8 @@ namespace omath::primitives
|
|||||||
using VertexType = VertType;
|
using VertexType = VertType;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
using Vbo = std::vector<VertexType>;
|
using Vbo = VboType;
|
||||||
using Ebo = std::vector<Vector3<std::uint32_t>>;
|
using Ebo = EboType;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Vbo m_vertex_buffer;
|
Vbo m_vertex_buffer;
|
||||||
@@ -102,7 +103,9 @@ namespace omath::primitives
|
|||||||
VectorType vertex_position_to_world_space(const Vector3<float>& vertex_position) const
|
VectorType vertex_position_to_world_space(const Vector3<float>& vertex_position) const
|
||||||
requires HasPosition<VertexType>
|
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)};
|
return {abs_vec.at(0, 0), abs_vec.at(1, 0), abs_vec.at(2, 0)};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,50 +5,5 @@
|
|||||||
|
|
||||||
namespace omath::primitives
|
namespace omath::primitives
|
||||||
{
|
{
|
||||||
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, 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<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<Triangle<Vector3<float>>, 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
|
} // namespace omath::primitives
|
||||||
|
|||||||
11
tests/general/unit_test_primitive_box.cpp
Normal file
11
tests/general/unit_test_primitive_box.cpp
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
//
|
||||||
|
// Created by Vladislav on 11.01.2026.
|
||||||
|
//
|
||||||
|
#include "omath/3d_primitives/box.hpp"
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user