为什么在 OpenGL 中使用 FreeType 渲染文本时,较高的字母会出现变形?

如何解决为什么在 OpenGL 中使用 FreeType 渲染文本时,较高的字母会出现变形?

我已经设法在 OpenGL 4 中使用 FreeType 绘制文本,但是较高的字母(例如 g、d、f 等)不知何故被绘制得太高了。 This is what it looks like. This is what it is supposed to look like. 高的字母太高了,而“正常高度”的字母还好。

struct FontChar {
    float tx; // texcoord x position
    float tw; // texcoord x width
    glm::ivec2 size; // face->glyph->bitmap.width,face->glyph->bitmap.rows
    glm::ivec2 bearing; // face->glyph->bitmap_left,face->glyph->bitmap_top
    glm::ivec2 advance; // face->glyph->advance.x,face->glyph->advance.y
} fontChars[128]; // this is populated properly with FreeType

std::vector<float> vertices;

const float sx = 2.0f / 1920.0f;
const float sy = 2.0f / 1080.0f;

float x = 0.0f;
float y = 0.0f;
for (char c : text) {
    const float vx = x + fontChars[c].bearing.x * sx;
    const float vy = y + fontChars[c].bearing.y * sy;
    const float w = fontChars[c].size.x * sx;
    const float h = fontChars[c].size.y * sy;
    float tx = fontChars[c].tx;
    float tw = fontChars[c].tw;
    std::vector<float> quad = { // pos_x,pos_y,tex_x,tex_y
        vx,vy,tx,0.0f,vx + w,tx + tw,vy - h,1.0f,vx,0.0f
    };
    vertices.insert(vertices.begin(),quad.begin(),quad.end());
    x += float(fontChars[c].advance.x >> 6) * sx;
    y += float(fontChars[c].advance.y >> 6) * sy;
}

然后我将顶点缓冲到顶点缓冲区中,然后绘制它。唯一可能影响高度的代码const float h = fontChars[c].size.y * sy,但大小直接取自 FreeType,sy 适用于“正常高度”字母。这让我相信这可能是由于字形纹理被放入纹理图集中所致。

FT_Set_Pixel_Sizes(face,size);

glPixelStorei(GL_UNPACK_ALIGNMENT,1);

std::array<FontChar,128> characters{};

unsigned int w = 0;
unsigned int h = 0;
for (unsigned char c = 0; c < 128; c++) {
    if (FT_Load_Char(face,c,FT_LOAD_BITMAP_METRICS_ONLY)) {
        throw std::runtime_error("Failed to load glyph");
    }
    w += face->glyph->bitmap.width;
    h = std::max(face->glyph->bitmap.rows,h); // maybe this is the issue???
}

gluint texture;
glGenTextures(1,&texture);
glBindTexture(GL_TEXTURE_2D,texture);
glTexImage2D(GL_TEXTURE_2D,GL_RED,w,h,GL_UNSIGNED_BYTE,nullptr);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

unsigned int x = 0;
for (unsigned char c = 0; c < 128; c++) {
    if (FT_Load_Char(face,FT_LOAD_RENDER)) {
        throw std::runtime_error("Failed to load glyph");
    }
    glTexSubImage2D(GL_TEXTURE_2D,x,face->glyph->bitmap.width,face->glyph->bitmap.rows,face->glyph->bitmap.buffer);

    FontChar character = {
        (float)x / (float)w,(float)face->glyph->bitmap.width / (float)w,glm::ivec2(face->glyph->bitmap.width,face->glyph->bitmap.rows),glm::ivec2(face->glyph->bitmap_left,face->glyph->bitmap_top),glm::ivec2(face->glyph->advance.x,face->glyph->advance.y)
    };
    characters[c] = character;

    x += face->glyph->bitmap.width;
}

我做任何可能影响这种垂直拉伸行为的唯一其他地方是当我找到字符的最大高度时。我这样做是为了找到纹理图集的正确尺寸,它只有 1 个字符高 x n 个字符宽。我仍然不确定这会如何导致这种行为。

解决方法

我发现了这个问题。我的直觉是正确的;该问题与纹理图集的高度有关。我没有将字形位图的高度插入实际顶点,而是使用纹理的整个高度。我所要做的就是在填充 fontChars 数组时将字符的高度传递到 FontChar 结构中,然后我让我的顶点从 0.0f 到高度,而不是从 0.0f 到 1.0f。除了现在我所有的文字都太高之外,这很有效。然后我意识到我正在使用一个正交矩阵,它将 x 坐标从 [-1,1] 扩展到 [-width/height,width/height],并且由于我使用了单独的比例因子(sx 和 sy),我的缩放比例不正确。为了解决这个问题,我刚刚摆脱了 sy 并用 sx 替换了每个 sy。我还在图集中的每个纹理之间添加了 2 个像素,因此我不会在纹理之间产生任何拖尾。 Here is the final result.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?