返回

c++将OpenGL场景渲染到Imgui视口会产生奇怪的结果

发布时间:2022-06-28 11:40:13 200
# golang

我试图使用ImGui将我的OpenGL场景添加到一个视口,我成功地使用framebuffer和纹理做到这一点,如下面的代码所示

// main.cpp
  ...

  uint32_t fbo;
  GLCall(glGenFramebuffers(1, &fbo));
  GLCall(glBindFramebuffer(GL_FRAMEBUFFER, fbo));

  uint32_t tex;
  GLCall(glGenTextures(1, &tex));
  GLCall(glBindTexture(GL_TEXTURE_2D, tex));
  GLCall(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, WIDTH, HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL));
  GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
  GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
  GLCall(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex, 0));

  if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
    fmt::print("Framebuffer not complete!\n");

...

  while (!glfwWindowShouldClose(window)) {
    GLCall(glBindFramebuffer(GL_FRAMEBUFFER, 0));
    renderer.clear();
    glfwPollEvents();

    ImGui_ImplOpenGL3_NewFrame();
    ImGui_ImplGlfw_NewFrame();
    ImGui::NewFrame();
    ImGuizmo::BeginFrame();

    ImGui::DockSpaceOverViewport(ImGui::GetMainViewport());
    if (currentScene) {
      GLCall(glBindFramebuffer(GL_FRAMEBUFFER, fbo));
      currentScene->onSceneRender(); // Rendering the scene using the framebuffer created

      ImGui::Begin("BloomGL");
      if (currentScene != menu && ImGui::ArrowButton("##left", ImGuiDir_Left)) {
        delete currentScene;
        currentScene = menu;
      }

      GLCall(glBindFramebuffer(GL_FRAMEBUFFER, 0));
      currentScene->onImGuiRender(); // Render the GUI using the default framebuffer
      renderer.clear();
      ImGui::End();
    }

    // https://gamedev.stackexchange.com/questions/140693/how-can-i-render-an-opengl-scene-into-an-imgui-window
    ImGui::Begin("ViewPort");
    {
      // Using a Child allow to fill all the space of the window.
      // It also alows customization
      ImGui::BeginChild("GameRender");
      // Get the size of the child (i.e. the whole draw size of the windows).
      ImVec2 wsize = ImGui::GetWindowSize();
      // Because I use the texture from OpenGL, I need to invert the V from the UV.
      ImGui::Image((ImTextureID)tex, wsize, ImVec2(0, 1), ImVec2(1, 0));

      ImGui::EndChild();
    }
    ImGui::End();

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

    glfwSwapBuffers(window);
    GLCall(glBindFramebuffer(GL_FRAMEBUFFER, 0));
  }

 
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像