Rendering Engine 0.2.9
Modular Graphics Rendering Engine | v0.2.9
texture_atlas_maker.cpp
Go to the documentation of this file.
2#include <cmath>
3#include "font_resources.hpp"
4
5namespace rendering_engine
6{
7
9
10TextureAtlasMaker::TextureAtlasMaker(std::map<char, ImageData> images)
11{
12 mImageCollection = images;
13}
14
15bool TextureAtlasMaker::CreateTextureAtlas(std::map<char, std::pair<unsigned int, unsigned int>>& texAtlasData, ImageData& texAtlasImage)
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}
59
60ImageData TextureAtlasMaker::CreateTextureAtlas(std::unordered_map<std::uint32_t, std::pair<GlyphMetrics, ImageData>>& ioFontAtlas)
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}
103
104void TextureAtlasMaker::FindCellDimensions(unsigned int& outputWidth, unsigned int& outputHeight)
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}
123
124void TextureAtlasMaker::FindCellDimensions(unsigned int& outputWidth, unsigned int& outputHeight, std::unordered_map<std::uint32_t, std::pair<GlyphMetrics, ImageData>>& fontAtlas)
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}
143
144void TextureAtlasMaker::CalculateGridDimensions(unsigned int& outputNumberOfColumns, unsigned int& outputNumberOfRows)
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}
157
158void TextureAtlasMaker::CalculateGridDimensions(unsigned int& outputNumberOfColumns, unsigned int& outputNumberOfRows, std::unordered_map<std::uint32_t, std::pair<GlyphMetrics, ImageData>>& fontAtlas)
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}
171
172}// rendering_engine
Represents raw 2D image data stored in memory.
Definition: image_data.hpp:80
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 Fill(Color color)
Fills the image with a solid color.
Definition: image_data.cpp:135
void FindCellDimensions(unsigned int &outputWidth, unsigned int &outputHeight)
Computes maximum cell dimensions for the stored images.
bool CreateTextureAtlas(std::map< char, std::pair< unsigned int, unsigned int > > &texAtlasData, ImageData &texAtlasImage)
Creates a texture atlas from the stored image collection.
TextureAtlasMaker(std::map< char, ImageData > images)
Constructs an atlas maker from a collection of images.
std::map< char, ImageData > mImageCollection
void CalculateGridDimensions(unsigned int &outputNumberOfColumns, unsigned int &outputNumberOfRows)
Calculates grid dimensions for the stored images.