PDA

View Full Version : I can't get glutBitmapCharacter() to work


SOUR-Monkey
2004.09.14, 03:27 AM
First, the code:
void drawText(char *string)
{
int i, len;

glColor3f(1.0f, 1.0f, 1.0f);
glRasterPos2f(25.0f, 10.0f);

glLoadIdentity();
glTranslatef(0.0f, 0.0f, -1.0f);

for (i = 0, len = strlen(string); i < len; i++)
{
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, (int)string[i]);
}
}

I can't see anything wrong with that, but no text is getting printed to the screen. The colour is correct and so forth, and it is getting translated far enough out that clipping is not a problem, so can anyone figure out why it would not be working?

skyhawk
2004.09.14, 03:39 AM
question, are you passing a real char array, or are you passing a "look at me I'm a char" ?

SOUR-Monkey
2004.09.14, 04:09 AM
I have no idea what a "look at me I'm a char" is :P

I declare the original char array as char str[40]; and then pass it to the drawText function. I doubt that has anything to do with it though, I've tried just passing a number (ie. 97), a single character string, a char inside ' things, etc. None of them work.

SOUR-Monkey
2004.09.15, 06:06 PM
Does no-one have any idea?

Josh
2004.09.15, 06:56 PM
How's your view/projection matrix setup? I've always had problems with that method of drawing text...

SOUR-Monkey
2004.09.15, 07:28 PM
I set the matrix-mode to projection and then call glOrtho(), and then set the matrix-mode to modelview.

I'm almost sure that it is something to do with this, but I can't see exactly what. I thought it may be getting covered up by other sprites like the skybox, but because I'm not using a depth buffer and the text is the very last thing to get drawn, that won't be the case.

I think I'll have a look at alternate ways of drawing text, because this doesn't look like it is a very reliable method. It originally looked a lot easier than having to make my own bitmap font, but it doesn't seem quite as inviting now.

PowerMacX
2004.09.16, 06:38 PM
I found this example in Google:

void output(int x, int y, char *string)
{
int len, i;
glRasterPos2f(x, y);
len = (int) strlen(string);
for (i = 0; i < len; i++)
{
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, string[i]);
}
}

The only difference is that in your code you also have
glTranslatef(0.0f, 0.0f, -1.0f);

Now, if you switch to glOrtho() mode, that shouldn't be necessary. :???:

SOUR-Monkey
2004.09.16, 07:09 PM
I'm already in glOrtho() mode. I wondered if maybe the text was simply being clipped, so I tried translating it out but that didn't work.

I can't see anything wrong with the code at all, and when I run it the only thing that appears out of place is the fact that there is no text appearing. It is very odd.

NCarter
2004.09.16, 07:28 PM
Try rendering a quad in the same place as the text. If you can't see the quad either, there must be something wrong with your glOrtho() call or something.

Although glOrtho() is really simple, I always forget that the parameters are "left, right, bottom, top, near, far" and not "left, top, right, bottom, near, far"! Maybe you've done something similar.

SOUR-Monkey
2004.09.18, 08:52 PM
I did render a quad there, and it flickered very fast. I've noticed now, the text does appear, but it only seems to be for 1 frame, and it seems to appear in the center of the explosions. The text also seems to follow the explosion down, sometimes I see it flicker again.

Vertizor
2004.09.19, 05:01 AM
Make sure you disable textures or bitmap fonts won't work.

glDisable(GL_TEXTURE_2D);
glColor3fv((float*)&txtColor);
glLoadIdentity();
glRasterPos2i(m_textX,m_textY);
glutBitmapString(GLUT_BITMAP_HELVETICA_12, (UCHAR*)m_text);
glEnable(GL_TEXTURE_2D);
The glutBitmapString() routine is actually from the freeglut library, an alternative to glut. What you're doing now still works, I just wanted to point out this difference. In your case, you should call glDisable(GL_TEXTURE_2D) before the start of the FOR loop. Don't forget to re-enable it after you're done rendering the string.

Also, here is my resize routine from one of my projects, it helps to make 2D drawing in GLUT easier. Basically it sets up a coord system to resemble Windows' coord system (0x,0y is at the upper left corner of the window).

static void resize(int w, int h)
{
glViewport(0, 0, w, h); // Establish viewing area to cover entire window.
glMatrixMode(GL_PROJECTION); // Start modifying the projection matrix.
glLoadIdentity(); // Reset project matrix.
glOrtho(0, w, 0, h, -1, 1); // Map abstract coords directly to window coords.
glScalef(1, -1, 1); // Invert Y axis so increasing Y goes down.
glTranslatef(0.0f, (float)-h, 0.0f);
glutPostRedisplay ();
}

SOUR-Monkey
2004.09.19, 08:36 AM
I tried disabling textures, but it still doesn't work. Thanks for the tip though, and welcome to the forum.

Vertizor
2004.09.19, 06:02 PM
Looking at your original code, I think you should omit the glLoadIdentity() and glTranslatef() calls after you make the call to glRasterPos2f(). The glRasterPos2f() call is enough to position text output, your translate call looks like you're trying to move it back along -z. Bitmap text output is not geometry so it won't be affected by glTranslatef().

Start calling your glutBitmapCharacter routines right after glRasterPos2f().

SOUR-Monkey
2004.09.19, 09:02 PM
Well, the glLoadIdentity() and glTranslatef() calls are now gone. It still doesn't work :(

I can't see anything wrong with this, I must have screwed something up earlier.

NCarter
2004.09.19, 09:07 PM
I set the matrix-mode to projection and then call glOrtho(), and then set the matrix-mode to modelview.
Are you doing glLoadIdentity() just before glOrtho()?

arekkusu
2004.09.19, 09:20 PM
The glAArg Demo source (http://homepage.mac.com/arekkusu/SW/glAArg_0.2.2.zip) contains working glutBitmapCharacter code in an ortho view. It is used to draw the renderer name at the bottom of the window.

SOUR-Monkey
2004.09.19, 11:02 PM
Yeah, I am.

SOUR-Monkey
2004.09.22, 06:49 PM
I've managed to get text rendering now, using the following code:
void drawText(int x, int y, char *string)
{
int i, len;

glColor4f(1.0f, 1.0f, 1.0f, 1.0f);

glLoadIdentity();
glTranslatef(0.0f, 0.0f, -5.0f);
glRasterPos2i(x, y);

glDisable(GL_TEXTURE);
glDisable(GL_TEXTURE_2D);
for (i = 0, len = strlen(string); i < len; i++)
{
glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int)string[i]);
}
glEnable(GL_TEXTURE);
glEnable(GL_TEXTURE_2D);
}

Oddly enough, my application crashes the second time I call glRasterPos2i() in a frame. This means I can only print one line of text. Something, I expect, that will need to researched.

Thanks for all the tips and help you guys have given me though. I appreciate it.