PDA

View Full Version : Texture-based text rendering



Felipe Meneguzzi
12-17-2004, 03:45 AM
Perhaps this question has already been asked in this forum, but I have searched the posts for a specific answer to my question and have not found it. I apologize if I am repeating a question.

I am trying to render text in screen coordinates using that texture+quad writing trick. More specifically I am trying to run the code included in the Game Programming Gems I book to render an FPS counter on the screen. It says there that all the OpenGL setup and cleanup code is included in the class, so I figured it would work easily.

Nevertheless, when I try to render text using that code, I get a strip of solid quads in the place where I expected the text to be rendered.

It seems to me that this is a texture and/or blending problem, because the texture is initialized using a GL_LUMINANCE_ALPHA encoding. Perhaps I am doing something that I should not be doing in the setup of my OpenGL environment.
I have stripped down my code to only the very basic initialization calls and I still get the problem.

I am using the glut API to structure my program with the folling code:

initDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_ALPHA);
initWindowPosition(20,20);
initWindowSize(800,600);
currentWindow = createWindow("My Window");

And setting up OpenGL with the folling code:

glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

In the display function, the only call that is made before I try to render the text is this one:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

I'd greatly appreciate if anyone could give me a hint to what I might be doing wrong here.

Thanks

Inquisitor
12-17-2004, 10:14 AM
glEnable(GL_TEXTURE_2D); ?

MikeC
12-17-2004, 11:53 AM
What he said, unless you already have that with your texture setup code. Also:

- I've been unimpressed by most code from books; it tends to make far too many undocumented assumptions about the surrounding framework. Try writing your own; this stuff is fairly straightforward, and you'll end up understanding it a lot better. To begin with, don't worry about text; start with a little checkerboard texture and get that looking the way you want it.

- Make sure you really need a pixel format with alpha. It might force software rendering on some older and/or low-end hardware. You don't need it to use alpha blending, if that's what you were thinking.

- Unless you're doing something terribly fancy, I doubt you need a GL_LUMINANCE_ALPHA texture. Just GL_ALPHA, with the blendfunc you have and the default GL_MODULATE texenv mode, should do nicely for texfonts.

Otherwise, try posting the texture setup code you're using - in particular any glTexEnv calls.

cheers
Mike

Felipe Meneguzzi
12-20-2004, 02:57 AM
Thanks for the hints. I agree with you that using LUMINANCE_ALPHA looks a little bit too fancy for me, but since the book said it would work out of the box I figured I was using some OpenGL code elsewhere that would be affecting that code.

The texture initialization is:
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &fontID);
glBindTexture(GL_TEXTURE_2D, fontID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0,
2,//GL_LUMINANCE_ALPHA,
FONT_TEX_W,
FONT_TEX_H,
0,
GL_LUMINANCE_ALPHA,
GL_UNSIGNED_BYTE,
(void *)fontData);

And right before trying to write text to the screen it uses the following code

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0.0, 640.0, 480.0, 0.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();

// Push the neccessary Attributes on the stack
glPushAttrib(GL_TEXTURE_BIT|GL_ENABLE_BIT);

glBindTexture(GL_TEXTURE_2D, fontID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glEnable(GL_TEXTURE_2D);

glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);

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

-----------------------
I have done some toying with textures in OpenGL in the past, and I was trying to refresh my skills with that, but with those problems I think I'll have to re-learn mostly everything :-D

Anyway, one thing that I also considered is that the "texture" used for text drawing in this code is a static array with the texture data hard-coded into it, so I can't relly check if the texture is okay.

I think that my next step will have to be starting from scratch with the text writing stuff (I'd never figure it would be so hard to get some text into the screen) using some regular textures and/or just bitmap fonts and direct drawing into the framebuffer.