• 【9】openGL调用imGUI&使用其自带例子测试


    参考视频
    github下载imgui源码,得到
    在这里插入图片描述
    将根目录下所有.h .cpp文件复制到你的项目文件夹imgui下面。

    进入exmaple文件夹,可以看到例子
    在这里插入图片描述
    这是它调用的头文件
    在这里插入图片描述
    在backends里找到你需要的 .h .cpp文件,总共四个文件,复制过来
    在这里插入图片描述
    你的项目文件结构:
    在这里插入图片描述
    记得将所有文件包含进项目除了main.cpp,因为我们实际使用在自己的主函数里而不是直接使用它的例子


    实际使用,主要参考main.cpp的代码:
    在绘图循环外面

       ImGui::CreateContext();
    
        ImGuiIO& io = ImGui::GetIO(); (void)io;
        io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;     // Enable Keyboard Controls
        io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;      // Enable Gamepad Controls
        const char* glsl_version = "#version 130";
    
        ImGui_ImplGlfw_InitForOpenGL(window, true); 
        ImGui_ImplOpenGL3_Init(glsl_version);
        ImGui::StyleColorsDark();
    
        
        bool show_demo_window = true;
        bool show_another_window = false;
        ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    在循环里面:

        /* Loop until the user closes the window */
        while (!glfwWindowShouldClose(window))
        {
            // glClearColor(1.0f, 1.0f, 1.0, 1.0f);底色设置为白色
            /* Render here */
            renderer.Clear();
             
            ImGui_ImplOpenGL3_NewFrame();
            ImGui_ImplGlfw_NewFrame();  
            ImGui::NewFrame();
     
     
            renderer.Draw(va,ib,shader); 
    
      
            // 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!).
             if (show_demo_window)
                ImGui::ShowDemoWindow(&show_demo_window); 
            // 2.
            {
                static float f = 0.0f;
                static int counter = 0; 
    
                ImGui::Begin("Hello, world!");                          // Create a window called "Hello, world!" and append into it.
    
                ImGui::Text("This is some useful text.");               // Display some text (you can use a format strings too)
                ImGui::Checkbox("Demo Window", &show_demo_window);      // Edit bools storing our window open/close state
                ImGui::Checkbox("Another Window", &show_another_window);
    
                ImGui::SliderFloat("float", &f, 0.0f, 1.0f);            // Edit 1 float using a slider from 0.0f to 1.0f
                ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
    
                if (ImGui::Button("Button"))                            // Buttons return true when clicked (most widgets return true when edited/activated)
                    counter++;
                ImGui::SameLine();
                ImGui::Text("counter = %d", counter);
    
                ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
                ImGui::End();
            }
            // 3. Show another simple window.
            if (show_another_window)
            {
                ImGui::Begin("Another Window", &show_another_window);   // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked)
                ImGui::Text("Hello from another window!");
                if (ImGui::Button("Close Me"))
                    show_another_window = false;
                ImGui::End();
            }
    
            ImGui::Render(); 
            ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
    
            /* Swap front and back buffers */
            glfwSwapBuffers(window);
    
            /* Poll for and process events */
            glfwPollEvents();
        } 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59

    结尾:

    ImGui_ImplGlfw_Shutdown();
        ImGui::DestroyContext();
        glfwTerminate();
    
    • 1
    • 2
    • 3

    完整项目:
    github

  • 相关阅读:
    微信小程序中复制文本
    遥测、遥信、遥控、定值?IEC61850?-----极简记录
    delegate使用方法C#(Demo)
    使用ffmpeg和sdl播放视频实现时钟同步
    Maven依赖的作用域你到底用对了没有
    [架构之路-247]:目标系统 - 设计方法 - 软件工程 - 结构化方法的基本思想、本质、特点以及在软件开发、在生活中的应用
    【遮天】韩老魔被灭小囡囡现身,好消息叶凡终于不跑酷了,但有坏消息
    数星星-树状数组(POJ2352)
    Arduino--音乐频谱
    分组后将成员拼成字符串
  • 原文地址:https://blog.csdn.net/m0_46499080/article/details/132940218