summaryrefslogtreecommitdiffhomepage
path: root/draw
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2022-10-09 07:09:23 +0200
committerStanislaw Halik <sthalik@misaki.pl>2022-10-09 07:09:23 +0200
commit5b97a6de06e7c44e7960159dc98c54860170203b (patch)
treed251705c2db0f7c9f53127e7dcc3e8c7d3737afa /draw
parent1f1951218e715e12eaf5d8cd338b6e5459872acb (diff)
a
Diffstat (limited to 'draw')
-rw-r--r--draw/wireframe-box.cpp53
-rw-r--r--draw/wireframe-box.hpp27
-rw-r--r--draw/wireframe-mesh.cpp16
-rw-r--r--draw/wireframe-mesh.hpp16
-rw-r--r--draw/wireframe-quad.cpp2
-rw-r--r--draw/wireframe-quad.hpp12
6 files changed, 105 insertions, 21 deletions
diff --git a/draw/wireframe-box.cpp b/draw/wireframe-box.cpp
new file mode 100644
index 00000000..e03c8ca2
--- /dev/null
+++ b/draw/wireframe-box.cpp
@@ -0,0 +1,53 @@
+#include "wireframe-box.hpp"
+#include <array>
+#include <Magnum/Math/Vector3.h>
+#include <Magnum/GL/Renderer.h>
+
+namespace Magnum::Examples::wireframe {
+
+box::box(Vector3 center, Vector3 size, float line_width) :
+ center{center}, size{size}, line_width{line_width}
+{}
+
+box::vertex_array box::make_vertex_array() const
+{
+ const auto Sx = size[0]*.5f, Sy = size[1]*.5f, Sz = size[2];
+ const auto Cx_0 = center[0] - Sx, Cx_1 = center[0] + Sx;
+ const auto Cy_0 = center[1] - Sy, Cy_1 = center[1] + Sy;
+ const auto Cz_0 = center[2] + 0, Cz_1 = center[2] + Sz;
+ return {{
+ {Cx_0, Cy_0, Cz_0}, // (0) left front bottom
+ {Cx_1, Cy_0, Cz_0}, // (1) right front bottom
+ {Cx_0, Cy_1, Cz_0}, // (2) left back bottom
+ {Cx_1, Cy_1, Cz_0}, // (3) right back bottom
+ {Cx_0, Cy_0, Cz_1}, // (4) left front top
+ {Cx_1, Cy_0, Cz_1}, // (5) right front top
+ {Cx_0, Cy_1, Cz_1}, // (6) left back top
+ {Cx_1, Cy_1, Cz_1}, // (7) right back top
+ }};
+}
+
+void box::on_draw() const
+{
+ GL::Renderer::setLineWidth(line_width);
+}
+
+box::index_array box::make_index_array()
+{
+ return {{
+ 0, 1,
+ 0, 2,
+ 0, 4,
+ 1, 3,
+ 1, 5,
+ 2, 3,
+ 2, 6,
+ 3, 7,
+ 4, 5,
+ 4, 6,
+ 5, 7,
+ 6, 7,
+ }};
+}
+
+} // namespace Magnum::Examples::wireframe
diff --git a/draw/wireframe-box.hpp b/draw/wireframe-box.hpp
new file mode 100644
index 00000000..b5cc1688
--- /dev/null
+++ b/draw/wireframe-box.hpp
@@ -0,0 +1,27 @@
+#pragma once
+#include <Magnum/Math/Vector3.h>
+#include <Magnum/GL/Mesh.h>
+
+namespace Magnum::Examples::wireframe {
+
+struct box final
+{
+ box(Vector3 center, Vector3 size, float line_width);
+
+ static constexpr std::size_t num_vertices = 8, num_indexes = 12*2;
+ static constexpr GL::MeshPrimitive primitive = GL::MeshPrimitive::Lines;
+
+ using vertex_array = std::array<Vector3, num_vertices>;
+ using index_array = std::array<UnsignedShort, num_indexes>;
+
+ vertex_array make_vertex_array() const;
+ static index_array make_index_array();
+ void on_draw() const;
+
+private:
+ Vector3 center;
+ Vector3 size;
+ float line_width = 2;
+};
+
+} // namespace Magnum::Examples::wireframe
diff --git a/draw/wireframe-mesh.cpp b/draw/wireframe-mesh.cpp
index 1425df60..35103055 100644
--- a/draw/wireframe-mesh.cpp
+++ b/draw/wireframe-mesh.cpp
@@ -1,7 +1,7 @@
#include "wireframe-mesh.hpp"
#include "shaders/tile-shader.hpp"
+#include <Corrade/Containers/ArrayViewStl.h>
#include <Corrade/Containers/Array.h>
-#include <Magnum/GL/Renderer.h>
#include <Magnum/GL/TextureFormat.h>
#include <Magnum/ImageFlags.h>
#include <Magnum/ImageView.h>
@@ -15,8 +15,7 @@ namespace Magnum::Examples::wireframe
GL::RectangleTexture mesh_base::make_constant_texture()
{
const Vector4ub data[] = { {255, 255, 255, 255} };
- Trade::ImageData2D img{PixelStorage{}.setImageHeight(1).setRowLength(1).setAlignment(1),
- PixelFormat::RGBA8Unorm, {1, 1}, {},
+ Trade::ImageData2D img{PixelFormat::RGBA8Unorm, {1, 1}, {},
Containers::arrayView(data, 1), {}, {}};
GL::RectangleTexture tex;
tex.setWrapping(GL::SamplerWrapping::ClampToEdge)
@@ -28,13 +27,18 @@ GL::RectangleTexture mesh_base::make_constant_texture()
return tex;
}
-mesh_base::mesh_base(GL::MeshPrimitive primitive, std::size_t num_vertices) :
- _texcoords_buffer{std::vector<Vector2>{num_vertices}}
+mesh_base::mesh_base(GL::MeshPrimitive primitive, Containers::ArrayView<const void> index_data,
+ std::size_t num_vertices, std::size_t num_indexes) :
+ _vertex_buffer{Containers::Array<Vector3>{ValueInit, num_vertices}},
+ _texcoords_buffer{Containers::Array<Vector2>{ValueInit, num_vertices}},
+ _index_buffer{num_indexes == 0 ? GL::Buffer{NoCreate} : GL::Buffer{index_data}}
{
- _mesh.setCount((int)num_vertices)
+ _mesh.setCount((int)(num_indexes > 0 ? num_indexes : num_vertices))
.setPrimitive(primitive)
.addVertexBuffer(_vertex_buffer, 0, tile_shader::Position{})
.addVertexBuffer(_texcoords_buffer, 0, tile_shader::TextureCoordinates{});
+ if (num_indexes > 0)
+ _mesh.setIndexBuffer(_index_buffer, 0, GL::MeshIndexType::UnsignedShort);
}
void mesh_base::draw(tile_shader& shader)
diff --git a/draw/wireframe-mesh.hpp b/draw/wireframe-mesh.hpp
index 0ae122f5..016a92af 100644
--- a/draw/wireframe-mesh.hpp
+++ b/draw/wireframe-mesh.hpp
@@ -3,7 +3,7 @@
#include "tile-defs.hpp"
#include <array>
#include <utility>
-#include <Corrade/Containers/ArrayViewStl.h>
+#include <Corrade/Containers/ArrayView.h>
#include <Magnum/Math/Vector2.h>
#include <Magnum/GL/Buffer.h>
#include <Magnum/GL/Mesh.h>
@@ -19,19 +19,22 @@ namespace wireframe
template<typename T>
concept traits = requires (const T& x) {
{T::num_vertices} -> std::convertible_to<std::size_t>;
+ {T::num_indexes} -> std::convertible_to<std::size_t>;
{x.primitive} -> std::convertible_to<GL::MeshPrimitive>;
{x.make_vertex_array() } -> std::convertible_to<Containers::ArrayView<const void>>;
+ {T::make_index_array() } -> std::convertible_to<Containers::ArrayView<const void>>;
{x.on_draw()} -> std::same_as<void>;
};
struct mesh_base
{
static GL::RectangleTexture make_constant_texture();
- GL::Buffer _vertex_buffer, _texcoords_buffer;
+ GL::Buffer _vertex_buffer, _texcoords_buffer, _index_buffer;
GL::RectangleTexture _texture = make_constant_texture();
GL::Mesh _mesh;
- mesh_base(GL::MeshPrimitive primitive, std::size_t num_vertices);
+ mesh_base(GL::MeshPrimitive primitive, Containers::ArrayView<const void> index_data,
+ std::size_t num_vertices, std::size_t num_indexes);
void draw(tile_shader& shader);
};
@@ -46,12 +49,13 @@ struct wireframe_mesh final : private wireframe::mesh_base
template<wireframe::traits T>
wireframe_mesh<T>::wireframe_mesh() :
- wireframe::mesh_base{T::primitive, T::num_vertices}
-{}
+ wireframe::mesh_base{T::primitive, T::make_index_array(), T::num_vertices, T::num_indexes}
+{
+}
template <wireframe::traits T> void wireframe_mesh<T>::draw(tile_shader& shader, T x)
{
- _vertex_buffer.setData(x.make_vertex_array(), GL::BufferUsage::DynamicDraw);
+ _vertex_buffer.setSubData(0, x.make_vertex_array());
mesh_base::draw(shader);
}
diff --git a/draw/wireframe-quad.cpp b/draw/wireframe-quad.cpp
index 18b49628..e167a074 100644
--- a/draw/wireframe-quad.cpp
+++ b/draw/wireframe-quad.cpp
@@ -15,7 +15,7 @@ quad::vertex_array quad::make_vertex_array() const
}};
}
-quad::quad(Vector3 center, Vector2 size) : center(center), size(size) {}
+quad::quad(Vector3 center, Vector2 size, float line_width) : center(center), size(size), line_width{line_width} {}
void quad::on_draw() const
{
diff --git a/draw/wireframe-quad.hpp b/draw/wireframe-quad.hpp
index 062bf913..28ab651a 100644
--- a/draw/wireframe-quad.hpp
+++ b/draw/wireframe-quad.hpp
@@ -6,27 +6,23 @@
namespace Magnum::Examples::wireframe {
-template<typename T>
-struct wireframe_mesh;
-
struct quad final
{
- quad(Vector3 center, Vector2 size);
+ quad(Vector3 center, Vector2 size, float line_width);
- static constexpr std::size_t num_vertices = 4;
+ static constexpr std::size_t num_vertices = 4, num_indexes = 0;
static constexpr GL::MeshPrimitive primitive = GL::MeshPrimitive::LineLoop;
using vertex_array = std::array<Vector3, num_vertices>;
+ static Containers::ArrayView<const void> make_index_array() { return {}; }
vertex_array make_vertex_array() const;
void on_draw() const;
private:
Vector3 center;
Vector2 size;
- float line_width = 2;
+ float line_width;
};
-
-
} // namespace Magnum::Examples::wireframe