summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2022-10-09 02:21:01 +0200
committerStanislaw Halik <sthalik@misaki.pl>2022-10-09 02:21:01 +0200
commit542bc1c103f129c2f648dbe77affb485cfdf93ab (patch)
tree86c11f4e4f55de619ce1a9026988479caaefe583 /src
parentd5212f948304b575b494b2065f14e934aaa426e1 (diff)
a
Diffstat (limited to 'src')
-rw-r--r--src/wireframe-mesh.cpp29
-rw-r--r--src/wireframe-mesh.hpp17
2 files changed, 31 insertions, 15 deletions
diff --git a/src/wireframe-mesh.cpp b/src/wireframe-mesh.cpp
index 14af4a15..1332c682 100644
--- a/src/wireframe-mesh.cpp
+++ b/src/wireframe-mesh.cpp
@@ -1,13 +1,14 @@
#include "wireframe-mesh.hpp"
#include "shaders/tile-shader.hpp"
+#include "tile-atlas.hpp"
#include <Corrade/Containers/Array.h>
-#include <Magnum/ImageView.h>
#include <Magnum/GL/Renderer.h>
#include <Magnum/GL/TextureFormat.h>
+#include <Magnum/ImageFlags.h>
+#include <Magnum/ImageView.h>
#include <Magnum/PixelFormat.h>
#include <Magnum/PixelStorage.h>
#include <Magnum/Trade/ImageData.h>
-#include <Magnum/ImageFlags.h>
namespace Magnum::Examples::wireframe
{
@@ -30,15 +31,23 @@ GL::RectangleTexture wireframe::null::make_constant_texture()
quad::vertex_array quad::make_vertex_array() const
{
+#if 0
constexpr auto X = TILE_SIZE[0], Y = TILE_SIZE[1];
constexpr float Z = 0;
- vertex_array ret = {{
+ return {{
{ -X + center[0], -Y + center[1], Z + center[2] },
{ X + center[0], -Y + center[1], Z + center[2] },
{ X + center[0], Y + center[1], Z + center[2] },
{ -X + center[0], Y + center[1], Z + center[2] },
}};
- return ret;
+#else
+ return tile_atlas::floor_quad(center, {TILE_SIZE[0], TILE_SIZE[1]});
+#endif
+}
+
+quad::index_array quad::make_index_array()
+{
+ return tile_atlas::indices(0);
}
quad::quad(Vector3 center, Vector2 size) : center(center), size(size) {}
@@ -49,18 +58,18 @@ namespace Magnum::Examples {
template <wireframe::traits T> wireframe_mesh<T>::wireframe_mesh()
{
- _mesh.setCount((int)T::num_vertices)
- .setPrimitive(T::primitive)
+ _mesh.setCount((int)T::num_indexes)
.addVertexBuffer(_texcoords_buffer, 0, tile_shader::TextureCoordinates{})
- .addVertexBuffer(_vertex_buffer, 0, tile_shader::Position{});
- CORRADE_INTERNAL_ASSERT(!_mesh.isIndexed());
+ .addVertexBuffer(_vertex_buffer, 0, tile_shader::Position{})
+ .setIndexBuffer(_index_buffer, 0, GL::MeshIndexType::UnsignedShort);
}
template <wireframe::traits T> void wireframe_mesh<T>::draw(tile_shader& shader, T x)
{
GL::Renderer::setLineWidth(2);
- _vertex_buffer.setData(x.make_vertex_array(), GL::BufferUsage::DynamicDraw);
- //_texture.bind(0);
+ _vertex_buffer.setSubData(0, x.make_vertex_array());
+ _index_buffer.setSubData(0, x.make_index_array());
+ _texture.bind(0);
shader.draw(_mesh);
}
diff --git a/src/wireframe-mesh.hpp b/src/wireframe-mesh.hpp
index 7a6892c2..2a7cf10c 100644
--- a/src/wireframe-mesh.hpp
+++ b/src/wireframe-mesh.hpp
@@ -1,6 +1,5 @@
#pragma once
-#include "tile-atlas.hpp"
#include "tile-defs.hpp"
#include <array>
#include <utility>
@@ -20,16 +19,19 @@ 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::same_as<std::array<Vector3, T::num_vertices>>;
+ {x.make_index_array() } -> std::same_as<std::array<UnsignedShort, T::num_indexes>>;
};
struct null final
{
static constexpr auto primitive = GL::MeshPrimitive::Triangles;
- static constexpr std::size_t num_vertices = 0;
+ static constexpr std::size_t num_vertices = 0, num_indexes = 0;
static GL::RectangleTexture make_constant_texture();
static std::array<Vector3, 0> make_vertex_array() { return {}; }
+ static std::array<UnsignedShort, 0> make_index_array() { return {}; }
};
struct quad final
@@ -37,11 +39,14 @@ struct quad final
quad(Vector3 center, Vector2 size);
constexpr quad() = default;
- static constexpr std::size_t num_vertices = 4;
- static constexpr GL::MeshPrimitive primitive = GL::MeshPrimitive::LineLoop;
+ static constexpr std::size_t num_vertices = 4, num_indexes = 6;
+ static constexpr GL::MeshPrimitive primitive = GL::MeshPrimitive::Triangles;
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() ;
private:
Vector3 center = {};
@@ -57,7 +62,9 @@ struct wireframe_mesh final
void draw(tile_shader& shader, T traits);
private:
- GL::Buffer _vertex_buffer{std::array<Vector3, T::num_vertices>{}}, _texcoords_buffer{std::array<Vector2, T::num_vertices>{}};
+ GL::Buffer _vertex_buffer{std::array<Vector3, T::num_vertices>{}, GL::BufferUsage::DynamicDraw},
+ _texcoords_buffer{std::array<Vector2, T::num_vertices>{}, GL::BufferUsage::DynamicDraw},
+ _index_buffer{std::array<UnsignedShort , T::num_indexes>{}, GL::BufferUsage::DynamicDraw};
GL::RectangleTexture _texture = wireframe::null::make_constant_texture();
GL::Mesh _mesh;
};