我正在尝试在我的项目中使用FreeType 2.4.11来使用 openGL 呈现文本。
虽然我初始化库,面对并将charmap设置为unicode只有一次。在每次绘制的字符循环中使用 FT_Load_Char 和 glTexImage2D 会使文本渲染减慢到不可接受的水平。
我需要一些适当的示例来说明如何在FreeType CacheSubSystem中缓存字形以避免渲染延迟
unicode库、面、charmap的一次初始化如下
if(m_FontLibrary == NULL)
{
if(!FT_Init_FreeType(&m_FontLibrary))
{
if(!FT_New_Face(m_FontLibrary, "C:\\Windows\\winsxs\\amd64_microsoft-windows-f..etype-timesnewroman_31bf3856ad364e35_6.1.7601.17514_none_3b958c66aff6cdb7\\times.ttf", 0, &m_FontFace))
{
FT_Set_Pixel_Sizes(m_FontFace, 0, 48);
if(!FT_Load_Char(m_FontFace, 'X', FT_LOAD_RENDER))
{
m_FontGlyph = m_FontFace->glyph;
if(!FT_Select_Charmap(m_FontFace, FT_ENCODING_UNICODE))
{
loaded = true;
}
}
}
}
}
在屏幕刷新率上,rendermodel 函数调用 DrawText 函数,该函数循环遍历字符并加载字形并构造 Image 进行渲染。
const char *text = "Tetrode\0";
const char *p;
for(p = text; *p; p++)
{
// Load a single glyph into the glyph slot of a face object, according to its character code.
if(FT_Load_Char(m_FontFace, *p, FT_LOAD_RENDER))
continue;
// specify a two-dimensional texture image
glTexImage2D(
GL_TEXTURE_2D, // Target texture
0, // Base image level, Level n is the nth mipmap reduction image
GL_ALPHA, // Internal format
m_FontGlyph->bitmap.width, // Width of texture image
m_FontGlyph->bitmap.rows, // height of texture image
0, // Border, This value must be 0.
GL_ALPHA, // Format of the pixel data.
GL_UNSIGNED_BYTE, // Data type of the pixel data
m_FontGlyph->bitmap.buffer // Pointer to the image data in memory.
);
float x2 = x + m_FontGlyph->bitmap_left * sx;
float y2 = -y - m_FontGlyph->bitmap_top * sy;
float w = m_FontGlyph->bitmap.width * sx;
float h = m_FontGlyph->bitmap.rows * sy;
GLfloat box[4][4] = {
{x2, -y2 , 0, 0},
{x2 + w, -y2 , 1, 0},
{x2, -y2 - h, 0, 1},
{x2 + w, -y2 - h, 1, 1},
};
glBufferData(GL_ARRAY_BUFFER, sizeof box, box, GL_DYNAMIC_DRAW);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
x += (m_FontGlyph->advance.x >> 6) * sx;
y += (m_FontGlyph->advance.y >> 6) * sy;
}