首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >与ImGui相关的ImGui_ImplOpenGL3_CreateDeviceObjects错误

与ImGui相关的ImGui_ImplOpenGL3_CreateDeviceObjects错误
EN

Stack Overflow用户
提问于 2022-07-07 16:10:18
回答 1查看 365关注 0票数 1

当我运行我的代码时,我会得到这个错误。考虑一下,只有在启用线程时,我才从(运行时错误)从ImGui 获得这个错误。

代码语言:javascript
复制
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

代码语言:javascript
复制
#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

代码语言:javascript
复制
#include "gui.h"

int main(){

  MyGUI gui;
  gui.init();
  gui.run();

  return 0;
}

我已经查过了,这条线的错误来自贵h,

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

我的代码有什么问题?BTW这段代码在不定义线程时运行良好。

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

如果有谁知道如何在不中断呈现循环的情况下处理大型文件,也请删除您的解决方案!

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-07 17:14:44

OpenGL上下文是线程本地的,需要在线程(glfwMakeContextCurrent(window)中生成“当前”。上下文不能同时在多个线程中“当前”。必须使OpenGL上下文在render线程中保持当前状态:

代码语言:javascript
复制
int render(){

    glfwMakeContextCurrent(window);   // <---

    while(!glfwWindowShouldClose(window)){
        // [...]  
    }
    // [...]
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72901096

复制
相关文章

相似问题

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