我已经和SDL2一起工作很长时间了,现在我想和亲爱的ImGui切换到GLFW。所以我下载了GLFW并将它集成到项目中。简单的GLFW窗口工作没有问题。然后,我集成了ImGui,并一直得到这些错误:
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线上:
ImGui_ImplOpenGL3_Init();我以前从未和ImGui合作过,如果你能帮我的话,我会很高兴的。
我的代码:
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;
}我的内容包括:
#include "GL/glew.h"
#include "GLFW/glfw3.h"
#include "imgui/imgui.h"
#include "imgui/imgui_impl_glfw.h"
#include "imgui/imgui_impl_opengl3.h"谢谢你的回答:)
发布于 2022-09-18 09:32:52
您的代码在我的机器上运行良好,它不是在ImGui_ImplOpenGL3_NewFrame函数上崩溃,而是在ImGui::Render();上崩溃,这可能是问题所在。您需要按以下顺序呈现:
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();
}https://stackoverflow.com/questions/73761259
复制相似问题