class mrpt::imgui::CImGuiSceneView

Overview

Renders an mrpt::viz::Scene into an OpenGL FBO texture and displays it as a Dear ImGui image widget, with built-in orbit camera controls.

Usage:

// In your ImGui frame loop:
if (ImGui::Begin("3D View"))
{
    sceneView.render();
}
ImGui::End();

The class assumes an OpenGL 3.3+ context is already current (as provided by the Dear ImGui GLFW/SDL backend). No EGL context is created.

#include <mrpt/imgui/CImGuiSceneView.h>

class CImGuiSceneView
{
public:
    // fields

    mrpt::viz::COrbitCameraController cameraController;
    std::function<void()> onOverlayGui;
    std::function<void(float, float)> onLeftClick;

    // construction

    CImGuiSceneView();
    CImGuiSceneView(const CImGuiSceneView&);
    CImGuiSceneView(CImGuiSceneView&&);

    // methods

    void setScene(const mrpt::viz::Scene::Ptr& scene);
    mrpt::viz::Scene::Ptr scene() const;
    void render();
    void renderAsBackground();
    std::optional<mrpt::math::TLine3D> mouseRay() const;
    bool isHovered() const;
    void setBackgroundColor(float r, float g, float b, float a = 1.0f);
    CImGuiSceneView& operator = (const CImGuiSceneView&);
    CImGuiSceneView& operator = (CImGuiSceneView&&);
};

Fields

mrpt::viz::COrbitCameraController cameraController

The orbit camera controller.

Use this to read/write azimuth, elevation, zoom distance, pointing-at position, and interaction sensitivities.

Example:

view.cameraController.setZoomDistance(20.0f);
view.cameraController.setAzimuthDegrees(-135.0f);
view.cameraController.orbitSensitivity = 0.5f;
std::function<void()> onOverlayGui

Called after the MRPT scene is rendered but before ImGui::Image().

Useful for drawing overlay ImGui widgets on top.

std::function<void(float, float)> onLeftClick

Called when the user left-clicks on the 3D view without dragging.

Arguments: (pixel_x, pixel_y) in widget-local coordinates.

Methods

void setScene(const mrpt::viz::Scene::Ptr& scene)

Set the scene to render.

The caller retains ownership.

mrpt::viz::Scene::Ptr scene() const

Get the scene being rendered (may be nullptr).

void render()

Renders the scene into the FBO and displays it via ImGui::Image().

Call this inside an ImGui window (between Begin/End).

The widget fills the available content region. If the region size changed since the last call, the FBO is recreated automatically.

void renderAsBackground()

Like render(), but draws the scene straight into the framebuffer bound while ImGui renders (normally, the window’s default one), behind all ImGui windows.

This avoids the intermediary FBO and its extra copy, and keeps the window multisampling (MSAA), if any.

Call it inside a transparent ImGui window covering the region where the scene must appear (typically, the whole main viewport), which is only used to capture the mouse:

const ImGuiViewport* vp = ImGui::GetMainViewport();
ImGui::SetNextWindowPos(vp->WorkPos);
ImGui::SetNextWindowSize(vp->WorkSize);
ImGui::SetNextWindowBgAlpha(0.0f);
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0));
ImGui::Begin("##bg", nullptr,
    ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove |
    ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoNavFocus |
    ImGuiWindowFlags_NoDocking | ImGuiWindowFlags_NoSavedSettings);
ImGui::PopStyleVar();
view.renderAsBackground();
ImGui::End();

The actual GL rendering happens later, from an ImGui draw callback inside ImGui_ImplOpenGL3_RenderDrawData(), so the scene and this object must stay alive until then. The framebuffer needs a depth buffer.

The scene gamma correction (sRGB encoding, see TLightParameters) is only applied if the framebuffer is sRGB-capable, e.g. with glfwWindowHint(GLFW_SRGB_CAPABLE, GLFW_TRUE) before creating a GLFW window. Otherwise, colors are written linearly, as with render().

std::optional<mrpt::math::TLine3D> mouseRay() const

Returns the 3D ray (in scene coordinates) for the mouse position in the last render call, or nullopt if the mouse was not over the view or the scene has not been rendered yet.

bool isHovered() const

True if the mouse was over the view in the last render call.

void setBackgroundColor(float r, float g, float b, float a = 1.0f)

Background color override.

If never called, the scene’s own viewport background (typically a vertical gradient set up by mrpt::viz::Scene) is preserved.