#include "font.h" #include Font::Font(const std::string filepath){ FT_Library ft; if (FT_Init_FreeType(&ft)) { std::cout << "ERROR::FREETYPE: Could not init FreeType Library" << std::endl; } FT_Face face; if (FT_New_Face(ft, filepath.data(), 0, &face)) { std::cout << "ERROR::FREETYPE: Failed to load font" << std::endl; } FT_Set_Pixel_Sizes(face, 0, 48); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // disable byte-alignment restriction for (unsigned char c = 0; c < 128; c++) { createGlyphTexture(c, face); } glPixelStorei(GL_UNPACK_ALIGNMENT, 4); // Back to default value FT_Done_Face(face); FT_Done_FreeType(ft); } Font::~Font(){ } void Font::createGlyphTexture(unsigned char c, FT_Face &face){ if (FT_Load_Char(face, c, FT_LOAD_RENDER)) { std::cout << "ERROR::FREETYTPE: Failed to load Glyph" << std::endl; return; } // generate texture unsigned int texture; glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); glTexImage2D( GL_TEXTURE_2D, 0, GL_RED, face->glyph->bitmap.width, face->glyph->bitmap.rows, 0, GL_RED, GL_UNSIGNED_BYTE, face->glyph->bitmap.buffer ); // set texture options glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // now store character for later use unsigned int advance = static_cast(face->glyph->advance.x); Character character = { texture, glm::ivec2(face->glyph->bitmap.width, face->glyph->bitmap.rows), glm::ivec2(face->glyph->bitmap_left, face->glyph->bitmap_top), advance }; m_characters.insert(std::pair(c, character)); } Character& Font::getCharacter(char c){ return m_characters[c]; }