class mrpt::opengl::COpenGLScene¶
This class allows the user to create, load, save, and render 3D scenes using OpenGL primitives.
The class can be understood as a program to be run over OpenGL, containing a sequence of viewport definitions, rendering primitives, etc.
It can contain from 1 up to any number of Viewports, each one associated a set of OpenGL objects and, optionally, a preferred camera position. Both orthogonal (2D/3D) and projection camera models can be used for each viewport independently, greatly increasing the possibilities of rendered scenes.
An object of COpenGLScene always contains at least one viewport (utils::COpenGLViewport), named “main”. Optionally, any number of other viewports may exist. Viewports are referenced by their names, case-sensitive strings. Each viewport contains a different 3D scene (i.e. they render different objects), though a mechanism exist to share the same 3D scene by a number of viewports so memory is not wasted replicating the same objects (see COpenGLViewport::setCloneView).
The main rendering method, COpenGLScene::render(), assumes a viewport has been set-up for the entire target window. That method will internally make the required calls to opengl for creating the additional viewports. Note that only the depth buffer is cleared by default for each (non-main) viewport, to allow transparencies. This can be disabled by the approppriate member in COpenGLViewport.
An object COpenGLScene can be saved to a “.3Dscene” file using CFileOutputStream or with the direct method COpenGLScene::saveToFile() for posterior visualization from the standalone application Application: SceneViewer3D.
It can be also displayed in real-time using windows in mrpt::gui or serialized over a network socket, etc.
#include <mrpt/opengl/COpenGLScene.h> class COpenGLScene: public mrpt::serialization::CSerializable { public: // typedefs typedef std::vector<COpenGLViewport::Ptr> TListViewports; // construction COpenGLScene(); COpenGLScene(const COpenGLScene& obj); // methods COpenGLScene& operator = (const COpenGLScene& obj); template <class T> void insertCollection( const T& objs, const std::string& vpn = std::string("main") ); void insert(const CRenderizable::Ptr& newObject, const std::string& viewportName = std::string("main")); template <class T_it> void insert( const T_it& begin, const T_it& end, const std::string& vpn = std::string("main") ); COpenGLViewport::Ptr createViewport(const std::string& viewportName); COpenGLViewport::Ptr getViewport(const std::string& viewportName = std::string("main")) const; const TListViewports& viewports() const; void render() const; size_t viewportsCount() const; void clear(bool createMainViewport = true); void enableFollowCamera(bool enabled); bool followCamera() const; CRenderizable::Ptr getByName(const std::string& str, const std::string& viewportName = std::string("main")); template <typename T> T::Ptr getByClass(size_t ith = 0) const; void removeObject(const CRenderizable::Ptr& obj, const std::string& viewportName = std::string("main")); void initializeTextures(); void dumpListOfObjects(std::vector<std::string>& lst) const; mrpt::containers::yaml asYAML() const; bool saveToFile(const std::string& fil) const; bool loadFromFile(const std::string& fil); bool traceRay(const mrpt::poses::CPose3D& o, double& dist) const; mrpt::math::TBoundingBox getBoundingBox(const std::string& vpn = std::string("main")) const; template <typename FUNCTOR> void visitAllObjects(FUNCTOR functor) const; void unloadShaders(); void freeOpenGLResources(); };
Methods¶
template <class T> void insertCollection( const T& objs, const std::string& vpn = std::string("main") )
Inserts a set of objects into the scene, in the given viewport (“main” by default).
Any iterable object will be accepted.
See also:
void insert( const CRenderizable::Ptr& newObject, const std::string& viewportName = std::string("main") )
Insert a new object into the scene, in the given viewport (by default, into the “main” viewport).
The viewport must be created previously, an exception will be raised if the given name does not correspond to an existing viewport.
See also:
template <class T_it> void insert( const T_it& begin, const T_it& end, const std::string& vpn = std::string("main") )
Inserts a set of objects into the scene, in the given viewport (“main” by default).
See also:
COpenGLViewport::Ptr createViewport(const std::string& viewportName)
Creates a new viewport, adding it to the scene and returning a pointer to the new object.
Names (case-sensitive) cannot be duplicated: if the name provided coincides with an already existing viewport, a pointer to the existing object will be returned. The first, default viewport, is named “main”.
COpenGLViewport::Ptr getViewport(const std::string& viewportName = std::string("main")) const
Returns the viewport with the given name, or nullptr if it does not exist; note that the default viewport is named “main” and initially occupies the entire rendering area.
void render() const
Render this scene.
void clear(bool createMainViewport = true)
Clear the list of objects and viewports in the scene, deleting objects’ memory, and leaving just the default viewport with the default values.
void enableFollowCamera(bool enabled)
If disabled (default), the SceneViewer application will ignore the camera of the “main” viewport and keep the viewport selected by the user by hand; otherwise, the camera in the “main” viewport prevails.
See also:
bool followCamera() const
Return the value of “followCamera”.
See also:
CRenderizable::Ptr getByName( const std::string& str, const std::string& viewportName = std::string("main") )
Returns the first object with a given name, or nullptr (an empty smart pointer) 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 = myscene.getByClass<CSphere>();
By default (ith=0), the first observation is returned.
void removeObject( const CRenderizable::Ptr& obj, const std::string& viewportName = std::string("main") )
Removes the given object from the scene (it also deletes the object to free its memory).
void initializeTextures()
Initializes all textures in the scene (See opengl::CTexturedPlane::initializeTextures)
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 viewports and objects in human-readable YAML form.
Note that not all objects data is serialized, so this method is not suitable for deserialization (for that, use saveToFile(), loadFromFile() instead). (New in MRPT 2.1.3)
bool saveToFile(const std::string& fil) const
Saves the scene to a “.3Dscene” file, loadable by: Application: SceneViewer3D.
Returns:
false on any error.
See also:
bool loadFromFile(const std::string& fil)
Loads the scene from a “.3Dscene” file.
Returns:
false on any error.
See also:
bool traceRay(const mrpt::poses::CPose3D& o, double& dist) const
Traces a ray.
mrpt::math::TBoundingBox getBoundingBox(const std::string& vpn = std::string("main")) const
Evaluates the bounding box of the scene in the given viewport (default: “main”).
Evaluates the bounding box of this object (including possible children) in the coordinate frame of the object parent.
template <typename FUNCTOR> void visitAllObjects(FUNCTOR functor) const
Recursive depth-first visit all objects in all viewports of the scene, calling the user-supplied function The passed function must accept only one argument of type “const mrpt::opengl::CRenderizable::Ptr &”.
void unloadShaders()
Ensure all shaders are unloaded in all viewports.
void freeOpenGLResources()
Ensure all OpenGL buffers are destroyed.