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



Page generated by Doxygen 1.9.1 for MRPT 1.9.9 Git: 63ea9d1f1 Thu Nov 23 00:06:53 2017 +0100 at mar 26 may 2026 12:19:29 CEST