c++ - 带有高兴的 openGL 加载器的 ImGui 抛出段错误(核心已转储)

标签 c++ user-interface opengl glfw

我是 ImGui 库的新手,最近我一直在试用其中包含的示例。在我将 gl3w 的 include(和函数)更改为 glad(我想使用的加载器)之前,一切都很顺利。当我在两个加载器之间交换时,我在 imgui_impl_glfw_gl3.cpp 文件中遇到了段错误异常。我发现一篇文章暗示这可能是由于某些函数未能“绑定(bind)”并产生空指针而发生的。

我已在 imgui_impl_glfw_gl3.cpp216 行 中找到错误 这是第 216 行的代码:

glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);

我还将 imgui_impl_glfw_gl3.cpp 中的包含文件从 gl3w 更改为 glad 但没有结果。

这是我正在执行的主要功能(这是使用 glad 的 imgui 的基本 opengl3 示例):

#include "gui/imgui.h"
#include "gui/imgui_impl_glfw_gl3.h"
#include <stdio.h>
#include <glad/glad.h>    // This example is using gl3w to access OpenGL functions (because it is small). You may use glew/glad/glLoadGen/etc. whatever already works for you.
#include <GLFW/glfw3.h>

static void error_callback(int error, const char* description)
{
    fprintf(stderr, "Error %d: %s\n", error, description);
}

int main(int, char**)
{
    // Setup window
    glfwSetErrorCallback(error_callback);
    if (!glfwInit())
        return 1;
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    GLFWwindow* window = glfwCreateWindow(1280, 720, "ImGui OpenGL3 example", NULL, NULL);
    glfwMakeContextCurrent(window);
    glfwSwapInterval(1); // Enable vsync
    glfwInit();

    // Setup ImGui binding
    ImGui_ImplGlfwGL3_Init(window, true);

    // Setup style
    //ImGui::StyleColorsDark();
    ImGui::StyleColorsClassic();

    bool show_demo_window = true;
    bool show_another_window = false;
    bool algo = true;
    ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);

    // Main loop
    while (!glfwWindowShouldClose(window))
    {

        glfwPollEvents();
        ImGui_ImplGlfwGL3_NewFrame();

        // 1. Show a simple window.
        // Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets automatically appears in a window called "Debug".
        {
            static float f = 0.0f;
            static int counter = 0;
            ImGui::Text("Hello, world!");                           // Display some text (you can use a format string too)
            ImGui::SliderFloat("float", &f, 0.0f, 1.0f);            // Edit 1 float using a slider from 0.0f to 1.0f    
            ImGui::ColorEdit3("COLORINES", (float*)&clear_color); // Edit 3 floats representing a color

            ImGui::Checkbox("Demo Window", &show_demo_window);      // Edit bools storing our windows open/close state
            ImGui::Checkbox("Booleanooooo", &algo);
            ImGui::Checkbox("Another Window", &show_another_window);

            if (ImGui::Button("Button"))                            // Buttons return true when clicked (NB: most widgets return true when edited/activated)
                counter++;
            ImGui::SameLine();
            ImGui::Text("counter = %d", counter);
            ImGui::Text("pues se ve que hay texto: %d", algo);
            ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
        }

        {
            ImGui::Begin("VENTANA WAPA");
            ImGui::Text("POS SA QUEDAO BUENA VENTANA");

            static float yee = 0.0f;
            ImGui::SliderFloat("lel", &yee,1.0f,0.5f);

            ImGui::End();
        }

        // 2. Show another simple window. In most cases you will use an explicit Begin/End pair to name your windows.
        if (show_another_window)
        {
            ImGui::Begin("Another Window", &show_another_window);
            ImGui::Text("Hello from another window!");
            if (ImGui::Button("Close Me"))
                show_another_window = false;
            ImGui::End();
        }

        // 3. Show the ImGui demo window. Most of the sample code is in ImGui::ShowDemoWindow(). Read its code to learn more about Dear ImGui!
        if (show_demo_window)
        {
            ImGui::SetNextWindowPos(ImVec2(650, 20), ImGuiCond_FirstUseEver); // Normally user code doesn't need/want to call this because positions are saved in .ini file anyway. Here we just want to make the demo initial state a bit more friendly!
            ImGui::ShowDemoWindow(&show_demo_window);
        }

        // Rendering
        int display_w, display_h;
        glfwGetFramebufferSize(window, &display_w, &display_h);
        glViewport(0, 0, display_w, display_h);
        glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
        glClear(GL_COLOR_BUFFER_BIT);
        ImGui::Render();
        glfwSwapBuffers(window);
    }

    // Cleanup
    //ImGui_ImplGlfwGL3_Shutdown();
    glfwTerminate();

    return 0;
}

我不知道为什么会这样,而且我对 openGL 和 ImGui 还很陌生,有什么想法吗? :(

最佳答案

Glad 和 gl3w 都是 extension loader libraries .它们通常需要在使用前在当前 GL 上下文中初始化。

original code称为 gl3wInit()。你的缺少任何形式的 glad init。

确保在 glfwMakeContextCurrent() 之后和调用任何 OpenGL 函数之前初始化 glad (gladLoadGLLoader((GLADloadproc) glfwGetProcAddress))。

否则,glad 声明的所有 OpenGL 函数指针都将保持为 NULL。一般尝试调用 NULL 函数指针 doesn't go well for a process .

关于c++ - 带有高兴的 openGL 加载器的 ImGui 抛出段错误(核心已转储),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48582444/

相关文章:

c++ - 如何从 C++ 中的函数正确返回不可修改的对象引用?

c++ - 为什么 "error: invalid application of ' sizeof' 为使用 unique_ptr 的不完整类型”通过添加空析构函数来修复?

c++ - LibCurl HTTPS 凭据,内部处理的序列?

html - 关于流体网页设计的问题

c++ - 使用 true type 和 opengl core 的轮廓字体 (3.3)

java - 尽管链接和验证成功,为什么着色器程序无法编译?

c++ - QGraphicsView 问题

ios - trigger.io 自定义原生按钮工具栏和标签栏

java - 解析从 Arduino 上的多个传感器读取的数据

c++ - 为什么建议使用多个像素缓冲区对象。肯定是多余的?