无法使用FreeType将字形渲染为OpenGL纹理

时间:2019-05-28 14:44:58

标签: opengl freetype2

由于某种原因,今天Freetype停止为我工作。昨天一切正常,我什至没有碰过代码。而不是呈现具有特定文本颜色的字母平面。

我已经检查了是否存在混合问题。

用于加载字形的代码。

// 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);

这里还有我的渲染器代码:

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;
        }

这是我昨天得到的结果: Example1

这就是我今天要得到的: Example2

1 个答案:

答案 0 :(得分:0)

我刚刚注意到: //_character->Bind(1); 这意味着纹理在绘制之前没有被绑定。 评论出来解决了这个问题。