Words…

So, I bit the bullet the other day and implemented something that I knew I’d be needing soon. Annoyed that I didn’t have a text-rendering system in place to display things like the current FPS on the screen (and because I hate reading console output), I sought out a decent portable text rendering solution for OpenGL that I could easily port to Direct3D later down the road if needed.  Normally, on the mac platform, I’d just drop into Core Graphics and render the text string to a blank bitmap and roll that bitmap into a texture.  But since I’m trying to be portable, I can’t rely on anything specific to mac os, or windows, or anything!

One thing became readily apparent was that any solution I devise will use FreeType2, a very useful portable font library for rendering TrueType fonts.  One thing that I was not predicting however, was that there exists a multitude of libraries that run on top of FreeType2 (such FreeTypeGL) as whose sole purpose is to use freetype to render text on OpenGL.  This annoyed me.  I don’t want to drop in an entire heavy-weight font library complete with 30+ source files, with its own license, just to render a single static string to a texture.  Furthermore, these libraries become another barrier for portability in terms of OpenGL/Direct3D abstraction.  I’m ok with linking graphics-independent libs such as freetype, but entire subsystems relying on OpenGL, that’s where I draw the line.

So, I decided to do it myself. Freetype can’t be that hard anyway right?  So it really only takes about 100 lines of code to use freetype to render to a image buffer suitable for rolling into an OpenGL texture.  So here you have it, my experimental, non-optimized Text Texture class..

 

Not very efficient yet, but it works!

To save memory, I decided to store the text image in a single channel texture (GL_RED format).  So, I have to use a special swizzle in the GLSL code so the texture doesn’t render red.

And.. it works!

Finally.  I can show my FPS on the screen like a proper game.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>