yogesh veeraraghavan
yogesh veeraraghavan

Reputation: 93

Tutorial on Freetype cache subsystem

I am trying to use FreeType 2.4.11 in my project for rendering text using openGL.

Although I Initialize Library, Face and set the charmap to be unicode only once. Usage of FT_Load_Char and glTexImage2D in the loop of characters for every draw slows the text rendering to an unacceptable level.

I need some proper example of how to cache glyphs in the FreeType CacheSubSystem to avoid this delay in rendering

One time initialization of library, face and charmap of unicode as follows

        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;
                        }
                    }
                }
            }
        }

On screen refresh rate, rendermodel function call DrawText function which loops through the characters and loads the glyphs and constructs Image for rendering.

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;
        }

Upvotes: 6

Views: 3734

Answers (1)

Eric
Eric

Reputation: 19873

I've done a project with FreeType where I used a caching mechanism to speed up drawing. Unfortunately I didn't rely on the built-in caching and made my own. Really all I did was to use a std::map and associate chars with bitmaps. If the char was in the map, then use the cached version of it, otherwise add it to the cache then use the cached version.

Upvotes: 1

Related Questions