53 ERROR(
"Text not initialized");
57 if (FT_Init_FreeType(&ft))
59 ERROR(
"Could not init FreeType library");
64 std::filesystem::absolute(
"engine/shaders/text.vs"),
66 std::filesystem::absolute(
"engine/shaders/text.fs"));
67 text_shader =
"TextShader";
71 std::string font_name = std::filesystem::absolute(
"assets/fonts/" + font);
72 if (font_name.empty())
74 ERROR(
"Could not find font");
79 if (FT_New_Face(ft, font_name.c_str(), 0, &face))
81 ERROR(
"Could not load font");
87 FT_Set_Pixel_Sizes(face, 0, 48);
90 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
93 for (
unsigned char c = 0; c < 128; c++)
96 if (FT_Load_Char(face, c, FT_LOAD_RENDER))
98 ERROR(
"Could not load glyph");
105 glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, face->glyph->bitmap.width,
106 face->glyph->bitmap.rows, 0, GL_RED, GL_UNSIGNED_BYTE,
107 face->glyph->bitmap.buffer);
109 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
110 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
111 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
112 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
116 glm::ivec2(face->glyph->bitmap.width, face->glyph->bitmap.rows),
117 glm::ivec2(face->glyph->bitmap_left, face->glyph->bitmap_top),
118 static_cast<unsigned int>(face->glyph->advance.x)};
119 characters.insert(std::pair<char, character>(c, character_));
125 FT_Done_FreeType(ft);
130 glBufferData(GL_ARRAY_BUFFER,
sizeof(
float) * 6 * 4, NULL, GL_DYNAMIC_DRAW);
131 glEnableVertexAttribArray(0);
132 glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 *
sizeof(
float), 0);
142 ERROR(
"Text not initialized");
149 glUniform3f(glGetUniformLocation(textShaderId,
"textColor"), color.x,
152 glm::mat4 projection =
155 glUniformMatrix4fv(glGetUniformLocation(textShaderId,
"projection"), 1,
156 GL_FALSE, glm::value_ptr(projection));
158 glActiveTexture(GL_TEXTURE0);
162 std::string::const_iterator c;
163 for (c =
text.begin(); c !=
text.end(); c++)
167 float xpos = x + ch.bearing.x * scale;
168 float ypos = y - (ch.size.y - ch.bearing.y) * scale;
170 float w = ch.size.x * scale;
171 float h = ch.size.y * scale;
175 float vertices[6][4] = {
176 {xpos, ypos + h, 0.0, 0.0}, {xpos, ypos, 0.0, 1.0},
177 {xpos + w, ypos, 1.0, 1.0},
179 {xpos, ypos + h, 0.0, 0.0}, {xpos + w, ypos, 1.0, 1.0},
180 {xpos + w, ypos + h, 1.0, 0.0}};
183 glBindTexture(GL_TEXTURE_2D, ch.texture_id);
186 glBindBuffer(GL_ARRAY_BUFFER, text_vbo.
id);
187 glBufferSubData(GL_ARRAY_BUFFER, 0,
sizeof(vertices), vertices);
189 glBindBuffer(GL_ARRAY_BUFFER, 0);
191 glDrawArrays(GL_TRIANGLES, 0, 6);
194 x += (ch.advance >> 6)
197 glBindVertexArray(0);
198 glBindTexture(GL_TEXTURE_2D, 0);
static void render_text(std::string text, float x, float y, float scale, glm::vec3 color)
Render text.
static void init()
Initialize the text subsystem.
static void load(std::string font_name, unsigned int font_size)
Load a font.
static std::map< char, types::character > characters
Map of characters.
static void bind_texture(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.