Skip to main content
Add missing child window in example
Source Link
FloFu
  • 392
  • 5
  • 14
ImGui::Begin("GameWindow");
{
  // 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();
// 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::Begin("GameWindow");
{
  // 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();
simpler example
Source Link
FloFu
  • 392
  • 5
  • 14

EDIT New (simpler) Example:

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

Previous Version

Here is an example:

Here is an example:

EDIT New (simpler) Example:

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

Previous Version

Here is an example:

Source Link
FloFu
  • 392
  • 5
  • 14

First you need to render you scene to a Frame Buffer Object (here is a good course on FBO: https://learnopengl.com/#!Advanced-OpenGL/Framebuffers)

After that you will end up with a Texture (of type GLuint) containing your rendered scene. To print it into Dear imGUI, just call a Draw Image Command.

Here is an example:

// My Game has a different viewport than the editor's one:
const int W = 1080 / 2;
const int H = 1920 / 2;
// We set the same viewport size (plus margin) to the next window (if first use)
ImGui::SetNextWindowSize(ImVec2(W + 10, H + 10),
                           ImGuiSetCond_FirstUseEver);
  ImGui::Begin("Game rendering");
  {
    // Get the current cursor position (where your window is)
    ImVec2 pos = ImGui::GetCursorScreenPos();

    // A boolean to allow me to stop the game rendering
    if (runApp) {
      glViewport(0, 0, W, H);
      // Render the scene into an FBO
      game->render(time);
      // Restore previous viewport
      glViewport(0, 0, w, h);
    }
    // Get the texture associated to the FBO
    auto tex = game->getRendered();

    // Ask ImGui to draw it as an image:
    // Under OpenGL the ImGUI image type is GLuint
    // So make sure to use "(void *)tex" but not "&tex"
    ImGui::GetWindowDrawList()->AddImage(
        (void *)tex, ImVec2(ImGui::GetItemRectMin().x + pos.x,
                            ImGui::GetItemRectMin().y + pos.y),
        ImVec2(pos.x + h / 2, pos.y + w / 2), ImVec2(0, 1), ImVec2(1, 0));
  }
  ImGui::End();