class mrpt::opengl::Viewport

A viewport within a Scene, containing a set of OpenGL objects to render.

This class has protected constuctor, thus it cannot be created by users. Use Scene::createViewport instead. A viewport has these “operation modes”:

  • Normal (default): It renders the contained objects.

  • Cloned: It clones the objects from another viewport. See setCloneView()

  • Image mode: It renders an image (e.g. from a video stream) efficiently using a textued quad. See setImageView().

In any case, the viewport can be resized to only fit a part of the entire parent viewport. There will be always at least one viewport in a Scene named “main”.

This class can be observed (see mrpt::system::CObserver) for the following events (see mrpt::system::mrptEvent):

Two directional light sources at infinity are created by default, with directions (-1,-1,-1) and (1,2,1), respectively.

Lighting parameters are accessible via lightParameters().

Refer to mrpt::opengl::Scene for further details.

#include <mrpt/opengl/Viewport.h>

class Viewport:
    public mrpt::serialization::CSerializable,
    public mrpt::system::CObservable,
    public mrpt::opengl::CTextMessageCapable
{
public:
    // typedefs

    typedef CListOpenGLObjects::const_iterator const_iterator;
    typedef CListOpenGLObjects::iterator iterator;
    typedef std::shared_ptr<CObject> Ptr;
    typedef std::shared_ptr<const CObject> ConstPtr;
    typedef std::unique_ptr<CObject> UniquePtr;
    typedef std::unique_ptr<const CObject> ConstUniquePtr;

    // structs

    struct PerThreadData;

    // construction

    Viewport(Scene* parent = nullptr, const std::string& name = std::string(""));

    //
methods

    const_iterator begin() const;
    const_iterator end() const;
    iterator begin();
    iterator end();
    void clear();
    void insert(const CRenderizable::Ptr& newObject);
    void getCurrentCameraPose(mrpt::poses::CPose3D& out_cameraPose) const;
    mrpt::poses::CPose3D getCurrentCameraPose() const;
    void setCurrentCameraFromPose(mrpt::poses::CPose3D& p);
    CRenderizable::Ptr getByName(const std::string& str);

    template <typename T>
    T::Ptr getByClass(size_t ith = 0) const;

    void removeObject(const CRenderizable::Ptr& obj);
    size_t size() const;
    bool empty() const;
    opengl::CCamera& getCamera();
    const opengl::CCamera& getCamera() const;
    mrpt::math::TBoundingBox getBoundingBox() const;
    TRenderMatrices getRenderMatrices() const;
    void setCloneView(const std::string& clonedViewport);
    void setImageView(const mrpt::img::CImage& img, bool transparentBackground = true);

    void setImageView(
        mrpt::img::CImage&& img,
        bool transparentBackground = true
        );

    bool isImageViewMode() const;
    void resetCloneView();
    void setCloneCamera(bool enable);
    void setClonedCameraFrom(const std::string& viewPortName);
    void setNormalMode();
    void setViewportVisibility(bool visible);
    bool getViewportVisibility() const;
    void enablePolygonNicest(bool enable = true);
    bool isPolygonNicestEnabled() const;
    const TLightParameters& lightParameters() const;
    TLightParameters& lightParameters();
    std::string getName();
    void setViewportPosition(const double x, const double y, const double width, const double height);
    void getViewportPosition(double& x, double& y, double& width, double& height);
    void setViewportClipDistances(const float clip_min, const float clip_max);
    void getViewportClipDistances(float& clip_min, float& clip_max) const;

    void setLightShadowClipDistances(
        const float clip_min,
        const float clip_max
        );

    void getLightShadowClipDistances(
        float& clip_min,
        float& clip_max
        ) const;

    void setBorderSize(unsigned int lineWidth);
    unsigned int getBorderSize() const;
    void setBorderColor(const mrpt::img::TColor& c);
    const mrpt::img::TColor& getBorderColor() const;
    bool isTransparent();
    void setTransparent(bool trans);
    void setCustomBackgroundColor(const mrpt::img::TColorf& color);
    mrpt::img::TColorf getCustomBackgroundColor() const;

    void get3DRayForPixelCoord(
        const double x_coord,
        const double y_coord,
        mrpt::math::TLine3D& out_ray,
        mrpt::poses::CPose3D* out_cameraPose = nullptr
        ) const;

    void enableShadowCasting(
        bool enabled = true,
        unsigned int SHADOW_MAP_SIZE_X = 0,
        unsigned int SHADOW_MAP_SIZE_Y = 0
        );

    bool isShadowCastingEnabled() const;

    void render(
        const int render_width,
        const int render_height,
        const int render_offset_x = 0,
        const int render_offset_y = 0,
        const CCamera* forceThisCamera = nullptr
        ) const;

    void updateMatricesFromCamera(const CCamera& myCamera) const;
    const std::map<shader_id_t, mrpt::opengl::Program::Ptr>& shaders() const;
    void loadDefaultShaders() const;
    std::map<shader_id_t, mrpt::opengl::Program::Ptr>& shaders();
    void clearTextMessages();
    const TListTextMessages& getTextMessages() const;
    static const mrpt::rtti::TRuntimeClassId& GetRuntimeClassIdStatic();
};

