Rendering Engine 0.2.17
Modular Graphics Rendering Engine | v0.2.17
Loading...
Searching...
No Matches
rendering_engine::TextureAtlasMaker Class Reference

Builds texture atlases from collections of images. More...

#include <texture_atlas_maker.hpp>

Public Member Functions

 TextureAtlasMaker (std::map< char, ImageData > images)
 Constructs an atlas maker from a collection of images.
 
 ~TextureAtlasMaker ()=default
 
bool CreateTextureAtlas (std::map< char, std::pair< unsigned int, unsigned int > > &texAtlasData, ImageData &texAtlasImage)
 Creates a texture atlas from the stored image collection.
 

Static Public Member Functions

static ImageData CreateTextureAtlas (std::unordered_map< std::uint32_t, std::pair< GlyphMetrics, ImageData > > &ioFontAtlas)
 Creates a texture atlas from glyph images and updates glyph metrics.
 

Protected Member Functions

void FindCellDimensions (unsigned int &outputWidth, unsigned int &outputHeight)
 Computes maximum cell dimensions for the stored images.
 
void CalculateGridDimensions (unsigned int &outputNumberOfColumns, unsigned int &outputNumberOfRows)
 Calculates grid dimensions for the stored images.
 

Static Protected Member Functions

static void FindCellDimensions (unsigned int &outputWidth, unsigned int &outputHeight, std::unordered_map< std::uint32_t, std::pair< GlyphMetrics, ImageData > > &fontAtlas)
 Computes maximum cell dimensions for glyph images.
 
static void CalculateGridDimensions (unsigned int &outputNumberOfColumns, unsigned int &outputNumberOfRows, std::unordered_map< std::uint32_t, std::pair< GlyphMetrics, ImageData > > &fontAtlas)
 Calculates grid dimensions for glyph images.
 

Protected Attributes

std::map< char, ImageData > mImageCollection
 

Static Protected Attributes

static const Color sDefaultPaletteBackgroundColor
 

Detailed Description

Builds texture atlases from collections of images.

Arranges input images into a grid-based atlas, aiming for a compact, near-square layout. Supports generic image atlases and font glyph atlases.

Definition at line 29 of file texture_atlas_maker.hpp.

Constructor & Destructor Documentation

◆ TextureAtlasMaker()

rendering_engine::TextureAtlasMaker::TextureAtlasMaker ( std::map< char, ImageData >  images)

Constructs an atlas maker from a collection of images.

Parameters
imagesMap of identifiers to image data.

Definition at line 10 of file texture_atlas_maker.cpp.

11{
12 mImageCollection = images;
13}
std::map< char, ImageData > mImageCollection

◆ ~TextureAtlasMaker()

rendering_engine::TextureAtlasMaker::~TextureAtlasMaker ( )
default

Member Function Documentation

◆ CalculateGridDimensions() [1/2]

void rendering_engine::TextureAtlasMaker::CalculateGridDimensions ( unsigned int &  outputNumberOfColumns,
unsigned int &  outputNumberOfRows 
)
protected

Calculates grid dimensions for the stored images.

Parameters
outputNumberOfColumnsOutput number of columns.
outputNumberOfRowsOutput number of rows.

Definition at line 144 of file texture_atlas_maker.cpp.

145{
146 unsigned int cellWidth;
147 unsigned int cellHeight;
148 FindCellDimensions(cellWidth, cellHeight);
149
150 const std::size_t cellsNum = mImageCollection.size();
151 const double totalSquare =
152 static_cast<double>(cellWidth) *
153 static_cast<double>(cellHeight) *
154 static_cast<double>(cellsNum);
155
156 const double rootSq = std::sqrt(totalSquare);
157
158 outputNumberOfColumns = static_cast<unsigned int>(
159 std::ceil(rootSq / static_cast<double>(cellWidth)));
160
161 outputNumberOfRows = static_cast<unsigned int>(
162 std::ceil(
163 static_cast<double>(cellsNum) /
164 static_cast<double>(outputNumberOfColumns)));
165}
void FindCellDimensions(unsigned int &outputWidth, unsigned int &outputHeight)
Computes maximum cell dimensions for the stored images.

