使用FreeType绘制文本(DirectX 11)

时间:2018-04-26 08:16:01

标签: c++ directx directx-11 freetype

所以我想用FreeType(在DirectX 11上)绘制文本。问题是我并不真正了解如何创建my_draw_bitmap函数(我遵循this tutorial)。

我得到所有字形,然后将它们转换为位图,但我不知道如何将FT_Bitmap *转换为ID3D11Texture2D *(这样 - 理论上 - 允许我渲染一个文字)

这是我的代码:

FontLoader.cpp

    void FontLoader::RenderText(ID3D11Device* p_device, Text* p_text, Math::Vec2 p_position)
    {
    const char* text = p_text->GetText();

    FT_Face face = p_text->GetFont()->GetFace();
    FT_GlyphSlot slot = face->glyph;

    Math::Vec2 pen = p_position;

    for (unsigned int i = 0; i < strlen(text); ++i)
    {
        if (FT_Load_Char(face, text[i], FT_LOAD_RENDER))
            continue;

        // draw to our target surface
        CreateTextureFromBitmap(p_device, &slot->bitmap, Math::Vec2((float)slot->bitmap_left, (float)slot->bitmap_top));

        // Increment pen position
        pen._x += slot->advance.x >> 6;
    }
}

ID3D11Texture2D* FontLoader::CreateTextureFromBitmap(ID3D11Device* p_device, FT_Bitmap* p_bitmap, Math::Vec2 p_position)
{
    D3D11_TEXTURE2D_DESC textureDesc;
    textureDesc.Width = p_bitmap->width;
    textureDesc.Height = p_bitmap->pitch;
    textureDesc.MipLevels = textureDesc.ArraySize = 1;
    textureDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    textureDesc.SampleDesc.Count = 1;
    textureDesc.Usage = D3D11_USAGE_DYNAMIC;
    textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
    textureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
    textureDesc.MiscFlags = 0;

    ID3D11Texture2D *texture2D = NULL;
    // don't know how and when to use p_bitmap
    p_device->CreateTexture2D(&textureDesc, NULL, &texture2D);

    return texture2D;
}

非常感谢!

0 个答案:

没有答案