diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/chunk-render.cpp | 70 | ||||
-rw-r--r-- | src/chunk-walls.cpp | 71 | ||||
-rw-r--r-- | src/chunk.hpp | 6 | ||||
-rw-r--r-- | src/tile.cpp | 12 |
4 files changed, 90 insertions, 69 deletions
diff --git a/src/chunk-render.cpp b/src/chunk-render.cpp index 173a8f8b..586018b1 100644 --- a/src/chunk-render.cpp +++ b/src/chunk-render.cpp @@ -11,8 +11,9 @@ namespace floormat { using namespace floormat::Quads; -template<size_t N = 1> -static auto make_index_array(size_t max) +template<size_t N> +std::array<std::array<UnsignedShort, 6>, N*TILE_COUNT> +chunk::make_index_array(size_t max) { std::array<std::array<UnsignedShort, 6>, N*TILE_COUNT> array; // NOLINT(cppcoreguidelines-pro-type-member-init) for (auto i = 0uz; i < max; i++) @@ -20,18 +21,15 @@ static auto make_index_array(size_t max) return array; } +template std::array<std::array<UnsignedShort, 6>, 1*TILE_COUNT> chunk::make_index_array<1>(size_t); +template std::array<std::array<UnsignedShort, 6>, 2*TILE_COUNT> chunk::make_index_array<2>(size_t); + void chunk::ensure_alloc_ground() { if (!_ground) [[unlikely]] _ground = Pointer<ground_stuff>{InPlaceInit}; } -void chunk::ensure_alloc_walls() -{ - if (!_walls) [[unlikely]] - _walls = Pointer<wall_stuff>{InPlaceInit}; -} - auto chunk::ensure_ground_mesh() noexcept -> ground_mesh_tuple { if (!_ground) @@ -67,7 +65,7 @@ auto chunk::ensure_ground_mesh() noexcept -> ground_mesh_tuple v[j] = { quad[j], texcoords[j], depth }; } - const auto indexes = make_index_array(count); + const auto indexes = make_index_array<1>(count); const auto vertex_view = ArrayView{vertexes.data(), count}; const auto vert_index_view = ArrayView{indexes.data(), count}; @@ -79,59 +77,5 @@ auto chunk::ensure_ground_mesh() noexcept -> ground_mesh_tuple return { ground_mesh, _ground->ground_indexes, count }; } -fm_noinline -GL::Mesh chunk::make_wall_mesh(size_t count) -{ - fm_debug_assert(_walls); - //std::array<std::array<vertex, 4>, TILE_COUNT*2> vertexes; - vertex vertexes[TILE_COUNT*2][4]; - for (auto k = 0uz; k < count; k++) - { - const uint16_t i = _walls->wall_indexes[k]; - const auto& atlas = _walls->_wall_atlases[i]; - const auto& variant = _walls->_wall_variants[i]; - const local_coords pos{i / 2u}; - const auto center = Vector3(pos) * TILE_SIZE; - const auto quad = i & 1 ? wall_quad_W(center, TILE_SIZE) : wall_quad_N(center, TILE_SIZE); - const float depth = tile_shader::depth_value(pos, tile_shader::wall_depth_offset); - const auto texcoords = atlas->texcoords_for_id(variant); - auto& v = vertexes[k]; - for (auto j = 0uz; j < 4; j++) - v[j] = { quad[j], texcoords[j], depth, }; - } - - auto indexes = make_index_array<2>(count); - const auto vertex_view = ArrayView{&vertexes[0], count}; - const auto vert_index_view = ArrayView{indexes.data(), count}; - - GL::Mesh mesh{GL::MeshPrimitive::Triangles}; - mesh.addVertexBuffer(GL::Buffer{vertex_view}, 0, tile_shader::Position{}, tile_shader::TextureCoordinates{}, tile_shader::Depth{}) - .setIndexBuffer(GL::Buffer{vert_index_view}, 0, GL::MeshIndexType::UnsignedShort) - .setCount(int32_t(6 * count)); - return mesh; -} - -auto chunk::ensure_wall_mesh() noexcept -> wall_mesh_tuple -{ - if (!_walls) - return {wall_mesh, {}, 0}; - - if (!_walls_modified) - return { wall_mesh, _walls->wall_indexes, size_t(wall_mesh.count()/6) }; - _walls_modified = false; - - size_t count = 0; - for (auto i = 0uz; i < TILE_COUNT*2; i++) - if (_walls->_wall_atlases[i]) - _walls->wall_indexes[count++] = uint16_t(i); - - std::sort(_walls->wall_indexes.data(), _walls->wall_indexes.data() + (ptrdiff_t)count, - [this](uint16_t a, uint16_t b) { - return _walls->_wall_atlases[a] < _walls->_wall_atlases[b]; - }); - - wall_mesh = make_wall_mesh(count); - return { wall_mesh, _walls->wall_indexes, count }; -} } // namespace floormat diff --git a/src/chunk-walls.cpp b/src/chunk-walls.cpp index 2c09a79d..1d976932 100644 --- a/src/chunk-walls.cpp +++ b/src/chunk-walls.cpp @@ -1,7 +1,11 @@ #include "chunk.hpp" -#include "src/tile-bbox.hpp" +#include "tile-bbox.hpp" +#include "quads.hpp" +#include "wall-atlas.hpp" +#include "shaders/shader.hpp" #include <Corrade/Containers/ArrayView.h> #include <Corrade/Containers/PairStl.h> +#include <algorithm> // +x +y +z // +x +y -z @@ -10,13 +14,78 @@ namespace floormat { +using namespace floormat::Quads; using Wall::Group_; + +void chunk::ensure_alloc_walls() +{ + if (!_walls) [[unlikely]] + _walls = Pointer<wall_stuff>{InPlaceInit}; +} + +// ----------------------- + // wall north template<> auto chunk::make_wall_vertex_data<Group_::wall, false>(size_t tile, float depth) -> vertex { } +// ----------------------- + +fm_noinline +GL::Mesh chunk::make_wall_mesh(size_t count) +{ + fm_debug_assert(_walls); + //std::array<std::array<vertex, 4>, TILE_COUNT*2> vertexes; + vertex vertexes[TILE_COUNT*2][4]; + for (auto k = 0uz; k < count; k++) + { + const uint16_t i = _walls->indexes[k]; + const auto& atlas = _walls->atlases[i]; + const auto& variant = _walls->variants[i]; + const local_coords pos{i / 2u}; + const auto center = Vector3(pos) * TILE_SIZE; + const auto quad = i & 1 ? wall_quad_W(center, TILE_SIZE) : wall_quad_N(center, TILE_SIZE); + const float depth = tile_shader::depth_value(pos, tile_shader::wall_depth_offset); + const auto texcoords = atlas->texcoords_for_id(variant); + auto& v = vertexes[k]; + for (auto j = 0uz; j < 4; j++) + v[j] = { quad[j], texcoords[j], depth, }; + } + + auto indexes = make_index_array<2>(count); + const auto vertex_view = ArrayView{&vertexes[0], count}; + const auto vert_index_view = ArrayView{indexes.data(), count}; + + GL::Mesh mesh{GL::MeshPrimitive::Triangles}; + mesh.addVertexBuffer(GL::Buffer{vertex_view}, 0, tile_shader::Position{}, tile_shader::TextureCoordinates{}, tile_shader::Depth{}) + .setIndexBuffer(GL::Buffer{vert_index_view}, 0, GL::MeshIndexType::UnsignedShort) + .setCount(int32_t(6 * count)); + return mesh; +} + +auto chunk::ensure_wall_mesh() noexcept -> wall_mesh_tuple +{ + if (!_walls) + return {wall_mesh, {}, 0}; + + if (!_walls_modified) + return { wall_mesh, _walls->wall_indexes, size_t(wall_mesh.count()/6) }; + _walls_modified = false; + + size_t count = 0; + for (auto i = 0uz; i < TILE_COUNT*2; i++) + if (_walls->_wall_atlases[i]) + _walls->wall_indexes[count++] = uint16_t(i); + std::sort(_walls->wall_indexes.data(), _walls->wall_indexes.data() + (ptrdiff_t)count, + [this](uint16_t a, uint16_t b) { + return _walls->_wall_atlases[a] < _walls->_wall_atlases[b]; + }); + + wall_mesh = make_wall_mesh(count); + return { wall_mesh, _walls->wall_indexes, count }; +} } // namespace floormat diff --git a/src/chunk.hpp b/src/chunk.hpp index e4db815d..0c10e1be 100644 --- a/src/chunk.hpp +++ b/src/chunk.hpp @@ -119,6 +119,7 @@ struct chunk final private: struct ground_stuff { + // todo remove "_ground" prefix std::array<std::shared_ptr<tile_atlas>, TILE_COUNT> _ground_atlases; std::array<uint8_t, TILE_COUNT> ground_indexes = {}; std::array<variant_t, TILE_COUNT> _ground_variants = {}; @@ -128,7 +129,7 @@ private: { std::array<std::shared_ptr<wall_atlas>, 2*TILE_COUNT> atlases; std::array<variant_t, 2*TILE_COUNT> variants; - std::array<uint8_t, TILE_COUNT> indexes_N, indexes_W; + std::array<uint_fast16_t, 2*TILE_COUNT> indexes; size_t count_N = 0, count_W = 0; bool empty() const { return count_N == 0 && count_W == 0; } @@ -162,12 +163,15 @@ private: bool operator==(const bbox& other) const noexcept; }; + static bool _bbox_for_scenery(const object& s, bbox& value) noexcept; static bool _bbox_for_scenery(const object& s, local_coords local, Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_size, bbox& value) noexcept; void _remove_bbox(const bbox& x); void _add_bbox(const bbox& x); void _replace_bbox(const bbox& x0, const bbox& x, bool b0, bool b); GL::Mesh make_wall_mesh(size_t count); + + template<size_t N> static std::array<std::array<UnsignedShort, 6>, N*TILE_COUNT> make_index_array(size_t max); }; } // namespace floormat diff --git a/src/tile.cpp b/src/tile.cpp index 0a365f35..4f49f214 100644 --- a/src/tile.cpp +++ b/src/tile.cpp @@ -38,14 +38,18 @@ tile_image_proto tile_ref::ground() const noexcept wall_image_proto tile_ref::wall_north() const noexcept { - _chunk->ensure_alloc_walls(); - return { _chunk->_walls->atlases[i*2+0], _chunk->_walls->variants[i*2+0] }; + if (!_chunk->_walls) [[unlikely]] + return {}; + else + return { _chunk->_walls->atlases[i*2+0], _chunk->_walls->variants[i*2+0] }; } wall_image_proto tile_ref::wall_west() const noexcept { - _chunk->ensure_alloc_walls(); - return { _chunk->_walls->atlases[i*2+1], _chunk->_walls->variants[i*2+1] }; + if (!_chunk->_walls) [[unlikely]] + return {}; + else + return { _chunk->_walls->atlases[i*2+1], _chunk->_walls->variants[i*2+1] }; } tile_ref::operator tile_proto() const noexcept |