【问题标题】:Can't get FreeType to render无法让 FreeType 呈现
【发布时间】:2017-11-13 12:23:20
【问题描述】:

使用 OpenGL、GLM、GLAD、GLFW 和 FreeType 尝试获取要渲染的字体。我已经逐步确保在渲染时没有发生 OpenGL 错误,没有任何内容发布错误代码,但没有任何内容呈现到屏幕上。我也不确定我是否正确使用了 glm 的正交函数,因为我不确定在哪里实际使用该变量:

glm::mat4 projection = glm::ortho(0.0f, 800.0f, 0.0f, 600.0f);

这是我的 Draw Loop,它在场景初始化后被调用:

void GameLoop::Run()
{
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glClearColor(0.2f, 0.3f, 0.3f, 1.0f);

    while (!glfwWindowShouldClose(m_game_window))
    {
        // Do That thing where you draw everything
    }
}

将字体加载到纹理中的 Font 构造函数:

Salem::Graphics::Font::Font(std::string p_font_path)
{
    FT_Library ft;
    if (FT_Init_FreeType(&ft))
    {
        std::cout << "Error loading FreeType library" << std::endl;
    }

    FT_Face face;
    if (FT_New_Face(ft, p_font_path.c_str(), 0, &face))
    {
        std::cout << "Error loading font;\n" << p_font_path << std::endl;
    }

    FT_Set_Pixel_Sizes(face, 0, 28);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // Disable byte-alignment restriction

    for (GLubyte c = 0; c < 128; c++)
    {
        if (FT_Load_Char(face, c, FT_LOAD_RENDER))
        {
            std::cout << "Failed to load a character glyph." << std::endl;
            continue;
        }

        GLuint texture;
        glGenTextures(1, &texture);
        glBindTexture(GL_TEXTURE_2D, texture);
        glTexImage2D(
                GL_TEXTURE_2D,
                0,
                GL_RED,
                face->glyph->bitmap.width,
                face->glyph->bitmap.rows,
                0,
                GL_RED,
                GL_UNSIGNED_BYTE,
                face->glyph->bitmap.buffer);

        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);

        Character character = {
                texture,
                glm::ivec2(face->glyph->bitmap.width, face->glyph->bitmap.rows),
                glm::ivec2(face->glyph->bitmap_left, face->glyph->bitmap_top),
                face->glyph->advance.x
        };

        m_characters.insert(std::make_pair(c, character));
    }

    FT_Done_Face(face);
    FT_Done_FreeType(ft);
    std::cout << "Font loaded. " << std::endl;
}

我的文本的初始化函数:

void Text::Initialize()
{
    glm::mat4 projection = glm::ortho(0.0f, 800.0f, 0.0f, 600.0f);
    glGenVertexArrays(1, &m_vao);
    glGenBuffers(1, &m_vbo);

    glBindVertexArray(m_vao);
    glBindBuffer(GL_ARRAY_BUFFER, m_vbo);

    glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 6 * 4, nullptr, m_draw_method);

    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), nullptr);

    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);
}

还有我的文字的绘制功能:

void Text::Draw()
{
    glm::mat4 projection = glm::ortho(0.0f, 800.0f, 0.0f, 600.0f);

    shader()->Use();
    glUniform3f(glGetUniformLocation(shader()->id(), "textColor"), m_color.x, m_color.y, m_color.z);
    glActiveTexture(GL_TEXTURE0);
    glBindVertexArray(m_vao);

    for (std::string::const_iterator c = m_text.begin(); c != m_text.end(); ++c)
    {
        Character ch = m_font.characters()[*c];

        GLfloat xPosition = m_position.x + ch.Bearing.x * m_scale;
        GLfloat yPosition = m_position.y - (ch.Size.y - ch.Bearing.y) * m_scale;

        GLfloat width = ch.Size.x * m_scale;
        GLfloat height = ch.Size.y * m_scale;

        GLfloat vertices[6][4] = {
                {xPosition,         yPosition + height, 0.0, 0.0},
                {xPosition,         yPosition,          0.0, 1.0},
                {xPosition + width, yPosition,          1.0, 1.0},

                {xPosition,         yPosition + height, 0.0, 0.0},
                {xPosition + width, yPosition,          1.0, 1.0},
                {xPosition + width, yPosition + height, 1.0, 0.0}
        };

        glBindTexture(GL_TEXTURE_2D, ch.TextureID);
        glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
        glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
        //glBindBuffer(GL_ARRAY_BUFFER, 0);

        glDrawArrays(GL_TRIANGLES, 0, 6);

        m_position.x += (ch.Advance >> 6) * m_scale;
    }

    glBindVertexArray(0);
    glBindTexture(GL_TEXTURE_2D, 0);
}

不想让帖子太长,如果需要我的着色器,请告诉我。

【问题讨论】:

  • 方式太长了。请提供minimal reproducible example
  • 怎么样?抱歉,似乎有几个地方可能会失败,我不确定我应该包括什么。调试 openGL 可能会很痛苦。
  • 阅读链接的文章。然后,如果您仍然迷路,请尝试以下操作:编写一次在最方便的位置渲染一个没有着色器和纹理的对象,如果失败则发布它,如果没有则扩展它。
  • @PasserBy:在核心 OpenGL 中无法进行无着色器渲染。
  • @NicolBolas 告诉你我对 OpenGL 的了解有多少;)尽管公平地说,基于 OpenGL 的库在使这一事实不明显方面做得很好。

标签: c++ opengl freetype


【解决方案1】:

分解代码后,我发现了一个有用的资源,它引导我解决了我的问题。从https://learnopengl.com/#!In-Practice/Text-Rendering项目源,我发现在构造字体时,我的正交投影变量必须传递给我的着色器程序。我通过将以下代码添加到我的字体构造函数来做到这一点:

glm::mat4 projection = glm::ortho(0.0f, 800.0f, 0.0f, 600.0f);
p_shader->Use();
glUniformMatrix4fv(glGetUniformLocation(p_shader->id(), "projection"), 1, GL_FALSE, glm::value_ptr(projection));

【讨论】:

    猜你喜欢
    • 2011-08-07
    • 2018-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多