Rendering Engine 0.2.17
Modular Graphics Rendering Engine | v0.2.17
Loading...
Searching...
No Matches
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(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}
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 (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}
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 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}
166
167void TextureAtlasMaker::CalculateGridDimensions(unsigned int& outputNumberOfColumns, unsigned int& outputNumberOfRows, std::unordered_map<std::uint32_t, std::pair<GlyphMetrics, ImageData>>& fontAtlas)
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}
190
191}// rendering_engine
Represents raw 2D image data stored in memory.
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 Fill(Color color)
Fills the image with a solid color.
unsigned int GetHeight() const
Returns the image height.
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.