user3808059
user3808059

Reputation: 383

Rendering fonts with Freetype and OpenGL

I have problem freetype and OpenGL. I need just draw all loaded symbols on single texture. Here's:

    FT_Init_FreeType(&lib);
    FT_New_Face(lib, "C:\\verdana.ttf", 0, &face);
    FT_Set_Pixel_Sizes(face, 0, size);

    auto ww = 256 * size;
    auto hh = size;

    std::vector<unsigned char> buffer(ww * hh, 0);

    int off = 0;

    for (int c = 0; c < 256; c++)
    {
        FT_UInt GlyphIndex;

        GlyphIndex = FT_Get_Char_Index(face, c);

        FT_Load_Char(face, GlyphIndex, FT_LOAD_RENDER);

        FT_Bitmap bmp = face->glyph->bitmap;

        int advance = (face->glyph->advance.x >> 6);
        int bW = bmp.width; 
        int bH = bmp.rows;

        for (int h = 0; h < bH; ++h) {
            for (int w = 0; w < bW; ++w) {

                buffer[h * bW + off + w] = bmp.buffer[w + bW * h];
            }
        }

        off += advance;

    }


    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);

    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    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);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, ww, hh, 0, GL_ALPHA, GL_UNSIGNED_BYTE, &buffer[0]);

I tried many ways to do this. But all I get is absolutely black texture... What's wrong with my code?

Upvotes: 0

Views: 2190

Answers (2)

Omid
Omid

Reputation: 309

I think the problem is that some values between 0-255 is not visible or drawable character and that's why you get nothing.

you should check for GlyphIndex:

GlyphIndex = FT_Get_Char_Index(face, c);
if (!GlyphIndex) continue;

then you can expect freetype to draw the rest of characters for you.

Upvotes: 1

user3808059
user3808059

Reputation: 383

hallelujah, I got solution!

This should look like this:

for (int c = 0; c < 256; c++)
    {
        FT_UInt GlyphIndex;

        GlyphIndex = FT_Get_Char_Index(face, c);

        FT_Load_Char(face, GlyphIndex, FT_LOAD_RENDER);

        FT_Bitmap bmp = face->glyph->bitmap;

        int advance = (face->glyph->advance.x >> 6);
        int bW = bmp.width; 
        int bH = bmp.rows;

        for (int h = 0; h < bH; ++h) {
            for (int w = 0; w < bW; ++w) {

                buffer[h * ww + off + w] = bmp.buffer[w + bW * h];
            }
        }

        off += advance;

    }


    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);

    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    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);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, ww, hh, 0, GL_RED, GL_UNSIGNED_BYTE, &buffer[0]);

Upvotes: 0

Related Questions