Main MRPT website > C++ reference
MRPT logo
COpenGLScene.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) C++ library |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (C) 2005-2012 University of Malaga |
7  | |
8  | This software was written by the Machine Perception and Intelligent |
9  | Robotics Lab, University of Malaga (Spain). |
10  | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11  | |
12  | This file is part of the MRPT project. |
13  | |
14  | MRPT is free software: you can redistribute it and/or modify |
15  | it under the terms of the GNU General Public License as published by |
16  | the Free Software Foundation, either version 3 of the License, or |
17  | (at your option) any later version. |
18  | |
19  | MRPT is distributed in the hope that it will be useful, |
20  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22  | GNU General Public License for more details. |
23  | |
24  | You should have received a copy of the GNU General Public License |
25  | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26  | |
27  +---------------------------------------------------------------------------+ */
28 #ifndef opengl_COpenGLScene_H
29 #define opengl_COpenGLScene_H
30 
33 
34 namespace mrpt
35 {
36  /** The namespace for 3D scene representation and rendering. See also the <a href="mrpt-opengl.html" > summary page</a> of the mrpt-opengl library for more info and thumbnails of many of the render primitive.
37  */
38  namespace opengl
39  {
40  // This must be added to any CSerializable derived class:
42 
43 
44  /** This class allows the user to create, load, save, and render 3D scenes using OpenGL primitives.
45  * The class can be understood as a program to be run over OpenGL, containing a sequence of viewport definitions,
46  * rendering primitives, etc...
47  *
48  * It can contain from 1 up to any number of <b>Viewports</b>, each one
49  * associated a set of OpenGL objects and, optionally, a preferred camera position. Both orthogonal (2D/3D) and projection
50  * camera models can be used for each viewport independently, greatly increasing the possibilities of rendered scenes.
51  *
52  * An object of COpenGLScene always contains at least one viewport (utils::COpenGLViewport), named "main". Optionally, any
53  * number of other viewports may exist. Viewports are referenced by their names, case-sensitive strings. Each viewport contains
54  * a different 3D scene (i.e. they render different objects), though a mechanism exist to share the same 3D scene by a number of
55  * viewports so memory is not wasted replicating the same objects (see COpenGLViewport::setCloneView ).
56  *
57  * The main rendering method, COpenGLScene::render(), assumes a viewport has been set-up for the entire target window. That
58  * method will internally make the required calls to opengl for creating the additional viewports. Note that only the depth
59  * buffer is cleared by default for each (non-main) viewport, to allow transparencies. This can be disabled by the approppriate
60  * member in COpenGLViewport.
61  *
62  * An object COpenGLScene can be saved to a ".3Dscene" file using CFileOutputStream, for posterior visualization from
63  * the standalone application <a href="http://www.mrpt.org/Application:SceneViewer" >SceneViewer</a>.
64  * It can be also displayed in real-time using gui::CDisplayWindow3D.
65  * \ingroup mrpt_opengl_grp
66  */
67  class OPENGL_IMPEXP COpenGLScene : public mrpt::utils::CSerializable
68  {
70  public:
71  /** Constructor
72  */
73  COpenGLScene();
74 
75  /** Destructor:
76  */
77  virtual ~COpenGLScene();
78 
79  /** Copy operator:
80  */
81  COpenGLScene & operator =( const COpenGLScene &obj );
82 
83  /** Copy constructor:
84  */
85  COpenGLScene( const COpenGLScene &obj );
86 
87  /**
88  * Inserts a set of objects into the scene, in the given viewport ("main" by default). Any iterable object will be accepted.
89  * \sa createViewport,getViewport
90  */
91  template<class T> inline void insertCollection(const T &objs,const std::string &vpn=std::string("main")) {
92  insert(objs.begin(),objs.end(),vpn);
93  }
94  /** Insert a new object into the scene, in the given viewport (by default, into the "main" viewport).
95  * The viewport must be created previously, an exception will be raised if the given name does not correspond to
96  * an existing viewport.
97  * \sa createViewport, getViewport
98  */
99  void insert( const CRenderizablePtr &newObject, const std::string &viewportName=std::string("main"));
100 
101  /**
102  * Inserts a set of objects into the scene, in the given viewport ("main" by default).
103  * \sa createViewport,getViewport
104  */
105  template<class T_it> inline void insert(const T_it &begin,const T_it &end,const std::string &vpn=std::string("main")) {
106  for (T_it it=begin;it!=end;it++) insert(*it,vpn);
107  }
108 
109  /**Creates a new viewport, adding it to the scene and returning a pointer to the new object.
110  * 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.
111  * The first, default viewport, is named "main".
112  */
113  COpenGLViewportPtr createViewport( const std::string &viewportName );
114 
115  /** Returns the viewport with the given name, or NULL if it does not exist; note that the default viewport is named "main" and initially occupies the entire rendering area.
116  */
117  COpenGLViewportPtr getViewport( const std::string &viewportName = std::string("main") ) const;
118 
119  /** Render this scene.
120  */
121  void render() const;
122 
123  size_t viewportsCount() const { return m_viewports.size(); }
124 
125  /** Clear the list of objects and viewports in the scene, deleting objects' 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 camera of the "main" viewport and keep the viewport selected by the user by hand; otherwise, the camera in the "main" viewport prevails.
130  * \sa followCamera
131  */
132  void enableFollowCamera( bool enabled ) { m_followCamera = enabled; }
133 
134  /** Return the value of "followCamera"
135  * \sa enableFollowCamera
136  */
137  bool followCamera() const { return m_followCamera; }
138 
139  /** Returns the first object with a given name, or NULL (an empty smart pointer) if not found.
140  */
141  CRenderizablePtr getByName( const std::string &str, const std::string &viewportName = std::string("main") );
142 
143  /** Returns the i'th object of a given class (or of a descendant class), or NULL (an empty smart pointer) if not found.
144  * Example:
145  * \code
146  CSpherePtr obs = myscene.getByClass<CSphere>();
147  * \endcode
148  * By default (ith=0), the first observation is returned.
149  */
150  template <typename T>
151  typename T::SmartPtr getByClass( const size_t &ith = 0 ) const
152  {
153  MRPT_START
154  for (TListViewports::const_iterator it = m_viewports.begin();it!=m_viewports.end();++it)
155  {
156  typename T::SmartPtr o = (*it)->getByClass<T>(ith);
157  if (o.present()) return o;
158  }
159  return typename T::SmartPtr(); // Not found: return empty smart pointer
160  MRPT_END
161  }
162 
163 
164  /** Removes the given object from the scene (it also deletes the object to free its memory).
165  */
166  void removeObject( const CRenderizablePtr &obj, const std::string &viewportName = std::string("main") );
167 
168  /** Initializes all textures in the scene (See opengl::CTexturedPlane::loadTextureInOpenGL)
169  */
170  void initializeAllTextures();
171 
172  /** Retrieves a list of all objects in text form.
173  */
174  void dumpListOfObjects( utils::CStringList &lst );
175 
176  /** Saves the scene to a 3Dscene file, loadable by the application SceneViewer3D
177  * \sa loadFromFile
178  * \return false on any error.
179  */
180  bool saveToFile(const std::string &fil) const;
181 
182  /** Loads the scene from a 3Dscene file, the format used by the application SceneViewer3D.
183  * \sa saveToFile
184  * \return false on any error.
185  */
186  bool loadFromFile(const std::string &fil);
187 
188  /** Traces a ray
189  */
190  bool traceRay(const mrpt::poses::CPose3D&o,double &dist) const;
191 
192 
193  /** Recursive depth-first visit all objects in all viewports of the scene, calling the user-supplied function
194  * The passed function must accept only one argument of type "const mrpt::opengl::CRenderizablePtr &"
195  */
196  template <typename FUNCTOR>
197  void visitAllObjects( FUNCTOR functor) const
198  {
199  MRPT_START
200  for (TListViewports::const_iterator it = m_viewports.begin();it!=m_viewports.end();++it)
201  for (COpenGLViewport::const_iterator itO = (*it)->begin();itO!=(*it)->end();++itO)
202  internal_visitAllObjects(functor, *itO);
203  MRPT_END
204  }
205 
206  /** Recursive depth-first visit all objects in all viewports of the scene, calling the user-supplied function
207  * The passed function must accept a first argument of type "const mrpt::opengl::CRenderizablePtr &"
208  * and a second one of type EXTRA_PARAM
209  */
210  template <typename FUNCTOR,typename EXTRA_PARAM>
211  inline void visitAllObjects( FUNCTOR functor, const EXTRA_PARAM &userParam) const {
212  visitAllObjects( std::bind2nd(functor,userParam) );
213  }
214 
215  protected:
217 
218  typedef std::vector<COpenGLViewportPtr> TListViewports;
219 
220  TListViewports m_viewports; //!< The list of viewports, indexed by name.
221 
222 
223  template <typename FUNCTOR>
224  static void internal_visitAllObjects(FUNCTOR functor, const CRenderizablePtr &o)
225  {
226  functor(o);
227  if (IS_CLASS(o,CSetOfObjects))
228  {
230  for (CSetOfObjects::const_iterator it=obj->begin();it!=obj->end();++it)
231  internal_visitAllObjects(functor,*it);
232  }
233  }
234 
235  };
236  /**
237  * Inserts an openGL object into a scene. Allows call chaining.
238  * \sa mrpt::opengl::COpenGLScene::insert
239  */
241  s->insert(r);
242  return s;
243  }
244  /**
245  * Inserts any iterable collection of openGL objects into a scene, allowing call chaining.
246  * \sa mrpt::opengl::COpenGLScene::insert
247  */
248  template <class T> inline COpenGLScenePtr &operator<<(COpenGLScenePtr &s,const std::vector<T> &v) {
249  s->insert(v.begin(),v.end());
250  return s;
251  }
252  } // end namespace
253 
254 } // End of namespace
255 
256 
257 #endif



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013