PDA

View Full Version : Transparent background for texture mapped fonts



sansan
10-22-2003, 02:52 AM
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... http://www.opengl.org/discussion_boards/ubb/frown.gif

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

dorbie
10-22-2003, 03:08 AM
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).]

sansan
10-22-2003, 03:20 AM
Originally posted by dorbie:
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).]

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.

sansan
12-08-2003, 11:48 PM
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 http://www.opengl.org/discussion_boards/ubb/frown.gif

Tom Nuydens
12-09-2003, 12:51 AM
Originally posted by sansan:
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MAGNIFY);

http://www.opengl.org/discussion_boards/ubb/confused.gif

sansan
12-09-2003, 01:08 AM
Of course, GL_MODULATE !!! http://www.opengl.org/discussion_boards/ubb/tongue.gif

I'm sorry for misprinting!