Brad Fish's Homepage [Home] [glFont] [Mirage] [Articles] [Links] [Finger bfish@cs.byu.edu] [Mail bhf5@email.byu.edu]

glFont [Home] [News] [Download] [Bugs and Comments] [glFont in a Nutshell] [Origin of glFont] [glFont Usage] [glFont API] [glFont FAQ]

glFont Frequently Asked Questions

Contents

i.  Updates

1.  General

1.1.  What is glFont?
1.2.  Why glFont?
1.3.  About the author

2.  Using glFont.exe

2.1.  Creating a font

3.  glFont API

3.1.  What are those dx and dy variables in the GLFONTCHAR structure?
3.2.  How can I get pixels from dx and dy?
3.3.  Why should I not modify dx and dy directly in order to change the font size?
3.4.  Why am I getting an access violation when I try to create my glFont?
3.5.  I see a solid strip where my text is supposed to be. What's going on?
3.6.  I'm new to glFont.  Do you have any sample code that I can use to get started?

i.  Updates

Thursday, November 2, 2001

Updated 3.2  How can I get pixels from dx and dy?

Updated 3.5  I see a solid strip where my text is supposed to be.  What's going on?

1.  General

1.1.  What is glFont?

glFont is a program executable and accompanying API source code to make it a snap to add 2D text to any OpenGL application.  It takes any Windows TrueType font and turns it into an OpenGL texture, with appropriate texture coordinates, to display text with correct spacing and size.  Because it draws text as OpenGL quads, glFont is very fast, and works well in high-demand situations.

glFont was not designed to create 3D text.  glFont was intended for fast, good-looking 2D text for use in games and other graphics applications which want to display text, but for which 3D text is unnecessary and overkill.

1.2. Why glFont?

Many have pointed out to me GLTT, a library that creates full 3D text.  I also understand that it is able to do bitmapped text as well.  However, I designed glFont for a totally different purpose. Let me point out the differences between the two:

glFont was designed for displaying fast, 2D text in high performance programs such as games, and as I stated before, programs that need to display text but do not need fully 3D characters.  GLTT was designed for fully 3D text (ideal for modelling applications, in my opinion) which does not suit such programs as games and things.  For simple programs that just want to output text, GLTT is probably a bit much.

