class mrpt::opengl::RenderableProxy

Overview

Base class for GPU-side representations of mrpt::viz::CVisualObject instances.

This is the core abstraction that bridges the abstract scene graph (mrpt::viz) with the actual OpenGL rendering (mrpt::opengl).

Key responsibilities:

  • GPU Resource Management : Owns OpenGL buffers (VBOs, VAOs, textures)

  • Compilation : Translates abstract object data into GPU buffers

  • Incremental Updates : Efficiently updates only changed data

  • Rendering : Issues OpenGL draw calls using bound shaders

Design pattern: Each concrete CVisualObject type has a corresponding RenderableProxy :

Lifecycle:

  1. Created by CompiledScene during compilation

  2. compile() called once to upload initial data to GPU

  3. updateBuffers() called when the source object data changes (dataVersion())

  4. render() called every frame to draw

  5. Destroyed when source object deleted or scene recompiled

Thread safety:

  • All methods must be called from the OpenGL context thread

  • Source object access is read-only (via const pointers)

  • Source objects are tracked via weak_ptr in CompiledScene

See also:

CompiledScene, CompiledViewport, mrpt::viz::CVisualObject

#include <mrpt/opengl/RenderableProxy.h>

class RenderableProxy
{
public:
    // typedefs

    typedef std::shared_ptr<RenderableProxy> Ptr;

    // fields

    mrpt::math::CMatrixFloat44 m_modelMatrix = mrpt::math::CMatrixFloat44::Identity();
    bool m_visible = true;
    bool m_castShadows = true;
    uint64_t m_changeCount = 0;
    mrpt::math::TPoint3Df m_sortPointLocal {0, 0, 0};

    // construction

    RenderableProxy();
    RenderableProxy(const RenderableProxy&);
    RenderableProxy(RenderableProxy&&);

    // methods

    virtual void compile(const mrpt::viz::CVisualObject* sourceObj) = 0;
    virtual void updateBuffers(const mrpt::viz::CVisualObject* sourceObj);
    virtual void render(const RenderContext& rc) const = 0;
    virtual std::vector<shader_id_t> requiredShaders() const = 0;
    virtual bool castsShadows() const;
    virtual bool isTransparent() const;
    virtual bool isBackground() const;
    virtual shader_id_t shadowMapShader() const;
    virtual bool cullEligible() const;
    const std::optional<mrpt::math::TBoundingBoxf>& localBoundingBox() const;
    virtual const char* typeName() const;
    void setSourceObject(std::weak_ptr<const mrpt::viz::CVisualObject> obj);
    void setResourceScope(const void* scope);
    std::shared_ptr<const mrpt::viz::CVisualObject> getSourceObject() const;
    bool isSourceValid() const;
    bool sourceNeedsUpdate() const;
    RenderableProxy& operator = (const RenderableProxy&);
    RenderableProxy& operator = (RenderableProxy&&);
};

// direct descendants

class LinesProxyBase;
class PointsProxyBase;
class SkyBoxProxy;
class TrianglesProxyBase;

Fields

mrpt::math::CMatrixFloat44 m_modelMatrix = mrpt::math::CMatrixFloat44::Identity()

Model matrix: object local frame -> world frame.

Composed from the object’s pose (and parent container poses). Set during compilation by CompiledScene.

bool m_visible = true

Effective visibility (accounts for parent container visibility).

Updated by CompiledScene during dirty-object updates.

bool m_castShadows = true

Effective shadow casting: mrpt::viz::CVisualObject::castShadows() of the object and all its parents.

Updated by CompiledScene.

uint64_t m_changeCount = 0

Incremented by CompiledScene whenever the buffers, model matrix, visibility or shadow casting of this proxy may have changed.

Used to detect when cached shadow maps must be regenerated.

mrpt::math::TPoint3Df m_sortPointLocal {0, 0, 0}

Point (in the object local frame) whose eye-space depth is used to sort transparent objects.

Set by CompiledScene.

Methods

virtual void compile(const mrpt::viz::CVisualObject* sourceObj) = 0

Initial compilation: uploads object data to GPU.

This is called once when the proxy is first created. It should:

  • Create OpenGL buffers (VBOs, VAOs, textures)

  • Upload initial vertex/color/normal/texture data

  • Cache any frequently-used values

Must be called from OpenGL context thread

After this call, the proxy should be ready to render

Parameters:

sourceObj

The abstract viz object (read-only access)

virtual void updateBuffers(const mrpt::viz::CVisualObject* sourceObj)

Incremental update: refreshes GPU buffers with changed data.

This is called when the data version of the source object changes (mrpt::viz::CVisualObject::dataVersion()). It should:

  • Re-upload only the changed data (vertices, colors, etc.)

  • Be as efficient as possible (don’t recompile everything)

Must be called from OpenGL context thread

Default implementation calls compile() - override for efficiency

Parameters:

sourceObj

The abstract viz object (read-only access)

virtual void render(const RenderContext& rc) const = 0

Renders this object using the provided context.

This is called every frame for visible objects. It should:

  • Bind appropriate buffers (VAO, VBO, textures)

  • Set shader uniforms (model matrix, material properties, etc.)

  • Issue draw calls (glDrawArrays, glDrawElements, etc.)

Must be called from OpenGL context thread

The shader program is already bound when this is called

Common matrices (P, V, M) are already uploaded by CompiledViewport

Parameters:

rc

Rendering context (shader, matrices, lights)

virtual std::vector<shader_id_t> requiredShaders() const = 0

Returns the list of shader programs this object needs.

Most objects use a single shader, but some may use multiple (e.g., different shaders for shadow map pass vs. normal rendering).

Returns:

Vector of shader IDs, typically with 1 element

virtual bool castsShadows() const

Does this object cast shadows?

Used to determine if the object should be rendered during the shadow map generation pass (1st pass of shadow rendering).

Returns:

m_castShadows by default, which follows mrpt::viz::CVisualObject::castShadows() of the object and its parents.

virtual bool isTransparent() const

Whether the object is (partly) translucent, so it must be blended over whatever is behind it: such objects are drawn after all the opaque ones, from back to front.

virtual bool isBackground() const

Background objects (e.g.

sky boxes) are drawn after all opaque objects, so only the uncovered pixels are shaded, and before transparent ones.

virtual shader_id_t shadowMapShader() const

The shader for the shadow map generation pass (depth only).

Returns:

TRIANGLES_SHADOW_1ST by default

virtual bool cullEligible() const

Should this object be checked for frustum culling?

Some objects (like skyboxes) should never be culled even if their bounding box is outside the view frustum.

Returns:

true if eligible for culling (default: true)

const std::optional<mrpt::math::TBoundingBoxf>& localBoundingBox() const

Returns the bounding box of the uploaded geometry, in the object local frame (before applying its model matrix), or nullopt if unknown or empty, in which case the object is never culled.

virtual const char* typeName() const

Returns a human-readable type name for this proxy.

Used for debugging and logging.

void setSourceObject(std::weak_ptr<const mrpt::viz::CVisualObject> obj)

Sets the source object reference.

Called by CompiledScene during compilation.

void setResourceScope(const void* scope)

Sets the scope in which GPU resources (e.g.

textures uploaded from the same image) can be shared with other proxies. All proxies of a CompiledScene use the same scope, which must not be shared by proxies rendered with different OpenGL contexts. Must be called before compile().

std::shared_ptr<const mrpt::viz::CVisualObject> getSourceObject() const

Returns the source object, or nullptr if it has been deleted.

bool isSourceValid() const

Check if source object still exists.

bool sourceNeedsUpdate() const

Check if source object has pending changes (dirty flag)