-1

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:

text

Can anyone spot a problem in my code?

genpfault
  • 51,148
  • 11
  • 85
  • 139
user1760770
  • 365
  • 5
  • 17

2 Answers2

1

Two issues with your code.

First one is a buffer overflow: Texture coodinates in your box structure are vec2, however you tell glVertexAttribPointer it was a vec4 (the stride of 4*sizeof(float) is what matters, and the mismatched size parameters makes OpenGL read out of bounds 2 elements over the end of the box array).

That your texture looks pixelated stems from the fact that texture coordinates 0 and 1 do not come to lie on pixel centers, but the edges of the texture. Either use texelFetch in the fragment shader to address pixels by their pixel coordinate, or remap the texture extents to the range [0…1] properly like explained in https://stackoverflow.com/a/5879551/524368

Community
  • 1
  • 1
datenwolf
  • 159,371
  • 13
  • 185
  • 298
0

I think for having transparent color or smooth or anti-aliased glyphs, you must enable blending in opengl and also disable depth-testing. (you can find out why and how by searching in the internet).

Something like this:

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
//and if it didn't work, then disable depth testing by uncommenting this:
//glDisable(GL_DEPTH_TEST); 

hope it helps!

Omid
  • 199
  • 1
  • 7