Inherited Members

public:
    // structs

    struct DataPerText;
    struct TListTextMessages;

    //
methods

    void addTextMessage(
        const double x_frac,
        const double y_frac,
        const std::string& text,
        const size_t unique_index = 0,
        const TFontParams& fontParams = TFontParams()
        );

    bool updateTextMessage(size_t unique_index, const std::string& text);

Construction

Viewport(Scene* parent = nullptr, const std::string& name = std::string(""))

Constructor, invoked from Scene only.

Methods

void clear()

Delete all internal obejcts.

See also:

insert

void insert(const CRenderizable::Ptr& newObject)

Insert a new object into the list.

The object MUST NOT be deleted, it will be deleted automatically by this object when not required anymore.

void getCurrentCameraPose(mrpt::poses::CPose3D& out_cameraPose) const

Compute the current 3D camera pose: +Z points forward, +X to the right, +Y down.

See also:

get3DRayForPixelCoord

mrpt::poses::CPose3D getCurrentCameraPose() const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

void setCurrentCameraFromPose(mrpt::poses::CPose3D& p)

Changes the point of view of the camera, from a given pose.

See also:

getCurrentCameraPose

CRenderizable::Ptr getByName(const std::string& str)

Returns the first object with a given name, or nullptr if not found.

template <typename T>
T::Ptr getByClass(size_t ith = 0) const

Returns the i’th object of a given class (or of a descendant class), or nullptr (an empty smart pointer) if not found.

Example:

CSphere::Ptr obs = view.getByClass<CSphere>();

By default (ith=0), the first observation is returned.

void removeObject(const CRenderizable::Ptr& obj)

Removes the given object from the scene (it also deletes the object to free its memory).

size_t size() const

Number of objects contained.

opengl::CCamera& getCamera()

Get a reference to the camera associated with this viewport.

const opengl::CCamera& getCamera() const

Get a reference to the camera associated with this viewport.

mrpt::math::TBoundingBox getBoundingBox() const

Evaluates the bounding box of this object (including possible children) in the coordinate frame of the object parent.

TRenderMatrices getRenderMatrices() const

Returns a copy of the latest render matrices structure.

void setCloneView(const std::string& clonedViewport)

Set this viewport as a clone of some other viewport, given its name - as a side effect, current list of internal OpenGL objects is cleared.

By default, only the objects are cloned, not the camera. See

See also:

resetCloneView

void setImageView(const mrpt::img::CImage& img, bool transparentBackground = true)

Set this viewport into “image view”-mode, where an image is efficiently drawn (fitting the viewport area) using an OpenGL textured quad.

Call this method with the new image to update the displayed image (but recall to first lock the parent openglscene’s critical section, then do the update, then release the lock, and then issue a window repaint). Internally, the texture is drawn using a mrpt::opengl::CTexturedPlane The viewport can be reverted to behave like a normal viewport by calling setNormalMode()

Parameters:

transparentBackground

This method can also make the viewport transparent (default), so the area not filled with the image still allows seeing an underlying viewport.

bool isImageViewMode() const

Returns true if setImageView() has been called on this viewport.

void resetCloneView()

Reset the viewport to normal mode: rendering its own objects.

See also:

setCloneView, setNormalMode

void setCloneCamera(bool enable)

If set to true, and setCloneView() has been called, this viewport will be rendered using the camera of the cloned viewport.

void setClonedCameraFrom(const std::string& viewPortName)

Use the camera of another viewport.

Note this works even for viewports not in “clone” mode, so you can render different scenes but using the same camera.

void setNormalMode()

Resets the viewport to a normal 3D viewport.

See also:

setCloneView, setImageView

