我试图用ImGUI、OpenGL和GLFW构建一个C++应用程序。
然而,当我试图运行我的程序时,我得到了一个白色,然后崩溃,并给我一个错误代码-1073741819。
这是我创建窗口的类,很多东西都是从ImGUI opengl 3示例中复制的:
#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>
#include <imgui_internal.h>
#include "imgui.h"
#include "AppWindow.h"
static void glfw_error_callback(int error, const char* description)
{
fprintf(stderr, "GLFW Error %d: %s\n", error, description);
}
AppWindow::AppWindow(const char* title, int width, int height)
{
glfwSetErrorCallback(glfw_error_callback);
if (!glfwInit())
return ;
// Decide GL+GLSL versions
#if defined(IMGUI_IMPL_OPENGL_ES2)
// GL ES 2.0 + GLSL 100
const char* glsl_version = "#version 100";
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
#elif defined(__APPLE__)
// GL 3.2 + GLSL 150
const char* glsl_version = "#version 150";
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ only
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // Required on Mac
#else
// GL 3.0 + GLSL 130
const char* glsl_version = "#version 130";
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
//glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ only
//glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // 3.0+ only
#endif
// Create window with graphics context
window = glfwCreateWindow(width, height, title, nullptr, nullptr);
if (window == nullptr)
return ;
glfwMakeContextCurrent(window);
glfwSwapInterval(1); // Enable vsync
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
io = ImGui::GetIO(); (void)io;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows
//io.ConfigViewportsNoAutoMerge = true;
//io.ConfigViewportsNoTaskBarIcon = true;
// Setup Dear ImGui style
ImGui::StyleColorsDark();
//ImGui::StyleColorsLight();
// When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
ImGuiStyle& style = ImGui::GetStyle();
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
style.WindowRounding = 0.0f;
style.Colors[ImGuiCol_WindowBg].w = 1.0f;
}
// Setup Platform/Renderer backends
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init(glsl_version);
// Our state
bool show_demo_window = true;
bool show_another_window = false;
}
AppWindow::~AppWindow()
{
#ifdef __EMSCRIPTEN__
EMSCRIPTEN_MAINLOOP_END;
#endif
// Cleanup
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
glfwDestroyWindow(window);
glfwTerminate();
return;
}
void AppWindow::setUpdateFunction(void (*function)())
{
updateFunction = function;
}
void AppWindow::update()
{
if (updateFunction != nullptr)
updateFunction();
}
void AppWindow::run()
{
#ifdef __EMSCRIPTEN__
// For an Emscripten build we are disabling file-system access, so let's not attempt to do a fopen() of the imgui.ini file.
// You may manually call LoadIniSettingsFromMemory() to load settings from your own storage.
io.IniFilename = nullptr;
EMSCRIPTEN_MAINLOOP_BEGIN
#else
while (!glfwWindowShouldClose(window))
#endif
{
// Poll and handle events (inputs, window resize, etc.)
// You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
// - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
// - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
// Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
glfwPollEvents();
// Start the Dear ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
update();
// Rendering
ImGui::Render();
int display_w, display_h;
glfwGetFramebufferSize(window, &display_w, &display_h);
glViewport(0, 0, 1280, 720);
glClearColor(0.5f,0.5f, 0.5f, 0.5f);
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
// Update and Render additional Platform Windows
// (Platform functions may change the current OpenGL context, so we save/restore it to make it easier to paste this code elsewhere.
// For this specific demo app we could also call glfwMakeContextCurrent(window) directly)
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
GLFWwindow* backup_current_context = glfwGetCurrentContext();
ImGui::UpdatePlatformWindows();
ImGui::RenderPlatformWindowsDefault();
glfwMakeContextCurrent(backup_current_context);
}
glfwSwapBuffers(window);
}
}
void AppWindow::addMouseCallBack(GLFWmousebuttonfun action)
{
glfwSetMouseButtonCallback(window, action);
}
void AppWindow::addKeyCallBack(GLFWkeyfun action)
{
glfwSetKeyCallback(window, action);
}
我的主要方法:
int main(int , char**) {
AppWindow window("My app", 1280, 720);
window.run();
return 0;
}
AppWindow.h:
#pragma once
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include "imgui.h"
class AppWindow
{
public:
ImVec4 BgColour = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
ImGuiIO io;
GLFWwindow* window;
void (*updateFunction)() = nullptr;
AppWindow(const char* title, int width, int height);
~AppWindow();
void setUpdateFunction(void (*updateFunction)());
void update();
void run();
void addMouseCallBack(GLFWmousebuttonfun action);
void addKeyCallBack(GLFWkeyfun action);
下面是我文件夹结构的图像:my folder structure
下面是我复制的imgui文件:imgui files
我的包含目录包含:Desktop\ImgGUIApps\LearningImGUI\ImGUIwithOpenGL\imguiDependencies Desktop\ImgGUIApps\LearningImGUI\ImGUIwithOpenGL\Libraries\include
我的图书馆目录:桌面\ImgGUIApps\LearningImGUI\ImGUIwithOpenGL\Libraries\lib
附加依赖项:glfw3.lib
我发现这是由于代码运行时这部分中的内存访问冲突导致的:
glViewport(0, 0, 1280, 720);
glClearColor(0.5f,0.5f, 0.5f, 0.5f);
glClear(GL_COLOR_BUFFER_BIT);
评论出来只会给我一个不崩溃的黑屏。
我怀疑这可能是由于我错误地安装了opengl和glfw和imgui。
我也不认为这是因为我的窗口变量被释放了,就像我在类声明中声明的那样。
1条答案
按热度按时间shstlldc1#
好吧,所以我只能简单地写:
后
我不记得在教程中看到过,或者我没有注意。感谢@G.M.让我明白了答案