diff options
| author | Stanislaw Halik <sthalik@misaki.pl> | 2022-02-19 17:24:19 +0100 |
|---|---|---|
| committer | Stanislaw Halik <sthalik@misaki.pl> | 2022-02-19 17:24:19 +0100 |
| commit | 932ee8367c7e5641b7bdcd495e7f92429c516021 (patch) | |
| tree | 329581bfb72e39e3ba7acd81496d3333d4ff9bbb | |
| parent | ef7d3528ef51f02307717a5ec65b49c913d677a6 (diff) | |
buffer flush
| -rw-r--r-- | atlas.cpp | 7 | ||||
| -rw-r--r-- | atlas.hpp | 1 | ||||
| -rw-r--r-- | main.cpp | 52 |
3 files changed, 38 insertions, 22 deletions
@@ -22,11 +22,10 @@ atlas_texture::atlas_texture(const Trade::ImageData2D& image, Vector2i dims) : std::array<Vector2, 4> atlas_texture::texcoords_for_id(int id_) const { CORRADE_INTERNAL_ASSERT(id_ >= 0 && id_ < dims_.product()); - constexpr Vector2 _05 = { 0.5f, 0.5f }; constexpr Vector2i _1 = { 1, 1 }; - Vector2i id = { id_ % dims_[1], id_ / dims_[1] }; - auto p0 = (Vector2(id * tile_size_) + _05) / Vector2(size_); - auto p1 = (Vector2((id + _1) * tile_size_) + _05) / Vector2(size_); + Vector2i id = { id_ % dims_[0], id_ / dims_[0] }; + auto p0 = Vector2(id * tile_size_) / Vector2(size_); + auto p1 = Vector2((id + _1) * tile_size_) / Vector2(size_); return {{ { p1[0], p1[1] }, // bottom right { p1[0], p0[1] }, // top right @@ -14,6 +14,7 @@ struct atlas_texture final static std::array<Vector3, 4> floor_quad(Vector3 center, Vector2 size); static std::array<UnsignedShort, 6> indices(int N); GL::Texture2D& texture() { return tex_; } + constexpr int size() const { return dims_.product(); } atlas_texture(const atlas_texture&) = delete; atlas_texture& operator=(const atlas_texture&) = delete; @@ -5,6 +5,8 @@ #include <Corrade/Containers/ArrayViewStl.h> #include <Corrade/PluginManager/Manager.h> #include <Corrade/Utility/Resource.h> +#include <Magnum/Magnum.h> +#include <Magnum/Math/Vector.h> #include <Magnum/ImageView.h> #include <Magnum/GL/Buffer.h> #include <Magnum/GL/DefaultFramebuffer.h> @@ -34,7 +36,7 @@ struct application final : Platform::Application GL::Mesh _mesh; tile_shader _shader; - atlas_texture atlas = make_atlas("images/tiles.tga", {8, 4}); + atlas_texture atlas = make_atlas("../share/game/images/tiles.tga", {8, 4}); atlas_texture make_atlas(const std::string& file, Vector2i dims); Matrix4x4 make_projection(Vector3 offset); @@ -52,15 +54,27 @@ application::application(const Arguments& arguments): Vector2 textureCoordinates; // todo gl_FragDepth }; - QuadVertex vertices[4]; - auto positions = atlas.floor_quad({}, {48, 48}); - auto texcoords = atlas.texcoords_for_id(2); - auto indices = atlas.indices(0); - for (unsigned i = 0; i < std::size(vertices); i++) - vertices[i] = { positions[i], texcoords[i] }; - - _mesh.setCount((int)std::size(indices)) + std::vector<QuadVertex> vertices; vertices.reserve(64*64*4); + std::vector<Short> indices; indices.reserve(64*64*4); + + int k = 0; + for (int j = -2; j <= 2; j++) + for (int i = -2; i <= 2; i++) + { + constexpr int sz = 48; + auto positions = atlas.floor_quad({(float)(sz*i), (float)(sz*j), 0}, {sz, sz}); + auto texcoords = atlas.texcoords_for_id(k % atlas.size()); + auto indices_ = atlas.indices(k); + + for (unsigned x = 0; x < 4; x++) + vertices.push_back({ positions[x], texcoords[x] }); + for (auto x : indices_) + indices.push_back((Short)x); + k++; + } + + _mesh.setCount((int)indices.size()) .addVertexBuffer(GL::Buffer{vertices}, 0, tile_shader::Position{}, tile_shader::TextureCoordinates{}) .setIndexBuffer(GL::Buffer{indices}, 0, GL::MeshIndexType::UnsignedShort); @@ -96,16 +110,18 @@ atlas_texture application::make_atlas(const std::string& file, Vector2i dims) Matrix4x4 application::make_projection(Vector3 offset) { - auto m = glm::mat4{1}; + using vec3 = glm::vec<3, double, glm::highp>; + using mat4 = glm::mat<4, 4, double, glm::highp>; + auto m = mat4{1}; auto size = windowSize(); - float x = size[0]*.5f, y = size[1]*.5f, w = 4*sqrt(x*x+y*y); - m = glm::ortho<float>(-x, x, -y, y, -w, w); - m = glm::translate(m, { offset[0], -offset[1], offset[2] }); - m = glm::scale(m, { 1.f, 0.6f, 1.f }); - m = glm::rotate(m, glm::radians(-45.f), glm::vec3(1.0f, 0.0f, 0.0f)); - m = glm::rotate(m, glm::radians(0.0f), glm::vec3(0.0f, 1.0f, 0.0f)); - m = glm::rotate(m, glm::radians(-45.0f), glm::vec3(0.0f, 0.0f, 1.0f)); - return Matrix4x4{m}; + double x = size[0]*.5, y = size[1]*.5, w = 4*sqrt(x*x+y*y); + m = glm::ortho<double>(-x, x, -y, y, -w, w); + m = glm::translate(m, { (double)offset[0], (double)-offset[1], (double)offset[2] }); + m = glm::scale(m, { 1., 0.6, 1. }); + m = glm::rotate(m, glm::radians(-45.), vec3(1, 0, 0)); + m = glm::rotate(m, glm::radians(0.), vec3(0, 1, 0)); + m = glm::rotate(m, glm::radians(-45.), vec3(0, 0, 1)); + return Matrix4x4{glm::mat4(m)}; } } // namespace Magnum::Examples |
