From f1df7c20129a1f1cc47c47e5731efd10f8e49aeb Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Tue, 25 Oct 2022 16:37:58 +0200 Subject: rename in draw/ --- draw/box.cpp | 53 ++++++++++++++++++++++++++++ draw/box.hpp | 29 +++++++++++++++ draw/floor-mesh.cpp | 87 --------------------------------------------- draw/floor-mesh.hpp | 39 -------------------- draw/floor.cpp | 87 +++++++++++++++++++++++++++++++++++++++++++++ draw/floor.hpp | 39 ++++++++++++++++++++ draw/quad.cpp | 27 ++++++++++++++ draw/quad.hpp | 30 ++++++++++++++++ draw/wall-mesh.cpp | 94 ------------------------------------------------- draw/wall-mesh.hpp | 43 ---------------------- draw/wall.cpp | 94 +++++++++++++++++++++++++++++++++++++++++++++++++ draw/wall.hpp | 43 ++++++++++++++++++++++ draw/wireframe-box.cpp | 53 ---------------------------- draw/wireframe-box.hpp | 29 --------------- draw/wireframe-mesh.cpp | 55 ----------------------------- draw/wireframe-mesh.hpp | 61 -------------------------------- draw/wireframe-quad.cpp | 27 -------------- draw/wireframe-quad.hpp | 30 ---------------- draw/wireframe.cpp | 55 +++++++++++++++++++++++++++++ draw/wireframe.hpp | 61 ++++++++++++++++++++++++++++++++ editor/app.hpp | 6 ++-- main/main-impl.hpp | 4 +-- 22 files changed, 523 insertions(+), 523 deletions(-) create mode 100644 draw/box.cpp create mode 100644 draw/box.hpp delete mode 100644 draw/floor-mesh.cpp delete mode 100644 draw/floor-mesh.hpp create mode 100644 draw/floor.cpp create mode 100644 draw/floor.hpp create mode 100644 draw/quad.cpp create mode 100644 draw/quad.hpp delete mode 100644 draw/wall-mesh.cpp delete mode 100644 draw/wall-mesh.hpp create mode 100644 draw/wall.cpp create mode 100644 draw/wall.hpp delete mode 100644 draw/wireframe-box.cpp delete mode 100644 draw/wireframe-box.hpp delete mode 100644 draw/wireframe-mesh.cpp delete mode 100644 draw/wireframe-mesh.hpp delete mode 100644 draw/wireframe-quad.cpp delete mode 100644 draw/wireframe-quad.hpp create mode 100644 draw/wireframe.cpp create mode 100644 draw/wireframe.hpp diff --git a/draw/box.cpp b/draw/box.cpp new file mode 100644 index 00000000..18d6f861 --- /dev/null +++ b/draw/box.cpp @@ -0,0 +1,53 @@ +#include "box.hpp" +#include +#include +#include + +namespace floormat::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) front left bottom + {Cx_1, Cy_0, Cz_0}, // (1) front right bottom + {Cx_0, Cy_1, Cz_0}, // (2) back left bottom + {Cx_1, Cy_1, Cz_0}, // (3) back right bottom + {Cx_0, Cy_0, Cz_1}, // (4) front left top + {Cx_1, Cy_0, Cz_1}, // (5) front right top + {Cx_0, Cy_1, Cz_1}, // (6) back left top + {Cx_1, Cy_1, Cz_1}, // (7) back right 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 floormat::wireframe diff --git a/draw/box.hpp b/draw/box.hpp new file mode 100644 index 00000000..b187dc83 --- /dev/null +++ b/draw/box.hpp @@ -0,0 +1,29 @@ +#pragma once +#include +#include +#include +#include + +namespace floormat::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; + using index_array = std::array; + + 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 floormat::wireframe diff --git a/draw/floor-mesh.cpp b/draw/floor-mesh.cpp deleted file mode 100644 index d7e45e71..00000000 --- a/draw/floor-mesh.cpp +++ /dev/null @@ -1,87 +0,0 @@ -#include "floor-mesh.hpp" -#include "shaders/tile-shader.hpp" -#include "tile.hpp" -#include "chunk.hpp" -#include "tile-atlas.hpp" -#include - -namespace floormat { - -constexpr auto quad_index_count = 6; - -floor_mesh::floor_mesh() -{ - _mesh.setCount((int)(quad_index_count * TILE_COUNT)) - .addVertexBuffer(_positions_buffer, 0, tile_shader::Position{}) - .addVertexBuffer(_vertex_buffer, 0, tile_shader::TextureCoordinates{}) - .setIndexBuffer(_index_buffer, 0, GL::MeshIndexType::UnsignedShort); -} - -void floor_mesh::set_tile(quad_data& data, tile& x) -{ - if (x.ground_image) - { - auto texcoords = x.ground_image.atlas->texcoords_for_id(x.ground_image.variant); - for (size_t i = 0; i < 4; i++) - data[i] = { texcoords[i] }; - } - else - for (size_t i = 0; i < 4; i++) - data[i] = {}; -} - -void floor_mesh::draw(tile_shader& shader, chunk& c) -{ - //_vertex_buffer.setData({nullptr, sizeof(quad_data) * TILE_COUNT}, Magnum::GL::BufferUsage::DynamicDraw); // orphan the buffer - std::array data; - for (auto& [x, idx, pt] : c) { - set_tile(data[idx], x); - } - _vertex_buffer.setSubData(0, data); - Magnum::GL::MeshView mesh{_mesh}; - - tile_atlas* last_atlas = nullptr; - std::size_t last_pos = 0; - - const auto do_draw = [&](std::size_t i, tile_atlas* atlas) { - if (atlas == last_atlas) - return; - if (auto len = i - last_pos; last_atlas != nullptr && len > 0) - { - last_atlas->texture().bind(0); - mesh.setCount((int)(quad_index_count * len)); - mesh.setIndexRange((int)(last_pos*quad_index_count), 0, quad_index_count*TILE_COUNT - 1); - shader.draw(mesh); - } - last_atlas = atlas; - last_pos = i; - }; - - for (auto& [x, i, pt] : c) - do_draw(i, x.ground_image.atlas.get()); - do_draw(TILE_COUNT, nullptr); -} - -std::array, TILE_COUNT> floor_mesh::make_index_array() -{ - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init) - std::array, TILE_COUNT> array; - for (std::size_t i = 0; i < std::size(array); i++) - array[i] = tile_atlas::indices(i); - return array; -} - -std::array, TILE_COUNT> floor_mesh::make_position_array() -{ - std::array, TILE_COUNT> array; - constexpr float X = TILE_SIZE[0], Y = TILE_SIZE[1]; - for (std::size_t j = 0, k = 0; j < TILE_MAX_DIM; j++) - for (std::size_t i = 0; i < TILE_MAX_DIM; i++, k++) - { - Vector3 center {(float)(X*i), (float)(Y*j), 0}; - array[k] = { tile_atlas::floor_quad(center, {X, Y}) }; - } - return array; -} - -} // namespace floormat diff --git a/draw/floor-mesh.hpp b/draw/floor-mesh.hpp deleted file mode 100644 index f81a7593..00000000 --- a/draw/floor-mesh.hpp +++ /dev/null @@ -1,39 +0,0 @@ -#pragma once -#include "tile-defs.hpp" -#include -#include -#include -#include -#include -#include -#include - -namespace floormat { - -struct tile_shader; -struct chunk; -struct tile; - -struct floor_mesh final -{ - floor_mesh(); - floor_mesh(floor_mesh&&) = delete; - floor_mesh(const floor_mesh&) = delete; - - void draw(tile_shader& shader, chunk& c); - -private: - struct vertex_data final { Vector2 texcoords; }; - using quad_data = std::array; - - static std::array, TILE_COUNT> make_index_array(); - static std::array, TILE_COUNT> make_position_array(); - - GL::Mesh _mesh; - GL::Buffer _vertex_buffer{std::array{}, Magnum::GL::BufferUsage::DynamicDraw}, - _index_buffer{make_index_array()}, _positions_buffer{make_position_array()}; - - static void set_tile(quad_data& data, tile& x); -}; - -} // namespace floormat diff --git a/draw/floor.cpp b/draw/floor.cpp new file mode 100644 index 00000000..3813cf56 --- /dev/null +++ b/draw/floor.cpp @@ -0,0 +1,87 @@ +#include "floor.hpp" +#include "shaders/tile-shader.hpp" +#include "tile.hpp" +#include "chunk.hpp" +#include "tile-atlas.hpp" +#include + +namespace floormat { + +constexpr auto quad_index_count = 6; + +floor_mesh::floor_mesh() +{ + _mesh.setCount((int)(quad_index_count * TILE_COUNT)) + .addVertexBuffer(_positions_buffer, 0, tile_shader::Position{}) + .addVertexBuffer(_vertex_buffer, 0, tile_shader::TextureCoordinates{}) + .setIndexBuffer(_index_buffer, 0, GL::MeshIndexType::UnsignedShort); +} + +void floor_mesh::set_tile(quad_data& data, tile& x) +{ + if (x.ground_image) + { + auto texcoords = x.ground_image.atlas->texcoords_for_id(x.ground_image.variant); + for (size_t i = 0; i < 4; i++) + data[i] = { texcoords[i] }; + } + else + for (size_t i = 0; i < 4; i++) + data[i] = {}; +} + +void floor_mesh::draw(tile_shader& shader, chunk& c) +{ + //_vertex_buffer.setData({nullptr, sizeof(quad_data) * TILE_COUNT}, Magnum::GL::BufferUsage::DynamicDraw); // orphan the buffer + std::array data; + for (auto& [x, idx, pt] : c) { + set_tile(data[idx], x); + } + _vertex_buffer.setSubData(0, data); + Magnum::GL::MeshView mesh{_mesh}; + + tile_atlas* last_atlas = nullptr; + std::size_t last_pos = 0; + + const auto do_draw = [&](std::size_t i, tile_atlas* atlas) { + if (atlas == last_atlas) + return; + if (auto len = i - last_pos; last_atlas != nullptr && len > 0) + { + last_atlas->texture().bind(0); + mesh.setCount((int)(quad_index_count * len)); + mesh.setIndexRange((int)(last_pos*quad_index_count), 0, quad_index_count*TILE_COUNT - 1); + shader.draw(mesh); + } + last_atlas = atlas; + last_pos = i; + }; + + for (auto& [x, i, pt] : c) + do_draw(i, x.ground_image.atlas.get()); + do_draw(TILE_COUNT, nullptr); +} + +std::array, TILE_COUNT> floor_mesh::make_index_array() +{ + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init) + std::array, TILE_COUNT> array; + for (std::size_t i = 0; i < std::size(array); i++) + array[i] = tile_atlas::indices(i); + return array; +} + +std::array, TILE_COUNT> floor_mesh::make_position_array() +{ + std::array, TILE_COUNT> array; + constexpr float X = TILE_SIZE[0], Y = TILE_SIZE[1]; + for (std::size_t j = 0, k = 0; j < TILE_MAX_DIM; j++) + for (std::size_t i = 0; i < TILE_MAX_DIM; i++, k++) + { + Vector3 center {(float)(X*i), (float)(Y*j), 0}; + array[k] = { tile_atlas::floor_quad(center, {X, Y}) }; + } + return array; +} + +} // namespace floormat diff --git a/draw/floor.hpp b/draw/floor.hpp new file mode 100644 index 00000000..f81a7593 --- /dev/null +++ b/draw/floor.hpp @@ -0,0 +1,39 @@ +#pragma once +#include "tile-defs.hpp" +#include +#include +#include +#include +#include +#include +#include + +namespace floormat { + +struct tile_shader; +struct chunk; +struct tile; + +struct floor_mesh final +{ + floor_mesh(); + floor_mesh(floor_mesh&&) = delete; + floor_mesh(const floor_mesh&) = delete; + + void draw(tile_shader& shader, chunk& c); + +private: + struct vertex_data final { Vector2 texcoords; }; + using quad_data = std::array; + + static std::array, TILE_COUNT> make_index_array(); + static std::array, TILE_COUNT> make_position_array(); + + GL::Mesh _mesh; + GL::Buffer _vertex_buffer{std::array{}, Magnum::GL::BufferUsage::DynamicDraw}, + _index_buffer{make_index_array()}, _positions_buffer{make_position_array()}; + + static void set_tile(quad_data& data, tile& x); +}; + +} // namespace floormat diff --git a/draw/quad.cpp b/draw/quad.cpp new file mode 100644 index 00000000..dee53a75 --- /dev/null +++ b/draw/quad.cpp @@ -0,0 +1,27 @@ +#include "quad.hpp" +#include +#include + +namespace floormat::wireframe { + +quad::vertex_array quad::make_vertex_array() const +{ + const float X = size[0]*.5f, Y = size[1]*.5f; + return {{ + { -X + center[0], -Y + center[1], center[2] }, + { X + center[0], -Y + center[1], center[2] }, + { X + center[0], Y + center[1], center[2] }, + { -X + center[0], Y + center[1], center[2] }, + }}; +} + +quad::quad(Vector3 center, Vector2 size, float line_width) : + center(center), size(size), line_width{line_width} +{} + +void quad::on_draw() const +{ + GL::Renderer::setLineWidth(line_width); +} + +} // namespace floormat::wireframe diff --git a/draw/quad.hpp b/draw/quad.hpp new file mode 100644 index 00000000..050eeb78 --- /dev/null +++ b/draw/quad.hpp @@ -0,0 +1,30 @@ +#pragma once + +#include +#include +#include +#include +#include + +namespace floormat::wireframe { + +struct quad final +{ + quad(Vector3 center, Vector2 size, float line_width); + + static constexpr std::size_t num_vertices = 4, num_indexes = 0; + static constexpr GL::MeshPrimitive primitive = GL::MeshPrimitive::LineLoop; + + using vertex_array = std::array; + + static ArrayView make_index_array() { return {}; } + vertex_array make_vertex_array() const; + void on_draw() const; + +private: + Vector3 center; + Vector2 size; + float line_width; +}; + +} // namespace floormat::wireframe diff --git a/draw/wall-mesh.cpp b/draw/wall-mesh.cpp deleted file mode 100644 index dfbb739e..00000000 --- a/draw/wall-mesh.cpp +++ /dev/null @@ -1,94 +0,0 @@ -#include "wall-mesh.hpp" -#include "tile-atlas.hpp" -#include "shaders/tile-shader.hpp" -#include "chunk.hpp" -#include -#include - -namespace floormat { - -constexpr auto quad_index_count = 6; - -wall_mesh::wall_mesh() -{ - _mesh.setCount((int)(quad_index_count * COUNT)) - .addVertexBuffer(_vertex_buffer, 0, tile_shader::TextureCoordinates{}) - .addVertexBuffer(_positions_buffer, 0, tile_shader::Position{}) - .setIndexBuffer(_index_buffer, 0, GL::MeshIndexType::UnsignedShort); - CORRADE_INTERNAL_ASSERT(_mesh.isIndexed()); -} - -void wall_mesh::add_wall(vertex_array& data, texture_array& textures, tile_image& img, std::size_t pos) -{ - CORRADE_INTERNAL_ASSERT(pos < data.size()); - auto texcoords = img.atlas->texcoords_for_id(img.variant); - for (std::size_t i = 0; i < 4; i++) - { - data[pos][i] = { texcoords[i] }; - textures[pos] = &img.atlas->texture(); - } -} - -void wall_mesh::maybe_add_tile(vertex_array& data, texture_array& textures, tile& x, std::size_t pos) -{ - if (auto& wall = x.wall_north; wall.atlas) - add_wall(data, textures, wall, pos * 2 + 0); - if (auto& wall = x.wall_west; wall.atlas) - add_wall(data, textures, wall, pos * 2 + 1); -} - -void wall_mesh::draw(tile_shader& shader, chunk& c) -{ - //_vertex_buffer.setData({nullptr, sizeof(vertex_array)}, Magnum::GL::BufferUsage::DynamicDraw); // orphan the buffer - texture_array textures = {}; - { - vertex_array data; - for (auto& [x, idx, pt] : c) { - maybe_add_tile(data, textures, x, idx); - } - _vertex_buffer.setSubData(0, data); - } - - const GL::Texture2D* last_texture = nullptr; - Magnum::GL::MeshView mesh{_mesh}; - for (std::size_t i = 0; i < COUNT; i++) - { - auto* const tex = textures[i]; - if (!tex) - continue; - mesh.setCount(quad_index_count); - mesh.setIndexRange((int)(i*quad_index_count), 0, quad_index_count*COUNT - 1); - if (tex != last_texture) - tex->bind(0); - last_texture = tex; - shader.draw(mesh); - } -} - -std::array, wall_mesh::COUNT> wall_mesh::make_index_array() -{ - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init) - std::array, COUNT> array; - - for (std::size_t i = 0; i < std::size(array); i++) - array[i] = tile_atlas::indices(i); - return array; -} - -std::array, wall_mesh::COUNT> wall_mesh::make_position_array() -{ - std::array, COUNT> array; - constexpr float X = TILE_SIZE[0], Y = TILE_SIZE[1], Z = TILE_SIZE[2]; - constexpr Vector3 size = {X, Y, Z}; - for (std::size_t j = 0; j < TILE_MAX_DIM; j++) - for (std::size_t i = 0; i < TILE_MAX_DIM; i++) - { - const auto idx = (j*TILE_MAX_DIM + i) * 2; - Vector3 center{(float)(X*i), (float)(Y*j), 0}; - array[idx + 0] = tile_atlas::wall_quad_N(center, size); - array[idx + 1] = tile_atlas::wall_quad_W(center, size); - } - return array; -} - -} // namespace floormat diff --git a/draw/wall-mesh.hpp b/draw/wall-mesh.hpp deleted file mode 100644 index b021ef95..00000000 --- a/draw/wall-mesh.hpp +++ /dev/null @@ -1,43 +0,0 @@ -#pragma once - -#include "tile.hpp" -#include -#include -#include -#include -#include -#include - -namespace floormat { - -struct tile_shader; -struct chunk; - -struct wall_mesh final -{ - wall_mesh(); - void draw(tile_shader& shader, chunk& c); - -private: - static constexpr auto COUNT1 = TILE_MAX_DIM*2, COUNT = COUNT1 * COUNT1; - - struct vertex final { - Vector2 texcoords; - }; - - using quad = std::array; - using vertex_array = std::array; - using texture_array = std::array; - - static void maybe_add_tile(vertex_array& data, texture_array& textures, tile& x, std::size_t pos); - static void add_wall(vertex_array& data, texture_array& textures, tile_image& img, std::size_t pos); - - GL::Mesh _mesh; - GL::Buffer _vertex_buffer{vertex_array{}, Magnum::GL::BufferUsage::DynamicDraw}, - _index_buffer{make_index_array()}, - _positions_buffer{make_position_array()}; - static std::array, COUNT> make_index_array(); - static std::array, COUNT> make_position_array(); -}; - -} // namespace floormat diff --git a/draw/wall.cpp b/draw/wall.cpp new file mode 100644 index 00000000..51e09add --- /dev/null +++ b/draw/wall.cpp @@ -0,0 +1,94 @@ +#include "wall.hpp" +#include "tile-atlas.hpp" +#include "shaders/tile-shader.hpp" +#include "chunk.hpp" +#include +#include + +namespace floormat { + +constexpr auto quad_index_count = 6; + +wall_mesh::wall_mesh() +{ + _mesh.setCount((int)(quad_index_count * COUNT)) + .addVertexBuffer(_vertex_buffer, 0, tile_shader::TextureCoordinates{}) + .addVertexBuffer(_positions_buffer, 0, tile_shader::Position{}) + .setIndexBuffer(_index_buffer, 0, GL::MeshIndexType::UnsignedShort); + CORRADE_INTERNAL_ASSERT(_mesh.isIndexed()); +} + +void wall_mesh::add_wall(vertex_array& data, texture_array& textures, tile_image& img, std::size_t pos) +{ + CORRADE_INTERNAL_ASSERT(pos < data.size()); + auto texcoords = img.atlas->texcoords_for_id(img.variant); + for (std::size_t i = 0; i < 4; i++) + { + data[pos][i] = { texcoords[i] }; + textures[pos] = &img.atlas->texture(); + } +} + +void wall_mesh::maybe_add_tile(vertex_array& data, texture_array& textures, tile& x, std::size_t pos) +{ + if (auto& wall = x.wall_north; wall.atlas) + add_wall(data, textures, wall, pos * 2 + 0); + if (auto& wall = x.wall_west; wall.atlas) + add_wall(data, textures, wall, pos * 2 + 1); +} + +void wall_mesh::draw(tile_shader& shader, chunk& c) +{ + //_vertex_buffer.setData({nullptr, sizeof(vertex_array)}, Magnum::GL::BufferUsage::DynamicDraw); // orphan the buffer + texture_array textures = {}; + { + vertex_array data; + for (auto& [x, idx, pt] : c) { + maybe_add_tile(data, textures, x, idx); + } + _vertex_buffer.setSubData(0, data); + } + + const GL::Texture2D* last_texture = nullptr; + Magnum::GL::MeshView mesh{_mesh}; + for (std::size_t i = 0; i < COUNT; i++) + { + auto* const tex = textures[i]; + if (!tex) + continue; + mesh.setCount(quad_index_count); + mesh.setIndexRange((int)(i*quad_index_count), 0, quad_index_count*COUNT - 1); + if (tex != last_texture) + tex->bind(0); + last_texture = tex; + shader.draw(mesh); + } +} + +std::array, wall_mesh::COUNT> wall_mesh::make_index_array() +{ + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init) + std::array, COUNT> array; + + for (std::size_t i = 0; i < std::size(array); i++) + array[i] = tile_atlas::indices(i); + return array; +} + +std::array, wall_mesh::COUNT> wall_mesh::make_position_array() +{ + std::array, COUNT> array; + constexpr float X = TILE_SIZE[0], Y = TILE_SIZE[1], Z = TILE_SIZE[2]; + constexpr Vector3 size = {X, Y, Z}; + for (std::size_t j = 0; j < TILE_MAX_DIM; j++) + for (std::size_t i = 0; i < TILE_MAX_DIM; i++) + { + const auto idx = (j*TILE_MAX_DIM + i) * 2; + Vector3 center{(float)(X*i), (float)(Y*j), 0}; + array[idx + 0] = tile_atlas::wall_quad_N(center, size); + array[idx + 1] = tile_atlas::wall_quad_W(center, size); + } + return array; +} + +} // namespace floormat diff --git a/draw/wall.hpp b/draw/wall.hpp new file mode 100644 index 00000000..b021ef95 --- /dev/null +++ b/draw/wall.hpp @@ -0,0 +1,43 @@ +#pragma once + +#include "tile.hpp" +#include +#include +#include +#include +#include +#include + +namespace floormat { + +struct tile_shader; +struct chunk; + +struct wall_mesh final +{ + wall_mesh(); + void draw(tile_shader& shader, chunk& c); + +private: + static constexpr auto COUNT1 = TILE_MAX_DIM*2, COUNT = COUNT1 * COUNT1; + + struct vertex final { + Vector2 texcoords; + }; + + using quad = std::array; + using vertex_array = std::array; + using texture_array = std::array; + + static void maybe_add_tile(vertex_array& data, texture_array& textures, tile& x, std::size_t pos); + static void add_wall(vertex_array& data, texture_array& textures, tile_image& img, std::size_t pos); + + GL::Mesh _mesh; + GL::Buffer _vertex_buffer{vertex_array{}, Magnum::GL::BufferUsage::DynamicDraw}, + _index_buffer{make_index_array()}, + _positions_buffer{make_position_array()}; + static std::array, COUNT> make_index_array(); + static std::array, COUNT> make_position_array(); +}; + +} // namespace floormat diff --git a/draw/wireframe-box.cpp b/draw/wireframe-box.cpp deleted file mode 100644 index 49606bd8..00000000 --- a/draw/wireframe-box.cpp +++ /dev/null @@ -1,53 +0,0 @@ -#include "wireframe-box.hpp" -#include -#include -#include - -namespace floormat::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) front left bottom - {Cx_1, Cy_0, Cz_0}, // (1) front right bottom - {Cx_0, Cy_1, Cz_0}, // (2) back left bottom - {Cx_1, Cy_1, Cz_0}, // (3) back right bottom - {Cx_0, Cy_0, Cz_1}, // (4) front left top - {Cx_1, Cy_0, Cz_1}, // (5) front right top - {Cx_0, Cy_1, Cz_1}, // (6) back left top - {Cx_1, Cy_1, Cz_1}, // (7) back right 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 floormat::wireframe diff --git a/draw/wireframe-box.hpp b/draw/wireframe-box.hpp deleted file mode 100644 index b187dc83..00000000 --- a/draw/wireframe-box.hpp +++ /dev/null @@ -1,29 +0,0 @@ -#pragma once -#include -#include -#include -#include - -namespace floormat::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; - using index_array = std::array; - - 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 floormat::wireframe diff --git a/draw/wireframe-mesh.cpp b/draw/wireframe-mesh.cpp deleted file mode 100644 index 55af19a1..00000000 --- a/draw/wireframe-mesh.cpp +++ /dev/null @@ -1,55 +0,0 @@ -#include "wireframe-mesh.hpp" -#include "shaders/tile-shader.hpp" -//#include -#include -#include -#include -//#include -#include -#include -//#include -#include - -namespace floormat::wireframe -{ - -GL::Texture2D mesh_base::make_constant_texture() -{ - const Vector4ub data[] = { {255, 255, 255, 255} }; - Trade::ImageData2D img{PixelFormat::RGBA8Unorm, {1, 1}, {}, - Containers::arrayView(data, 1), {}, {}}; - GL::Texture2D tex; - tex.setWrapping(GL::SamplerWrapping::ClampToEdge) - .setMagnificationFilter(GL::SamplerFilter::Nearest) - .setMinificationFilter(GL::SamplerFilter::Nearest) - .setStorage(1, GL::textureFormat(img.format()), img.size()) - .setSubImage(0, {}, std::move(img)); - return tex; -} - -mesh_base::mesh_base(GL::MeshPrimitive primitive, ArrayView index_data, - std::size_t num_vertices, std::size_t num_indexes) : - _vertex_buffer{Containers::Array{ValueInit, num_vertices}, GL::BufferUsage::DynamicDraw}, - _texcoords_buffer{Containers::Array{ValueInit, num_vertices}}, - _index_buffer{num_indexes == 0 ? GL::Buffer{NoCreate} : GL::Buffer{index_data}} -{ - _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) -{ - _texture.bind(0); - shader.draw(_mesh); -} - -void mesh_base::set_subdata(ArrayView array) -{ - _vertex_buffer.setSubData(0, array); -} - -} // namespace floormat::wireframe diff --git a/draw/wireframe-mesh.hpp b/draw/wireframe-mesh.hpp deleted file mode 100644 index 9d736419..00000000 --- a/draw/wireframe-mesh.hpp +++ /dev/null @@ -1,61 +0,0 @@ -#pragma once - -#include "tile-defs.hpp" -#include -#include -#include "Magnum/GL/Texture.h" - -namespace floormat { - -struct tile_shader; - -namespace wireframe -{ - -template -concept traits = requires (const T& x) { - {T::num_vertices} -> std::convertible_to; - {T::num_indexes} -> std::convertible_to; - {x.primitive} -> std::convertible_to; - {x.make_vertex_array() } -> std::convertible_to>; - {T::make_index_array() } -> std::convertible_to>; - {x.on_draw()} -> std::same_as; -}; - -struct mesh_base -{ - static GL::Texture2D make_constant_texture(); - GL::Buffer _vertex_buffer{{}, GL::BufferUsage::DynamicDraw}, _texcoords_buffer, _index_buffer; - GL::Texture2D _texture = make_constant_texture(); - GL::Mesh _mesh; - - mesh_base(GL::MeshPrimitive primitive, ArrayView index_data, - std::size_t num_vertices, std::size_t num_indexes); - void draw(tile_shader& shader); - void set_subdata(ArrayView array); -}; - -} // namespace wireframe - -template -struct wireframe_mesh final : private wireframe::mesh_base -{ - wireframe_mesh(); - void draw(tile_shader& shader, T traits); -}; - -template -wireframe_mesh::wireframe_mesh() : - wireframe::mesh_base{T::primitive, T::make_index_array(), T::num_vertices, T::num_indexes} -{ -} - -template void wireframe_mesh::draw(tile_shader& shader, T x) -{ - //_vertex_buffer.setData({nullptr, sizeof(Vector3) * T::num_vertices}, GL::BufferUsage::DynamicDraw); // orphan the buffer - set_subdata(x.make_vertex_array()); - x.on_draw(); - mesh_base::draw(shader); -} - -} // namespace floormat diff --git a/draw/wireframe-quad.cpp b/draw/wireframe-quad.cpp deleted file mode 100644 index 454189f9..00000000 --- a/draw/wireframe-quad.cpp +++ /dev/null @@ -1,27 +0,0 @@ -#include "wireframe-quad.hpp" -#include -#include - -namespace floormat::wireframe { - -quad::vertex_array quad::make_vertex_array() const -{ - const float X = size[0]*.5f, Y = size[1]*.5f; - return {{ - { -X + center[0], -Y + center[1], center[2] }, - { X + center[0], -Y + center[1], center[2] }, - { X + center[0], Y + center[1], center[2] }, - { -X + center[0], Y + center[1], center[2] }, - }}; -} - -quad::quad(Vector3 center, Vector2 size, float line_width) : - center(center), size(size), line_width{line_width} -{} - -void quad::on_draw() const -{ - GL::Renderer::setLineWidth(line_width); -} - -} // namespace floormat::wireframe diff --git a/draw/wireframe-quad.hpp b/draw/wireframe-quad.hpp deleted file mode 100644 index 050eeb78..00000000 --- a/draw/wireframe-quad.hpp +++ /dev/null @@ -1,30 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include - -namespace floormat::wireframe { - -struct quad final -{ - quad(Vector3 center, Vector2 size, float line_width); - - static constexpr std::size_t num_vertices = 4, num_indexes = 0; - static constexpr GL::MeshPrimitive primitive = GL::MeshPrimitive::LineLoop; - - using vertex_array = std::array; - - static ArrayView make_index_array() { return {}; } - vertex_array make_vertex_array() const; - void on_draw() const; - -private: - Vector3 center; - Vector2 size; - float line_width; -}; - -} // namespace floormat::wireframe diff --git a/draw/wireframe.cpp b/draw/wireframe.cpp new file mode 100644 index 00000000..4ff99adb --- /dev/null +++ b/draw/wireframe.cpp @@ -0,0 +1,55 @@ +#include "wireframe.hpp" +#include "shaders/tile-shader.hpp" +//#include +#include +#include +#include +//#include +#include +#include +//#include +#include + +namespace floormat::wireframe +{ + +GL::Texture2D mesh_base::make_constant_texture() +{ + const Vector4ub data[] = { {255, 255, 255, 255} }; + Trade::ImageData2D img{PixelFormat::RGBA8Unorm, {1, 1}, {}, + Containers::arrayView(data, 1), {}, {}}; + GL::Texture2D tex; + tex.setWrapping(GL::SamplerWrapping::ClampToEdge) + .setMagnificationFilter(GL::SamplerFilter::Nearest) + .setMinificationFilter(GL::SamplerFilter::Nearest) + .setStorage(1, GL::textureFormat(img.format()), img.size()) + .setSubImage(0, {}, std::move(img)); + return tex; +} + +mesh_base::mesh_base(GL::MeshPrimitive primitive, ArrayView index_data, + std::size_t num_vertices, std::size_t num_indexes) : + _vertex_buffer{Containers::Array{ValueInit, num_vertices}, GL::BufferUsage::DynamicDraw}, + _texcoords_buffer{Containers::Array{ValueInit, num_vertices}}, + _index_buffer{num_indexes == 0 ? GL::Buffer{NoCreate} : GL::Buffer{index_data}} +{ + _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) +{ + _texture.bind(0); + shader.draw(_mesh); +} + +void mesh_base::set_subdata(ArrayView array) +{ + _vertex_buffer.setSubData(0, array); +} + +} // namespace floormat::wireframe diff --git a/draw/wireframe.hpp b/draw/wireframe.hpp new file mode 100644 index 00000000..9d736419 --- /dev/null +++ b/draw/wireframe.hpp @@ -0,0 +1,61 @@ +#pragma once + +#include "tile-defs.hpp" +#include +#include +#include "Magnum/GL/Texture.h" + +namespace floormat { + +struct tile_shader; + +namespace wireframe +{ + +template +concept traits = requires (const T& x) { + {T::num_vertices} -> std::convertible_to; + {T::num_indexes} -> std::convertible_to; + {x.primitive} -> std::convertible_to; + {x.make_vertex_array() } -> std::convertible_to>; + {T::make_index_array() } -> std::convertible_to>; + {x.on_draw()} -> std::same_as; +}; + +struct mesh_base +{ + static GL::Texture2D make_constant_texture(); + GL::Buffer _vertex_buffer{{}, GL::BufferUsage::DynamicDraw}, _texcoords_buffer, _index_buffer; + GL::Texture2D _texture = make_constant_texture(); + GL::Mesh _mesh; + + mesh_base(GL::MeshPrimitive primitive, ArrayView index_data, + std::size_t num_vertices, std::size_t num_indexes); + void draw(tile_shader& shader); + void set_subdata(ArrayView array); +}; + +} // namespace wireframe + +template +struct wireframe_mesh final : private wireframe::mesh_base +{ + wireframe_mesh(); + void draw(tile_shader& shader, T traits); +}; + +template +wireframe_mesh::wireframe_mesh() : + wireframe::mesh_base{T::primitive, T::make_index_array(), T::num_vertices, T::num_indexes} +{ +} + +template void wireframe_mesh::draw(tile_shader& shader, T x) +{ + //_vertex_buffer.setData({nullptr, sizeof(Vector3) * T::num_vertices}, GL::BufferUsage::DynamicDraw); // orphan the buffer + set_subdata(x.make_vertex_array()); + x.on_draw(); + mesh_base::draw(shader); +} + +} // namespace floormat diff --git a/editor/app.hpp b/editor/app.hpp index 1cf118f9..5b9727fe 100644 --- a/editor/app.hpp +++ b/editor/app.hpp @@ -3,9 +3,9 @@ #include "compat/enum-bitset.hpp" #include "editor.hpp" #include "src/global-coords.hpp" -#include "draw/wireframe-mesh.hpp" -#include "draw/wireframe-quad.hpp" -#include "draw/wireframe-box.hpp" +#include "draw/wireframe.hpp" +#include "draw/quad.hpp" +#include "draw/box.hpp" #include "floormat/app.hpp" #include diff --git a/main/main-impl.hpp b/main/main-impl.hpp index 39a877fe..b5828d6c 100644 --- a/main/main-impl.hpp +++ b/main/main-impl.hpp @@ -2,8 +2,8 @@ #include "floormat/main.hpp" #include "floormat/settings.hpp" #include "src/world.hpp" -#include "draw/floor-mesh.hpp" -#include "draw/wall-mesh.hpp" +#include "draw/floor.hpp" +#include "draw/wall.hpp" #include "shaders/tile-shader.hpp" #include -- cgit v1.2.3