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

参考视频
去github下载imgui源码,得到
【9】openGL调用imGUI&使用其自带例子测试_第1张图片
将根目录下所有.h .cpp文件复制到你的项目文件夹imgui下面。

进入exmaple文件夹,可以看到例子
【9】openGL调用imGUI&使用其自带例子测试_第2张图片
这是它调用的头文件
【9】openGL调用imGUI&使用其自带例子测试_第3张图片
在backends里找到你需要的 .h .cpp文件,总共四个文件,复制过来
【9】openGL调用imGUI&使用其自带例子测试_第4张图片
你的项目文件结构:
【9】openGL调用imGUI&使用其自带例子测试_第5张图片
记得将所有文件包含进项目除了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);

在循环里面:

    /* 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();
    } 

结尾:

ImGui_ImplGlfw_Shutdown();
    ImGui::DestroyContext();
    glfwTerminate();

完整项目:
github

你可能感兴趣的:(openGL,c++)