简体   繁体   English

带有 ImGui 和 SDL2 的 OpenGL 渲染器

[英]OpenGL renderer with ImGui and SDL2

I'm trying to use all 3 libraries or whatnot but i'm quite confused by the sample code and I can't quite follow the documentation.我正在尝试使用所有 3 个库或诸如此类的东西,但我对示例代码感到很困惑,而且我不能完全遵循文档。 This is the code and ill explain my confusions below:这是代码,并在下面解释了我的困惑:


#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;
}


There are a few confusions I have here, starting with there being no SDL_Renderer anywhere within the code.我在这里有一些困惑,首先是代码中的任何地方都没有SDL_Renderer I notice that the display draw color is handled by OpenGL, but the rendering is called via glClear(GL_COLOR_BUFFER_BIT);我注意到显示绘制颜色由 OpenGL 处理,但渲染是通过glClear(GL_COLOR_BUFFER_BIT);调用的。 (I THINK). (我认为)。 I'm unsure, though, how I could actually then call any SDL2 functions such as SDL_RenderFillRect() with no SDL_Renderer?不过,我不确定我如何才能在没有 SDL_Renderer 的情况下调用任何 SDL2 函数,例如SDL_RenderFillRect() My best hint is this line:我最好的提示是这一行:

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

where its SDL_GL_SwapWindow() but this I believe just also renders for the OpenGL?它的 SDL_GL_SwapWindow() 在哪里,但我相信这也适用于 OpenGL? I'm not really sure what line out of all the rending actually does what.我不确定所有撕裂中的哪条线实际上做了什么。 I mean I would have thought ImGui::Render() would render all ImGui things, but then theres an ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());我的意思是我会认为ImGui::Render()会渲染所有 ImGui 的东西,但是有一个ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); and then I'm not sure how the SDL_GL_SwapWindow ties in since i've already called glClear() .然后我不确定SDL_GL_SwapWindow是如何关联的,因为我已经调用glClear() Additionally, why is there a function called ImGui::EndFrame() but not called in the sample code at the end of a frame and then there is ImGui::NewFrame() for each loop and same for ImGui_ImplOpenGL3_NewFrame();此外,为什么有一个名为ImGui::EndFrame()的 function 但在帧末尾的示例代码中没有调用,然后每个循环都有ImGui::NewFrame()ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplSDL2_NewFrame(window); Can someone please explain some of these things its very confusing.有人可以解释其中一些非常令人困惑的事情。

SDL_Renderer is something you need if you want to use the SDL API for drawing tasks, but it is not required if you just create the OpenGL context with SDL and do all the drawing directly with OpenGL.如果您想将 SDL SDL_Renderer用于绘图任务,则需要 SDL_Renderer,但如果您只是使用 SDL 创建 OpenGL 上下文并直接使用 Z760716B590EE7C0ADC25F434FZ 进行所有绘图,则不需要它。

but the rendering is called via glClear(GL_COLOR_BUFFER_BIT);但是渲染是通过glClear(GL_COLOR_BUFFER_BIT);

No, glClear clearse part of the current render buffer, in this case, the color ( What is the purpose of GL_COLOR_BUFFER_BIT and GL_DEPTH_BUFFER_BIT? )不, glClear清除当前渲染缓冲区的一部分,在这种情况下,颜色( GL_COLOR_BUFFER_BIT 和 GL_DEPTH_BUFFER_BIT 的目的是什么?

SDL_GL_SwapWindow(window); brings the contents of the current render buffer (the rendering) to the window SDL_GL_SwapWindow将当前渲染缓冲区(渲染)的内容带到 window SDL_GL_SwapWindow

ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); invokes he drawing of the ImGUI components.调用 ImGUI 组件的绘图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM