I can't get glutBitmapCharacter() to work

Member
Posts: 70
Joined: 2004.06
Post: #1
First, the code:
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?
Quote this message in a reply
Moderator
Posts: 916
Joined: 2002.10
Post: #2
question, are you passing a real char array, or are you passing a "look at me I'm a char" ?
Quote this message in a reply
Member
Posts: 70
Joined: 2004.06
Post: #3
I have no idea what a "look at me I'm a char" is Rasp

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.
Quote this message in a reply
Member
Posts: 70
Joined: 2004.06
Post: #4
Does no-one have any idea?
Quote this message in a reply
Moderator
Posts: 608
Joined: 2002.04
Post: #5
How's your view/projection matrix setup? I've always had problems with that method of drawing text...
Quote this message in a reply
Member
Posts: 70
Joined: 2004.06
Post: #6
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.
Quote this message in a reply
Moderator
Posts: 770
Joined: 2003.04
Post: #7
I found this example in Google:

Code:
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
Code:
glTranslatef(0.0f, 0.0f, -1.0f);

Now, if you switch to glOrtho() mode, that shouldn't be necessary. Huh
Quote this message in a reply
Member
Posts: 70
Joined: 2004.06
Post: #8
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.
Quote this message in a reply
Moderator
Posts: 365
Joined: 2002.04
Post: #9
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.

Neil Carter
Nether - Mac games and comic art
Quote this message in a reply
Member
Posts: 70
Joined: 2004.06
Post: #10
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.
Quote this message in a reply
Vertizor
Unregistered
 
Post: #11
Make sure you disable textures or bitmap fonts won't work.

Code:
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).

Code:
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 ();
}
Quote this message in a reply
Member
Posts: 70
Joined: 2004.06
Post: #12
I tried disabling textures, but it still doesn't work. Thanks for the tip though, and welcome to the forum.
Quote this message in a reply
Vertizor
Unregistered
 
Post: #13
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().
Quote this message in a reply
Member
Posts: 70
Joined: 2004.06
Post: #14
Well, the glLoadIdentity() and glTranslatef() calls are now gone. It still doesn't work Sad

I can't see anything wrong with this, I must have screwed something up earlier.
Quote this message in a reply
Moderator
Posts: 365
Joined: 2002.04
Post: #15
SOUR-Monkey Wrote: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()?

Neil Carter
Nether - Mac games and comic art
Quote this message in a reply
Post Reply