From dbc638717df7e4639693e06e4a581f57188d2647 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Fri, 18 Feb 2022 06:32:11 +0100 Subject: rename atlas type filename --- atlas.cpp | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ atlas.hpp | 26 ++++++++++++++++++++++++++ chunk.cpp | 59 ----------------------------------------------------------- chunk.hpp | 26 -------------------------- main.cpp | 2 +- 5 files changed, 86 insertions(+), 86 deletions(-) create mode 100644 atlas.cpp create mode 100644 atlas.hpp delete mode 100644 chunk.cpp delete mode 100644 chunk.hpp diff --git a/atlas.cpp b/atlas.cpp new file mode 100644 index 00000000..46153629 --- /dev/null +++ b/atlas.cpp @@ -0,0 +1,59 @@ +#include "atlas.hpp" +#include +#include + +namespace Magnum::Examples { + +atlas_texture::atlas_texture(const Trade::ImageData2D& image, Vector2i dims) : + size_{image.size()}, + dims_{dims}, + tile_size_{size_ / dims} +{ + CORRADE_INTERNAL_ASSERT(dims_[0] > 0 && dims_[1] > 0); + CORRADE_INTERNAL_ASSERT(tile_size_ * dims_ == size_); + CORRADE_INTERNAL_ASSERT(size_ % dims_ == Vector2i{}); + tex_.setWrapping(GL::SamplerWrapping::ClampToEdge) + .setMagnificationFilter(GL::SamplerFilter::Nearest) + .setMinificationFilter(GL::SamplerFilter::Linear) + .setStorage(1, GL::textureFormat(image.format()), image.size()) + .setSubImage(0, {}, image); +} + +std::array 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_); + return {{ + { p1[0], p1[1] }, // bottom right + { p1[0], p0[1] }, // top right + { p0[0], p1[1] }, // bottom left + { p0[0], p0[1] } // top left + }}; +} + +std::array atlas_texture::floor_quad(Vector3 center, Vector2 size) +{ + float x = size[0]*.5f, y = size[1]*.5f; + return {{ + { x + center[0], -y + center[1], 0}, + { x + center[0], y + center[1], 0}, + {-x + center[0], -y + center[1], 0}, + {-x + center[0], y + center[1], 0}, + }}; +} + +std::array atlas_texture::indices(int N) +{ + CORRADE_INTERNAL_ASSERT(N >= 0); + using u16 = UnsignedShort; + return { /* 3--1 1 */ + (u16)(0+N*4), (u16)(1+N*4), (u16)(2+N*4), /* | / /| */ + (u16)(2+N*4), (u16)(1+N*4), (u16)(3+N*4), /* |/ / | */ + }; /* 2 2--0 */ +} + +} // namespace Magnum::Examples diff --git a/atlas.hpp b/atlas.hpp new file mode 100644 index 00000000..4d747137 --- /dev/null +++ b/atlas.hpp @@ -0,0 +1,26 @@ +#pragma once +#include +#include +#include +#include +#include + +namespace Magnum::Examples { +using Vector2i = Math::Vector<2, int>; + +struct atlas_texture final +{ + atlas_texture(const Trade::ImageData2D& img, Vector2i dims); + std::array texcoords_for_id(int id) const; + static std::array floor_quad(Vector3 center, Vector2 size); + static std::array indices(int N); + GL::Texture2D& texture() { return tex_; } + + atlas_texture(const atlas_texture&) = delete; + atlas_texture& operator=(const atlas_texture&) = delete; +private: + GL::Texture2D tex_; + Vector2i size_, dims_, tile_size_; +}; + +} // namespace Magnum::Examples diff --git a/chunk.cpp b/chunk.cpp deleted file mode 100644 index 62dcdc0e..00000000 --- a/chunk.cpp +++ /dev/null @@ -1,59 +0,0 @@ -#include "chunk.hpp" -#include -#include - -namespace Magnum::Examples { - -atlas_texture::atlas_texture(const Trade::ImageData2D& image, Vector2i dims) : - size_{image.size()}, - dims_{dims}, - tile_size_{size_ / dims} -{ - CORRADE_INTERNAL_ASSERT(dims_[0] > 0 && dims_[1] > 0); - CORRADE_INTERNAL_ASSERT(tile_size_ * dims_ == size_); - CORRADE_INTERNAL_ASSERT(size_ % dims_ == Vector2i{}); - tex_.setWrapping(GL::SamplerWrapping::ClampToEdge) - .setMagnificationFilter(GL::SamplerFilter::Nearest) - .setMinificationFilter(GL::SamplerFilter::Linear) - .setStorage(1, GL::textureFormat(image.format()), image.size()) - .setSubImage(0, {}, image); -} - -std::array 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_); - return {{ - { p1[0], p1[1] }, // bottom right - { p1[0], p0[1] }, // top right - { p0[0], p1[1] }, // bottom left - { p0[0], p0[1] } // top left - }}; -} - -std::array atlas_texture::floor_quad(Vector3 center, Vector2 size) -{ - float x = size[0]*.5f, y = size[1]*.5f; - return {{ - { x + center[0], -y + center[1], 0}, - { x + center[0], y + center[1], 0}, - {-x + center[0], -y + center[1], 0}, - {-x + center[0], y + center[1], 0}, - }}; -} - -std::array atlas_texture::indices(int N) -{ - CORRADE_INTERNAL_ASSERT(N >= 0); - using u16 = UnsignedShort; - return { /* 3--1 1 */ - (u16)(0+N*4), (u16)(1+N*4), (u16)(2+N*4), /* | / /| */ - (u16)(2+N*4), (u16)(1+N*4), (u16)(3+N*4), /* |/ / | */ - }; /* 2 2--0 */ -} - -} // namespace Magnum::Examples diff --git a/chunk.hpp b/chunk.hpp deleted file mode 100644 index 4d747137..00000000 --- a/chunk.hpp +++ /dev/null @@ -1,26 +0,0 @@ -#pragma once -#include -#include -#include -#include -#include - -namespace Magnum::Examples { -using Vector2i = Math::Vector<2, int>; - -struct atlas_texture final -{ - atlas_texture(const Trade::ImageData2D& img, Vector2i dims); - std::array texcoords_for_id(int id) const; - static std::array floor_quad(Vector3 center, Vector2 size); - static std::array indices(int N); - GL::Texture2D& texture() { return tex_; } - - atlas_texture(const atlas_texture&) = delete; - atlas_texture& operator=(const atlas_texture&) = delete; -private: - GL::Texture2D tex_; - Vector2i size_, dims_, tile_size_; -}; - -} // namespace Magnum::Examples diff --git a/main.cpp b/main.cpp index 2e34393e..07dd6130 100644 --- a/main.cpp +++ b/main.cpp @@ -1,4 +1,4 @@ -#include "chunk.hpp" +#include "atlas.hpp" #include #include -- cgit v1.2.3