OpenGL Text Rendering With FreeType

The latest post in my voxel dev journal is my work on rendering text using the FreeType  library. I was pleasantly surprised at how easy it was to use the FreeType library to render TrueType fonts (TTF) as bitmaps.

You can get some quick and dirty text rendering up and running with just four functions:

1. FT_Init_FreeType, to initialize the FreeType library;
2. FT_New_Face, to initialize a new face from a TTF file;
3. FT_Load_Char, to load character data from a face; and
4. FT_Set_Pixel_Sizes, to specify pixel dimensions for rendered characters.

You'll probably want to clean up after yourself, so keep FT_Done_Face and FT_Done_FreeType in mind. Might I suggest having an RAII wrapper for these guys to make sure things get cleaned up in all circumstances. Loading a font from a TTF file is quite simple:

1FT_Library ft_lib{nullptr};
2if(FT_Init_FreeType(&ft_lib) != 0) {
3  std::cerr << "Couldn't initialize FreeType library\n";
4  return 1;
5}
6
7FT_Face face{nullptr};
8if(FT_New_Face(ft_lib, "my_font.ttf", 0, &face) != 0) {
9  std::cerr << "Couldn't initialize FreeType library\n";
10  return 1;
11}

We initialize the FreeType library and then load the face from file. Pretty simple, eh? Now the slightly harder, but still fairly straightforward part: rendering the text. Let's step through it in small chunks:

1void render_text(const std::string &str, FT_Face face, float x, float y, float sx, float sy) {
2  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
3  const FT_GlyphSlot g = face->glyph;

First, this function takes the following parameters, in order:

1. the string we want to render,
2. the FreeType face we'll use for rendering,
3. the x/y coordinates for drawing in normalized device coordinates (NDC), and
4. sx/sy scaling parameters that convert pixel values to NDC.

The scaling parameters are simply 2 divided by the window's width/height in pixels. This function assumes you've taken care of binding a 2D texture and vertex buffer object before calling render_text. First we have to set the unpack alignment to 1 byte, because FreeType renders 8-bit bitmaps. Next we iterate through the string:

1for(auto c : str) {
3    continue;
4
5  glTexImage2D(
6    GL_TEXTURE_2D,
7    0,
8    GL_R8,
9    glyph->bitmap.width,
10    glyph->bitmap.rows,
11    0,
12    GL_RED,
13    GL_UNSIGNED_BYTE,
14    glyph->bitmap.buffer
15  );

First, we make sure we successfully load the current character. We pass the FT_LOAD_RENDER flag to tell FreeType to render the character to the bitmap. We then upload the bitmap's buffer to the bound texture. Remember, the bitmap is only 8 bits per pixel, so we have to use a single byte format. Next we create a quad to render the texture:

1const float vx = x + glyph->bitmap_left * sx;
2const float vy = y + glyph->bitmap_top * sy;
3const float w = glyph->bitmap.width * sx;
4const float h = glyph->bitmap.rows * sy;
5
6struct {
7  float x, y, s, t;
8} data[6] = {
9  {vx    , vy    , 0, 0},
10  {vx    , vy - h, 0, 1},
11  {vx + w, vy    , 1, 0},
12  {vx + w, vy    , 1, 0},
13  {vx    , vy - h, 0, 1},
14  {vx + w, vy - h, 1, 1}
15};

A fairly straightforward generation of a quad. We just need to remember to scale pixel values to NDC values. Finally, we draw the quads and advance our position:

1  glBufferData(GL_ARRAY_BUFFER, 24*sizeof(float), data, GL_DYNAMIC_DRAW);
2  glDrawArrays(GL_TRIANGLES, 0, 6);
3
4  x += (glyph->advance.x << 6) * sx;
5  y += (glyph->advance.y << 6) * sy;
6}
7
8glPixelStorei(GL_UNPACK_ALIGNMENT, 4);

Again, we need to make sure we advance our location in NDC values, not pixels. FreeType uses 26.6 fixed-point advance values (1/64th of a pixel), so we shift it appropriate to get pixel values. We also reset the pixel alignment to its default (let's play nice with our friends). Before calling this function, you should call FT_Set_Pixel_Sizes to set the pixel size of the font face before rendering. Here are some simple vertex/fragment shaders to render this data:

1#version 410 core
2
3in vec4 position;
4out vec2 texCoords;
5
6void main(void) {
7  gl_Position = vec4(position.xy, 0, 1);
8  texCoords = position.zw;
9}
1#version 410 core
2
3uniform sampler2D tex;
4in vec2 texCoords;
5out vec4 fragColor;
6const vec4 color = vec4(1, 1, 1, 1);
7
8void main(void) {
9  fragColor = vec4(1, 1, 1, texture(tex, texCoords).r) * color;
10}

It seems like a lot when you write about it, but I was actually surprised by how little there was to do. Now, what I've shown here was my initial implementation, which performs terribly due to uploading texture/vertex data for every character, on every frame. To get good performance there's a couple of things you can do:

1. Store a texture atlas of characters. For this we'll need to maintain a mapping between characters and their location within the atlas.
2. For strings that rarely change, we can cache the vertex data in a VBO.

Example GLFW Application Source Code