【问题标题】:ImGui+OpenGL, render function can't render fonts [closed]ImGui + OpenGL,渲染功能无法渲染字体[关闭]
【发布时间】:2018-01-18 02:03:41
【问题描述】:

正如标题所说,我正在使用 ImGui,但我无法使用我的渲染函数来渲染字体。

我做过的事情:

  1. 使用 RenderDoc 验证我的纹理已正确加载
  2. 已验证我的顶点属性指针是否符合 ImGui 的约定(结构数组)。

下面是我的渲染代码。您还可以在此处查看开发人员的 OpenGL 示例代码:https://github.com/ocornut/imgui/blob/master/examples/opengl3_example/imgui_impl_glfw_gl3.cpp

// Setup some GL state
glEnable(GL_BLEND);
glBlendEquation(GL_FUNC_ADD);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
glEnable(GL_SCISSOR_TEST);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

// Setup orthographic projection
glViewport(0, 0, (GLsizei)fb_width, (GLsizei)fb_height);
const float ortho_projection[4][4] =
{
    { 2.0f/io.DisplaySize.x, 0.0f,                   0.0f, 0.0f },
    { 0.0f,                  2.0f/-io.DisplaySize.y, 0.0f, 0.0f },
    { 0.0f,                  0.0f,                  -1.0f, 0.0f },
    {-1.0f,                  1.0f,                   0.0f, 1.0f },
};

// Setup the shader. bind() calls glUseProgram and enables/disables the proper vertex attributes
shadeTextured->bind(); 

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, g_FontTexture);

shadeTextured->setUniformM4(Shader::uhi_transform, *(glm::mat4*)&ortho_projection[0][0]);
shadeTextured->setUniformSampler(1, 0);

// Set my vertex attribute pointers for position and tex coords
glVertexAttribPointer(0,
                      2,
                      GL_FLOAT,
                      GL_FALSE,
                      sizeof(ImDrawVert),
                      (GLvoid*)IM_OFFSETOF(ImDrawVert, pos));

glVertexAttribPointer(1,
                      2,
                      GL_FLOAT,
                      GL_FALSE,
                      sizeof(ImDrawVert),
                      (GLvoid*)IM_OFFSETOF(ImDrawVert, uv));

// Loop through all commands ImGui has 
for (int n = 0; n < draw_data->CmdListsCount; n++) {
    const ImDrawList* cmd_list = draw_data->CmdLists[n];
    const ImDrawIdx* idx_buffer_offset = 0;

    glBindBuffer(GL_ARRAY_BUFFER, g_VboHandle);
    glBufferData(GL_ARRAY_BUFFER,
                 (GLsizeiptr)cmd_list->VtxBuffer.Size * sizeof(ImDrawVert),
                 (const GLvoid*)cmd_list->VtxBuffer.Data,
                 GL_STREAM_DRAW);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_ElementsHandle);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER,
                 (GLsizeiptr)cmd_list->IdxBuffer.Size * sizeof(ImDrawIdx),
                 (const GLvoid*)cmd_list->IdxBuffer.Data,
                 GL_STREAM_DRAW);

    for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++) {
        const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
        glScissor((int)pcmd->ClipRect.x,
                  (int)(fb_height - pcmd->ClipRect.w),
                  (int)(pcmd->ClipRect.z - pcmd->ClipRect.x),
                  (int)(pcmd->ClipRect.w - pcmd->ClipRect.y));
        glDrawElements(GL_TRIANGLES,
                       (GLsizei)pcmd->ElemCount,
                       sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
                       idx_buffer_offset);

        idx_buffer_offset += pcmd->ElemCount;
    }
}

这是我编写的(非常非常简单的)着色器。着色器之前已经对按钮进行了纹理处理,所以我假设它们在功能上是正确的。

顶点着色器: #版本 330 核心

layout (location = 0) in vec2 pos;
layout (location = 1) in vec2 texCoord;

out vec2 fragTexCoord;

uniform mat4 transform;

void main() {   
    gl_Position = transform * vec4(pos, 0.0, 1.0);
    fragTexCoord = texCoord;
}

片段着色器:

#version 330 core

out vec4 fragColor;

in vec2 fragTexCoord;
uniform sampler2D sampler;

void main() {
    fragColor = texture(sampler, fragTexCoord);
}

我完全失去了!任何帮助将不胜感激

【问题讨论】:

  • @Rabbid76 下面的评论解决了这个问题,但感谢您的回复!

标签: c++ opengl graphics textures


【解决方案1】:

调试不正确的 OpenGL 设置/状态可能非常困难。目前尚不清楚您为什么不完全使用 imgui_impl_glfw_gl3.cpp 中提供的代码并重写自己的代码,但您可以做的是:

  • 从应该正常工作的 imgui_impl_glfw_gl3.cpp 重新开始,然后逐步将其转变为您自己的,看看是什么导致它崩溃?
  • 暂时禁用剪刀。
  • 既然您已经在使用 RenderDoc:它会显示正确的网格吗?它显示的顶点是否正常?

【讨论】:

  • 谢谢!我只是复制/粘贴了示例代码,删除了对 GLFW 的引用,它就起作用了。我之前尝试过,但遇到了字体渲染问题,但肯定是我第一次犯了错误。 (我已经尝试过(1)和(2))。感谢优秀的图书馆和对问题的快速回复,我非常感谢。
猜你喜欢
  • 1970-01-01
  • 2021-05-14
  • 2021-02-19
  • 1970-01-01
  • 2020-09-17
  • 2020-05-12
  • 2017-06-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多