【问题标题】:Tutorial on Freetype cache subsystemFreetype 缓存子系统教程
【发布时间】:2013-02-13 19:42:22
【问题描述】:

我正在尝试在我的项目中使用 FreeType 2.4.11 来使用 openGL 呈现文本。

虽然我初始化库,面对并将charmap设置为unicode只有一次。 在每次绘制的字符循环中使用 FT_Load_Char 和 glTexImage2D 会使文本渲染速度减慢到不可接受的程度。

我需要一些适当的示例来说明如何在 FreeType CacheSubSystem 中缓存字形以避免渲染延迟

unicode的库、face和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;
        }

【问题讨论】:

  • 你真的为你绘制的每个字符调用 glTexImage2D 吗?

标签: c++ opengl graphics freetype text-rendering


【解决方案1】:

我使用 FreeType 完成了一个项目,我使用缓存机制来加速绘图。不幸的是,我没有依赖内置缓存,而是自己制作。实际上,我所做的只是使用 std::map 并将字符与位图相关联。如果字符在地图中,则使用它的缓存版本,否则将其添加到缓存中,然后使用缓存版本。

【讨论】:

  • 所以你的意思是从字符到位图的映射。那么如何将字形加载到字体的字形槽中。
  • 我认为您需要 FT_Render_Glyph,我不太记得我是如何做到的,但我认为我遵循了教程所涵盖的内容
猜你喜欢
  • 2015-09-22
  • 1970-01-01
  • 1970-01-01
  • 2016-07-16
  • 2015-03-19
  • 1970-01-01
  • 1970-01-01
  • 2011-10-20
  • 2015-05-16
相关资源
最近更新 更多