Rendering Engine 0.2.9
Modular Graphics Rendering Engine | v0.2.9
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. More...
 
 ~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. More...
 

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. More...
 

Protected Member Functions

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

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. More...
 
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. More...
 

Protected Attributes

std::map< char, ImageDatamImageCollection
 

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 28 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 unsigned int cellsNum = mImageCollection.size();
151 int totalSquare = cellWidth * cellHeight * cellsNum;
152 float rootSq = sqrtf(totalSquare);
153
154 outputNumberOfColumns = std::ceil(rootSq / cellWidth);
155 outputNumberOfRows = std::ceil((float)(cellsNum) / (float)(outputNumberOfColumns));
156}
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 158 of file texture_atlas_maker.cpp.

159{
160 unsigned int cellWidth;
161 unsigned int cellHeight;
162 FindCellDimensions(cellWidth, cellHeight, fontAtlas);
163
164 unsigned int cellsNum = fontAtlas.size();
165 int totalSquare = cellWidth * cellHeight * cellsNum;
166 float rootSq = sqrtf(totalSquare);
167
168 outputNumberOfColumns = std::ceil(rootSq / cellWidth);
169 outputNumberOfRows = std::ceil((float)(cellsNum) / (float)(outputNumberOfColumns));
170}

◆ 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( int y = 0; y < numberOfRows; y++ )
41 {
42 for( 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.
Definition: image_data.cpp:159
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 (int y = 0; y < numberOfRows; y++)
86 {
87 for (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 93 of file texture_atlas_maker.hpp.

◆ sDefaultPaletteBackgroundColor

const Color rendering_engine::TextureAtlasMaker::sDefaultPaletteBackgroundColor
staticprotected

Definition at line 95 of file texture_atlas_maker.hpp.


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