# OpenGL Freetype2 - Arial gives weird 'i' at 13 pixels

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

## Recommended Posts

Hey, I've been making my own wrapper around Freetype2 for OpenGL. However, I can't seem to easily fix this bug...it's bugging me ^^ Whenever I render the Arial font at 13 pixels height, the 'i' seems to be too high or w/e happens...this only happens when I use the Smooth feature of my wrapper, the Sharp feature works fine. Examples: 14 pixels smooth 13 pixels sharp 13 pixels smooth This is the code I use (without error handling):
                FT_Load_Char(face, character, FT_LOAD_RENDER);

FT_GlyphSlot &glyph = face->glyph;

FT_Glyph bbox_glyph;
FT_Get_Glyph(glyph, &bbox_glyph);

FT_BBox bbox;
FT_Glyph_Get_CBox(bbox_glyph, FT_GLYPH_BBOX_TRUNCATE, &bbox);

int width = pow2(glyph->bitmap.width);
int height = pow2(glyph->bitmap.rows);

if (width && height) // not a space character
{
GLuint texture = 0;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

GLubyte *buffer = NULL;
if (_sharpness == Sharpness::Smooth)
{
buffer = new GLubyte[height * width];
memset(buffer, 0x00, height * width);

for (int j = 0; j < glyph->bitmap.rows; j++)
{
memcpy(buffer + (j * width), glyph->bitmap.buffer + (j * glyph->bitmap.pitch), glyph->bitmap.pitch);
}

glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA8, width, height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, buffer);
}
else if (_sharpness == Sharpness::Sharp)
{
buffer = new GLubyte[height * width * 2];
memset(buffer, 0x00, height * width * 2);

for (int j = 0; j < glyph->bitmap.rows; j++)
{
for (int i = 0; i < glyph->bitmap.pitch; i++)
{
buffer[(j * width * 2) + (i * 2)] = buffer[(j * width * 2) + (i * 2) + 1] = glyph->bitmap.buffer[(j * glyph->bitmap.pitch) + i];
}
}

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, buffer);
}
delete[] buffer;
}

// when drawing I use something like
glBindTexture(GL_TEXTURE_2D, character.texture);
glTexCoord2d(0,            0);            glVertex2f(0,           0);
glTexCoord2d(0,            (float) glyph->bitmap.rows / (float) height); glVertex2f(0,           glyph->bitmap.rows);
glTexCoord2d((float) glyph->bitmap.pitch / (float) width, (float) glyph->bitmap.rows / (float) height); glVertex2f(glyph->bitmap.pitch, glyph->bitmap.rows);
glTexCoord2d((float) glyph->bitmap.pitch / (float) width, 0);            glVertex2f(glyph->bitmap.pitch, 0);
glEnd();


What could possibly be wrong in the pixel copying (since I guess it's the pixel copying part, it only applies to Smooth not Sharp)? Thanks in advance.

##### Share on other sites
NeHe Productions has a very nice OpenGL text tutorial. I don't think it even relies on FreeType2. On the other hand I'm pretty sure it uses Windows-only functions. I followed it and never had any funny defects. Check it out if you haven't already. If you aren't using Windows, I apologize for making that assumption. I also don't understand why you calculate your width and height by using pow2(). Is pow2() your own function? I only know about pow().

##### Share on other sites
Hello fellow citizen! :)

I've written a font render engine once, only for learning how to do font rendering and for testing purposes. Didn't test it very well, so it might show the same problems. Anyway: I used the tutorials on the freetype 2 website itself and I found them quite easy to follow. It gave me results quickly.

My program ran on Linux using OpenGL and the tutorial I used can be found here:
http://www.freetype.org/freetype2/docs/tutorial/step1.html

You might want to compare your steps with the ones in this tutorial if you haven't seen it yet.

##### Share on other sites
@kittycat
I would like to use TTF fonts using FreeType2, and draw them in OpenGL. Don't want to be bound to windows only ;). And pow2 outputs the next following power of 2, which is needed for OGL textures (for older cards).

@TheFlyingDutchman

Hey, greetings! Nice weather eh xD

I followed the same tutorial ;). It works perfectly fine for almost anything, except this weird little bug. I think it has to do with the connection of FT2 with OpenGL...ie, the pixel copying. Not 100% sure, but it looks like it. Either that or the texture coordinates are wrong (which I don't think, since I had some rather random results...as if it was unused memory).

##### Share on other sites
You might want to try this, as I remember correctly I have seen several implementations having image corruption problems because the texture parameters weren't setup correctly:

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texSize, texSize, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, data);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

Especially the first line of code is important (the last four don't really matter for this I'd say). Hope this helps.

##### Share on other sites
Thanks! The first line made it work!

Awesome, it looks so sweet now :D

1. 1
2. 2
JoeJ
20
3. 3
frob
18
4. 4
5. 5

• 10
• 11
• 12
• 13
• 9
• ### Forum Statistics

• Total Topics
632204
• Total Posts
3004754

×