void enablePolygonNicest(bool enable = true)

Sets glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST) is enabled, or GL_FASTEST otherwise.

std::string getName()

Returns the name of the viewport.

void setViewportPosition(
    const double x,
    const double y,
    const double width,
    const double height
    )

Change the viewport position and dimension on the rendering window.

X & Y coordinates here can have two interpretations:

  • If in the range [0,1], they are factors with respect to the actual window sizes (i.e. width=1 means the entire width of the rendering window).

  • If >1, they are interpreted as pixels.

width & height can be interpreted as:

  • If >1, they are the size of the viewport in that dimension, in pixels.

  • If in [0,1], they are the size of the viewport in that dimension, in a factor of the width/height.

  • If in [-1,0[, the size is computed such as the right/top border ends up in the given coordinate, interpreted as a factor (e.g. -1: up to the end of the viewport, -0.5: up to the middle of it).

  • If <-1 the size is computed such as the right/top border ends up in the given absolute coordinate (e.g. -200: up to the row/column 200px).

(x,y) specify the lower left corner of the viewport rectangle.

See also:

getViewportPosition

void getViewportPosition(double& x, double& y, double& width, double& height)

Get the current viewport position and dimension on the rendering window.

X & Y coordinates here can have two interpretations:

  • If in the range [0,1], they are factors with respect to the actual window sizes (i.e. width=1 means the entire width of the rendering window).

  • If >1, they are interpreted as pixels. (x,y) specify the lower left corner of the viewport rectangle.

See also:

setViewportPosition

void setViewportClipDistances(const float clip_min, const float clip_max)

Set the min/max clip depth distances of the rendering frustum (default: 0.1 - 1000)

See also:

getViewportClipDistances

void getViewportClipDistances(float& clip_min, float& clip_max) const

Get the current min/max clip depth distances of the rendering frustum (default: 0.1 - 1000)

See also:

setViewportClipDistances

void setBorderSize(unsigned int lineWidth)

Set the border size (“frame”) of the viewport (default=0)

bool isTransparent()

Return whether the viewport will be rendered transparent over previous viewports.

void setTransparent(bool trans)

Set the transparency, that is, whether the viewport will be rendered transparent over previous viewports (default=false).

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

Defines the viewport background color.

void get3DRayForPixelCoord(
    const double x_coord,
    const double y_coord,
    mrpt::math::TLine3D& out_ray,
    mrpt::poses::CPose3D* out_cameraPose = nullptr
    ) const

Compute the 3D ray corresponding to a given pixel; this can be used to allow the user to pick and select 3D objects by clicking onto the 2D image.

(x,y) refer to VIEWPORT coordinates. Take into account this when viewports do not extend to the whole window size.

x and y are double instead of integers to allow sub-pixel precision.

Parameters:

x_coord

Horizontal coordinate with the usual meaning (0:left of the viewport, W-1: right border).

y_coord

Horizontal coordinate with the usual meaning (0:top of the viewport, H-1: right border).

out_cameraPose

If not nullptr, will have the camera 3D pose as a mrpt::poses::CPose3D. See also

See also:

getCurrentCameraPose

void enableShadowCasting(
    bool enabled = true,
    unsigned int SHADOW_MAP_SIZE_X = 0,
    unsigned int SHADOW_MAP_SIZE_Y = 0
    )

Enables or disables rendering of shadows cast by the unidirectional light.

Parameters:

enabled

Set to true to enable shadow casting (default at ctor=false).

SHADOW_MAP_SIZE_X

Width of the shadow cast map (1st pass of rendering with shadows). Larger values are slower but gives more precise shadows. Default=2048x2048. Zero means do not change.

SHADOW_MAP_SIZE_Y

Like SHADOW_MAP_SIZE_X but defines the height.

void render(
    const int render_width,
    const int render_height,
    const int render_offset_x = 0,
    const int render_offset_y = 0,
    const CCamera* forceThisCamera = nullptr
    ) const

Render the objects in this viewport (called from Scene)

void updateMatricesFromCamera(const CCamera& myCamera) const

myCamera must come from internalResolveActiveCamera()

const std::map<shader_id_t, mrpt::opengl::Program::Ptr>& shaders() const

Provides read access to the opengl shaders.

void loadDefaultShaders() const

Load all MPRT predefined shader programs into m_shaders.

std::map<shader_id_t, mrpt::opengl::Program::Ptr>& shaders()

Provides write access to the opengl shaders.