ImGui error: Assertion failed: (bd != __null && "Did you call ImGui_ImplOpenGL3_Init()?")

Viewed 34

I've been working with SDL2 for a long time and now I wanted to switch to GLFW with Dear ImGui. So I downloaded GLFW and integrated it into the project. The simple GLFW window works without problems. Then I integrated ImGui and I get these errors all the time:

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.

although I'm on line 33:

ImGui_ImplOpenGL3_Init();

I have never worked with ImGui before and would be really happy if you could help me

My code:

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

And my includes:

#include "GL/glew.h"
#include "GLFW/glfw3.h"
#include "imgui/imgui.h"
#include "imgui/imgui_impl_glfw.h"
#include "imgui/imgui_impl_opengl3.h"

Thank you for your answer :)

1 Answers

Your code runs fine on my machine, it didn't crash at the ImGui_ImplOpenGL3_NewFrame function, but at ImGui::Render(); which might be the problem. You need to render in the order below:

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();
}
Related