c++ - FreeType2 - 字体较小

标签 c++ opengl freetype

我正在使用 FreeType2 进行格式渲染。我的问题是字体大小。我在左上角有原点 [0,0],字体大小设置为屏幕的全高。我的渲染结果可以在图片中看到。

enter image description here

为什么字体没有填满我窗口的整个高度?

我的渲染代码:

int devW, devH;
    device->GetViewport(&devW, &devH);

    float sx = 2.0f / static_cast<float>(devW);
    float sy = 2.0f / static_cast<float>(devH);

    x = MyMath::MyMathUtils::MapRange(0, 1, -1, 1, x);
    y = MyMath::MyMathUtils::MapRange(0, 1, -1, 1, y);


    MyStringWide wText = MyStringWide(text);
    this->fontQuad->PrepareForRender();
    for (int i = 0; i < wText.GetLength(); i++)
    {
        int znak = wText[i];

        unsigned long c = FT_Get_Char_Index(this->fontFace, znak);
        FT_Error error = FT_Load_Glyph(this->fontFace, c, FT_LOAD_RENDER);


        FT_GlyphSlot glyph = this->fontFace->glyph;

        MyStringAnsi textureName = "Font_Renderer_Texture";
        textureName += "_";
        textureName += znak;

        if (GetTexturePoolInstance()->ExistTexture(textureName) == false)
        {
            GetTexturePoolInstance()->AddTexture2D(textureName, 
                glyph->bitmap.buffer, glyph->bitmap.width * glyph->bitmap.rows, 
                MyGraphics::A8, 
                glyph->bitmap.width, glyph->bitmap.rows, 
                false,
                true);
        }



        float x2 = x + glyph->bitmap_left * sx;
        float y2 = -y - glyph->bitmap_top * sy;
        float w = glyph->bitmap.width * sx;
        float h = glyph->bitmap.rows * sy;


        this->fontQuad->GetEffect()->SetVector4("cornersData", MyMath::Vector4(x2, y2, w, h));
        this->fontQuad->GetEffect()->SetVector4("fontColor", fontColor);
        this->fontQuad->GetEffect()->SetVector4("texCoordData", MyMath::Vector4(0, 0, 1, 1));       
        this->fontQuad->GetEffect()->SetTexture("fontTexture", textureName);

        this->fontQuad->RenderEffect("classic", 0, this->fontQuad->GetNumVertices(), 0, this->fontQuad->GetNumPrimitives());

        x += (glyph->advance.x >> 6) * sx;
        y += (glyph->advance.y >> 6) * sy;

    }

最佳答案

您需要使用更完整的文本进行测试才能理解问题。一个字形由内部引导、上升、下降组成。你想要的计算似乎是 Ascent。此外,您必须问问自己是否要支持像“Á”或小写“p”这样的文本。

Font Metrics

关于c++ - FreeType2 - 字体较小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22967287/

相关文章:

c++ - 在命名空间中定义结构的成员

c++ - C++ 中的继承和接口(interface)

c++ - 取消投影和重新投影的精度损失

opengl - 我应该总是使用 GL_CULL_FACE 吗?

c - 如何确保对象不会超出可见范围 ->opengl->c

fonts - 具有 GPos 字距调整表支持的字体文件解析器,用于呈现字形

python - 即使安装了所有必需的软件包,Matplotlib 也没有安装

c++ - ->second 是否为迭代器 my_map.end() 定义?

C++ - 按降序排序数组不起作用

c++ - 如何将opencv mat转换为字符串c++