Jump to content
  • Advertisement
Sign in to follow this  
DarkCybo1

OpenGL Problem with font with OpenGL+FreeType2

This topic is 4840 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

If I make a font with a size lower then 16, it becomes blurry or distorted. The font in my program: http://img170.imageshack.us/img170/2630/text20is.jpg The font from Nehe's Lesson 43: http://img230.imageshack.us/img230/619/text19tg.jpg I wrote the font-loading from Nehe's Lesson 43, and made it as similar as I could to his, but I don't know why the text looks different. I should note that if I disable blending, the font isnt distorted but now has black spots on it. Code:

/////////////////////////////

//CREATE FONT DISPLAY LIST//

////////////////////////////
void CreateFontDisplayList(FT_Face font, char ch, GLuint list_base, GLuint* tex)
{
   if(FT_Load_Glyph(font, FT_Get_Char_Index(font, ch), FT_LOAD_DEFAULT))
      cLog::WRITE(ERRORLOG, "CreateFontDisplayList(FT_Face, char, GLuint, GLuint*): "
         "FT_Load_Glyph failed");
         
   FT_Glyph glyph;
   
   if(FT_Get_Glyph(font->glyph, &glyph))
      cLog::WRITE(ERRORLOG, "CreateFontDisplayList(FT_Face, char, GLuint, GLuint*): "
         "FT_Get_Glyph failed");
   
   FT_Glyph_To_Bitmap(&glyph, ft_render_mode_normal, 0, 1);
   FT_BitmapGlyph bmGlyph = (FT_BitmapGlyph)glyph;
   
   FT_Bitmap& bitmap = bmGlyph->bitmap;
   
   int width = nextPower(bitmap.width);
   int height = nextPower(bitmap.rows);
   
   GLubyte* expandedData = new GLubyte[2 * width * height];
   
   for(int y = 0; y < height; y++)
   {
      for(int x = 0; x < width; x++)
      {
         expandedData[2 * (x + y * width)] = expandedData[2 * (x + y * width) + 1] =
            (y >= bitmap.rows || x >= bitmap.width)? 0 : bitmap.buffer[x + bitmap.width * y];
      }
   }
   
   glBindTexture(GL_TEXTURE_2D, tex[ch]);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   
   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_LUMINANCE_ALPHA,
      GL_UNSIGNED_BYTE, expandedData);
   
   delete [] expandedData;
   
   glNewList(list_base + ch, GL_COMPILE);
   glBindTexture(GL_TEXTURE_2D, tex[ch]);
   
   glPushMatrix();
   
   glTranslatef(bmGlyph->left, 0, 0);
   glTranslatef(0, bmGlyph->top - bitmap.rows, 0);
   
   float x = (float)bitmap.width / (float)width;
   float y = (float)bitmap.rows / (float)height;
   
   glBegin(GL_QUADS);
   
   glTexCoord2d(0,0); glVertex2f(0,bitmap.rows);
	glTexCoord2d(0,y); glVertex2f(0,0);
	glTexCoord2d(x,y); glVertex2f(bitmap.width,0);
	glTexCoord2d(x,0); glVertex2f(bitmap.width,bitmap.rows);
   
   glEnd();
   
   glPopMatrix();
   
   glTranslatef(font->glyph->advance.x >> 6, 0, 0);
   
   glEndList();
}

namespace freeType
{

////////////////////////

//FONT INITIALIZATION//

///////////////////////

void freeType::fontData::init(char* fontLoc, unsigned int height)
{
   textures = new GLuint[128];
   
   h = height;
   
   FT_Library lib;
   
   if(FT_Init_FreeType(&lib))
      cLog::WRITE(ERRORLOG, "fontData::init(char*, int): FT_Init_FreeType failed");
   
   FT_Face face;
   
   if(FT_New_Face(lib, fontLoc, 0, &face))
      cLog::WRITE(ERRORLOG, "fontData::init(char*, int): FT_New_Face failed");
   
   FT_Set_Char_Size(face, height << 6, height << 6, 96, 96);
   
   list_base = glGenLists(128);
   glGenTextures(128, textures);
   
   for(unsigned char j = 0; j < 128; j++)
      CreateFontDisplayList(face, j, list_base, textures);
   
   FT_Done_Face(face);
   FT_Done_FreeType(lib);
}

//FONT CLEANUP//
void freeType::fontData::clean()
{
   glDeleteLists(list_base, 128);
   glDeleteTextures(128, textures);
   
   delete [] textures;
}

//////////////////

//TEXT PRINTING//

////////////////

void freeType::print(fontData &FontData, float x, float y, char *text, ...)
{
   glPushAttrib(GL_TRANSFORM_BIT);
   
	int  viewPort[4];
	glGetIntegerv(GL_VIEWPORT, viewPort);
	
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
	
	gluOrtho2D(viewPort[0],viewPort[2],viewPort[1],viewPort[3]);
	
	glPopAttrib();
	
	y = (768.0f - FontData.GetHeight()) - y;
	
   GLuint font = FontData.GetListBase();
   float height = FontData.GetHeight() / .63f;
   
   char fmtText[512];
   va_list arg;
   
   if(!text)
      return;
   
   va_start(arg, text);
   vsprintf(fmtText, text, arg);
   va_end(arg);
   
   sprintf(fmtText, "%s\n", fmtText);
   
   std::vector<std::string> stringVec;
   std::string sBuffer = " ";
   
   if(strlen(fmtText) < 512 && fmtText[strlen(fmtText) - 1] != '\n')
      fmtText[strlen(fmtText)] = '\n';

   for(int j = 0; j < strlen(fmtText); j++)
   {
      if(fmtText[j] == '\n')
      {
         stringVec.push_back(sBuffer);
         sBuffer = " ";
      } else
         sBuffer.append(1, fmtText[j]);
   }
   
   glPushAttrib(GL_LIST_BIT | GL_ENABLE_BIT | GL_CURRENT_BIT | GL_TRANSFORM_BIT);
   glMatrixMode(GL_MODELVIEW);
   
   glEnable(GL_TEXTURE_2D);
   glDisable(GL_ALPHA_TEST);
   glEnable(GL_BLEND);
   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);	
   
   glListBase(font);
   
   float modelview_mat[16];
   glGetFloatv(GL_MODELVIEW_MATRIX, modelview_mat);
   
   for(int j = 0; j < stringVec.size(); j++)
   {
      glPushMatrix();
      glLoadIdentity();
      
      glTranslatef(x, y - (height * j), 0);
      glMultMatrixf(modelview_mat);
      
      glCallLists(stringVec[j].length(), GL_UNSIGNED_BYTE, stringVec[j].c_str());
      
      glPopMatrix();
   }
   
   glPopAttrib();
   
   glDisable(GL_TEXTURE_2D);
   glDisable(GL_BLEND);
   glEnable(GL_ALPHA_TEST);
   
   glPushAttrib(GL_TRANSFORM_BIT);
	glMatrixMode(GL_PROJECTION);
	
	glPopMatrix();
	glPopAttrib();
}

}




[edit: added source tags -SiCrane]

Share this post


Link to post
Share on other sites
Advertisement
Alright, I replaced my code entirely with nehe's code, and it still doesnt work. I guess the problem isnt the Freetype code but something else in the program. But I don't see what's causing the problem. Any ideas?

[Edited by - DarkCybo1 on August 17, 2005 2:58:57 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!