【发布时间】:2019-05-27 17:52:50
【问题描述】:
似乎 freetype 库没有正确加载字体,也没有将其转换为字节以供以后用作 opengl 纹理。
这是我使用 freetype 得到的结果:
我已经尝试过使用其他纹理,所以这不太可能是我的纹理管理器的问题。
Font(const std::string font) //constructor
:name(font)
{
std::string _font = "fonts/" + font + ".ttf";
FT_Library ft;
FT_Init_FreeType(&ft);
FT_Face face;
if (FT_New_Face(ft, _font.c_str(), 0, &face))
EXIT_ERROR(-11);
FT_Set_Pixel_Sizes(face, 0, 48);
for (unsigned int a = 1; a < 128; a++)
{
char c = a;
if (FT_Load_Char(face, c, FT_LOAD_RENDER))
{
EXIT_ERROR(-12);
}
//This just creates the FontTexture type, as I said before it works
//fine,FontTexture is abstracted from BaseTexture which stores char* with
//data. I also made sure that the stuff is loaded
//there properly from inside FontTexture.
FontTexture* _char = new FontTexture(
//here is buffer passing
face->glyph->bitmap.buffer,
0,
{ static_cast<float>(face->glyph->bitmap.width),
static_cast<float>(face->glyph->bitmap.rows) });
_char->SetAdvance(face->glyph->advance.x);
_char->SetBearing({ static_cast<float>(face->glyph->bitmap_left),
static_cast<float>(face->glyph->bitmap_top )});
//This just caches texture, so instead of loading it multiple times I can
//just call "getTexture(name) store that pointer in the entitie's memory
//and bind when Draw() is being called.
TextureManager::getTextureManager().PrecacheTexture(std::to_string(a) + font, _char);
Characters.insert(std::pair<char, FontTexture*>(c, _char));
}
FT_Done_Face(face);
FT_Done_FreeType(ft);
}
【问题讨论】:
-
“所以这不太可能是我的纹理管理器的问题” 不,这是你的纹理管理器的问题。什么是纹理格式?你在哪里
FT_Glyph_To_Bitmap? -
GL_ALPHA 如果这是您的要求。
-
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, size.x, size.y, 0, GL_ALPHA, GL_UNSIGNED_BYTE, m_LocalBuffer);
-
之前做
glPixelStorei( GL_UNPACK_ALIGNMENT, 1);。 -
必须每帧都这样做吗?我在主循环之前已经这样做了。