-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFont.cpp
More file actions
218 lines (195 loc) · 6.41 KB
/
Copy pathFont.cpp
File metadata and controls
218 lines (195 loc) · 6.41 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//
// Created by Y500 on 26.03.2017.
//
#include "Font.h"
#include "../glm/ext.hpp"
Character::Character(const Character &origin)
{
texture = origin.texture;
size = origin.size;
bearing = origin.bearing;
advance = origin.advance;
}
Character& Character::operator=(const Character &rValue)
{
texture = rValue.texture;
size = rValue.size;
bearing = rValue.bearing;
advance = rValue.advance;
}
Font::Font(const FT_Library &library, const std::string &path, FT_Long faceIndex, FT_UInt height, FT_UInt width) :
errorStream(std::cout.rdbuf())
{
if(FT_New_Face(library, path.c_str(), faceIndex, &face))
errorStream << "Failed to load font\n";
FT_Set_Pixel_Sizes(face, width, height);
glyphCoord.setType(GL_ARRAY_BUFFER);
glyphCoord.bind();
glyphCoord.data(6 * 4 * sizeof(GLfloat), NULL, GL_DYNAMIC_DRAW);
}
Font::~Font()
{
FT_Done_Face(face);
}
void Font::setErrorStream(const std::ostream &inErrorStream)
{
errorStream.rdbuf(inErrorStream.rdbuf());
}
void Font::setOrthoMatrix(GLfloat left, GLfloat right, GLfloat top, GLfloat bottom)
{
projection = glm::ortho(left, right, bottom, top);
}
void Font::setScale(GLfloat inScale)
{
scale = inScale;
}
void Font::setColor(const glm::vec3 &inColor)
{
color = inColor;
}
void Font::loadProgram()
{
const char vertexShader[] =
"#version 330 core\n"
"layout (location = 0) in vec4 vertex;\n"
"out vec2 TexCoords;\n"
"uniform mat4 projection;\n"
"\n"
"void main(void) {\n"
" gl_Position = projection * vec4(vertex.xy, 0.0, 1.0);\n"
" TexCoords = vertex.zw;\n"
"}\n";
const char fragmentShader[] =
"#version 330 core\n"
"in vec2 TexCoords;\n"
"out vec4 color;\n"
"uniform sampler2D text;\n"
"uniform vec3 textColor;\n"
"\n"
"void main(void) {\n"
" vec4 sampled = vec4(1.0, 1.0, 1.0, texture(text, TexCoords).r);\n"
" color = vec4(textColor, 1.0) * sampled;\n"
"}\n";
program.setErrorStream(errorStream);
if(!program.loadShaderFromString(vertexShader, GL_VERTEX_SHADER))
program.printError();
if(!program.loadShaderFromString(fragmentShader, GL_FRAGMENT_SHADER))
program.printError();
if(!program.link())
program.printError();
}
void Font::load(const FT_Library &library, const std::string &path, FT_Long faceIndex, FT_UInt height, FT_UInt width)
{
FT_New_Face(library, path.c_str(), faceIndex, &face);
FT_Set_Pixel_Sizes(face, width, height);
glyphCoord.setType(GL_ARRAY_BUFFER);
glyphCoord.bind();
glyphCoord.data(6 * 4 * sizeof(GLfloat), NULL, GL_DYNAMIC_DRAW);
}
Font& Font::operator()(GLint x, GLint y)
{
startPosition.x = x;
startPosition.y = y;
return *this;
}
Font& Font::render(const std::string &str)
{
program.use();
program.bindUniformVector(SP_VEC3, "textColor", glm::value_ptr(color));
program.bindUniformMatrix(SP_MAT4, "projection", glm::value_ptr(projection), GL_FALSE);
glActiveTexture(GL_TEXTURE0);
std::string::const_iterator iter;
for(iter = str.begin(); iter != str.end(); ++iter)
{
glyphToTexture(*iter);
Character character = characters[*iter];
GLfloat xPos = startPosition.x + character.bearing.x * scale;
GLfloat yPos = startPosition.y + (character.size.y - character.bearing.y) * scale;
GLfloat width = character.size.x * scale;
GLfloat height = character.size.y * scale;
GLfloat vertices[6][4] = {
{xPos, yPos - height, 0.0f, 0.0f},
{xPos, yPos, 0.0f, 1.0f},
{xPos + width, yPos, 1.0f, 1.0f},
{xPos, yPos - height, 0.0f, 0.0f},
{xPos + width, yPos, 1.0f, 1.0f},
{xPos + width, yPos - height, 1.0f, 0.0f}
};
character.texture.bind();
glyphCoord.bind();
glyphCoord.subData(0, sizeof(vertices), vertices);
program.bindAttributeData("vertex", 4, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_TRIANGLES, 0, 6);
startPosition.x += (character.advance >> 6) * scale;
}
glyphCoord.unbind();
program.unuse();
glBindTexture(GL_TEXTURE_2D, 0);
return *this;
}
Font& Font::render(const char *str)
{
std::string string(str);
return render(string);
}
Font& Font::render(char character)
{
stringstream.str("");
stringstream << character;
return render(stringstream.str());
}
Font& Font::render(GLfloat value)
{
stringstream.str("");
stringstream << value;
return render(stringstream.str());
}
Font& Font::render(GLdouble value)
{
stringstream.str("");
stringstream << value;
return render(stringstream.str());
}
Font& Font::render(GLint value)
{
stringstream.str("");
stringstream << value;
return render(stringstream.str());
}
Font& Font::render(GLuint value)
{
stringstream.str("");
stringstream << value;
return render(stringstream.str());
}
Font& Font::render(bool value)
{
stringstream.str("");
stringstream << std::boolalpha << value;
return render(stringstream.str());
}
void Font::glyphToTexture(GLchar character)
{
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
if(characters.count(character) == 0)
{
if(FT_Load_Char(face, character, FT_LOAD_RENDER))
{
errorStream << "Failed to load " << character << " glyph\n";
return;
}
Character glyph;
glyph.texture.setType(GL_TEXTURE_2D);
glyph.texture.bind();
glyph.texture.specifyImage(0, GL_RED, face->glyph->bitmap.width, face->glyph->bitmap.rows, 0, GL_RED,
GL_UNSIGNED_BYTE, face->glyph->bitmap.buffer);
glyph.texture.parameter(GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glyph.texture.parameter(GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glyph.texture.parameter(GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glyph.texture.parameter(GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glyph.size = glm::ivec2(face->glyph->bitmap.width, face->glyph->bitmap.rows);
glyph.bearing = glm::ivec2(face->glyph->bitmap_left, face->glyph->bitmap_top);
glyph.advance = face->glyph->advance.x;
characters[character] = glyph;
}
}