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



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020