c++openglfreetype2

Freetype correct size rendering


I'm currently writing an OpenGL application that render a lot of text. Text rendering was previously done with Qt but for performence reason we change all by OpenGL rendering.

I made all the shader / rendering pipeline, it's working well. But after reading the doc of freetype again, I still don't understand how to render correctly at the right size.

For now I use the function

FT_Set_Pixel_Sizes(face, 0, mFontSize);

To set the size of my font, but I know that it's incorrect, because Qt was rendering in 'point' (I guess..), so all the text is now smaller.

I read about using the function

FT_Set_Char_Size(
      face,    /* handle to face object           */
      0,       /* char_width in 1/64th of points  */
      16*64,   /* char_height in 1/64th of points */
      300,     /* horizontal device resolution    */
      300 );   /* vertical device resolution      */

And here come my first question, what I should put in the resolution? I can't know the DPI of the screen... What standard I should use?

Also, I need the text to be at fixed size on the screen, at whatever the zoom is. For now, I pre-compute my glyph's vertex on CPU side as on this tutorial https://learnopengl.com/In-Practice/Text-Rendering But for the parameter "scale", I use 1.f / font_size.

And then, on the shader I do

(camera * vec3(char_position.xy, 1)).xy + vertex.xy / viewport * font_size.

With this I can have fixed size on the screen, with a maximum of 45 pixel as I asked to freetype. But it's not correct from the point of view of what was rendered by Qt.

But I don't see how to do it with the DPI 'solution'


Solution

  • If you can't calculate an accurate DPI for your system (a claim which I'm very skeptical of; check your documentation) then you're going to need to do what most other solutions do: guess.

    Windows, for example, assumes a DPI of 96 is "normal" for most systems, and if you're unable to provide an accurate number, it's probably a good number to use for your project.

    So if you absolutely cannot get an accurate DPI number, just punch in 96 and then scale your font sizes from there.