54 ERROR(
"Text not initialized");
58 if (FT_Init_FreeType(&ft))
60 ERROR(
"Could not init FreeType library");
65 std::filesystem::absolute(
"engine/shaders/text.vs"),
67 std::filesystem::absolute(
"engine/shaders/text.fs"));
68 textShader =
"TextShader";
72 std::string font_name = std::filesystem::absolute(
"assets/fonts/" + font);
73 if (font_name.empty())
75 ERROR(
"Could not find font");
80 if (FT_New_Face(ft, font_name.c_str(), 0, &face))
82 ERROR(
"Could not load font");
88 FT_Set_Pixel_Sizes(face, 0, 48);
91 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
94 for (
unsigned char c = 0; c < 128; c++)
97 if (FT_Load_Char(face, c, FT_LOAD_RENDER))
99 ERROR(
"Could not load glyph");
103 unsigned int texture;
104 glGenTextures(1, &texture);
106 glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, face->glyph->bitmap.width,
107 face->glyph->bitmap.rows, 0, GL_RED, GL_UNSIGNED_BYTE,
108 face->glyph->bitmap.buffer);
110 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
111 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
112 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
113 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
117 glm::ivec2(face->glyph->bitmap.width, face->glyph->bitmap.rows),
118 glm::ivec2(face->glyph->bitmap_left, face->glyph->bitmap_top),
119 static_cast<unsigned int>(face->glyph->advance.x)};
120 characters.insert(std::pair<char, Character>(c, character));
126 FT_Done_FreeType(ft);
131 glBufferData(GL_ARRAY_BUFFER,
sizeof(
float) * 6 * 4, NULL, GL_DYNAMIC_DRAW);
132 glEnableVertexAttribArray(0);
133 glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 *
sizeof(
float), 0);
141 if (textVao.
GetVAO() == 0)
143 ERROR(
"Text not initialized");
150 glUniform3f(glGetUniformLocation(textShaderId,
"textColor"), color.x,
153 glm::mat4 projection =
156 glUniformMatrix4fv(glGetUniformLocation(textShaderId,
"projection"), 1,
157 GL_FALSE, glm::value_ptr(projection));
159 glActiveTexture(GL_TEXTURE0);
163 std::string::const_iterator c;
164 for (c = text.begin(); c != text.end(); c++)
168 float xpos = x + ch.Bearing.x * scale;
169 float ypos = y - (ch.Size.y - ch.Bearing.y) * scale;
171 float w = ch.Size.x * scale;
172 float h = ch.Size.y * scale;
176 float vertices[6][4] = {
177 {xpos, ypos + h, 0.0, 0.0}, {xpos, ypos, 0.0, 1.0},
178 {xpos + w, ypos, 1.0, 1.0},
180 {xpos, ypos + h, 0.0, 0.0}, {xpos + w, ypos, 1.0, 1.0},
181 {xpos + w, ypos + h, 1.0, 0.0}};
184 glBindTexture(GL_TEXTURE_2D, ch.TextureID);
187 glBindBuffer(GL_ARRAY_BUFFER, textVbo.
id);
188 glBufferSubData(GL_ARRAY_BUFFER, 0,
sizeof(vertices), vertices);
190 glBindBuffer(GL_ARRAY_BUFFER, 0);
192 glDrawArrays(GL_TRIANGLES, 0, 6);
195 x += (ch.Advance >> 6)
198 glBindVertexArray(0);
199 glBindTexture(GL_TEXTURE_2D, 0);
static void Load(std::string font_name, unsigned int fontSize)
Load a font.
static void RenderText(std::string text, float x, float y, float scale, glm::vec3 color)
Render text.
static std::map< char, Types::Character > characters
Map of characters.
static void Init()
Initialize the text subsystem.
static void BindTexture(GLenum target, unsigned int texture, GLint wrapping=GL_REPEAT, GLint filtering_min=GL_NEAREST, GLint filtering_mag=GL_NEAREST, GLboolean hasMipmap=GL_TRUE, GLint mipmap_min=GL_LINEAR_MIPMAP_LINEAR, GLint mipmap_mag=GL_LINEAR)
Bind a texture.