From e4629eb9ef1f1b22d42f03852bb914461e0b6e02 Mon Sep 17 00:00:00 2001 From: Dan Paulat Date: Wed, 5 Oct 2022 23:10:23 -0500 Subject: [PATCH] Expose texture attributes in interface --- scwx-qt/source/scwx/qt/util/texture_atlas.cpp | 54 +++++++++++++------ scwx-qt/source/scwx/qt/util/texture_atlas.hpp | 42 +++++++++++++++ 2 files changed, 80 insertions(+), 16 deletions(-) diff --git a/scwx-qt/source/scwx/qt/util/texture_atlas.cpp b/scwx-qt/source/scwx/qt/util/texture_atlas.cpp index 6047550e..7ff156f4 100644 --- a/scwx-qt/source/scwx/qt/util/texture_atlas.cpp +++ b/scwx-qt/source/scwx/qt/util/texture_atlas.cpp @@ -23,17 +23,6 @@ namespace util static const std::string logPrefix_ = "scwx::qt::util::texture_atlas"; static const auto logger_ = scwx::util::Logger::Create(logPrefix_); -struct TextureInfo -{ - TextureInfo(boost::gil::point_t position, boost::gil::point_t size) : - position_ {position}, size_ {size} - { - } - - boost::gil::point_t position_; - boost::gil::point_t size_; -}; - class TextureAtlas::Impl { public: @@ -52,9 +41,9 @@ public: std::unordered_map texturePathMap_; std::shared_mutex texturePathMutex_; - boost::gil::rgba8_image_t atlas_; - std::unordered_map atlasMap_; - std::shared_mutex atlasMutex_; + boost::gil::rgba8_image_t atlas_; + std::unordered_map atlasMap_; + std::shared_mutex atlasMutex_; }; TextureAtlas::TextureAtlas() : p(std::make_unique()) {} @@ -146,6 +135,11 @@ void TextureAtlas::BuildAtlas(size_t width, size_t height) // Populate atlas logger_->trace("Populating atlas"); + const float xStep = 1.0f / width; + const float yStep = 1.0f / height; + const float xMin = xStep * 0.5f; + const float yMin = yStep * 0.5f; + for (size_t i = 0; i < images.size(); i++) { // If the image was packed successfully @@ -164,12 +158,26 @@ void TextureAtlas::BuildAtlas(size_t width, size_t height) boost::gil::copy_pixels(imageView, atlasSubView); // Add texture image to the index + const stbrp_coord x = stbrpRects[i].x; + const stbrp_coord y = stbrpRects[i].y; + + const float sLeft = x * xStep + xMin; + const float sRight = + sLeft + static_cast(imageView.width() - 1) / width; + const float tTop = y * yStep + yMin; + const float tBottom = + tTop + static_cast(imageView.height() - 1) / height; + p->atlasMap_.emplace( std::piecewise_construct, std::forward_as_tuple(images[i].first), std::forward_as_tuple( - boost::gil::point_t {stbrpRects[i].x, stbrpRects[i].y}, - boost::gil::point_t {imageView.width(), imageView.height()})); + boost::gil::point_t {x, y}, + boost::gil::point_t {imageView.width(), imageView.height()}, + sLeft, + sRight, + tTop, + tBottom)); } else { @@ -222,6 +230,20 @@ GLuint TextureAtlas::BufferAtlas(gl::OpenGLFunctions& gl) return texture; } +TextureAttributes TextureAtlas::GetTextureAttributes(const std::string& name) +{ + TextureAttributes attr {}; + std::shared_lock lock(p->atlasMutex_); + + const auto& it = p->atlasMap_.find(name); + if (it != p->atlasMap_.cend()) + { + attr = it->second; + } + + return attr; +} + boost::gil::rgba8_image_t TextureAtlas::Impl::LoadImage(const std::string& imagePath) { diff --git a/scwx-qt/source/scwx/qt/util/texture_atlas.hpp b/scwx-qt/source/scwx/qt/util/texture_atlas.hpp index b442f540..bf904e6c 100644 --- a/scwx-qt/source/scwx/qt/util/texture_atlas.hpp +++ b/scwx-qt/source/scwx/qt/util/texture_atlas.hpp @@ -5,6 +5,8 @@ #include #include +#include + namespace scwx { namespace qt @@ -12,6 +14,44 @@ namespace qt namespace util { +struct TextureAttributes +{ + TextureAttributes() : + valid_ {false}, + position_ {}, + size_ {}, + sLeft_ {}, + sRight_ {}, + tTop_ {}, + tBottom_ {} + { + } + + TextureAttributes(boost::gil::point_t position, + boost::gil::point_t size, + float sLeft, + float sRight, + float tTop, + float tBottom) : + valid_ {true}, + position_ {position}, + size_ {size}, + sLeft_ {sLeft}, + sRight_ {sRight}, + tTop_ {tTop}, + tBottom_ {tBottom} + { + } + + bool valid_; + boost::gil::point_t position_; + boost::gil::point_t size_; + float sLeft_; + float sRight_; + float tTop_; + float tBottom_; +}; + class TextureAtlas { public: @@ -30,6 +70,8 @@ public: void BuildAtlas(size_t width, size_t height); GLuint BufferAtlas(gl::OpenGLFunctions& gl); + TextureAttributes GetTextureAttributes(const std::string& name); + private: class Impl;