◆ CalculateGridDimensions() [2/2]

void rendering_engine::TextureAtlasMaker::CalculateGridDimensions ( unsigned int &  outputNumberOfColumns,
unsigned int &  outputNumberOfRows,
std::unordered_map< std::uint32_t, std::pair< GlyphMetrics, ImageData > > &  fontAtlas 
)
staticprotected

Calculates grid dimensions for glyph images.

Parameters
outputNumberOfColumnsOutput number of columns.
outputNumberOfRowsOutput number of rows.
fontAtlasGlyph image collection.

Definition at line 167 of file texture_atlas_maker.cpp.

168{
169 unsigned int cellWidth;
170 unsigned int cellHeight;
171 FindCellDimensions(cellWidth, cellHeight, fontAtlas);
172
173 const std::size_t cellsNum = fontAtlas.size();
174
175 const double totalSquare =
176 static_cast<double>(cellWidth) *
177 static_cast<double>(cellHeight) *
178 static_cast<double>(cellsNum);
179
180 const double rootSq = std::sqrt(totalSquare);
181
182 outputNumberOfColumns = static_cast<unsigned int>(
183 std::ceil(rootSq / static_cast<double>(cellWidth)));
184
185 outputNumberOfRows = static_cast<unsigned int>(
186 std::ceil(
187 static_cast<double>(cellsNum) /
188 static_cast<double>(outputNumberOfColumns)));
189}

◆ CreateTextureAtlas() [1/2]

bool rendering_engine::TextureAtlasMaker::CreateTextureAtlas ( std::map< char, std::pair< unsigned int, unsigned int > > &  texAtlasData,
ImageData &  texAtlasImage 
)

Creates a texture atlas from the stored image collection.

Parameters
texAtlasDataOutput map of image identifiers to atlas positions.
texAtlasImageOutput atlas image.
Returns
True if the atlas was created successfully.

Definition at line 15 of file texture_atlas_maker.cpp.

16{
17 if( mImageCollection.size() == 0 )
18 {
19 return false;
20 }
21
22 //Find palette specification
23
24 //Find dimension of each cell
25 unsigned int cellWidth;
26 unsigned int cellHeight;
27 FindCellDimensions(cellWidth, cellHeight);
28
29 //Find number of rows and columns
30 unsigned int numberOfColumns;
31 unsigned int numberOfRows;
32 CalculateGridDimensions(numberOfColumns, numberOfRows);
33
34 //Create palette
35 ImageData textureAtlasPalette(static_cast<unsigned int>(cellWidth * numberOfColumns), static_cast<unsigned int>(cellHeight * numberOfRows));
36 textureAtlasPalette.Fill(sDefaultPaletteBackgroundColor);
37
38 auto imageIterator = mImageCollection.begin();
39
40 for(unsigned int y = 0; y < numberOfRows; y++)
41 {
42 for(unsigned int x = 0; x < numberOfColumns; x++)
43 {
44 if( imageIterator != mImageCollection.end() )
45 {
46 ImageData::DrawImageOnImageAtPos(x * cellWidth, y * cellHeight, textureAtlasPalette, imageIterator->second);
47
48 texAtlasData.emplace(imageIterator->first, std::make_pair<unsigned int, unsigned int>(x * cellWidth, y * cellHeight));
49
50
51 imageIterator++;
52 }
53 }
54 }
55 texAtlasImage = textureAtlasPalette;
56
57 return true;
58}
static void DrawImageOnImageAtPos(unsigned int const x, unsigned int const y, ImageData &toImage, ImageData &fromImage)
Overlays one image on top of another at a given position.
void CalculateGridDimensions(unsigned int &outputNumberOfColumns, unsigned int &outputNumberOfRows)
Calculates grid dimensions for the stored images.

◆ CreateTextureAtlas() [2/2]

ImageData rendering_engine::TextureAtlasMaker::CreateTextureAtlas ( std::unordered_map< std::uint32_t, std::pair< GlyphMetrics, ImageData > > &  ioFontAtlas)
static

Creates a texture atlas from glyph images and updates glyph metrics.

Modifies glyph metrics in-place to include atlas coordinates.

