This section of the archives stores flipcode's complete Developer Toolbox collection, featuring a variety of mini-articles and source code contributions from our readers.

 

  Easy Polygonal Text Rendering
  Submitted by



* glyph.c glyph.c provides an efficient, portable means of rendering text using OpenGL. It renders text as polygons, since polygonal text scales both up and down better than texture-mapped, line-stroke, or bitmap text. Each glyph is represented using only a few triangles, specified using triangle strips. In most cases, each vertex is touched only once. Despite the low polygon count, text is extremely legible at most any size. This code does not rely upon GLX, WGL, Freetype, or any other external library, so it can be easily included in code for any platform. It has been tested under Win2K, Linux and IRIX. The font description requires less than 2KB of memory and is specified in less than 500 lines of C code. It is linked directly with the application, so no external file loading is necessary. Glyphs are drawn mono-spaced in a 1x1 unit square in the Z plane. Text may be transformed normally. In the opinion of the author, it looks best when scaled to a 1:2 aspect ratio. Using the default renderer, draw_glyph(), a translation one unit to the right is applied after each glyph is drawn. This allows a string to be rendered as a sequence of calls to draw_glyph(). Text is rendered using the current material properties. If it is to be correctly lit, set the current normal to [0 0 1] before calling the renderer. draw_glyph() uses OpenGL immediate mode, but it may be called during the creation of a display list. Only slightly more effort is required to create vertex arrays. Representations of all 128 ASCII characters are defined. Non-printing characters appear as a "no glyph" box.

* test.c test.c is a simple GLUT-based program that renders all 128 characters. Resize the window to see how the font looks at different sizes and aspect ratios. For very small text, it is recommended that polygon smoothing or full-screen anti-aliasing be enabled.

Download Associated File: glyph.tar.gz (4,928 bytes)

The zip file viewer built into the Developer Toolbox made use of the zlib library, as well as the zlibdll source additions.

 

Copyright 1999-2008 (C) FLIPCODE.COM and/or the original content author(s). All rights reserved.
Please read our Terms, Conditions, and Privacy information.