与ImGui_ImplOpenGL3_CreateDeviceObjects相关的ImGui错误 &



当我运行我的代码,我得到这个错误。考虑我得到这个错误(运行时错误)从ImGui仅当线程被启用.

ERROR: ImGui_ImplOpenGL3_CreateDeviceObjects: failed to compile vertex shader! With GLSL: #version 130
ERROR: ImGui_ImplOpenGL3_CreateDeviceObjects: failed to compile fragment shader! With GLSL: #version 130
ERROR: ImGui_ImplOpenGL3_CreateDeviceObjects: failed to link shader program! With GLSL #version 130

这是我的代码。

gui.h

#include "imgui.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
#define GLEW_STATIC
#include <GL/glew.h>
#define GLFW_STATIC
#include <GLFW/glfw3.h>
#include "nfd.h"
#include <string>
#include <vector>
#include <thread>
#include "fileHandler.h"
#include "errors.h"
class MyGUI{
private:
GLFWwindow *window;
ImFont *tabFont, *ctntFonr, *logFont;
ImGuiIO* io;
bool isOpen = true;
bool shouldRunFileOp = false;
std::string m_OriginalFilePath, m_PartSizeMB, m_OutputFolderPath;
void writeToFile(std::string _inputFile, unsigned long long _partByteSize, std::string _outputFolder){
while(isOpen){
if(shouldRunFileOp){
// Do Some Large Files Related Stuff
shouldRunFileOp = false;
}
}
}
void setCustomTheme(ImGuiStyle& _style){
ImVec4 *colors = _style.Colors;
colors[ImGuiCol_Tab] = ImVec4(0.4, 0.4, 0.4, 1.0);
colors[ImGuiCol_TabHovered] = ImVec4(0.7, 0.3, 0.2, 1.0);
colors[ImGuiCol_TabActive] = ImVec4(0.8, 0.3, 0.2, 1.0);
colors[ImGuiCol_WindowBg] = ImVec4(0.1, 0.1, 0.1, 0.85);
colors[ImGuiCol_TitleBgActive] = ImVec4(0.3, 0.3, 0.3, 1.0);
colors[ImGuiCol_Text] = ImVec4(1.0, 1.0, 1.0, 1.0);
_style.FrameRounding = 6;
_style.WindowPadding = ImVec2(10, 10);
_style.FramePadding = ImVec2(7.0, 4.0);
_style.ItemSpacing = ImVec2(10.0, 20.0);
_style.ItemInnerSpacing = ImVec2(2.0, 0.0);
}
public:
int init(){
if (!glfwInit())
return 1;
// GL 3.0 + GLSL 130
const char* glsl_version = "#version 130";
glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, GL_TRUE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
this->window = glfwCreateWindow(winSize.x, winSize.y, windowTitle.c_str(), NULL, NULL);
if (window == NULL)
return 1;
glfwMakeContextCurrent(window);
glfwSwapInterval(1); // Enable vsync
IMGUI_CHECKVERSION();
ImGui::CreateContext();
this->io = &ImGui::GetIO(); (void)*(this->io);
ImGui::StyleColorsDark();
ImGuiStyle& style = ImGui::GetStyle();
style.TabRounding = 4.0;
style.WindowRounding = 5.0;
style.WindowTitleAlign = ImVec2(0.5, 0.5);
this->setCustomTheme(style);
if(!ImGui_ImplGlfw_InitForOpenGL(window, true)) return 1;
if(!ImGui_ImplOpenGL3_Init(glsl_version)) return 1;
tabFont = io->Fonts->AddFontFromFileTTF("./assets/fonts/comic.ttf", 22.0f);
ctntFont = io->Fonts->AddFontFromFileTTF("./assets/fonts/comic.ttf", 20.0f);
logFont = io->Fonts->AddFontFromFileTTF("./assets/fonts/comic.ttf", 18.0f);
}
int run(){
std::thread t1(&MyGUI::render, this);
std::thread t2(&MyGUI::writeToFile, this, m_OriginalFilePath, m_PartSizeMB, m_OutputFolderPath);
t1.join();
t2.join();
}
int render(){
while(!glfwWindowShouldClose(window)){
glfwPollEvents();
ImGui_ImplOpenGL3_NewFrame(); // Error is Getting from this Line.
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
{
ImGui::Begin("File Window");
// Another GUI Elements
if(ImGui::Button("Proceed")){
shouldRunFileOp = true;
}
ImGui::End();
}
ImGui::Render();
int display_w, display_h;
glfwGetFramebufferSize(this->window, &display_w, &display_h);
glViewport(0, 0, display_w, display_h);
glClearColor(0.2, 0.2, 0.2, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());

glfwSwapBuffers(this->window);
}
isOpen = false;
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
glfwDestroyWindow(this->window);
glfwTerminate();
return 0;
}
};

main.cpp

#include "gui.h"
int main(){
MyGUI gui;
gui.init();
gui.run();
return 0;
}

我检查过了,错误来自gui.h

这一行
ImGui_ImplOpenGL3_NewFrame();

我的代码有什么问题?BTW这段代码在没有定义线程的情况下工作得很好

我必须使用线程,因为我的程序正在处理大文件。

如果任何人也知道如何处理大尺寸文件而不中断渲染循环,请也放弃你的解决方案!

谢谢!

OpenGL Context对于线程来说是局部的,需要设置为" current ";在线程(glfwMakeContextCurrent(window)中。上下文不能为"当前";同时在多个线程中。你必须使OpenGL Context当前在render线程中:

int render(){
glfwMakeContextCurrent(window);   // <---
while(!glfwWindowShouldClose(window)){
// [...]  
}
// [...]
}

最新更新