This is how I handle font rendering in pseudocode(simplified):
FontFace* CreateFontFace(fontName, fontSize)
{
FontFace* fontFace = LoadFromDiskCache(fontName, fontSize);
if ( fontFace == 0 )
{
for(each glyph)
{
Load glyph using FreeType;
Load its size and metric;
}
Pack all glyph bitmaps to a single UV-atlas
{
Calculate rectangles (RectangleBinPack); // Very slow!
Blit glyphs to UV-atlas; // Slow!
Insert rectangles to std::vector; // UV-dictionary;
Insert metrics to std::vector;
}
Wrap it all to a struct FontFace;
AddToDiskCache(fontName, fontSize);
// so on next startup it will be cached on HDD
}
return fontFace;
}
bool OnInit(fontName, fontSize)
{
for (each fontName)
{
for (each fontSize)
{
FontFace* fontFace = CreateFontFace(fontName, fontSize));
Insert fontFace to container based on std::map;
}
}
}
void OnSomtimes(FontFace, pDevice)
{
On demand create texture from FontFace.uvatlas on pDevice;
}
void OnRender(wstring, FontFace)
{
if(needUpdate)
{
// Generate std::vector<Sprite> using given FontFace
sprites = CreateSprites(wstring, FontFace);
}
Draw all vector<Sprite> at once with my SpriteBatch object;
// Big dynamic VertexBuffer, Instancing or GeometryShader
// depending on hardware caps
}
So I can cache some most used fonts or even all ever used in-game fonts. But in some apps user can just pick another font/size/style that app never seen before on the fly and it applies instantly. Or for example, in some games user can replace "font.tff" file in game folder and game will use it on startup in many sizes/styles and every of thousands of Unicode chars without additional time consuming preloading. Miracle!
I need something like this - an on-the-fly font loading.
Of couse, I can load glyphs separately on demand, and render glyph-by-glyph, but that means no batching, thousants of Draw calls per frame and huge GPU bandwidth. Not sure its ok or not for OpenGL, but its no go for D3D11.
How do they do that in game studios? Maybe you know a different approach? I heard about rendering fonts as geometry, GPU-processing and complicated curves algorithms, but cannot find helpful info in google.
Any help will be appreciated!