MRPT  2.0.4
COpenGLScene.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
13 
14 /** The namespace for 3D scene representation and rendering. See also the <a
15  * href="mrpt-opengl.html" > summary page</a> of the mrpt-opengl library for
16  * more info and thumbnails of many of the render primitive.
17  */
18 namespace mrpt::opengl
19 {
20 /** This class allows the user to create, load, save, and render 3D scenes using
21  * OpenGL primitives.
22  * The class can be understood as a program to be run over OpenGL, containing
23  * a sequence of viewport definitions,
24  * rendering primitives, etc...
25  *
26  * It can contain from 1 up to any number of <b>Viewports</b>, each one
27  * associated a set of OpenGL objects and, optionally, a preferred camera
28  * position. Both orthogonal (2D/3D) and projection
29  * camera models can be used for each viewport independently, greatly
30  * increasing the possibilities of rendered scenes.
31  *
32  * An object of COpenGLScene always contains at least one viewport
33  * (utils::COpenGLViewport), named "main". Optionally, any
34  * number of other viewports may exist. Viewports are referenced by their
35  * names, case-sensitive strings. Each viewport contains
36  * a different 3D scene (i.e. they render different objects), though a
37  * mechanism exist to share the same 3D scene by a number of
38  * viewports so memory is not wasted replicating the same objects (see
39  * COpenGLViewport::setCloneView ).
40  *
41  * The main rendering method, COpenGLScene::render(), assumes a viewport has
42  * been set-up for the entire target window. That
43  * method will internally make the required calls to opengl for creating the
44  * additional viewports. Note that only the depth
45  * buffer is cleared by default for each (non-main) viewport, to allow
46  * transparencies. This can be disabled by the approppriate
47  * member in COpenGLViewport.
48  *
49  * An object COpenGLScene can be saved to a ".3Dscene" file using
50  * CFileOutputStream, for posterior visualization from
51  * the standalone application <a
52  * href="http://www.mrpt.org/Application:SceneViewer" >SceneViewer</a>.
53  * It can be also displayed in real-time using gui::CDisplayWindow3D.
54  * \ingroup mrpt_opengl_grp
55  */
57 {
59  public:
60  using TListViewports = std::vector<COpenGLViewport::Ptr>;
61 
62  COpenGLScene();
63  ~COpenGLScene() override;
64  COpenGLScene& operator=(const COpenGLScene& obj);
65  COpenGLScene(const COpenGLScene& obj);
66 
67  /**
68  * Inserts a set of objects into the scene, in the given viewport ("main"
69  * by default). Any iterable object will be accepted.
70  * \sa createViewport,getViewport
71  */
72  template <class T>
73  inline void insertCollection(
74  const T& objs, const std::string& vpn = std::string("main"))
75  {
76  insert(objs.begin(), objs.end(), vpn);
77  }
78  /** Insert a new object into the scene, in the given viewport (by default,
79  * into the "main" viewport).
80  * The viewport must be created previously, an exception will be raised if
81  * the given name does not correspond to
82  * an existing viewport.
83  * \sa createViewport, getViewport
84  */
85  void insert(
86  const CRenderizable::Ptr& newObject,
87  const std::string& viewportName = std::string("main"));
88 
89  /**
90  * Inserts a set of objects into the scene, in the given viewport ("main"
91  * by default).
92  * \sa createViewport,getViewport
93  */
94  template <class T_it>
95  inline void insert(
96  const T_it& begin, const T_it& end,
97  const std::string& vpn = std::string("main"))
98  {
99  for (T_it it = begin; it != end; it++) insert(*it, vpn);
100  }
101 
102  /**Creates a new viewport, adding it to the scene and returning a pointer to
103  * the new object.
104  * Names (case-sensitive) cannot be duplicated: if the name provided
105  * coincides with an already existing viewport, a pointer to the existing
106  * object will be returned.
107  * The first, default viewport, is named "main".
108  */
109  COpenGLViewport::Ptr createViewport(const std::string& viewportName);
110 
111  /** Returns the viewport with the given name, or nullptr if it does not
112  * exist; note that the default viewport is named "main" and initially
113  * occupies the entire rendering area.
114  */
116  const std::string& viewportName = std::string("main")) const;
117 
118  const TListViewports& viewports() const { return m_viewports; }
119 
120  /** Render this scene */
121  void render() const;
122 
123  size_t viewportsCount() const { return m_viewports.size(); }
124  /** Clear the list of objects and viewports in the scene, deleting objects'
125  * memory, and leaving just the default viewport with the default values.
126  */
127  void clear(bool createMainViewport = true);
128 
129  /** If disabled (default), the SceneViewer application will ignore the
130  * camera of the "main" viewport and keep the viewport selected by the user
131  * by hand; otherwise, the camera in the "main" viewport prevails.
132  * \sa followCamera
133  */
134  void enableFollowCamera(bool enabled) { m_followCamera = enabled; }
135  /** Return the value of "followCamera"
136  * \sa enableFollowCamera
137  */
138  bool followCamera() const { return m_followCamera; }
139  /** Returns the first object with a given name, or nullptr (an empty smart
140  * pointer) if not found.
141  */
143  const std::string& str,
144  const std::string& viewportName = std::string("main"));
145 
146  /** Returns the i'th object of a given class (or of a descendant class), or
147  nullptr (an empty smart pointer) if not found.
148  * Example:
149  * \code
150  CSphere::Ptr obs = myscene.getByClass<CSphere>();
151  * \endcode
152  * By default (ith=0), the first observation is returned.
153  */
154  template <typename T>
155  typename T::Ptr getByClass(size_t ith = 0) const
156  {
157  MRPT_START
158  for (const auto& m_viewport : m_viewports)
159  {
160  typename T::Ptr o = m_viewport->getByClass<T>(ith);
161  if (o) return o;
162  }
163  return typename T::Ptr(); // Not found: return empty smart pointer
164  MRPT_END
165  }
166 
167  /** Removes the given object from the scene (it also deletes the object to
168  * free its memory).
169  */
170  void removeObject(
171  const CRenderizable::Ptr& obj,
172  const std::string& viewportName = std::string("main"));
173 
174  /** Initializes all textures in the scene (See
175  * opengl::CTexturedPlane::initializeTextures)
176  */
177  void initializeTextures();
178 
179  /** Retrieves a list of all objects in text form.
180  */
181  void dumpListOfObjects(std::vector<std::string>& lst);
182 
183  /** Saves the scene to a 3Dscene file, loadable by the application
184  * SceneViewer3D
185  * \sa loadFromFile
186  * \return false on any error.
187  */
188  bool saveToFile(const std::string& fil) const;
189 
190  /** Loads the scene from a 3Dscene file, the format used by the application
191  * SceneViewer3D.
192  * \sa saveToFile
193  * \return false on any error.
194  */
195  bool loadFromFile(const std::string& fil);
196 
197  /** Traces a ray
198  */
199  bool traceRay(const mrpt::poses::CPose3D& o, double& dist) const;
200 
201  /** Evaluates the bounding box of the scene in the given viewport (default:
202  * "main"). */
203  void getBoundingBox(
205  const std::string& vpn = std::string("main")) const;
206 
207  /** Recursive depth-first visit all objects in all viewports of the scene,
208  * calling the user-supplied function
209  * The passed function must accept only one argument of type "const
210  * mrpt::opengl::CRenderizable::Ptr &"
211  */
212  template <typename FUNCTOR>
213  void visitAllObjects(FUNCTOR functor) const
214  {
215  MRPT_START
216  for (const auto& m_viewport : m_viewports)
217  for (auto itO = m_viewport->begin(); itO != m_viewport->end();
218  ++itO)
219  internal_visitAllObjects(functor, *itO);
220  MRPT_END
221  }
222 
223  /** Ensure all shaders are unloaded in all viewports */
224  void unloadShaders();
225 
226  /** Ensure all OpenGL buffers are destroyed. */
227  void freeOpenGLResources();
228 
229  protected:
230  bool m_followCamera{false};
231 
232  /** The list of viewports, indexed by name. */
234 
235  template <typename FUNCTOR>
237  FUNCTOR functor, const CRenderizable::Ptr& o)
238  {
239  functor(o);
240  if (IS_CLASS(*o, CSetOfObjects))
241  {
242  CSetOfObjects::Ptr obj =
243  std::dynamic_pointer_cast<CSetOfObjects>(o);
244  for (auto it = obj->begin(); it != obj->end(); ++it)
245  internal_visitAllObjects(functor, *it);
246  }
247  }
248 };
249 
250 /** Inserts an openGL object into a scene. Allows call chaining. \sa
251  * mrpt::opengl::COpenGLScene::insert */
254 {
255  s->insert(r);
256  return s;
257 }
258 /**Inserts any iterable collection of openGL objects into a scene, allowing call
259  * chaining. \sa mrpt::opengl::COpenGLScene::insert */
260 template <class T>
262  COpenGLScene::Ptr& s, const std::vector<T>& v)
263 {
264  s->insert(v.begin(), v.end());
265  return s;
266 }
267 } // namespace mrpt::opengl
#define MRPT_START
Definition: exceptions.h:241
void freeOpenGLResources()
Ensure all OpenGL buffers are destroyed.
A set of object, which are referenced to the coordinates framework established in this object...
Definition: CSetOfObjects.h:26
void visitAllObjects(FUNCTOR functor) const
Recursive depth-first visit all objects in all viewports of the scene, calling the user-supplied func...
Definition: COpenGLScene.h:213
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).
Definition: COpenGLScene.h:73
COpenGLScene::Ptr & operator<<(COpenGLScene::Ptr &s, const CRenderizable::Ptr &r)
Inserts an openGL object into a scene.
Definition: COpenGLScene.h:252
bool traceRay(const mrpt::poses::CPose3D &o, double &dist) const
Traces a ray.
void getBoundingBox(mrpt::math::TPoint3D &bb_min, mrpt::math::TPoint3D &bb_max, const std::string &vpn=std::string("main")) const
Evaluates the bounding box of the scene in the given viewport (default: "main").
void initializeTextures()
Initializes all textures in the scene (See opengl::CTexturedPlane::initializeTextures) ...
const TListViewports & viewports() const
Definition: COpenGLScene.h:118
void enableFollowCamera(bool enabled)
If disabled (default), the SceneViewer application will ignore the camera of the "main" viewport and ...
Definition: COpenGLScene.h:134
void dumpListOfObjects(std::vector< std::string > &lst)
Retrieves a list of all objects in text form.
COpenGLViewport::Ptr createViewport(const std::string &viewportName)
Creates a new viewport, adding it to the scene and returning a pointer to the new object...
#define IS_CLASS(obj, class_name)
True if the given reference to object (derived from mrpt::rtti::CObject) is of the given class...
Definition: CObject.h:146
void clear(bool createMainViewport=true)
Clear the list of objects and viewports in the scene, deleting objects&#39; memory, and leaving just the ...
size_t viewportsCount() const
Definition: COpenGLScene.h:123
bool saveToFile(const std::string &fil) const
Saves the scene to a 3Dscene file, loadable by the application SceneViewer3D.
TListViewports m_viewports
The list of viewports, indexed by name.
Definition: COpenGLScene.h:233
static void internal_visitAllObjects(FUNCTOR functor, const CRenderizable::Ptr &o)
Definition: COpenGLScene.h:236
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).
Definition: COpenGLScene.h:95
const_iterator end() const
Definition: ts_hash_map.h:246
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 view...
const_iterator begin() const
Definition: ts_hash_map.h:240
bool loadFromFile(const std::string &fil)
Loads the scene from a 3Dscene file, the format used by the application SceneViewer3D.
COpenGLScene & operator=(const COpenGLScene &obj)
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:85
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...
T::Ptr getByClass(size_t ith=0) const
Returns the i&#39;th object of a given class (or of a descendant class), or nullptr (an empty smart point...
Definition: COpenGLScene.h:155
void render() const
Render this scene.
#define MRPT_END
Definition: exceptions.h:245
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:13
This class allows the user to create, load, save, and render 3D scenes using OpenGL primitives...
Definition: COpenGLScene.h:56
const auto bb_max
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:30
bool followCamera() const
Return the value of "followCamera".
Definition: COpenGLScene.h:138
#define DEFINE_SERIALIZABLE(class_name, NS)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
const auto bb_min
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)...
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 unloadShaders()
Ensure all shaders are unloaded in all viewports.
std::vector< COpenGLViewport::Ptr > TListViewports
Definition: COpenGLScene.h:60



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020