class mrpt::opengl::CompiledViewport

Overview

A compiled, GPU-ready representation of a mrpt::viz::Viewport.

This class is the rendering engine for a single viewport. It maintains:

  • Compiled proxies for all renderable objects in the viewport

  • Camera state and projection matrices

  • Lighting parameters

  • Special rendering modes (image view, cloned view, etc.)

  • Shadow mapping framebuffers (if enabled)

Key features:

  • Normal 3D rendering : Standard scene with objects, camera, lighting

  • Image view mode : Efficient rendering of 2D images (for video streams)

  • Cloned viewports : Share objects/camera from another viewport

  • Shadow mapping : Two-pass rendering for directional shadows

  • Frustum culling : Automatically skips objects outside camera view

The viewport can operate in several modes:

  1. Normal: Renders its own objects with its own camera

  2. Image view: Displays a textured quad (for images/video)

  3. Cloned objects: Renders objects from another viewport

  4. Cloned camera: Uses camera from another viewport

See also:

CompiledScene, RenderableProxy, mrpt::viz::Viewport

#include <mrpt/opengl/CompiledViewport.h>

class CompiledViewport
{
public:
    // typedefs

    typedef std::shared_ptr<CompiledViewport> Ptr;

    // structs

    struct CameraState;
    struct LightState;

    // construction

    CompiledViewport(const std::string& name);
    CompiledViewport(const CompiledViewport&);
    CompiledViewport(CompiledViewport&&);

    // methods

    void updateFromVizViewport(const mrpt::viz::Viewport& vizVp);
    const std::string& getName() const;
    void setImageViewMode(RenderableProxy::Ptr imageProxy);
    void clearImageViewMode();
    bool isImageViewMode() const;
    void setCloneMode(const std::string& clonedViewportName, bool cloneCamera = false);
    void clearCloneMode();
    bool isCloningObjects() const;
    bool isCloningCamera() const;
    const std::string& getClonedViewportName() const;

    void enableShadows(
        bool enabled,
        unsigned int shadowMapSizeX = 4096,
        unsigned int shadowMapSizeY = 4096
        );

    bool areShadowsEnabled() const;
    void addProxy(const RenderableProxy::Ptr& proxy, const std::shared_ptr<mrpt::viz::CVisualObject>& sourceObj);
    size_t cleanupOrphanedProxies();

    void updateProxiesForObject(
        const std::weak_ptr<mrpt::viz::CVisualObject>& weakObj,
        const mrpt::viz::CVisualObject* sourceObj,
        const mrpt::math::CMatrixFloat44& modelMatrix,
        bool effectiveVisible = true
        );

    void removeProxy(const RenderableProxy::Ptr& proxy);
    void clearProxies();
    size_t getProxyCount() const;
    const std::vector<RenderableProxy::Ptr>& getProxies() const;

    void render(
        int renderWidth,
        int renderHeight,
        int renderOffsetX,
        int renderOffsetY,
        ShaderProgramManager& shaderManager,
        const CompiledViewport* sourceViewport = nullptr
        );

    bool updateIfNeeded();
    bool hasPendingUpdates() const;
    void forceMatrixUpdate();
    void updateCamera(const mrpt::viz::CCamera& camera);
    const TRenderMatrices& getRenderMatrices() const;
    TRenderMatrices& getRenderMatrices();
    void setViewportBounds(double x, double y, double width, double height);

    void getViewportBounds(
        double& x,
        double& y,
        double& width,
        double& height
        ) const;

    void setClipPlanes(float nearPlane, float farPlane);

    void getClipPlanes(
        float& nearPlane,
        float& farPlane
        ) const;

    void setBackgroundColor(const mrpt::img::TColorf& color);
    const mrpt::img::TColorf& getBackgroundColor() const;
    void setTransparent(bool transparent);
    bool isTransparent() const;
    void setBorder(unsigned int width, const mrpt::img::TColor& color);
    unsigned int getBorderWidth() const;
    const mrpt::img::TColor& getBorderColor() const;
    void setVisible(bool visible);
    bool isVisible() const;
    mrpt::viz::TLightParameters& lightParameters();
    const mrpt::viz::TLightParameters& lightParameters() const;
    const ViewportRenderStats& lastRenderStats() const;
    void flipVerticalProjection(bool flipEnabled);
    bool flipVerticalProjection() const;
    CompiledViewport& operator = (const CompiledViewport&);
    CompiledViewport& operator = (CompiledViewport&&);
};

Construction

CompiledViewport(const std::string& name)

Constructor.

Parameters:

name

Viewport name (must match the name in viz::Viewport)

Methods

void updateFromVizViewport(const mrpt::viz::Viewport& vizVp)

Synchronizes this compiled viewport with its source viz::Viewport.

Copies all configuration: camera, lights, rendering options, viewport bounds, special modes, etc.

This is called automatically by CompiledScene during compilation.

Parameters:

vizVp

The source viewport. A non-owning pointer is kept to allow writing back the rendered viewport size for get3DRayForPixelCoord().

