Implementing text rendering into my game engine using FreeType (2.10.1), I am encountering odd looking glyphs, containing the letter repeated four times, mirrored on the x axis and turned upside down.
Up above the desired letter there seems to be neighboring memory interpreted as glyph, which changes every run and causes a segfault on some launches.
This is what I get when I try to render the word "sphinx" .
Here is the full example sentence "sphinx of black quartz, judge my vow" flipped horizontally and rotated 180 degrees.
I have compiled this code to rule out my MinGW environment being erroneous .
I'm very sure my usage of OpenGL is not the issue, since my texture uploading and rendering code works for other images.
Currently I'm wrapping the important bits of the FT_Glyph_Slot
in a struct called Letter
and caching that struct. Removing the wrapping and caching did not fix the error.
Here are the relevant code snippets:
FreeType initialization.
// 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);
}
The code using FreeType to create a 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);
}
The graphics system initializing 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");
}
The code getting the Letter
in the text renderer.
// 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
}
}
The code for generating a Texture
from the 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;
}
If the supplied code snippets should not suffice, I will gladly add more. This project is open source and available here on GitHub .
You assume that FreeType always generates 8-bits-per-channel RGBA images, but it does not.
You need to check bitmap.pixel_mode
to see what image format you got.
Usually it will be either FT_PIXEL_MODE_GRAY
, meaning 8-bits-per-pixel greyscale, or FT_PIXEL_MODE_MONO
, meaning 1-bit-per-pixel monochrome.
See the manual for more details.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.