#### Archived

This topic is now archived and is closed to further replies.

# OpenGL OpenGL with Freetype font library has weird quirk

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

## Recommended Posts

All of my characters that comprise simply of a vertical line have dashes at the bottom (i.e. i, l, and |). This makes the character totally unreadable. Here is a screenshot: The "Name" box contains all "i''s" and the pass box contains all "l''s" (lowercase L) and the cursor is a blinking "|" which you can see suffers from the same torment.. I pretty much just encapsulated the Freetype font code from Nehe. However, I''ve totally gone through it and understand all of it (maybe not everything, obviously). It''s had this quirk for as long as I can remember. Here is the Freetype loading and printing code.. I know you might not want to go through it, but I''m hoping some people who have used this code before can help me out. A small bug like this make it look very bad, not to mention it bugs the crap out of me.. {code}
///Create a display list coresponding to the give character.

int font::MakeDispList(FT_Face face, char ch) {

//The first thing we do is get FreeType to render our character

//into a bitmap.  This actually requires a couple of FreeType commands:

//Load the Glyph for our character.

return 1;
}

//Move the face''s glyph into a Glyph object.

FT_Glyph glyph;
if(FT_Get_Glyph(face->glyph, &glyph)) {
printf("FT_Get_Glyph failed.");
return 2;
}

//Convert the glyph to a bitmap.

FT_Glyph_To_Bitmap(&glyph, ft_render_mode_normal, 0, 1);
FT_BitmapGlyph bitmap_glyph = (FT_BitmapGlyph)glyph;

//This reference will make accessing the bitmap easier

FT_Bitmap& bitmap=bitmap_glyph->bitmap;

//Use our helper function to get the widths of

//the bitmap data that we will need in order to create

//our texture.

int width = next_p2( bitmap.width );
int height = next_p2( bitmap.rows );

//Allocate memory for the texture data.

GLubyte* expanded_data = new GLubyte[2 * width * height];

//Here we fill in the data for the expanded bitmap.

//Notice that we are using two channel bitmap (one for

//luminocity and one for alpha), but we assign

//both luminocity and alpha to the value that we

//find in the FreeType bitmap.

//We use the ?: operator so that value which we use

//will be 0 if we are in the padding zone, and whatever

//is the the Freetype bitmap otherwise.

for(int j=0; j <height;j++) {
for(int i=0; i < width; i++){
expanded_data[2*(i+j*width)]=
0xFF;
expanded_data[2*(i+j*width)+1] =
(i>=bitmap.width || j>=bitmap.rows) ? 0 : bitmap.buffer[i + bitmap.width*j];
}
}

//Now we just setup some texture paramaters.

glBindTexture(GL_TEXTURE_2D, textures[ch]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);

//Here we actually create the texture itself, notice

//that we are using GL_LUMINANCE_ALPHA to indicate that

//we are using 2 channel data.

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height,
0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, expanded_data);

//With the texture created, we don''t need to expanded data anymore

delete [] expanded_data;

//So now we can create the display list

glNewList(list_base+ch,GL_COMPILE);

glPushMatrix();

glBindTexture(GL_TEXTURE_2D,textures[ch]);

//first we need to move over a little so that

//the character has the right amount of space

//between it and the one before it.

//Then we move down a little in the case that the

//bitmap extends past the bottom of the line

//(this is only true for characters like ''g'' or ''y''.

glTranslatef(bitmap_glyph->left, bitmap_glyph->top - bitmap.rows, 0);

//Now we need to account for the fact that many of

//our textures are filled with empty padding space.

//We figure what portion of the texture is used by

//the actual character and store that information in

//the x and y variables, then when we draw the

//quad, we will only reference the parts of the texture

//that we contain the character itself.

float	x = bitmap.width / (float)width,
y = bitmap.rows  / (float)height;

//Here we draw the texturemaped quads.

//The bitmap that we got from FreeType was not

//oriented quite like we would like it to be,

//so we need to link the texture to the quad

//so that the result will be properly aligned.

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(face->glyph->advance.x >> 6 ,0,0);

//increment the raster position as if we were a bitmap font.

//(only needed if you want to calculate text length)

//Finnish the display list

glEndList();

return 0;
}

int font::Init(const char * fname, unsigned int h) {

//Allocate some memory to store the texture ids.

textures = new GLuint[128];

this->h=h;

//Create and initilize a freetype font library.

FT_Library library;
if (FT_Init_FreeType( &library )) {
printf("FreeType init failed");
return 1;
}

//The object in which Freetype holds information on a given

//font is called a "face".

FT_Face face;

//This is where we load in the font information from the file.

//Of all the places where the code might die, this is the most likely,

//as FT_New_Face will die if the font file does not exist or is somehow broken.

if (FT_New_Face(library, fname, 0, &face)) {
printf("Creating the font face failed.  Check the font name");
return 2;
}

//For some twisted reason, Freetype measures font size

//in terms of 1/64ths of pixels.  Thus, to make a font

//h pixels high, we need to request a size of h*64.

//(h << 6 is just a prettier way of writting h*64)

FT_Set_Char_Size(face, h << 6, h << 6, 96, 96);

//Here we ask opengl to allocate resources for

//all the textures and displays lists which we

//are about to create.

list_base=glGenLists(128);
glGenTextures(128, textures);

//This is where we actually create each of the fonts display lists.

for(unsigned char i=0;i<128;i++)
MakeDispList(face,i);

//We don''t need the face information now that the display

//lists have been created, so we free the assosiated resources.

FT_Done_Face(face);

//Ditto for the library.

FT_Done_FreeType(library);

return 0;
}