const std::string& getName() const

Returns the viewport name.

void setImageViewMode(RenderableProxy::Ptr imageProxy)

Enables/disables image view mode.

When enabled, the viewport displays a single textured quad instead of 3D objects. Used for efficient video/image display.

Parameters:

imageProxy

The textured quad proxy (created from CTexturedPlane)

void clearImageViewMode()

Disables image view mode, returns to normal 3D rendering.

bool isImageViewMode() const

Returns true if viewport is in image view mode.

void setCloneMode(
    const std::string& clonedViewportName,
    bool cloneCamera = false
    )

Sets this viewport to clone objects from another viewport.

Parameters:

clonedViewportName

Name of viewport to clone from

cloneCamera

If true, also clone camera settings

void clearCloneMode()

Disables cloning.

bool isCloningObjects() const

Returns true if this viewport clones objects from another.

bool isCloningCamera() const

Returns true if this viewport clones camera from another.

const std::string& getClonedViewportName() const

Returns name of cloned viewport, or empty if not cloning.

void enableShadows(
    bool enabled,
    unsigned int shadowMapSizeX = 4096,
    unsigned int shadowMapSizeY = 4096
    )

Enables/disables shadow casting for this viewport.

Parameters:

enabled

Enable shadow rendering

shadowMapSizeX

Shadow map texture width (default 4096)

shadowMapSizeY

Shadow map texture height (default 4096)

bool areShadowsEnabled() const

Returns true if shadow casting is enabled.

void addProxy(const RenderableProxy::Ptr& proxy, const std::shared_ptr<mrpt::viz::CVisualObject>& sourceObj)

Adds a renderable proxy to this viewport.

Parameters:

proxy

The GPU-side representation of an object

sourceObj

The original viz object (tracked via weak_ptr)

size_t cleanupOrphanedProxies()

Removes proxies whose source objects have been deleted.

Returns:

Number of orphaned proxies removed

void updateProxiesForObject(
    const std::weak_ptr<mrpt::viz::CVisualObject>& weakObj,
    const mrpt::viz::CVisualObject* sourceObj,
    const mrpt::math::CMatrixFloat44& modelMatrix,
    bool effectiveVisible = true
    )

Updates all proxies for a given source object (buffers + model matrix).

Called by CompiledScene::updateDirtyObjects().

void removeProxy(const RenderableProxy::Ptr& proxy)

Removes a proxy from this viewport.

void clearProxies()

Removes all proxies.

size_t getProxyCount() const

Number of proxies in this viewport.

const std::vector<RenderableProxy::Ptr>& getProxies() const

Access to the list of proxies (used by cloned viewports)

void render(
    int renderWidth,
    int renderHeight,
    int renderOffsetX,
    int renderOffsetY,
    ShaderProgramManager& shaderManager,
    const CompiledViewport* sourceViewport = nullptr
    )

Renders this viewport.

This handles:

  • Viewport positioning and clipping

  • Background color clearing

  • Shadow map rendering (if enabled)

  • Normal scene rendering

  • Image view rendering (if in image mode)

  • Text overlay rendering

  • Viewport border rendering

Parameters:

renderWidth

Full window width in pixels

renderHeight

Full window height in pixels

renderOffsetX

X offset for multi-window rendering

renderOffsetY

Y offset for multi-window rendering

shaderManager

Shader program manager for binding programs

sourceViewport

For cloned viewports, the source viewport whose proxies should be rendered. nullptr for normal viewports.

bool updateIfNeeded()

Checks if viewport configuration has changed and updates if needed.

This monitors changes in:

  • Camera position/orientation

  • Lighting parameters

  • Viewport dimensions

Returns:

true if any updates were performed

bool hasPendingUpdates() const

Returns true if there are pending configuration changes.

void forceMatrixUpdate()

Forces regeneration of all projection/view matrices.

void updateCamera(const mrpt::viz::CCamera& camera)

Updates camera state from a viz::CCamera.

const TRenderMatrices& getRenderMatrices() const

Returns current render matrices (projection, view, etc.)

TRenderMatrices& getRenderMatrices()

Direct access to render matrices (for manual manipulation)

void setViewportBounds(double x, double y, double width, double height)

Viewport position and size (normalized or pixel coordinates)

void setClipPlanes(float nearPlane, float farPlane)

Set near/far clip planes.

void setBackgroundColor(const mrpt::img::TColorf& color)

Set background color.

void setTransparent(bool transparent)

Set transparent rendering (doesn’t clear color buffer)

void setBorder(unsigned int width, const mrpt::img::TColor& color)

Set viewport border.

void setVisible(bool visible)

Set viewport visibility.

mrpt::viz::TLightParameters& lightParameters()

Access lighting parameters.

const ViewportRenderStats& lastRenderStats() const

Last rendering statistics.

void flipVerticalProjection(bool flipEnabled)

Flip vertically at projection level (useful for FBO rendering)

bool flipVerticalProjection() const

Flip vertically at projection level (useful for FBO rendering)