【发布时间】:2018-06-06 08:53:22
【问题描述】:
我正在尝试将 freetype 与 OpenGL 一起使用。我第一次尝试写我的东西,因为它没有用,我只是从 this tutorial 下载了一个很好用的 code 。正如我所说,它运作良好,但投影与我想要的不同。在教程中我们有这个:
glm::mat4 projection = glm::ortho(0.0f, static_cast<GLfloat>(WIDTH), 0.0f, static_cast<GLfloat>(HEIGHT));
我改变了这个
glm::mat4 projection = glm::ortho(0.0f, static_cast<GLfloat>(WIDTH), 0.0f, static_cast<GLfloat>(HEIGHT), -1.0f, 1.0f);
它仍然很好用。但我想要的是起源于左上角而不是左下角所以我使用这个:
glm::mat4 projection = glm::ortho(0.0f, static_cast<GLfloat>(WIDTH), static_cast<GLfloat>(HEIGHT),0.0f, -1.0f, 1.0f);
现在它不再起作用了,我的屏幕上没有文字。除文本外,其他对象正确呈现。请注意,在之前的 2 个投影中,我得到了文本,但存在其他对象但倒置(正常)。
我的问题是为什么如果我在 (0,0) 处绘制文本并期望在左上角看到它,我什么都没有。但是对于其他投影,我按预期将它放在左下角?
我可以保留投影并移动一个,但我真的很想了解它为什么不起作用。
注意:我试图查看投影 * 位置的结果。 假设 P1 是 0 的正交,而 P2 是 0 底部的正交。
对于 x,y = (92.8000, 99.7000) 我得到:
P1 * (x, y, 0.0, 1.0) = [ 0.2320 0.3323 0.0000 -192.5000]
P2 * (x, y, 0.0, 1.0) = [ 0.2320 -0.3323 0.0000 6.9000]
正如预期的那样 y1 = - y2。
根据 @vu1p3n0x 代码的要求,用于 textrender。但它在the first link
void RenderText(Shader &shader, std::string text, GLfloat x, GLfloat y, GLfloat scale, glm::vec3 color)
{
shader.Use();
glUniform3f(glGetUniformLocation(shader.Program, "textColor"), color.x, color.y, color.z);
glActiveTexture(GL_TEXTURE0);
glBindVertexArray(VAO);
std::string::const_iterator c;
for (c = text.begin(); c != text.end(); c++)
{
Character ch = Characters[*c];
GLfloat xpos = x + ch.Bearing.x * scale;
GLfloat ypos = y - (ch.Size.y - ch.Bearing.y) * scale;
GLfloat w = ch.Size.x * scale;
GLfloat h = ch.Size.y * scale;
GLfloat vertices[6][4] = {
{ xpos, ypos + h, 0.0, 0.0 },
{ xpos, ypos, 0.0, 1.0 },
{ xpos + w, ypos, 1.0, 1.0 },
{ xpos, ypos + h, 0.0, 0.0 },
{ xpos + w, ypos, 1.0, 1.0 },
{ xpos + w, ypos + h, 1.0, 0.0 }
};
// Render glyph texture over quad
glBindTexture(GL_TEXTURE_2D, ch.TextureID);
// Update content of VBO memory
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
glBindBuffer(GL_ARRAY_BUFFER, 0);
// Render quad
glDrawArrays(GL_TRIANGLES, 0, 6);
// Now advance cursors for next glyph (note that advance is number of 1/64 pixels)
x += (ch.Advance >> 6) * scale; // Bitshift by 6 to get value in pixels (2^6 = 64 (divide amount of 1/64th pixels by 64 to get amount of pixels))
}
glBindVertexArray(0);
glBindTexture(GL_TEXTURE_2D, 0);
}
【问题讨论】:
-
这个文本是如何呈现的?您的链接中不清楚,所有相关代码都应包含在您的问题中。
-
我正在添加渲染部分,它可能很有用。代码在第一个链接中,但无论如何我都会发布它。
标签: c++ opengl glm-math coordinate-transformation freetype