This commit is contained in:
viktorljung
2016-01-21 10:16:03 +01:00
parent ce27b224df
commit f3bfe388af
6 changed files with 99 additions and 55 deletions
+17 -12
View File
@@ -15,6 +15,10 @@ Font::Font(std::string path)
filePath = (*it).c_str();
it++;
if (it != tok.end()) {
if((*it).c_str() == "") {
throw std::runtime_error("");
}
try {
FontSize = boost::lexical_cast<int>((*it).c_str());
} catch (boost::bad_lexical_cast const&) {
@@ -28,21 +32,22 @@ Font::Font(std::string path)
FT_Library library;
FT_Face face;
if (FT_Init_FreeType(&library)) {
LOG_ERROR("FreeType error: init failed");
throw std::runtime_error("");;
}
if (FT_New_Face(library, filePath.c_str(), 0, &Face)) {
if (FT_New_Face(library, filePath.c_str(), 0, &face)) {
LOG_ERROR("FreeType error: loading font");
throw std::runtime_error("");;
}
FT_Set_Char_Size(Face, 0, FontSize*64, 300, 300); // temp
FT_Set_Pixel_Sizes(Face, 0, FontSize); //
FT_Set_Char_Size(face, 0, FontSize*64, 300, 300); // temp
FT_Set_Pixel_Sizes(face, 0, FontSize); //
if (FT_Load_Char(Face, 'X', FT_LOAD_RENDER)) {
if (FT_Load_Char(face, 'X', FT_LOAD_RENDER)) {
LOG_ERROR("FreeType error: loading char");
throw std::runtime_error("");;
}
@@ -53,7 +58,7 @@ Font::Font(std::string path)
//Load character glyph
if (FT_Load_Char(Face, c, FT_LOAD_RENDER)) {
if (FT_Load_Char(face, c, FT_LOAD_RENDER)) {
continue;
}
@@ -66,12 +71,12 @@ Font::Font(std::string path)
GL_TEXTURE_2D,
0,
GL_RED,
Face->glyph->bitmap.width,
Face->glyph->bitmap.rows,
face->glyph->bitmap.width,
face->glyph->bitmap.rows,
0,
GL_RED,
GL_UNSIGNED_BYTE,
Face->glyph->bitmap.buffer
face->glyph->bitmap.buffer
);
// Set texture options
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
@@ -81,22 +86,22 @@ Font::Font(std::string path)
// Now store character for later use
Character character = {
texture,
glm::ivec2(Face->glyph->bitmap.width, Face->glyph->bitmap.rows),
glm::ivec2(Face->glyph->bitmap_left, Face->glyph->bitmap_top),
Face->glyph->advance.x
glm::ivec2(face->glyph->bitmap.width, face->glyph->bitmap.rows),
glm::ivec2(face->glyph->bitmap_left, face->glyph->bitmap_top),
face->glyph->advance.x
};
m_Characters.insert(std::pair<GLchar, Character>(c, character));
}
FT_Done_Face(face);
FT_Done_FreeType(library);
GLERROR("Font Load");
}
Font::~Font()
{
FT_Done_Face(Face);
for (auto c : m_Characters) {
glDeleteTextures(1, &c.second.TextureID);
}
+5 -19
View File
@@ -35,25 +35,17 @@ void TextRenderer::Draw(RenderScene& scene)
auto textJob = std::dynamic_pointer_cast<TextJob>(job);
if (textJob) {
RenderText(textJob->Content, textJob->Resource, textJob->Alignment, textJob->Color, textJob->Matrix, scene.Camera->ProjectionMatrix(), scene.Camera->ViewMatrix());
renderText(textJob->Content, textJob->Resource, textJob->Alignment, textJob->Color, textJob->Matrix, scene.Camera->ProjectionMatrix(), scene.Camera->ViewMatrix());
}
}
}
void TextRenderer::RenderText(std::string text, Font* font, TextJob::AlignmentEnum alignment, glm::vec4 color, glm::mat4 modelMatrix, glm::mat4 projectionMatrix, glm::mat4 viewMatrix)
void TextRenderer::renderText(std::string text, Font* font, TextJob::AlignmentEnum alignment, glm::vec4 color, glm::mat4 modelMatrix, glm::mat4 projectionMatrix, glm::mat4 viewMatrix)
{
GLfloat penX = 0;
GLfloat penY = 0;
float scale = 1.0/font->FontSize;
FT_Bool use_kerning = FT_HAS_KERNING(font->Face);
FT_UInt previous = 0;
FT_UInt num_glyphs = 0;
FT_UInt glyph_index;
FT_Vector pos[128];
GLfloat stringWidth = 0.f;
for (std::string::const_iterator c = text.begin(); c != text.end(); c++) {
@@ -67,10 +59,7 @@ void TextRenderer::RenderText(std::string text, Font* font, TextJob::AlignmentEn
penX = -stringWidth;
} else {
penX = 0;
}
// Activate corresponding render state
}
glEnable(GL_BLEND);
glDisable(GL_CULL_FACE);
@@ -94,7 +83,7 @@ void TextRenderer::RenderText(std::string text, Font* font, TextJob::AlignmentEn
GLfloat w = ch.Size.x * scale;
GLfloat h = ch.Size.y * scale;
// Update VBO for each character
GLfloat vertices[6][4] = {
{ xpos, ypos + h, 0.0, 0.0 },
{ xpos, ypos, 0.0, 1.0 },
@@ -105,15 +94,12 @@ void TextRenderer::RenderText(std::string text, Font* font, TextJob::AlignmentEn
{ xpos + w, ypos + h, 1.0, 0.0 }
};
// Render glyph texture over quad
glBindTexture(GL_TEXTURE_2D, ch.TextureID);
// Update content of VBO memory
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
glBindBuffer(GL_ARRAY_BUFFER, 0);
// Render quad
glDrawArrays(GL_TRIANGLES, 0, 6);
// Now advance cursors for next glyph (note that advance is number of 1/64 pixels)
penX += (ch.Advance >> 6) * scale; // Bitshift by 6 to get value in pixels (2^6 = 64)
}
glBindVertexArray(0);