Transparent background for texture mapped fonts

I’m trying to use texture mapped font to display text thru OpenGL. I use the following code to prepare the texture (8 bpp):

GLubyte ifontBits[256 * 256];
GLuint fonttex;


// drawing font glyphs onto ifontBits[]
//

glGenTextures(1, &fonttex);
glBindTexture(GL_TEXTURE_2D, fonttex);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glTexImage2D(GL_TEXTURE_2D, 0, GL_INTENSITY4,
256, 256, 0, GL_ALPHA, GL_UNSIGNED_BYTE, ifontBits);

Then I try to display some text:

glEnable(GL_TEXTURE_2D);
glAlphaFunc(GL_GEQUAL, 0.5);
glEnable(GL_ALPHA_TEST);

glBindTexture(GL_TEXTURE_2D, fonttex);
glBegin(GL_QUADS);

int l = strlen(text);

for (int i = 0; i < l; i++)
{
// drawing characters…
}

glEnd();
glDisable(GL_TEXTURE_2D);
glDisable(GL_ALPHA_TEST);

But I see just bounding rectangles of my strings filled with the current color!

If I just change arguments of glTexImage2D in the following way:

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
256, 256, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, ifontBits);

then I see my text drawn but, naturally, with background and character colors coming from the texture, that is unacceptable in my case.

Do you have an idea how to fix this (I mean how to get transparent background and required foreground color for text)? Samples from this site tell this approach should work…

Video: TNT2 Pro
Platform: Win2k+SP3+nVIDIA Detonator (nearly the latest release)

Are you sure your font glyphs don’t have extraneous non zero borders around characters and that there is sufficient space between the various characters in the font texture?

[This message has been edited by dorbie (edited 10-22-2003).]

Originally posted by dorbie:
[b]Are you sure your font glyphs don’t have extraneous non zero borders around characters and that there is sufficient space between the various characters in the font texture?

[This message has been edited by dorbie (edited 10-22-2003).][/b]

Yes, I am. And the proof is that I can make my texture mapped font work (with opaque background) without changing ANY arguments of glTexImage2D but <internal_format> and <format>, and <format> used always corresponds to byte values.

Now I know the reason for my troubles:

instead of using

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);

I should have used

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MAGNIFY);

This is the right way to deal with texture mapped fonts.

Unfortunately, it is not highlighted or explained clearly anywhere

Originally posted by sansan:
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MAGNIFY);

Of course, GL_MODULATE !!!

I’m sorry for misprinting!