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)