I'm trying to display some text using OpenGL with FreeType library. It's working, yet text looks not so smooth. In FreeType documentation it says that there's some antialiasing happing to the texture during loading, but it doesn't look that way in my case. This is what I'm doing:
FT_Init_FreeType(&m_fontLibrary);
FT_New_Face(m_fontLibrary, "src/VezusLight.OTF", 0, &m_BFont);
FT_Set_Pixel_Sizes(m_BFont, 0, 80);
m_glyph = m_BFont->glyph;
GLuint tex;
glActiveTexture(GL_TEXTURE1);
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glUseProgram(m_textPipeline);
glUniform1i(m_texLocation, 1);
glUseProgram(0);
and then rendering:
glActiveTexture(GL_TEXTURE1);
glEnableVertexAttribArray(m_coordTex);
glBindBuffer(GL_ARRAY_BUFFER, m_VBO);
const char *p;
float x = x_i, y = y_i;
const char* result = text.c_str();
for (p = result; *p; p++)
{
if (FT_Load_Char(m_BFont, *p, FT_LOAD_RENDER))
continue;
glTexImage2D(
GL_TEXTURE_2D,
0,
GL_ALPHA,
m_glyph->bitmap.width,
m_glyph->bitmap.rows,
0,
GL_ALPHA,
GL_UNSIGNED_BYTE,
m_glyph->bitmap.buffer
);
float x2 = x - 1024 + m_glyph->bitmap_left;
float y2 = y - 600 - m_glyph->bitmap_top;
float w = m_glyph->bitmap.width;
float h = m_glyph->bitmap.rows;
GLfloat box[4][4] = {
{ x2, -y2 - h, 0, 1 },
{ x2 + w, -y2 - h, 1, 1 },
{ x2, -y2, 0, 0 },
{ x2 + w, -y2, 1, 0 },
};
glBufferData(GL_ARRAY_BUFFER, 16 * sizeof(GLfloat), box, GL_DYNAMIC_DRAW);
glVertexAttribPointer(m_coordTex, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), NULL);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
x += (m_glyph->advance.x >> 6);
y += (m_glyph->advance.y >> 6);
}
glDisableVertexAttribArray(m_coordTex);
Result looks like this:
Can anyone spot a problem in my code?