openglfreetype2

Can't render glyph using freetype to OpenGL texture


Today for some reason Freetype stopped working for me. Everything worked fine yesterday, I haven't even touched the code. Instead of letter planes with specific text colors are being rendered.

I checked already if that's blending problem.

Code for loading the glyph.

// This is the relative path to the font,
    // later used to load the font.
    std::string strFontPath = "fonts/" + strFont + ".ttf";

    // Initialising Freetype Library

    FT_Library _ft;
    FT_Init_FreeType(&_ft);

    // Loading font from the file.
    FT_Face _face;

    if (FT_New_Face(_ft, strFontPath.c_str(), 0, &_face))
        EXIT_ERROR(-11);

    FT_Set_Pixel_Sizes(_face, 0, 48);

    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

    // When I used char instead program misbehaved so I'm using 
    // unsigned short int and convert the type.
    for (unsigned short int usiCurrentCharacter = 30; usiCurrentCharacter < 128; usiCurrentCharacter++)
    {
        // Loading char to the face, fails when char fails to load.
        if (FT_Load_Glyph(_face, FT_Get_Char_Index(_face,(char)usiCurrentCharacter), FT_LOAD_RENDER))
        {
            EXIT_ERROR(-12);
        }

        std::cout << (char)usiCurrentCharacter  << _face->glyph->bitmap.width << " " << _face->glyph->bitmap.rows << std::endl;


        // Create New Font 
        FontTexture* _char = new FontTexture(_face->glyph->bitmap.buffer,
            0,
            { static_cast<float>(_face->glyph->bitmap.width),
            static_cast<float>(_face->glyph->bitmap.rows) });

        _char->SetAdvance(_face->glyph->advance.x);
        _char->SetBearing({ static_cast<float>(_face->glyph->bitmap_left),
            static_cast<float>(_face->glyph->bitmap_top) });

        TextureManager::getTextureManager().PrecacheTexture(std::to_string(usiCurrentCharacter) + strFont, _char);

        Characters.insert(std::pair<char, FontTexture*>(char(usiCurrentCharacter), _char));
    }

    FT_Done_Face(_face);
    FT_Done_FreeType(_ft);

Also here goes my renderer code:

shader->Bind();
        shader->SetUniform3f("u_TextureColor", color.x, color.y, color.z);
        //shader->SetUniform2f("u_Position", pos.x, pos.y); //We have to calculate array on cpu anyway :P
        shader->SetUniform1i("u_Texture", 1);
        //Iterate through all characters
        for (char c : text)
        {
            FontTexture* _character = font->getFontTexture(c);



            float xpos = pos.x + _character->GetBearing().x * scale;
            float ypos = pos.y - (_character->GetSize().y - _character->GetBearing().y) * scale;

            float w = _character->GetSize().x * scale;
            float h = _character->GetSize().y * scale;


            vertices[0] = xpos;     vertices[1] = ypos + h;     vertices[2] = 0.0;      vertices[3] = 0.0;
            vertices[4] = xpos;     vertices[5] = ypos;         vertices[6] = 0.0;      vertices[7] = 1.0;
            vertices[8] = xpos + w; vertices[9] = ypos;         vertices[10] = 1.0;     vertices[11] = 1.0;
            vertices[12] = xpos;    vertices[13] = ypos + h;    vertices[14] = 0.0;     vertices[15] = 0.0;
            vertices[16] = xpos + w;vertices[17] = ypos;        vertices[18] = 1.0;     vertices[19] = 1.0;
            vertices[20] = xpos + w;vertices[21] = ypos + h;    vertices[22] = 1.0;     vertices[23] = 0.0;

            //_character->Bind(1);
            shader->Bind();

            va->Bind();
            vb->Bind(vertices, sizeof(vertices));

            Renderer::getRenderer().DrawArrays(*va, *vb, vertices);
            //_character->Unbind();

            pos.x += (_character->GetAdvance() >> 6) * scale;
        }

This is the result I got yesterday: Example1

This is what I'm getting today: Example2


Solution

  • I've just noticed: //_character->Bind(1); That meant the texture wasn't binded before drawing. Commenting it out fixed the problem.