-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Text GL can be used for displaying text in the 3D library OpenGL, but it does more than that. For example: it can tell you on which character the mouse cursor is currently pointing. This enables you to have selectable text.
Useful OpenGL tutorials can be found here: http://nehe.gamedev.net/
Your font data has to be an SVG file, according to the standards at https://www.w3.org/TR/SVG11/fonts.html. Used variables are:
- bbox: the bounding box for the glyphs. Text GL assumes no glyph is rendered outside this box.
- glyph d: the path, or control points of the glyphs, this describes how to draw the glyph in 2D
- horiz-adv-x: the character horizontal coordinate incrementation, either per glyph or for the whole font
- horiz-origin-x: the character horizontal bearing, either per glyph or for the whole font
- horiz-origin-y: the character vertical bearing, either per glyph or for the whole font
- hkern k,g1/u1, g2/u2: the horizontal kerning values, can be single character combo's or group combo's (kerning by class)
Text GL currently assumes that glyphs are positioned in horizontal lines, from left to right. So Arabic or Chinese texts are not supported.
To generate SVG font files easily, use fontforge: https://fontforge.github.io/en-US/
Parsing only requires a single function, but that function needs a C++ istream object as input:
#include <iostream>
#include <fstream>
#include <text-gl/font.h>
using namespace TextGL;
FontData fontData;
std::ifstream is;
is.open("my-font.svg");
if (!is.good())
std::cerr << "Error opening file." << std::endl;
ParseSVGFontData(is, fontData);
Font data alone cannot be rendered in OpenGL. The next step is to rasterize the glyphs and create an image font. This also requires you to set the font style:
#include <text-gl/image.h>
FontStyle style;
style.size = 32.0;
style.strokeWidth = 2.0;
style.fillColor = {1.0, 1.0, 1.0, 1.0};
style.strokeColor = {0.0, 0.0, 0.0, 1.0};
style.lineJoin = LINEJOIN_MITER;
style.lineCap = LINECAP_SQUARE;
ImageFont *pImageFont = MakeImageFont(fontData, style);
Once an image font is created, it's glyph images and style can no longer be changed.
Glyph images need to be converted to textures, in order to render text in OpenGL. To do so however, A valid OpenGl context is needed. How this is created varies per platform. Here's an example.
#include <text-gl/tex.h>
GLContext context = CreateGLContext(canvas); // This is platform specific!
GLTextureFont *pTextureFont = MakeGLTextureFont(pImageFont);
Once you are done with your fonts, you must delete them:
DestroyGLTextureFont(pTextureFont);
DestroyGLContext(context); // This is platform specific!
DestroyImageFont(pImageFont);