/
Utility.cpp
176 lines (129 loc) · 5 KB
/
Utility.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "Utility.h"
#include <fstream>
#include <stdexcept>
#include <unordered_map>
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
#define TINYOBJLOADER_IMPLEMENTATION
#include <tiny_obj_loader.h>
#include <ft2build.h>
#include FT_FREETYPE_H
void Utility::LoadImage(const char* file_name, Image& texture) {
int texture_channels;
texture.pixels = stbi_load(file_name, &texture.texture_width, &texture.texture_height, &texture_channels, STBI_rgb_alpha);
if (!texture.pixels) {
throw std::runtime_error("failed to load texture image");
}
}
void Utility::FreeImage(Image& texture) {
stbi_image_free(texture.pixels);
}
void Utility::LoadModel(const char* file_name, std::vector<Vertex_Texture>& vertices, std::vector<uint32_t>& indices) {
tinyobj::attrib_t attrib;
std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials;
std::string warn, err;
if (!tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err, file_name)) {
throw std::runtime_error(warn + err);
}
std::unordered_map<Vertex_Texture, uint32_t, Vertex_Texture_Hash> unique_vertices = {};
for (const auto& shape : shapes) {
for (const auto& index : shape.mesh.indices) {
Vertex_Texture vertex = {};
vertex.pos = {
attrib.vertices[static_cast<size_t>(index.vertex_index) * 3 + 0],
attrib.vertices[static_cast<size_t>(index.vertex_index) * 3 + 1],
attrib.vertices[static_cast<size_t>(index.vertex_index) * 3 + 2]
};
vertex.texCoord = {
attrib.texcoords[static_cast<size_t>(index.texcoord_index) * 2 + 0],
1.0f - attrib.texcoords[static_cast<size_t>(index.texcoord_index) * 2 + 1]
};
if (unique_vertices.count(vertex) == 0) {
unique_vertices[vertex] = static_cast<uint32_t>(vertices.size());
vertices.push_back(vertex);
}
indices.push_back(unique_vertices[vertex]);
}
}
}
std::vector<unsigned char> Utility::ReadFile(const std::string& file_name) {
std::ifstream file(file_name, std::ios::ate | std::ios::binary);
if (!file.is_open()) {
throw std::runtime_error(std::string{"failed to open file "}+file_name);
}
size_t file_size = (size_t)file.tellg();
std::vector<unsigned char> buffer(file_size);
file.seekg(0);
file.read(reinterpret_cast<char*>(buffer.data()), file_size);
file.close();
return buffer;
}
void Utility::LoadFontImage(const char* file_name, uint32_t font_size, FontImage& font_image, float& font_image_size, uint32_t& height, std::map<unsigned char, FontCharacter>& character_map) {
FT_Library ft;
if (FT_Init_FreeType(&ft)) {
throw std::runtime_error("unable to initialize font library");
}
FT_Face face;
if (FT_New_Face(ft, file_name, 0, &face)) {
throw std::runtime_error("unable to load font");
}
FT_Set_Pixel_Sizes(face, 0, font_size);
uint32_t size;
for (size = 128; size < 4096; size *= 2) {
font_image.width = size;
font_image.height = size;
font_image.pixels = new unsigned char[font_image.width * font_image.height];
memset(font_image.pixels, 0, font_image.width * font_image.height);
bool resize = false;
uint32_t x = 0;
uint32_t y = 0;
uint32_t my = 0;
height = 0;
for (int i = 0; i < 256; i++) {
int index = FT_Get_Char_Index(face, i);
if (index == 0) {
continue;
}
if (FT_Load_Glyph(face, index, FT_LOAD_RENDER)) {
throw std::runtime_error("failed to load glyph");
}
FT_Bitmap b = face->glyph->bitmap;
if (x + b.width > font_image.width) {
x = 0;
y = my + 1;
}
if (y + b.rows > font_image.height) {
resize = true;
continue;
}
if (b.rows > height) {
height = b.rows;
}
unsigned char* src = b.buffer;
unsigned char* dst = font_image.pixels + y * font_image.width + x;
for (uint32_t r = 0; r < b.rows; r++) {
memcpy(dst, src, b.width);
dst += font_image.width;
src += b.width;
}
character_map[i] = {(uint16_t)x, (uint16_t)y, (uint8_t)(face->glyph->advance.x >> 6), (uint8_t)b.width, (uint8_t)b.rows, (uint8_t)face->glyph->bitmap_left, (uint8_t)face->glyph->bitmap_top};
x += b.width + 1;
if (y + b.rows > my) {
my = y + b.rows;
}
}
if (!resize) {
break;
}
character_map.clear();
delete[] font_image.pixels;
}
height = height * 3 / 2;
FT_Done_Face(face);
FT_Done_FreeType(ft);
font_image_size = static_cast<float>(size);
}
void Utility::FreeFontImage(FontImage& font) {
delete[] font.pixels;
}