【问题标题】:OpenGL renderer with ImGui and SDL2带有 ImGui 和 SDL2 的 OpenGL 渲染器
【发布时间】:2021-05-14 05:58:25
【问题描述】:

我正在尝试使用所有 3 个库或诸如此类的东西,但我对示例代码感到很困惑,而且我不能完全按照文档进行操作。这是代码,下面将解释我的困惑:


#include <iostream>
#include <string> 

#include <SDL2/SDL.h>

#include <GL/glew.h>

#include <imgui/imgui.h>
#include <imgui/imgui_stdlib.h>
#include <imgui/imgui_impl_sdl.h>
#include <imgui/imgui_impl_opengl3.h>




// Main code
int main(int argc, char* argv[])
{
    if (SDL_Init(SDL_INIT_VIDEO) != 0)
    {
        std::cout << SDL_GetError() << std::endl;
        return -1;
    }

    // GL 3.0 + GLSL 130
    const char* glsl_version = "#version 130";
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);

    // Create window with graphics context
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
    SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
    SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
    SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL2+OpenGL3 example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, window_flags);
    SDL_GLContext gl_context = SDL_GL_CreateContext(window);
    SDL_GL_MakeCurrent(window, gl_context);
    SDL_GL_SetSwapInterval(0); // Disable vsync

    
    if (glewInit() != GLEW_OK) {
        std::cout << "Error initializing glew\n";
    }

    IMGUI_CHECKVERSION();
    ImGui::CreateContext();
    ImGuiIO& io = ImGui::GetIO();
    io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;     // Enable Keyboard Controls
    //io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;      // Enable Gamepad Controls

    // Setup Dear ImGui style
    ImGui::StyleColorsDark();
    //ImGui::StyleColorsClassic();

    // Setup Platform/Renderer backends
    ImGui_ImplSDL2_InitForOpenGL(window, gl_context);
    ImGui_ImplOpenGL3_Init(glsl_version);



    ImFont* font = io.Fonts->AddFontFromFileTTF("C:\\Windows\\Fonts\\Arial.ttf", 30.0f);

    ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);

    // Main loop
    bool running = false;
    SDL_Event event;

    while (!running)
    {

        while (SDL_PollEvent(&event))
        {
            ImGui_ImplSDL2_ProcessEvent(&event);
            if (event.type == SDL_QUIT)
                running = true;
            if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(window))
                running = true;
        }

        // Start the Dear ImGui frame
        ImGui_ImplOpenGL3_NewFrame();
        ImGui_ImplSDL2_NewFrame(window);
        ImGui::NewFrame();


        {
                
            static std::string buf = "";

            ImGui::PushFont(font);

            ImGui::Begin("Window");


            ImGui::InputText("Hello", &buf);
            //std::cout << io.Fonts->Fonts.size() << std::endl;


            ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);


            ImGui::End();

            ImGui::PopFont();

        }



        // Rendering
        
        glViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y);
        glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
        glClear(GL_COLOR_BUFFER_BIT);
        ImGui::Render();
        ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
        SDL_GL_SwapWindow(window);

    }

    // Cleanup
    ImGui_ImplOpenGL3_Shutdown();
    ImGui_ImplSDL2_Shutdown();
    ImGui::DestroyContext();

    SDL_GL_DeleteContext(gl_context);
    SDL_DestroyWindow(window);
    SDL_Quit();

    return 0;
}


我在这里有一些困惑,首先是代码中的任何地方都没有SDL_Renderer。我注意到显示绘制颜色由 OpenGL 处理,但渲染是通过 glClear(GL_COLOR_BUFFER_BIT); 调用的(我认为)。不过,我不确定我如何才能在没有 SDL_Renderer 的情况下调用任何 SDL2 函数,例如 SDL_RenderFillRect() ?我最好的提示是这一行:

ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
SDL_GL_SwapWindow(window);

它的 SDL_GL_SwapWindow() 在哪里,但我相信这也只是为 OpenGL 渲染?我不确定所有撕裂中的哪条线实际上做了什么。我的意思是我本来以为ImGui::Render() 会渲染所有ImGui 的东西,但是有一个ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); 然后我不确定SDL_GL_SwapWindow 是如何关联的,因为我已经调用了glClear()。另外,为什么有一个名为ImGui::EndFrame()的函数,但在一帧末尾的示例代码中没有调用,然后每个循环都有ImGui::NewFrame()ImGui_ImplOpenGL3_NewFrame();也一样ImGui_ImplSDL2_NewFrame(window);有人可以解释其中的一些吗事情很混乱。

【问题讨论】:

    标签: c++ opengl sdl-2 imgui


    【解决方案1】:

    SDL_Renderer 是您想要使用 SDL API 进行绘图任务时需要的东西,但如果您只是使用 SDL 创建 OpenGL 上下文并直接使用 OpenGL 进行所有绘图,则不需要。

    但是渲染是通过glClear(GL_COLOR_BUFFER_BIT);调用的

    不,glClearclearse 是当前渲染缓冲区的一部分,在本例中是颜色 (What is the purpose of GL_COLOR_BUFFER_BIT and GL_DEPTH_BUFFER_BIT? )

    SDL_GL_SwapWindow(window); 将当前渲染缓冲区的内容(渲染)带到窗口SDL_GL_SwapWindow

    ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); 调用 ImGUI 组件的绘制。

    【讨论】:

    • 谢谢!这清除了很多事情。我仍然有点困惑invoke the drawing of ImGui components and why that's necessary 意味着什么,以及每个循环都有ImGui::NewFrame()ImplSDL2 and ImplOpenGL3 的原因,但也没有在它们上调用EndFrame
    • 对于SDL_Renderer api,如果考虑到您已经有渲染器而不能使用它的api,是否有任何理由将SDL2 作为项目的一部分?改用 GLFW 不是更高效吗?
    • @wiourowseoif invoke the drawing of ImGui components ImgGUI 执行不同的 OpenGL 调用来绘制组件,并且必须在每一帧中完成。 …as part of the project if you can't use its api's considering you already have a renderer SDL 提供上下文并进行交换,如果你想这样做,你仍然可以(如果你愿意)将SDL_RendererImgUI 结合使用。 Would it not be more efficient to use GLFW instead? 如果您不需要 SDL 提供的任何其他功能,您也可以使用 GLFW。 GLFW 只是一个比 SDL 更精简的框架。
    • 该行仅用于 ImGUIi opengl 调用吗? ImGui::Render 会做其他不是 opengl 的调用吗?嗯,我对 SDL 还有些不确定,当您说上下文时,您是指事件处理吗?交换我不太确定,因为你不能只调用glpresentrender() 之类的东西或任何正确的opengl 函数来绘制渲染器中的所有内容,那么这样就没有理由为此使用SDL2?
    猜你喜欢
    • 2016-09-29
    • 1970-01-01
    • 2015-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多