|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:
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).
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