class mrpt::opengl::COpenGLViewport

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

This class has protected constuctor, thus it cannot be created by users. Use COpenGLScene::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 COpenGLScene 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::COpenGLScene for further details.

#include <mrpt/opengl/COpenGLViewport.h>

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

    typedef CListOpenGLObjects::const_iterator const_iterator;
    typedef CListOpenGLObjects::iterator iterator;

    // construction

    COpenGLViewport(COpenGLScene* parent = nullptr, const std::string& name = std::string(""));

    //
methods

    void setCloneView(const std::string& clonedViewport);
    void setImageView(const mrpt::img::CImage& img);
    void setImageView(mrpt::img::CImage&& img);
    bool isImageViewMode() const;
    void resetCloneView();
    void setCloneCamera(bool enable);
    void setClonedCameraFrom(const std::string& viewPortName);
    void setNormalMode();
    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 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;

    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 render(
        const int render_width,
        const int render_height,
        const int render_offset_x = 0,
        const int render_offset_y = 0
        ) const;

    void updateMatricesFromCamera() const;

    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(const size_t unique_index, const std::string& text);
};

Inherited Members

public:
    // structs

    struct DataPerText;
    struct TListTextMessages;

    //
methods

    void clearTextMessages();
    const TListTextMessages& getTextMessages() const;

Construction

COpenGLViewport(COpenGLScene* parent = nullptr, const std::string& name = std::string(""))

Constructor, invoked from COpenGLScene only.

Methods

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)

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

void setImageView(mrpt::img::CImage&& img)

Just like setImageView but moves the internal image memory instead of making a copy, so it’s faster but empties the input image.

See also:

setImageView

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 - 10000)

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 - 10000)

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)

Set a background color different from that of the parent GUI window.

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 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 render(
    const int render_width,
    const int render_height,
    const int render_offset_x = 0,
    const int render_offset_y = 0
    ) const

Render the objects in this viewport (called from COpenGLScene)

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

Add 2D text messages overlapped to the 3D rendered scene.

overload with more font parameters - refer to mrpt::opengl::gl_utils::glDrawText()

The string will remain displayed in the 3D window until it’s changed with subsequent calls to this same method, or all the texts are cleared with clearTextMessages().

You’ll need to refresh the display manually with forceRepaint().

Parameters:

x

The X position, interpreted as absolute pixels from the left if X>=1, absolute pixels from the left if X<0 or as a width factor if in the range [0,1[.

y

The Y position, interpreted as absolute pixels from the bottom if Y>=1, absolute pixels from the top if Y<0 or as a height factor if in the range [0,1[.

text

The text string to display.

color

The text color. For example: TColorf(1.0,1.0,1.0)

unique_index

An “index” for this text message, so that subsequent calls with the same index will overwrite this text message instead of creating new ones.

See also:

clearTextMessages, updateTextMessage

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

Just updates the text of a given text message, without touching the other parameters.

Returns:

false if given ID doesn’t exist.