diff options
-rw-r--r-- | draw/wireframe-box.cpp | 53 | ||||
-rw-r--r-- | draw/wireframe-box.hpp | 27 | ||||
-rw-r--r-- | draw/wireframe-mesh.cpp | 16 | ||||
-rw-r--r-- | draw/wireframe-mesh.hpp | 16 | ||||
-rw-r--r-- | draw/wireframe-quad.cpp | 2 | ||||
-rw-r--r-- | draw/wireframe-quad.hpp | 12 | ||||
-rw-r--r-- | main/app.hpp | 5 | ||||
-rw-r--r-- | main/debug.cpp | 3 | ||||
-rw-r--r-- | main/main.cpp | 26 |
9 files changed, 134 insertions, 26 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 diff --git a/main/app.hpp b/main/app.hpp index c691f85d..35fb9481 100644 --- a/main/app.hpp +++ b/main/app.hpp @@ -7,6 +7,7 @@ #include "draw/wall-mesh.hpp" #include "draw/wireframe-mesh.hpp" #include "draw/wireframe-quad.hpp" +#include "draw/wireframe-box.hpp" #include "compat/enum-bitset.hpp" #include <Magnum/Timeline.h> #include <Magnum/Platform/Sdl2Application.h> @@ -30,7 +31,8 @@ struct app final : Platform::Application void keyReleaseEvent(KeyEvent& event) override; void do_key(KeyEvent::Key k, KeyEvent::Modifiers m, bool pressed, bool repeated); void draw_chunk(chunk& c); - void draw_wireframe(); + void draw_wireframe_quad(); + void draw_wireframe_box(); void update_window_scale(Vector2i window_size); void viewportEvent(ViewportEvent& event) override; void debug_callback(GL::DebugOutput::Source src, GL::DebugOutput::Type type, UnsignedInt id, @@ -55,6 +57,7 @@ struct app final : Platform::Application floor_mesh _floor_mesh; wall_mesh _wall_mesh; wireframe_mesh<wireframe::quad> _wireframe_quad; + wireframe_mesh<wireframe::box> _wireframe_box; Vector2 camera_offset; enum_bitset<key> keys; diff --git a/main/debug.cpp b/main/debug.cpp index ae545678..f9b48958 100644 --- a/main/debug.cpp +++ b/main/debug.cpp @@ -18,11 +18,14 @@ void app::debug_callback(GL::DebugOutput::Source src, GL::DebugOutput::Type type static thread_local auto clock = std::chrono::steady_clock{}; static const auto t0 = clock.now(); +#if 0 [[maybe_unused]] volatile auto _type = type; [[maybe_unused]] volatile auto _id = id; [[maybe_unused]] volatile auto _src = src; [[maybe_unused]] volatile auto _severity = severity; [[maybe_unused]] volatile const char* _str = str.data(); +#endif + (void)src; (void)type; const char* p = str.c_str(); if (str.starts_with("Buffer detailed info: ")) diff --git a/main/main.cpp b/main/main.cpp index bbbf8d87..ec065523 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -41,7 +41,8 @@ void app::drawEvent() { } draw_chunk(_chunk); - draw_wireframe(); + draw_wireframe_quad(); + draw_wireframe_box(); swapBuffers(); redraw(); @@ -50,20 +51,37 @@ void app::drawEvent() { void app::draw_chunk(chunk& c) { + _shader.set_tint({1, 1, 1, 1}); _floor_mesh.draw(_shader, c); _wall_mesh.draw(_shader, c); } -void app::draw_wireframe() +void app::draw_wireframe_quad() { + constexpr float LINE_WIDTH = 1; + constexpr auto X = TILE_SIZE[0], Y = TILE_SIZE[1]; constexpr float N = TILE_MAX_DIM/2.f; - const Vector3 center {(float)(X*N), (float)(Y*N), 0}; + const Vector3 center {X*N, Y*N, 0}; _shader.set_tint({1, 0, 0, 1}); - _wireframe_quad.draw(_shader, {center, {TILE_SIZE[0], TILE_SIZE[1]}}); + _wireframe_quad.draw(_shader, {center, {TILE_SIZE[0], TILE_SIZE[1]}, LINE_WIDTH}); _shader.set_tint({1, 1, 1, 1}); } +void app::draw_wireframe_box() +{ + constexpr float LINE_WIDTH = 1; + + constexpr auto X = TILE_SIZE[0], Y = TILE_SIZE[1]; + constexpr float N = TILE_MAX_DIM/2.f; + constexpr Vector3 size{TILE_SIZE[0], TILE_SIZE[1], TILE_SIZE[2]}; + const Vector3 center1{X*(N+3), Y*(N+2), 0}, + center2{X*(N-2), Y*(N-4), 0}; + _shader.set_tint({0, 1, 0, 1}); + _wireframe_box.draw(_shader, {center1, size, LINE_WIDTH}); + _wireframe_box.draw(_shader, {center2, size, LINE_WIDTH}); +} + } // namespace Magnum::Examples MAGNUM_APPLICATION_MAIN(Magnum::Examples::app) |