【发布时间】:2019-11-10 16:36:31
【问题描述】:
使用 FreeType (2.10.1) 在我的游戏引擎中实现文本渲染时,我遇到了奇怪的字形,其中包含重复四次的字母,在 x 轴上镜像并上下颠倒。
在所需字母上方似乎有相邻的内存被解释为字形,每次运行都会改变并导致某些启动时出现段错误。
这是我尝试渲染单词 "sphinx" 时得到的结果。
这里是完整的例句“黑色石英狮身人面像,判断我的誓言”水平翻转并旋转了 180 度。
我已编译this code 以排除my MinGW environment being erroneous。
我很确定我对 OpenGL 的使用不是问题,因为我的纹理上传和渲染代码适用于其他图像。
目前我将FT_Glyph_Slot 的重要位包装在一个名为Letter 的结构中并缓存该结构。删除包装和缓存并不能修复错误。
下面是相关代码sn-ps:
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
}
}
从bitmap->buffer 生成Texture 的代码。
// 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;
}
如果提供的代码 sn-ps 不够用,我很乐意添加更多。 该项目是开源的,可用here on GitHub。
【问题讨论】:
-
您遗漏了代码中最重要的部分:如何将
letter->bitmap.buffer放入纹理中? -
@HolyBlackCat 感谢您的反馈,我已添加代码。