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



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019