首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ImGui错误:断言失败:(bd != __null &“您调用ImGui_ImplOpenGL3_Init()了吗?”)

ImGui错误:断言失败:(bd != __null &“您调用ImGui_ImplOpenGL3_Init()了吗?”)
EN

Stack Overflow用户
提问于 2022-09-18 08:42:37
回答 1查看 229关注 0票数 0

我已经和SDL2一起工作很长时间了,现在我想和亲爱的ImGui切换到GLFW。所以我下载了GLFW并将它集成到项目中。简单的GLFW窗口工作没有问题。然后,我集成了ImGui,并一直得到这些错误:

代码语言:javascript
复制
Failed to initialize OpenGL loader!
Assertion failed: (bd != __null && "Did you call ImGui_ImplOpenGL3_Init()?"), function ImGui_ImplOpenGL3_NewFrame, file imgui_impl_opengl3.cpp, line 337.

虽然我在33线上:

代码语言:javascript
复制
ImGui_ImplOpenGL3_Init();

我以前从未和ImGui合作过,如果你能帮我的话,我会很高兴的。

我的代码:

代码语言:javascript
复制
int main() {
    //init glfw
    if (!glfwInit()) {
        std::cout << "Failed to initialize GLFW" << std::endl;
        return -1;
    }
    //create window
    GLFWwindow* window = glfwCreateWindow(640, 480, "Text Editor", NULL, NULL);
    if (!window) {
        std::cout << "Failed to create window" << std::endl;
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);
    //init glew
    if (glewInit() != GLEW_OK) {
        std::cout << "Failed to initialize GLEW" << std::endl;
        return -1;
    }
    ImGui::CreateContext();
    ImGuiIO& io = ImGui::GetIO(); (void)io;
    ImGui_ImplGlfw_InitForOpenGL(window, true);
    ImGui::StyleColorsDark();
    ImGui_ImplOpenGL3_Init();

    //main loop
    while (!glfwWindowShouldClose(window)) {
        glfwPollEvents();
        ImGui_ImplOpenGL3_NewFrame();
        ImGui_ImplGlfw_NewFrame();


        ImGui::Render();
        ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    return 0;
}

我的内容包括:

代码语言:javascript
复制
#include "GL/glew.h"
#include "GLFW/glfw3.h"
#include "imgui/imgui.h"
#include "imgui/imgui_impl_glfw.h"
#include "imgui/imgui_impl_opengl3.h"

谢谢你的回答:)

EN

回答 1

Stack Overflow用户

发布于 2022-09-18 09:32:52

您的代码在我的机器上运行良好,它不是在ImGui_ImplOpenGL3_NewFrame函数上崩溃,而是在ImGui::Render();上崩溃,这可能是问题所在。您需要按以下顺序呈现:

代码语言:javascript
复制
while (!glfwWindowShouldClose(window))
{
    glfwPollEvents();
    ImGui_ImplOpenGL3_NewFrame();
    ImGui_ImplGlfw_NewFrame();

    ImGui::NewFrame(); // <-- Added

    // do stuffs here

    ImGui::Render();
    ImGui::EndFrame(); // <-- Added

    ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
    glfwSwapBuffers(window);
    glfwPollEvents();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73761259

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档