summary refs log tree commit diff
path: root/src/GFX
diff options
context:
space:
mode:
Diffstat (limited to 'src/GFX')
-rw-r--r--src/GFX/Util/MeshBuilder.hpp4
-rw-r--r--src/GFX/Util/Primitives.cpp26
-rw-r--r--src/GFX/Util/Primitives.hpp14
3 files changed, 38 insertions, 6 deletions
diff --git a/src/GFX/Util/MeshBuilder.hpp b/src/GFX/Util/MeshBuilder.hpp
index cab06e1..eec77dd 100644
--- a/src/GFX/Util/MeshBuilder.hpp
+++ b/src/GFX/Util/MeshBuilder.hpp
@@ -55,8 +55,8 @@ public:
         m_indices.insert(m_indices.end(), from.begin(), from.end());
     }
 
-    template<uint PN>
-    void primitive(const Primitives::Primitive<PN>& primitive) {
+    template<uint PN, uint PI>
+    void primitive(const Primitives::BasePrimitive<PN, PI>& primitive) {
         decltype(primitive.indices) relativized_indices{};
         for (Int i = 0; i < primitive.indices.size(); i++) {
             relativized_indices[i] = primitive.indices[i] + m_positions.size();
diff --git a/src/GFX/Util/Primitives.cpp b/src/GFX/Util/Primitives.cpp
index 4b44864..daceb79 100644
--- a/src/GFX/Util/Primitives.cpp
+++ b/src/GFX/Util/Primitives.cpp
@@ -1,4 +1,5 @@
 #include "Primitives.hpp"
+#include "../../Math/Grid.hpp"
 
 namespace MC::GFX::Util::Primitives {
 
@@ -104,4 +105,27 @@ BoxPrimitive box(AABB aabb, FaceSet faces) {
     return box;
 }
 
-}
\ No newline at end of file
+LineBoxPrimitive line_box(AABB aabb) {
+    auto [min, max] = aabb;
+
+    auto cube = Math::cube_cell_from_point(min, max - min);
+
+    using P = Vector<3, F32>;
+    decltype(LineBoxPrimitive::positions) positions = {{
+        P(cube.front_top_left()), P(cube.front_top_right()),
+        P(cube.front_bottom_left()), P(cube.front_bottom_right()),
+        P(cube.back_top_left()), P(cube.back_top_right()),
+        P(cube.back_bottom_left()), P(cube.back_bottom_right()),
+    }};
+
+    decltype(LineBoxPrimitive::indices) indices = {{
+        0, 1, 1, 3, 3, 2, 2, 0, // Front
+        4, 5, 5, 7, 7, 6, 6, 4, // Back
+        0, 4, 1, 5, 2, 6, 3, 7, // Sides
+    }};
+
+    // TODO: Allow Primitives to not have normals.
+    return {positions, {{}}, indices};
+}
+
+}
diff --git a/src/GFX/Util/Primitives.hpp b/src/GFX/Util/Primitives.hpp
index a267130..6654da6 100644
--- a/src/GFX/Util/Primitives.hpp
+++ b/src/GFX/Util/Primitives.hpp
@@ -31,17 +31,25 @@ private:
     Value m_set;
 };
 
-template <uint N>
-struct Primitive {
+// A base primitive, could represent lines, triangles, quads primitives, etc.
+template <uint N, uint I>
+struct BasePrimitive {
     std::array<Vector<3, F32>, N> positions;
     std::array<Vector<3, F32>, N> normals;
-    std::array<U32, N + N / 2> indices;
+    std::array<U32, I> indices;
 };
 
+// Primitive made out of triangles (GL_TRIANGLES)
+template <uint N>
+using Primitive = BasePrimitive<N, N + N / 2>;
+
 using PlanePrimitive = Primitive<4>;
 PlanePrimitive plane(AABB aabb, FaceSet face);
 
 using BoxPrimitive = Primitive<4 * 6>;
 BoxPrimitive box(AABB aabb, FaceSet faces = FaceSet::all());
 
+using LineBoxPrimitive = BasePrimitive<4 * 2, 4 * 6>;
+LineBoxPrimitive line_box(AABB aabb);
+
 }