void font::Shutdown() {
glDeleteLists(list_base,128);
glDeleteTextures(128,textures);
delete[] textures;
}

/// A fairly straight forward function that pushes

/// a projection matrix that will make object world

/// coordinates identical to window coordinates.

inline void pushScreenCoordinateMatrix() {
glMatrixMode(GL_PROJECTION);
glPushMatrix();

glOrtho(0.0f,GameInfo::screenWidth,0.0f,GameInfo::screenHeight,-1.0f,1.0f);

glMatrixMode(GL_MODELVIEW);
}

/// Pops the projection matrix without changing the current

/// MatrixMode.

inline void pop_projection_matrix() {
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
}

void font::Print(float x, float y, char *text)  {

y = GameInfo::screenHeight-y;  //flip it

// We want a coordinate system where things coresponding to window pixels.

pushScreenCoordinateMatrix();

GLuint font=list_base;
float temph = h/.63f;						//We make the height about 1.5* that of

glPushAttrib(GL_LIST_BIT | GL_CURRENT_BIT  | GL_ENABLE_BIT | GL_TRANSFORM_BIT);
glDisable(GL_LIGHTING);
glEnable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glListBase(font);
glPushMatrix();
glTranslatef(x,y,0);

//  The commented out raster position stuff can be useful if you need to

//  know the length of the text that you are creating.

//  If you decide to use it make sure to also uncomment the glBitmap command

//  in make_dlist().

//	glRasterPos2f(0,0);

glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);
glBindTexture(GL_TEXTURE_2D, 0);
//	float rpos[4];

//	glGetFloatv(GL_CURRENT_RASTER_POSITION ,rpos);

//	float len=x-rpos[0];

glPopMatrix();
glPopAttrib();

pop_projection_matrix();

}

To narrow it down, here is the part where it actually makes the display list and the texture.. it might be in here somewhere?
//This reference will make accessing the bitmap easier

FT_Bitmap& bitmap=bitmap_glyph->bitmap;

//Use our helper function to get the widths of

//the bitmap data that we will need in order to create

//our texture.

int width = next_p2( bitmap.width );
int height = next_p2( bitmap.rows );

//Allocate memory for the texture data.

GLubyte* expanded_data = new GLubyte[2 * width * height];

//Here we fill in the data for the expanded bitmap.

//Notice that we are using two channel bitmap (one for

//luminocity and one for alpha), but we assign

//both luminocity and alpha to the value that we

//find in the FreeType bitmap.

//We use the ?: operator so that value which we use

//will be 0 if we are in the padding zone, and whatever

//is the the Freetype bitmap otherwise.

for(int j=0; j <height;j++) {
for(int i=0; i < width; i++){
expanded_data[2*(i+j*width)]=
0xFF;
expanded_data[2*(i+j*width)+1] =
(i>=bitmap.width || j>=bitmap.rows) ? 0 : bitmap.buffer[i + bitmap.width*j];
}
}

//Now we just setup some texture paramaters.

glBindTexture(GL_TEXTURE_2D, textures[ch]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);

//Here we actually create the texture itself, notice

//that we are using GL_LUMINANCE_ALPHA to indicate that

//we are using 2 channel data.

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height,
0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, expanded_data);

//With the texture created, we don''t need to expanded data anymore

delete [] expanded_data;

//So now we can create the display list

glNewList(list_base+ch,GL_COMPILE);

glPushMatrix();

glBindTexture(GL_TEXTURE_2D,textures[ch]);

//first we need to move over a little so that

//the character has the right amount of space

//between it and the one before it.

//Then we move down a little in the case that the

//bitmap extends past the bottom of the line

//(this is only true for characters like ''g'' or ''y''.

glTranslatef(bitmap_glyph->left, bitmap_glyph->top - bitmap.rows, 0);

//Now we need to account for the fact that many of

//our textures are filled with empty padding space.

//We figure what portion of the texture is used by

//the actual character and store that information in

//the x and y variables, then when we draw the

//quad, we will only reference the parts of the texture

//that we contain the character itself.

float	x = bitmap.width / (float)width,
y = bitmap.rows  / (float)height;

//Here we draw the texturemaped quads.

//The bitmap that we got from FreeType was not

//oriented quite like we would like it to be,

//so we need to link the texture to the quad

//so that the result will be properly aligned.

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(face->glyph->advance.x >> 6 ,0,0);

//increment the raster position as if we were a bitmap font.

//(only needed if you want to calculate text length)

//Finnish the display list

glEndList();

Thanks a lot for any help.

##### Share on other sites
Those glyphs look to be one pixel wide. Is the bitmap returned by FreeType actually one pixel wide? If so, then you might want to try one of a couple things:

Pad the width out to 2 pixels. glTexImage2D requires a width of the form 2n+2(border) where n is an integer. 1 doesn''t fit the bill.

Or, try setting glPixelStorei(GL_UNPACK_ALIGNMENT, 1) to take care of any alignment problems with your pixel buffer.

##### Share on other sites
Hey! Look at that! It worked! Simply forcing the texture to be 2 pixels wide minimum fixed it.. beautiful! Thank you so much!

1. 1
2. 2
frob
16
3. 3
4. 4
5. 5

• 20
• 13
• 14
• 76
• 22
• ### Forum Statistics

• Total Topics
632145
• Total Posts
3004381

×