Parameters
ioFontAtlasMap of glyph identifiers to glyph metrics and images.
Returns
Generated atlas image.

Definition at line 60 of file texture_atlas_maker.cpp.

61{
62 if (ioFontAtlas.size() == 0)
63 {
64 return ImageData();
65 }
66
67 //Find palette specification
68
69 //Find dimension of each cell
70 unsigned int cellWidth;
71 unsigned int cellHeight;
72 FindCellDimensions(cellWidth, cellHeight, ioFontAtlas);
73
74 //Find number of rows and columns
75 unsigned int numberOfColumns;
76 unsigned int numberOfRows;
77 CalculateGridDimensions(numberOfColumns, numberOfRows, ioFontAtlas);
78
79 //Create palette
80 ImageData textureAtlasPalette(static_cast<unsigned int>(cellWidth * numberOfColumns), static_cast<unsigned int>(cellHeight * numberOfRows));
81 textureAtlasPalette.Fill(sDefaultPaletteBackgroundColor);
82
83 auto imageIterator = ioFontAtlas.begin();
84
85 for (unsigned int y = 0; y < numberOfRows; y++)
86 {
87 for (unsigned int x = 0; x < numberOfColumns; x++)
88 {
89 if (imageIterator != ioFontAtlas.end())
90 {
91 ImageData::DrawImageOnImageAtPos(x * cellWidth, y * cellHeight, textureAtlasPalette, imageIterator->second.second);
92
93 imageIterator->second.first.atlasX = static_cast<std::uint32_t>(x * cellWidth) + imageIterator->second.first.padding;
94 imageIterator->second.first.atlasY = static_cast<std::uint32_t>(y * cellHeight) + imageIterator->second.first.padding;
95
96 imageIterator++;
97 }
98 }
99 }
100
101 return textureAtlasPalette;
102}

◆ FindCellDimensions() [1/2]

void rendering_engine::TextureAtlasMaker::FindCellDimensions ( unsigned int &  outputWidth,
unsigned int &  outputHeight 
)
protected

Computes maximum cell dimensions for the stored images.

Parameters
outputWidthOutput cell width in pixels.
outputHeightOutput cell height in pixels.

Definition at line 104 of file texture_atlas_maker.cpp.

105{
106 unsigned int height = 0U;
107 unsigned int width = 0U;
108 for( auto& it : mImageCollection )
109 {
110 if( it.second.GetHeight() >= height )
111 {
112 height = it.second.GetHeight();
113 }
114 if( it.second.GetWidth() >= width )
115 {
116 width = it.second.GetWidth();
117 }
118 }
119
120 outputWidth = width;
121 outputHeight = height;
122}

◆ FindCellDimensions() [2/2]

void rendering_engine::TextureAtlasMaker::FindCellDimensions ( unsigned int &  outputWidth,
unsigned int &  outputHeight,
std::unordered_map< std::uint32_t, std::pair< GlyphMetrics, ImageData > > &  fontAtlas 
)
staticprotected

Computes maximum cell dimensions for glyph images.

Parameters
outputWidthOutput cell width in pixels.
outputHeightOutput cell height in pixels.
fontAtlasGlyph image collection.

Definition at line 124 of file texture_atlas_maker.cpp.

125{
126 unsigned int height = 0U;
127 unsigned int width = 0U;
128 for (auto& it : fontAtlas)
129 {
130 if (it.second.second.GetHeight() >= height)
131 {
132 height = it.second.second.GetHeight();
133 }
134 if (it.second.second.GetWidth() >= width)
135 {
136 width = it.second.second.GetWidth();
137 }
138 }
139
140 outputWidth = width;
141 outputHeight = height;
142}

Member Data Documentation

◆ mImageCollection

std::map<char, ImageData> rendering_engine::TextureAtlasMaker::mImageCollection
protected

Definition at line 94 of file texture_atlas_maker.hpp.

◆ sDefaultPaletteBackgroundColor

const Color rendering_engine::TextureAtlasMaker::sDefaultPaletteBackgroundColor
staticprotected

Definition at line 96 of file texture_atlas_maker.hpp.


The documentation for this class was generated from the following files: