使用FreeType(2.10.1)在我的游戏引擎中实现了文本渲染,我遇到了奇怪的字形,其中包含重复四次的字母,在x轴上镜像并上下颠倒。
在所需字母的上方,似乎存在被解释为字形的相邻内存,该字形每次运行都会更改,并在某些启动时导致段错误。
这就是我尝试呈现单词“ sphinx” 时得到的。
这里是完整的例句示例:“黑色石英的狮身人面像,判断我的誓言”。水平翻转并旋转180度。
我已编译this code以排除my MinGW environment being erroneous。
我非常确定我对OpenGL的使用不是问题,因为我的纹理上传和渲染代码适用于其他图像。
当前,我将FT_Glyph_Slot
的重要位包装在称为Letter
的结构中并缓存该结构。删除包装和缓存并不能解决该错误。
以下是相关的代码段:
FreeType初始化。
// src/library/services/graphics/font/FreeType.cpp
void FreeType::initialize() {
Logger::info("Initializing FreeType");
if (FT_Init_FreeType(&m_library)) {
Logger::error("Could not initialize FreeType");
return;
}
}
void FreeType::useFont(const std::string& fontName, const unsigned int fontSize = 42) {
Logger::info("Loading font " + fontName);
if (FT_New_Face(m_library, fontName.c_str(), 0, &m_currentFace)) {
Logger::error("Could not open font " + fontName);
return;
}
FT_Set_Pixel_Sizes(m_currentFace, 0, fontSize);
}
使用FreeType创建Letter
的代码。
// src/library/services/graphics/font/FreeType.cpp
std::shared_ptr<Letter> FreeType::getLetter(unsigned long character) {
// Try loading from cache
if (std::shared_ptr<Letter> letter = m_letters.get(std::to_string(character))) {
return letter;
}
return loadLetter(character);
}
std::shared_ptr<Letter> FreeType::loadLetter(unsigned long character) {
if (FT_Load_Char(m_currentFace, character, FT_LOAD_RENDER)) {
Logger::error("Could not load character " + std::string(1, character));
return std::shared_ptr<Letter>();
}
FT_GlyphSlot& glyph = m_currentFace->glyph;
Letter letter = {
.id = character,
.textureId = 0,
.bitmap = {
.buffer = glyph->bitmap.buffer,
.width = glyph->bitmap.width,
.height = glyph->bitmap.rows
},
.offset = {
.x = glyph->bitmap_left,
.y = glyph->bitmap_top
},
.advance = {
.x = glyph->advance.x,
.y = glyph->advance.y
}
};
std::shared_ptr<Letter> sharedLetter = std::make_shared<Letter>(letter);
cache(sharedLetter);
return sharedLetter;
}
void FreeType::cache(std::shared_ptr<Letter> letter) {
m_letters.add(std::to_string(letter->id), letter);
}
图形系统初始化FreeType
// src/library/services/graphics/opengl/OpenGLGraphics.cpp
void OpenGLGraphics::initialize(int windowWidth, int windowHeight) {
// ... OpenGL initialization
m_freeType.initialize();
m_freeType.useFont("../../../src/library/assets/fonts/OpenSans-Regular.ttf");
}
代码在文本渲染器中获取Letter
。
// src/library/services/graphics/opengl/OpenGLGraphics.cpp
void OpenGLGraphics::drawText(const std::string &text, Vector2f location) {
for (auto iterator = text.begin(); iterator < text.end(); ++iterator) {
std::shared_ptr<Letter> letter = m_freeType.getLetter(*iterator);
if (!letter->textureId) {
std::shared_ptr<Texture> tex =
ImageLoader::loadFromCharArray(
letter->bitmap.buffer,
letter->bitmap.width,
letter->bitmap.height
);
letter->textureId = tex->id;
m_freeType.cache(letter);
}
// ... OpenGL text rendering
}
}
从Texture
生成bitmap->buffer
的代码。
// src/library/services/graphics/opengl/util/ImageLoader.cpp
std::shared_ptr<Texture>
ImageLoader::loadFromCharArray(const unsigned char *image, const unsigned int width, const unsigned int height) {
std::shared_ptr<Texture> texture = std::make_shared<Texture>();
texture->width = width;
texture->height = height;
glGenTextures(1, &texture->id);
glBindTexture(GL_TEXTURE_2D, texture->id);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei) width, (GLsizei) height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
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_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
return texture;
}
如果提供的代码段不够用,我会很乐意添加更多。 该项目是开源项目,可以使用here on GitHub。
答案 0 :(得分:1)
您假设FreeType总是生成每通道8位的RGBA图像,但是不会。
您需要检查bitmap.pixel_mode
以查看所获得的图像格式。
通常它是FT_PIXEL_MODE_GRAY
(意味着每像素8位灰度),或者是FT_PIXEL_MODE_MONO
(意味着每像素1位单色)。
有关更多详细信息,请参见the manual。