Main MRPT website > C++ reference
MRPT logo
COpenGLScene.h
Go to the documentation of this file.
00001 /* +---------------------------------------------------------------------------+
00002    |          The Mobile Robot Programming Toolkit (MRPT) C++ library          |
00003    |                                                                           |
00004    |                       http://www.mrpt.org/                                |
00005    |                                                                           |
00006    |   Copyright (C) 2005-2011  University of Malaga                           |
00007    |                                                                           |
00008    |    This software was written by the Machine Perception and Intelligent    |
00009    |      Robotics Lab, University of Malaga (Spain).                          |
00010    |    Contact: Jose-Luis Blanco  <jlblanco@ctima.uma.es>                     |
00011    |                                                                           |
00012    |  This file is part of the MRPT project.                                   |
00013    |                                                                           |
00014    |     MRPT is free software: you can redistribute it and/or modify          |
00015    |     it under the terms of the GNU General Public License as published by  |
00016    |     the Free Software Foundation, either version 3 of the License, or     |
00017    |     (at your option) any later version.                                   |
00018    |                                                                           |
00019    |   MRPT is distributed in the hope that it will be useful,                 |
00020    |     but WITHOUT ANY WARRANTY; without even the implied warranty of        |
00021    |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         |
00022    |     GNU General Public License for more details.                          |
00023    |                                                                           |
00024    |     You should have received a copy of the GNU General Public License     |
00025    |     along with MRPT.  If not, see <http://www.gnu.org/licenses/>.         |
00026    |                                                                           |
00027    +---------------------------------------------------------------------------+ */
00028 #ifndef opengl_COpenGLScene_H
00029 #define opengl_COpenGLScene_H
00030 
00031 #include <mrpt/opengl/CRenderizable.h>
00032 #include <mrpt/opengl/COpenGLViewport.h>
00033 
00034 namespace mrpt
00035 {
00036         /** 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.
00037           */
00038         namespace opengl
00039         {
00040                 // This must be added to any CSerializable derived class:
00041                 DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE_LINKAGE( COpenGLScene, mrpt::utils::CSerializable, OPENGL_IMPEXP )
00042 
00043 
00044                 /** This class allows the user to create, load, save, and render 3D scenes using OpenGL primitives.
00045                   *  The class can be understood as a program to be run over OpenGL, containing a sequence of viewport definitions,
00046                   *   rendering primitives, etc...
00047                   *
00048                   *  It can contain from 1 up to any number of <b>Viewports</b>, each one
00049                   *   associated a set of OpenGL objects and, optionally, a preferred camera position. Both orthogonal (2D/3D) and projection
00050                   *   camera models can be used for each viewport independently, greatly increasing the possibilities of rendered scenes.
00051                   *
00052                   *  An object of COpenGLScene always contains at least one viewport (utils::COpenGLViewport), named "main". Optionally, any
00053                   *   number of other viewports may exist. Viewports are referenced by their names, case-sensitive strings. Each viewport contains
00054                   *   a different 3D scene (i.e. they render different objects), though a mechanism exist to share the same 3D scene by a number of
00055                   *   viewports so memory is not wasted replicating the same objects (see COpenGLViewport::setCloneView ).
00056                   *
00057                   *  The main rendering method, COpenGLScene::render(), assumes a viewport has been set-up for the entire target window. That
00058                   *   method will internally make the required calls to opengl for creating the additional viewports. Note that only the depth
00059                   *   buffer is cleared by default for each (non-main) viewport, to allow transparencies. This can be disabled by the approppriate
00060                   *   member in COpenGLViewport.
00061                   *
00062                   *   An object COpenGLScene can be saved to a ".3Dscene" file using CFileOutputStream, for posterior visualization from
00063                   *    the standalone application <a href="http://www.mrpt.org/Application:SceneViewer" >SceneViewer</a>.
00064                   *    It can be also displayed in real-time using gui::CDisplayWindow3D.
00065                   * \ingroup mrpt_opengl_grp
00066                   */
00067                 class OPENGL_IMPEXP COpenGLScene : public mrpt::utils::CSerializable
00068                 {
00069                         DEFINE_SERIALIZABLE( COpenGLScene )
00070                 public:
00071                         /** Constructor
00072                           */
00073                         COpenGLScene();
00074 
00075                         /** Destructor:
00076                          */
00077                         virtual ~COpenGLScene();
00078 
00079                         /** Copy operator:
00080                           */
00081                         COpenGLScene & operator =( const COpenGLScene &obj );
00082 
00083                         /** Copy constructor:
00084                           */
00085                         COpenGLScene( const COpenGLScene &obj );
00086 
00087                         /**
00088                           * Inserts a set of objects into the scene, in the given viewport ("main" by default). Any iterable object will be accepted.
00089                           * \sa createViewport,getViewport
00090                           */
00091                         template<class T> inline void insertCollection(const T &objs,const std::string &vpn=std::string("main"))        {
00092                                 insert(objs.begin(),objs.end(),vpn);
00093                         }
00094                         /** Insert a new object into the scene, in the given viewport (by default, into the "main" viewport).
00095                           *  The viewport must be created previously, an exception will be raised if the given name does not correspond to
00096                           *   an existing viewport.
00097                           * \sa createViewport, getViewport
00098                           */
00099                         void insert( const CRenderizablePtr &newObject, const std::string &viewportName=std::string("main"));
00100 
00101                         /**
00102                           * Inserts a set of objects into the scene, in the given viewport ("main" by default).
00103                           * \sa createViewport,getViewport
00104                           */
00105                         template<class T_it> inline void insert(const T_it &begin,const T_it &end,const std::string &vpn=std::string("main"))   {
00106                                 for (T_it it=begin;it!=end;it++) insert(*it,vpn);
00107                         }
00108 
00109                         /**Creates a new viewport, adding it to the scene and returning a pointer to the new object.
00110                           *  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.
00111                           *  The first, default viewport, is named "main".
00112                           */
00113                         COpenGLViewportPtr createViewport( const std::string &viewportName );
00114 
00115                         /** Returns the viewport with the given name, or NULL if it does not exist
00116                           */
00117                         COpenGLViewportPtr getViewport( const std::string &viewportName ) const;
00118 
00119                         /** Render this scene.
00120                           */
00121                         void  render() const;
00122 
00123                         size_t  viewportsCount() const { return m_viewports.size(); }
00124 
00125                         /** Clear the list of objects and viewports in the scene, deleting objects' memory, and leaving just the default viewport with the default values.
00126                           */
00127                         void  clear( bool createMainViewport = true );
00128 
00129                         /** 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.
00130                           * \sa followCamera
00131                           */
00132                         void enableFollowCamera( bool enabled ) { m_followCamera = enabled; }
00133 
00134                         /** Return the value of "followCamera"
00135                           * \sa enableFollowCamera
00136                           */
00137                         bool followCamera() const { return m_followCamera; }
00138 
00139                         /** Returns the first object with a given name, or NULL (an empty smart pointer) if not found.
00140                           */
00141                         CRenderizablePtr        getByName( const std::string &str, const std::string &viewportName = std::string("main") );
00142 
00143                          /** Returns the i'th object of a given class (or of a descendant class), or NULL (an empty smart pointer) if not found.
00144                            *  Example:
00145                            * \code
00146                                         CSpherePtr obs = myscene.getByClass<CSphere>();
00147                            * \endcode
00148                            * By default (ith=0), the first observation is returned.
00149                            */
00150                          template <typename T>
00151                          typename T::SmartPtr getByClass( const size_t &ith = 0 ) const
00152                          {
00153                                 MRPT_START
00154                                 for (TListViewports::const_iterator it = m_viewports.begin();it!=m_viewports.end();++it)
00155                                 {
00156                                         typename T::SmartPtr o = (*it)->getByClass<T>(ith);
00157                                         if (o.present()) return o;
00158                                 }
00159                                 return typename T::SmartPtr();  // Not found: return empty smart pointer
00160                                 MRPT_END
00161                          }
00162 
00163 
00164                         /** Removes the given object from the scene (it also deletes the object to free its memory).
00165                           */
00166                         void removeObject( const CRenderizablePtr &obj, const std::string &viewportName = std::string("main") );
00167 
00168                         /** Initializes all textures in the scene (See opengl::CTexturedPlane::loadTextureInOpenGL)
00169                           */
00170                         void  initializeAllTextures();
00171 
00172                         /** Retrieves a list of all objects in text form.
00173                           */
00174                         void dumpListOfObjects( utils::CStringList  &lst );
00175 
00176                         /** Saves the scene to a 3Dscene file, loadable by the application SceneViewer3D
00177                           * \sa loadFromFile
00178                           * \return false on any error.
00179                           */
00180                         bool saveToFile(const std::string &fil) const;
00181 
00182                         /** Loads the scene from a 3Dscene file, the format used by the application SceneViewer3D.
00183                           * \sa saveToFile
00184                           * \return false on any error.
00185                           */
00186                         bool loadFromFile(const std::string &fil);
00187 
00188                         /** Traces a ray
00189                           */
00190                         bool traceRay(const mrpt::poses::CPose3D&o,double &dist) const;
00191 
00192 
00193                         /** Recursive depth-first visit all objects in all viewports of the scene, calling the user-supplied function
00194                           *  The passed function must accept only one argument of type "const mrpt::opengl::CRenderizablePtr &"
00195                           */
00196                         template <typename FUNCTOR>
00197                         void visitAllObjects( FUNCTOR functor) const
00198                         {
00199                                 MRPT_START
00200                                 for (TListViewports::const_iterator it = m_viewports.begin();it!=m_viewports.end();++it)
00201                                         for (COpenGLViewport::const_iterator itO = (*it)->begin();itO!=(*it)->end();++itO)
00202                                                 internal_visitAllObjects(functor, *itO);
00203                                 MRPT_END
00204                         }
00205 
00206                         /** Recursive depth-first visit all objects in all viewports of the scene, calling the user-supplied function
00207                           *  The passed function must accept a first argument of type "const mrpt::opengl::CRenderizablePtr &"
00208                           *  and a second one of type EXTRA_PARAM
00209                           */
00210                         template <typename FUNCTOR,typename EXTRA_PARAM>
00211                         inline void visitAllObjects( FUNCTOR functor, const EXTRA_PARAM &userParam) const {
00212                                 visitAllObjects( std::bind2nd(functor,userParam) );
00213                         }
00214 
00215                 protected:
00216                         bool            m_followCamera;
00217 
00218                         typedef std::vector<COpenGLViewportPtr> TListViewports;
00219 
00220                         TListViewports          m_viewports;    //!< The list of viewports, indexed by name.
00221 
00222 
00223                         template <typename FUNCTOR>
00224                         static void internal_visitAllObjects(FUNCTOR functor, const CRenderizablePtr &o)
00225                         {
00226                                 functor(o);
00227                                 if (IS_CLASS(o,CSetOfObjects))
00228                                 {
00229                                         CSetOfObjectsPtr obj = CSetOfObjectsPtr(o);
00230                                         for (CSetOfObjects::const_iterator it=obj->begin();it!=obj->end();++it)
00231                                                 internal_visitAllObjects(functor,*it);
00232                                 }
00233                         }
00234 
00235                 };
00236                 /**
00237                   * Inserts an openGL object into a scene. Allows call chaining.
00238                   * \sa mrpt::opengl::COpenGLScene::insert
00239                   */
00240                 inline COpenGLScenePtr &operator<<(COpenGLScenePtr &s,const CRenderizablePtr &r)        {
00241                         s->insert(r);
00242                         return s;
00243                 }
00244                 /**
00245                   * Inserts any iterable collection of openGL objects into a scene, allowing call chaining.
00246                   * \sa mrpt::opengl::COpenGLScene::insert
00247                   */
00248                 template <class T> inline COpenGLScenePtr &operator<<(COpenGLScenePtr &s,const std::vector<T> &v)       {
00249                         s->insert(v.begin(),v.end());
00250                         return s;
00251                 }
00252         } // end namespace
00253 
00254 } // End of namespace
00255 
00256 
00257 #endif



Page generated by Doxygen 1.7.5 for MRPT 0.9.5 SVN: at Thu Oct 13 21:25:36 UTC 2011