Drawing String of Characters using OpenGL (Bitmap font version)

The source code available in this page allows you to write platform-independent code to draw text strings using OpenGL.  It covers one of few weaknesses of OpenGL -- lack of standard text-drawing (or font drawing) functions.  More precisely, it solves two problems:

  • By replacing wglUseFontBitmap or glXUseXFont with YsGlUseFontBitmap?x?, you can write platform-independent code to set up display lists for font drawing.  In particular, you will no longer need to write platform-dependent font-creation functions.  (You will no longer have to shout, "I have no business with 10pt or 12pt, I just need fonts of specific pixels!")
  • In Windows, if you use wglUseFontBitmap, fonts' baseline is aligned with the raster position.  In Linux, if you use glXUseXFont, fonts' lower-left corner will be aligned with the raster position.  As a result, if you port a code written in one platform to the other, your texts will be shifted by some pixels up or down.  What makes the matter worse is there's no way for you to know how many pixels between the baseline and the bottom corner in Windows.  (What a bad specification!)  By using YsGlUseFontBitmap?x?, you will not have this problem.

These are the main benefits of the code available here.  6x7, 6x8, 8x8, 8x12, 12x16, 16x20, 16x24, 20x28, 20x32 fonts are available.

You can use the code (ysglfontdata.h, ysglfontdata.c, ysglusefontbitmap.c) in your program, free or commercial, open source or proprietary, as long as you agree on this point:  You will take responsibility for the consequence of using these code (i.e., I will not take responsibility for any consequence from these source codes.)  If you cannot agree, do not use the code available from this page.  I believe truly open-source program does not come with a string attached.  If the source code mandate you to open your source code, it doesn't look open at all to me.

One disadvantage of my fonts is because I am not a good font designer, fonts look ugly.  At least readable though.  And, all fonts are fixed width since I don't have much necessity of proportional font when I want to draw something with OpenGL (I rather want to align texts horizontally and vertically).

[Download Source Files]

[Sample with GLAUX]

[Sample with GLX]

[Sample with Cocoa (MacOSX)]

I think you can easily read sample files to understand how to use my functions.  Compiling direction is written in the first line of the sample code.  These are all C source, but you can use the functions from C++, too.  (I wrote #ifdef __cplusplus in ysglfontdata.h)

OpenGL is really a powerful API for drawing realistic 3D scene with good performance.  Nowadays, most of the GPUs support OpenGL hardware acceleration, and OpenGL can render 3D animation with high frame-ratio.  Just 10 years ago, OpenGL on a PC was something like, it can show a picture, but no animation, because it was too slow.  Rapid change in just 10 years or so.

Although OpenGL is powerful in drawing 3D animation, it is horribly poor at drawing texts.  First of all, although you can use common code for Windows, Linux, MacOS X, and even iPhone to draw points, lines, and polygons with textures, etc., you suddenly need to write platform-dependent code if you want to draw text.  Because of that, texts drawn by OpenGL in different platforms appear slightly differently.  If you port your OpenGL program from Windows to Linux or the other way round, your text will be shifted by a few pixels up or down from one platform to the other.  It is one of the major headaches when porting an OpenGL program to a different platform, and lack of standard text-drawing function has been one of few weaknesses of OpenGL.

So, I once tried to make my own function to draw texts.  If I draw texts by my own function, I will not have to worry about difference between different platforms.  That was long time back, and I tried to use glDrawPixels.  However, there were two problems in this approach.  One is I couldn't make it a display list because I was not able to shift raster position after drawing a single character.  The other was I was not able to change color.

But, a few months ago, I was looking at OpenGL reference to check a specification of a function, I happened to find glBitmap.  What?  Was there an additional function for drawing bitmap?  I looked at inside glBitmap, and I understood everything.  glBitmap can shift raster position after drawing a bitmap.  I had been using OpenGL nearly 15 years, and never noticed this function.  (If I had known this, many things should have been a lot easier!)  Those wglUseFontBitmap and glXUseXFont must be using glBitmap internally.

I finally made my own font-drawing functions.  Someone else may be having a same problem.  So, I made it public here.


2009/09/20 First release

2009/09/21 Modified header inclusion so that it works on MacOSX (In MacOSX, please define macro "MACOSX") [Download this version]

2010/08/15 Improved font bitmaps.  Added 6x10 and 7x10 fonts.

Screenshot from the GLAUX sample