【发布时间】:2021-01-19 04:00:28
【问题描述】:
我已经设法在 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, vy, tx + tw, 0.0f,
vx + w, vy - h, tx + tw, 1.0f,
vx + w, vy - h, tx + tw, 1.0f,
vx, vy - h, tx, 1.0f,
vx, vy, tx, 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, 0, 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, 0, GL_RED, w, h, 0, GL_RED, 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_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, 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, c, FT_LOAD_RENDER)) {
throw std::runtime_error("Failed to load glyph");
}
glTexSubImage2D(GL_TEXTURE_2D, 0, x, 0, face->glyph->bitmap.width, face->glyph->bitmap.rows, GL_RED, GL_UNSIGNED_BYTE, 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 个字符宽。不过,我仍然不确定这会如何导致这种行为。
【问题讨论】:
标签: c++ glm-math freetype opengl-4