diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2023-04-14 18:03:34 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2023-04-14 18:03:34 +0100 |
commit | 5776a36b454fac04617313da011d7aa2b0e834d3 (patch) | |
tree | 1eb96d07e9a17a51e5763f397fc003f762cd2e75 /assetFactory/texturePacker.cpp | |
parent | Merge branch 'model-factory-textures' (diff) | |
parent | Add an asset template and use it to define all the foliage assets in the plan... (diff) | |
download | ilt-5776a36b454fac04617313da011d7aa2b0e834d3.tar.bz2 ilt-5776a36b454fac04617313da011d7aa2b0e834d3.tar.xz ilt-5776a36b454fac04617313da011d7aa2b0e834d3.zip |
Merge branch 'assimp'
Diffstat (limited to 'assetFactory/texturePacker.cpp')
-rw-r--r-- | assetFactory/texturePacker.cpp | 29 |
1 files changed, 25 insertions, 4 deletions
diff --git a/assetFactory/texturePacker.cpp b/assetFactory/texturePacker.cpp index 68a6010..fcc935f 100644 --- a/assetFactory/texturePacker.cpp +++ b/assetFactory/texturePacker.cpp @@ -1,7 +1,9 @@ #include "texturePacker.h" #include "collections.hpp" +#include <GL/glew.h> #include <algorithm> #include <cstdio> +#include <glm/common.hpp> #include <numeric> #include <ostream> #include <set> @@ -12,6 +14,9 @@ TexturePacker::TexturePacker(std::span<const Image> in) : std::sort(sortedIndexes.rbegin(), sortedIndexes.rend(), [this](const auto a, const auto b) { return area(inputImages[a]) < area(inputImages[b]); }); + int mts; + glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mts); + maxTextureSize = static_cast<unsigned int>(mts); } TexturePacker::Result @@ -23,6 +28,9 @@ TexturePacker::pack() const TexturePacker::Result TexturePacker::pack(Size size) const { + if (size.x > maxTextureSize || size.y > maxTextureSize) { + return {}; + } using Spaces = std::set<Space>; Spaces spaces {{{}, size}}; @@ -47,14 +55,27 @@ TexturePacker::pack(Size size) const } } else { - if (size.x < size.y) { - return pack({size.x * 2, size.y}); + const auto x = pack({size.x * 2, size.y}), y = pack({size.x, size.y * 2}); + if (!x.first.empty() && (y.first.empty() || area(x.second) < area(y.second))) { + return x; } - else { - return pack({size.x, size.y * 2}); + else if (!y.first.empty()) { + return y; } + return {}; } } + if (GLEW_ARB_texture_non_power_of_two) { + // Crop the size back to minimum size + size = std::transform_reduce( + result.begin(), result.end(), inputImages.begin(), Size {}, + [](auto && max, auto && limit) { + return glm::max(max, limit); + }, + [](auto && pos, auto && size) { + return pos + size; + }); + } return {result, size}; } |