无法使用 freetype2 为 opengl 正确加载 "glyp"
Can't load "glyp" properly for opengl using freetype2
似乎 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);
}
问题是 glPixelStorei( GL_UNPACK_ALIGNMENT, 1) 标志未启用。我可能在 opengl 具有有效上下文之前启用它。
GL_UNPACK_ALIGNMENT
参数定义从缓冲区读取图像时图像每一行(线)中第一个像素的对齐方式。默认情况下,此参数为 4.
字形图像的每个像素都用一个字节编码,并且图像是紧密打包的。因此字形图像的对齐方式为 1,并且必须在读取图像和指定纹理之前更改参数:
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
如果遗漏了这一点,则会在图像的每一行产生偏移效果(除非图像的宽度可以被 4 整除)。
似乎 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);
}
问题是 glPixelStorei( GL_UNPACK_ALIGNMENT, 1) 标志未启用。我可能在 opengl 具有有效上下文之前启用它。
GL_UNPACK_ALIGNMENT
参数定义从缓冲区读取图像时图像每一行(线)中第一个像素的对齐方式。默认情况下,此参数为 4.
字形图像的每个像素都用一个字节编码,并且图像是紧密打包的。因此字形图像的对齐方式为 1,并且必须在读取图像和指定纹理之前更改参数:
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
如果遗漏了这一点,则会在图像的每一行产生偏移效果(除非图像的宽度可以被 4 整除)。