Class Viewport
Defined in File Viewport.h
Inheritance Relationships
Base Types
public mrpt::serialization::CSerializablepublic mrpt::system::CObservablepublic mrpt::viz::CTextMessageCapable(Class CTextMessageCapable)
Class Documentation
-
class Viewport : public mrpt::serialization::CSerializable, public mrpt::system::CObservable, public mrpt::viz::CTextMessageCapable
A viewport within a Scene, containing a set of OpenGL objects to render. This class has protected constructor, 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 textured 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):
mrpt::viz::mrptEventGLPreRender
mrpt::viz::mrptEventGLPostRender
One directional light source is created by default. Multiple lights (directional, point, spot) are supported via lightParameters().lights.
Lighting parameters are accessible via lightParameters().
Refer to mrpt::viz::Scene for further details.
Contained objects set/get/search
-
using const_iterator = ListVisualObjects::const_iterator
-
using iterator = ListVisualObjects::iterator
-
inline const_iterator begin() const
-
inline const_iterator end() const
-
void insert(const CVisualObject::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 setCurrentCameraFromPose(mrpt::poses::CPose3D &p)
Changes the point of view of the camera, from a given pose.
See also
getCurrentCameraPose
-
CVisualObject::Ptr getByName(const std::string &str)
Returns the first object with a given name, or nullptr if not found.
-
template<typename T>
inline 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:
By default (ith=0), the first observation is returned.CSphere::Ptr obs = view.getByClass<CSphere>();
-
void removeObject(const CVisualObject::Ptr &obj)
Removes the given object from the scene (it also deletes the object to free its memory).
-
inline size_t size() const
Number of objects contained.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Requires that the viewport has been rendered at least once (so that updateRenderedViewportSize() has been called by the rendering backend).
-
inline bool empty() const
-
inline mrpt::viz::CCamera &getCamera()
Get a reference to the camera associated with this viewport.
-
inline const mrpt::viz::CCamera &getCamera() const
Get a reference to the camera associated with this viewport.
-
mrpt::math::TBoundingBox getBoundingBox() const
Viewport “modes”
-
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
-
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 Scene’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::viz::CTexturedPlane The viewport can be reverted to behave like a normal viewport by calling setNormalMode()
- Parameters:
transparentBackground – [in] This method can also make the viewport transparent (default), so the area not filled with the image still allows seeing an underlying viewport.
-
void setImageView(mrpt::img::CImage &&img, bool transparentBackground = true)
-
inline bool isImageViewMode() const
Returns true if setImageView() has been called on this viewport
-
inline mrpt::viz::CTexturedPlane::Ptr getImageViewPlane() const
Returns the internal image view plane (or nullptr). Used by the renderer.
-
inline void resetCloneView()
Reset the viewport to normal mode: rendering its own objects.
See also
-
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.
-
inline 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.
-
inline bool isCloned() const
-
inline bool isClonedCamera() const
-
inline std::string getClonedViewportName() const
Returns the name of the viewport whose objects are being cloned, or empty if not cloning. Set by setCloneView().
-
inline std::string isClonedCameraFrom() const
-
void setNormalMode()
Resets the viewport to a normal 3D viewport
See also
-
inline void setViewportVisibility(bool visible)
-
inline bool getViewportVisibility() const
OpenGL global settings that affect rendering all objects in the
scene/viewport
-
inline void enablePolygonNicest(bool enable = true)
Sets glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST) is enabled, or GL_FASTEST otherwise.
-
inline bool isPolygonNicestEnabled() const
-
inline const TLightParameters &lightParameters() const
-
inline TLightParameters &lightParameters()
Change or read viewport properties (except “viewport modes”)
-
inline std::string getName() const
Returns the name of the viewport
-
void setViewportPosition(double x, double y, double width, 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).
See also
Note
(x,y) specify the lower left corner of the viewport rectangle.
-
void getViewportPosition(double &x, double &y, double &width, double &height) const
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.
See also
Note
(x,y) specify the lower left corner of the viewport rectangle.
-
void setViewportClipDistances(float clip_min, float clip_max)
Set the min/max clip depth distances of the rendering frustum (default: 0.1 - 1000)
See also
-
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
-
void setLightShadowClipDistances(float clip_min, float clip_max)
-
void getLightShadowClipDistances(float &clip_min, float &clip_max) const
-
inline void setBorderSize(unsigned int lineWidth)
Set the border size (“frame”) of the viewport (default=0)
-
inline unsigned int getBorderSize() const
-
inline void setBorderColor(const mrpt::img::TColor &c)
-
inline const mrpt::img::TColor &getBorderColor() const
-
inline bool isTransparent() const
Return whether the viewport will be rendered transparent over previous viewports.
-
inline void setTransparent(bool trans)
Set the transparency, that is, whether the viewport will be rendered transparent over previous viewports (default=false).
-
inline void setCustomBackgroundColor(const mrpt::img::TColorf &color)
Defines the viewport background color
-
inline mrpt::img::TColorf getCustomBackgroundColor() const
-
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.
-
inline bool isShadowCastingEnabled() const
-
inline void enableSSAO(bool enabled = true)
Enable or disable Screen-Space Ambient Occlusion (SSAO). Parameters are tuned via lightParameters().ssao_* fields. Default: disabled.
-
inline bool isSSAOEnabled() const
3D ray from pixel coordinates
-
mrpt::math::TLine3D get3DRayForPixelCoord(const mrpt::img::TPixelCoord &pixelCoord, const mrpt::img::TPixelCoord &viewportSize, mrpt::poses::CPose3D *out_cameraPose = nullptr) const
Computes the 3D ray passing through the viewport pixel (x,y), given explicit viewport dimensions.
See also
- Parameters:
pixelCoord – Pixel coordinates (0=left/top, increasing right/down)
viewportSize – Viewport size in pixels
out_cameraPose – If not nullptr, receives the camera SE(3) pose
- Returns:
The 3D ray in the world coordinates, starting at the camera, or nullopt if the viewport has not been rendered at least once (so that updateRenderedViewportSize() has been called by the rendering backend).
-
std::optional<mrpt::math::TLine3D> get3DRayForPixelCoord(const mrpt::img::TPixelCoord &pixelCoord, mrpt::poses::CPose3D *out_cameraPose = nullptr) const
-
inline std::pair<uint32_t, uint32_t> getLastRenderedViewportSize() const
Returns the size (width, height) of the viewport as rendered in the last frame. Returns {0,0} if not yet rendered.
-
inline void updateRenderedViewportSize(uint32_t w, uint32_t h) const
Called by the rendering backend to store the last rendered viewport size (in pixels).
Public Functions
-
~Viewport() override
Destructor: clears all objects.
Protected Functions
-
void dumpListOfObjects(std::vector<std::string> &lst) const
Retrieves a list of all objects in text form.
- Deprecated:
Prefer asYAML() (since MRPT 2.1.3)
-
mrpt::containers::yaml asYAML() const
Prints all viewport objects in human-readable YAML form.
Note
(New in MRPT 2.1.3)
-
void internal_enableImageView(bool transparentBackground)
Protected Attributes
-
bool m_isCloned = false
Set by setCloneView
-
bool m_isClonedCamera = false
Set by setCloneCamera
-
bool m_isViewportVisible = true
-
std::string m_clonedViewport
Only if m_isCloned=true
-
std::string m_clonedCameraViewport
If m_isClonedCamera && !m_isCloned, take the camera from another view, to render a different scene.
-
std::string m_name
The viewport’s name
-
bool m_isTransparent = false
Whether to clear color buffer.
-
uint32_t m_borderWidth = 0
Default=0, the border around the viewport.
-
mrpt::img::TColor m_borderColor = {255, 255, 255, 255}
-
double m_view_x = {0}
The viewport position [0,1]
-
double m_view_y = {0}
-
double m_view_width = {1}
-
double m_view_height = {1}
-
float m_clip_min = 0.01f
The min/max clip depth distances (default: 0.01 - 1000)
-
float m_clip_max = 1000.0f
-
float m_lightShadowClipMin = 0.01f
The near/far plane clip distances for unidirectional light shadow casting
-
float m_lightShadowClipMax = 1000.0f
-
mrpt::img::TColorf m_background_color = {0.4f, 0.4f, 0.4f}
-
mrpt::viz::CTexturedPlane::Ptr m_imageViewPlane
The image to display, after calling setImageView()
-
mutable mrpt::viz::CSetOfLines::Ptr m_borderLines
-
mrpt::viz::ListVisualObjects m_objects
-
bool m_OpenGL_enablePolygonNicest = {true}
-
TLightParameters m_light
-
bool m_shadowsEnabled = false
-
uint32_t m_ShadowMapSizeX = 2048
-
uint32_t m_ShadowMapSizeY = 2048
-
mutable uint32_t m_lastRenderedWidth = 0
Updated by the rendering backend via updateRenderedViewportSize()
-
uint32_t m_lastRenderedHeight = 0