In the world of OpenGL, dealing with text is not always straightforward. GLUT is one method but it’s becoming old and in some cases deprecated (OSX 10.9). The freetype2 library is one method for generating text, using this library isn’t difficult but getting the best performance requires generating an “Atlas” for your font. An atlas is essentially a texture that contains every single character for a font at a given font size along with information on how to access that character. There are a few libraries that can generate an atlas for you, freetype-gl being one of them.
There is an alternative that in my mind is more portable and easier to use. freetype-gl comes with several awesome little demos on how to use the library, it also comes with a small executable called makefont that generates a header file with all of the needed information.
Provide it with a path for a .ttf font, the name of the header file, the font size and the variable name in the header that you will access it with.
If you find that the texture size is too small go into the code for makefont.c and change the following line so that it has the size you require. the default was set to 128x128 and I changed it to 256x256 . If your texture size cannot contain the full font, characters will be missing. Look at the output of the makefont command to make sure that characters are not missing.
There is a demo called demo-makefont that demonstrates usage of this file for classic GL, modern GL usage with shaders is slightly different.
Lets begin with the shaders and that get that out of the way
There are two things to notice here, first that the position variable contains both the position and the texture coordinates. Text is 2d so we can get away with this. 3D text would require a few modifications. Second the .r value of the texture contains the information we are interested in.
Init code: Generates our texture handle
Using your shader library/class get the location of the “tex” uniform that was declared in the fragment shader.
I am assuming that the shader has already been initialized/compiled at this point using your shader code.
Usage, make changes to fit your shader library as needed