关于Freetype缓存子系统的教程

Tutorial on Freetype cache subsystem

本文关键字:的教程 子系统 缓存 Freetype 关于      更新时间:2023-10-16

我正在尝试使用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";
        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;
        }

我用FreeType做了一个项目,在那里我使用了缓存机制来加速绘图。不幸的是,我没有依赖内置缓存,而是自己创建了缓存。实际上,我所做的只是使用std::map并将字符与位图关联起来。

如果该字符在映射中,则使用它的缓存版本,否则将其添加到缓存中,然后使用缓存版本。