Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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").

share|improve this question

put on hold as off-topic by Josh Petrie 19 hours ago

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions about debugging a problem in your project must present a concise selection of code and context so as to allow a reader to diagnose the issue without needing to read all of your code or to engage in extensive back-and-forth dialog. For more information, see this meta thread." – Josh Petrie
If this question can be reworded to fit the rules in the help center, please edit the question.