From d7d5cd4265aab0b939b57ea7237b56f2f5840642 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 11 Mar 2023 12:07:29 +0000 Subject: Initial version of texture packer Determines where a collection of smaller textures can be tiled into a single bigger image. Probably non-optimal. --- assetFactory/texturePacker.cpp | 77 ++++++++++++++++++++++++++++++++++++++++++ assetFactory/texturePacker.h | 36 ++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 assetFactory/texturePacker.cpp create mode 100644 assetFactory/texturePacker.h (limited to 'assetFactory') diff --git a/assetFactory/texturePacker.cpp b/assetFactory/texturePacker.cpp new file mode 100644 index 0000000..31c9a0e --- /dev/null +++ b/assetFactory/texturePacker.cpp @@ -0,0 +1,77 @@ +#include "texturePacker.h" +#include +#include +#include +#include +#include + +TexturePacker::TexturePacker(std::vector in) : inputImages {std::move(in)} +{ + std::sort(inputImages.rbegin(), inputImages.rend(), [](const auto & a, const auto & b) { + return area(a) < area(b); + }); +} + +TexturePacker::Result +TexturePacker::pack() const +{ + return pack(minSize()); +} + +TexturePacker::Result +TexturePacker::pack(Size size) const +{ + using Spaces = std::set; + Spaces spaces {{{}, size}}; + + Positions result; + for (const auto & image : inputImages) { + if (const auto spaceItr = std::find_if(spaces.begin(), spaces.end(), + [image](const Space & s) { + return image.x <= s.size.x && image.y <= s.size.y; + }); + spaceItr != spaces.end()) { + auto space = *spaceItr; + result.push_back(space.position); + spaces.erase(spaceItr); + if (space.size.x > image.x) { + spaces.emplace(Position {space.position.x + image.x, space.position.y}, + Size {space.size.x - image.x, image.y}); + } + if (space.size.y > image.y) { + spaces.emplace(Position {space.position.x, space.position.y + image.y}, + Size {space.size.x, space.size.y - image.y}); + } + } + else { + if (size.x < size.y) { + return pack({size.x * 2, size.y}); + } + else { + return pack({size.x, size.y * 2}); + } + } + } + + return {result, size}; +} + +TexturePacker::Size +TexturePacker::minSize() const +{ + return std::accumulate(inputImages.begin(), inputImages.end(), Size {1}, [](Size size, const Image & i) { + while (size.x < i.x) { + size.x *= 2; + } + while (size.y < i.y) { + size.y *= 2; + } + return size; + }); +} + +decltype(TexturePacker::Size::x) +TexturePacker::area(const Size & size) +{ + return size.x * size.y; +} diff --git a/assetFactory/texturePacker.h b/assetFactory/texturePacker.h new file mode 100644 index 0000000..8e2061b --- /dev/null +++ b/assetFactory/texturePacker.h @@ -0,0 +1,36 @@ +#pragma once + +#include +#include +#include + +class TexturePacker { +public: + using Position = glm::uvec2; + using Size = glm::uvec2; + + struct Area { + Position position; + Size size; + bool + operator<(const Area & other) const + { + return area(size) < area(other.size); + } + }; + using Image = Size; + using Space = Area; + using Positions = std::vector; + using Result = std::pair; + + TexturePacker(std::vector); + + Result pack(Size) const; + Result pack() const; + + Size minSize() const; + static decltype(Size::x) area(const Size & size); + +private: + std::vector inputImages; +}; -- cgit v1.2.3