Rendering text with stb_font results in glitches
Posted
by
Fabian Fritz
on Game Development
See other posts from Game Development
or by Fabian Fritz
Published on 2012-11-12T19:50:15Z
Indexed on
2012/11/12
23:14 UTC
Read the original article
Hit count: 306
I'm trying to render text with OpenGL and an "inline"-font taken from the stb_fonts
The relevant code for initializing the font & rendering:
LabelFactory::LabelFactory() {
static unsigned char fontpixels [STB_SOMEFONT_BITMAP_HEIGHT][STB_SOMEFONT_BITMAP_WIDTH];
STB_SOMEFONT_CREATE(fontdata, fontpixels, STB_SOMEFONT_BITMAP_HEIGHT);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, STB_SOMEFONT_BITMAP_WIDTH, STB_SOMEFONT_BITMAP_HEIGHT, 0, GL_ALPHA, GL_UNSIGNED_BYTE, fontdata);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
}
void LabelFactory::renderLabel(Label * label) {
int x = label->x;
int y = label->y;
const char * str = label->text;
glBindTexture(GL_TEXTURE_2D, texture);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_ALPHA_TEST);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
while (*str) {
int char_codepoint = *str++;
stb_fontchar *cd = &fontdata[char_codepoint - STB_FONT_arial_14_usascii_FIRST_CHAR];
glTexCoord2f(cd->s0, cd->t0); glVertex2i(x + cd->x0, y + cd->y0);
glTexCoord2f(cd->s1, cd->t0); glVertex2i(x + cd->x1, y + cd->y0);
glTexCoord2f(cd->s1, cd->t1); glVertex2i(x + cd->x1, y + cd->y1);
glTexCoord2f(cd->s0, cd->t1); glVertex2i(x + cd->x0, y + cd->y1);
x += cd->advance_int;
}
glEnd();
}
However this results in weird glitches
I guess I'm doing something wrong with the alpha blending, however I was unable to improve it by changing the parameters. The size and length of the outline of the text that should be shown seems about right (it should read "Test Test Test").
© Game Development or respective owner