Rendering text using OpenGL

Started by
1 comment, last by junkyska 11 years, 9 months ago
I am writing a game engine and I ran into trouble with text rendering. I can successfully load a bitmap font (with characters that cna have different widths). The height of the characters is a power of 2, the width is not neccessarily. All glyphs are loaded as a separate texture. I have OpenGL 1.4, so the textures must have a power-of-2 width, so I wrote a function that would resize them to a power-of-2 width (by padding the texture on the right), and then returns the width-to-new-width ratio, which would be the X texture coordinate of the right of the glyph. Here's the function:


double load_texture_special(void *data_, int width, int height)
{
char *data = (char*)data_;
/* convert the texture to have a width that is a power of 2 */
int new_width=1;
while (new_width < width)
{
new_width <<= 1;
};
printf("from %d to %d\n", width, new_width);
int pad = new_width-width;
printf("pad=%d\n", pad);
char *buffer = (char*) malloc(new_width*height);
if (buffer == NULL) return;
int count = height;
while (count--)
{
int cnt = width;
while (cnt--)
{
*buffer++ = *data++;
};
cnt = pad;
while (cnt--)
{
*buffer++ = 255;
};
};
glTexImage2D(GL_TEXTURE_2D,
0,
GL_ALPHA,
new_width,
height,
0,
GL_ALPHA,
GL_UNSIGNED_BYTE,
buffer);
/* return the old-to-new width ratio */
double ret = (double)width/(double)new_width;
return ret;
};


Then the function that actually displays the text is:


void text_overlay(MEOBJECT *object)
{
METEXT *text = (METEXT*)object;
char *string = text->str;
int penx, peny;
penx = text->x;
peny = text->y;
glColor4f(text->color.x, text->color.y, text->color.z, 1.0);
while (*string != 0)
{
MEGLYPH *glyph = get_character_glyph(text->font, *string++);
if (glyph == NULL) continue; /* skip all characters not included in the font */
glBindTexture(GL_TEXTURE_2D, glyph->name);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBegin(GL_TRIANGLES);
glTexCoord2f(0.0, 1.0);
glVertex2i(penx, peny+text->font->height);
glTexCoord2f(glyph->wc, 1.0);
glVertex2i(penx+glyph->width, peny+text->font->height);
glTexCoord2f(glyph->wc, 0.0);
glVertex2i(penx+glyph->width, peny);
glTexCoord2f(glyph->wc, 0.0);
glVertex2i(penx+glyph->width, peny);
glTexCoord2f(0.0, 0.0);
glVertex2i(penx, peny);
glTexCoord2f(0.0, 1.0);
glVertex2i(penx, peny+text->font->height);
glEnd();
penx += glyph->width;
};
};


Where glyph->wc is the width-to-new-width ratio (the rightmost X texture coordinate). But the function just renders a set of vertical bars! If I don't resize to a power-of-2 width, then you can see some of the letters, but they are either half-cut, curved, distorted, or otherwise unreadable. Does anyone have any idea of what could be going on here?
Advertisement
Have you tested your resize function? Make sure that works first. Are you sure the wc variable is correctly set?
Check glPixelStorei( GL_PACK_ALIGNMENT, 1 ) and glPixelStorei( GL_UNPACK_ALIGNMENT, 1 ).

This topic is closed to new replies.

Advertisement