Expose texture attributes in interface

This commit is contained in:
Dan Paulat 2022-10-05 23:10:23 -05:00
parent 71d873f4b4
commit e4629eb9ef
2 changed files with 80 additions and 16 deletions

View file

@ -23,17 +23,6 @@ namespace util
static const std::string logPrefix_ = "scwx::qt::util::texture_atlas"; static const std::string logPrefix_ = "scwx::qt::util::texture_atlas";
static const auto logger_ = scwx::util::Logger::Create(logPrefix_); 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 class TextureAtlas::Impl
{ {
public: public:
@ -53,7 +42,7 @@ public:
std::shared_mutex texturePathMutex_; std::shared_mutex texturePathMutex_;
boost::gil::rgba8_image_t atlas_; boost::gil::rgba8_image_t atlas_;
std::unordered_map<std::string, TextureInfo> atlasMap_; std::unordered_map<std::string, TextureAttributes> atlasMap_;
std::shared_mutex atlasMutex_; std::shared_mutex atlasMutex_;
}; };
@ -146,6 +135,11 @@ void TextureAtlas::BuildAtlas(size_t width, size_t height)
// Populate atlas // Populate atlas
logger_->trace("Populating 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++) for (size_t i = 0; i < images.size(); i++)
{ {
// If the image was packed successfully // 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); boost::gil::copy_pixels(imageView, atlasSubView);
// Add texture image to the index // 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<float>(imageView.width() - 1) / width;
const float tTop = y * yStep + yMin;
const float tBottom =
tTop + static_cast<float>(imageView.height() - 1) / height;
p->atlasMap_.emplace( p->atlasMap_.emplace(
std::piecewise_construct, std::piecewise_construct,
std::forward_as_tuple(images[i].first), std::forward_as_tuple(images[i].first),
std::forward_as_tuple( std::forward_as_tuple(
boost::gil::point_t {stbrpRects[i].x, stbrpRects[i].y}, boost::gil::point_t {x, y},
boost::gil::point_t {imageView.width(), imageView.height()})); boost::gil::point_t {imageView.width(), imageView.height()},
sLeft,
sRight,
tTop,
tBottom));
} }
else else
{ {
@ -222,6 +230,20 @@ GLuint TextureAtlas::BufferAtlas(gl::OpenGLFunctions& gl)
return texture; 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 boost::gil::rgba8_image_t
TextureAtlas::Impl::LoadImage(const std::string& imagePath) TextureAtlas::Impl::LoadImage(const std::string& imagePath)
{ {

View file

@ -5,6 +5,8 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include <boost/gil/point.hpp>
namespace scwx namespace scwx
{ {
namespace qt namespace qt
@ -12,6 +14,44 @@ namespace qt
namespace util 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 class TextureAtlas
{ {
public: public:
@ -30,6 +70,8 @@ public:
void BuildAtlas(size_t width, size_t height); void BuildAtlas(size_t width, size_t height);
GLuint BufferAtlas(gl::OpenGLFunctions& gl); GLuint BufferAtlas(gl::OpenGLFunctions& gl);
TextureAttributes GetTextureAttributes(const std::string& name);
private: private:
class Impl; class Impl;