Also, from the GLTT homepage (http://home.worldnet.fr/~rehel/gltt/gltt.html):

The GLTTBitmapFont, GLTTOutlineFont and GLTTFont classes act as font servers: they internaly render requested glyphs and cache them.

glFont was intended to be simple and fast.  Consequently, it pre-renders the font beforehand, and then is able to display text in almost no time, using little processing power in the application.

It is up to you to decide which one is best suited for your needs.  If you need 3D text, you should definitely check out GLTT.  But, if you want high performance 2D text, glFont might be right for you.

1.3.  About the author

glFont was written by Brad Fish.  You can check out his homepage to read more about him and check out the things he's done:

http://students.cs.byu.edu/~bfish/

2.  Using glFont.exe

2.1.  Creating a font

Creating a font is a snap.  Using the program is not very difficult, and is pretty self-explanatory.  The general process is to:

  1. Choose a font, using the standard Windows font dialog, and choose the size, style, etc.
  2. Specify the size of the texture to be used (the bigger the texture, the better the quality of the font)
  3. Specify the range of ASCII characters to draw onto the texture
  4. Generate the texture
  5. And finally, to save the texture to a .glf file

3.  glFont API

3.1.  What are those dx and dy variables in the GLFONTCHAR structure?

dx and dy stand for "delta x" and "delta y".  dx and dy specify the change in the x and y coordinates of characters in the font.  These variables allow the spacing and size of each character in the font to be preserved (which allows us to do more than monospaced fonts).  dx and dy are used when drawing text to determine where to go to draw the next character.

dx and dy are calculated by taking the width and height of each character, in pixels, and dividing it by the average width and height of font, in pixels, respectively.  dx and dy are therefore ratios, and are not pixels.

Usually dy is bigger than dx.  You might wonder, why would every character in the font be taller than it is wide?  Well, the answer is simple: because some characters extend below the baseline, it is necessary to include an extra amount of space between rows so that some characters don't get overwritten (by the tail of a 'j' for instance). So, although dy is usually bigger, it's only because there is some empty space there. It won't affect the output of your text in the least.

3.2. How can I get pixels from dx and dy?

The GLFONT structure has an array of GLFONTCHAR structures, which hold sizing and texture information for each character. You can use the texture coordinates for each individual character, along with the texture width and height, to calculate the size of each character in pixels, like so:

//This is our glFont (assume already created)
GLFONT font;

//This holds the character we want to find the dimensions of
char c;

int i = c - font.IntStart;
width = (int)((font.Char[i].tx2 - font.Char[i].tx1) * font.TexWidth);
height = (int)((font.Char[i].ty2 - font.Char[i].ty1) * font.TexHeight);

Since texture coordinates are between 0 and 1, once you subtract tx1 from tx2 and ty1 from ty2, you have a fraction representing the width and the height of the portion of the texture used to draw that character. Scale it by the size of the texture, and you get the amount in pixels.

3.3. Why should I not modify dx and dy directly in order to change the font size?

glFont was designed to be flexible. You can use standard OpenGL calls such as the glColor family, glRotatef, glTranslatef, glScalef, etc., to manipulate the font.

I recommend that if you want to change the font size, use the glScalef command. This way, you can use many different sizes in your application simply by issuing the scale command. Changing dx and dy is a big hassle, not worth the effort, and unnecessary when a single glScalef will do the job.

3.4. Why am I getting an access violation when I try to create my glFont?

glFontCreate() does not handle memory allocation for the GLFONT structure, so you can't pass it a pointer to one. This will cause bad things to happen. Instead, allocate memory for the structure yourself, or do something like this:

GLFONT font;
glFontCreate(&font, "arial.glf", 0);

3.5. I see a solid strip where my text is supposed to be. What's going on?

There are three possibilities.  The first common pitfall is you have not enabled texture mapping.

glEnable(GL_TEXTURE_2D);

Second, you haven't enabled alpha blending. This will cause the glFont quads to be solid, and you won't see any text. Enable alpha blending:

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

Third, your font just isn't the right size compared to your viewing area. Try playing around with glScalef.

3.6.  I'm new to glFont.  Do you have any sample code that I can use to get started?

Sure.  The following code is all it takes to use glFont.  After initializing your window and OpenGL, you can issue the following OpenGL and glFont commands to draw a single string.  It's very simple, and it's all you really need to get started.

//glFont font
GLFONT font;

//Initialize OpenGL
glClearColor(0.0, 0.0, 0.0, 1.0);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

//Create font
if (!glFontCreate(&font, "arial.glf", 0))
	return false;

//Initialize the viewport
glViewport(0, 0, width, height);

//Initialize OpenGL projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 640, 0, 480, -1, 1);

//Clear back buffer
glClear(GL_COLOR_BUFFER_BIT);

//Draw some stuff
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glFontBegin(&font);
glScalef(8.0, 8.0, 8.0);
glTranslatef(30, 30, 0);
glFontTextOut("Test string", 5, 5, 0);
glFontEnd();
glFlush();

//Destroy the font
glFontDestroy(&font);

I would suggest to anyone that is having trouble with glFont to first create a new project and include this code.  If you can't glFont to work using the above code as a template, then please e-mail me.

If you are new to OpenGL and aren't sure how to initialize a Window and OpenGL, etc., then please contact me and I'll send you some basic code to do the job.

 

Last Updated: 11/23/2001 06:14:37 PM

glFont [Home] [News] [Download] [Bugs and Comments] [glFont in a Nutshell] [Origin of glFont] [glFont Usage] [glFont API] [glFont FAQ]

Brad Fish's Homepage [Home] [glFont] [Mirage] [Articles] [Links] [Finger bfish@cs.byu.edu] [Mail bhf5@email.byu.edu]