00001 /* +---------------------------------------------------------------------------+ 00002 | The Mobile Robot Programming Toolkit (MRPT) C++ library | 00003 | | 00004 | http://mrpt.sourceforge.net/ | 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 */ 00066 class OPENGL_IMPEXP COpenGLScene : public mrpt::utils::CSerializable 00067 { 00068 DEFINE_SERIALIZABLE( COpenGLScene ) 00069 public: 00070 /** Constructor 00071 */ 00072 COpenGLScene(); 00073 00074 /** Destructor: 00075 */ 00076 virtual ~COpenGLScene(); 00077 00078 /** Copy operator: 00079 */ 00080 COpenGLScene & operator =( const COpenGLScene &obj ); 00081 00082 /** Copy constructor: 00083 */ 00084 COpenGLScene( const COpenGLScene &obj ); 00085 00086 /** 00087 * Inserts a set of objects into the scene, in the given viewport ("main" by default). Any iterable object will be accepted. 00088 * \sa createViewport,getViewport 00089 */ 00090 template<class T> inline void insertCollection(const T &objs,const std::string &vpn=std::string("main")) { 00091 insert(objs.begin(),objs.end(),vpn); 00092 } 00093 /** Insert a new object into the scene, in the given viewport (by default, into the "main" viewport). 00094 * The viewport must be created previously, an exception will be raised if the given name does not correspond to 00095 * an existing viewport. 00096 * \sa createViewport, getViewport 00097 */ 00098 void insert( const CRenderizablePtr &newObject, const std::string &viewportName=std::string("main")); 00099 00100 /** 00101 * Inserts a set of objects into the scene, in the given viewport ("main" by default). 00102 * \sa createViewport,getViewport 00103 */ 00104 template<class T_it> inline void insert(const T_it &begin,const T_it &end,const std::string &vpn=std::string("main")) { 00105 for (T_it it=begin;it!=end;it++) insert(*it,vpn); 00106 } 00107 00108 /**Creates a new viewport, adding it to the scene and returning a pointer to the new object. 00109 * 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. 00110 * The first, default viewport, is named "main". 00111 */ 00112 COpenGLViewportPtr createViewport( const std::string &viewportName ); 00113 00114 /** Returns the viewport with the given name, or NULL if it does not exist 00115 */ 00116 COpenGLViewportPtr getViewport( const std::string &viewportName ) const; 00117 00118 /** Render this scene. 00119 */ 00120 void render() const; 00121 00122 size_t viewportsCount() const { return m_viewports.size(); } 00123 00124 /** Clear the list of objects and viewports in the scene, deleting objects' memory, and leaving just the default viewport with the default values. 00125 */ 00126 void clear( bool createMainViewport = true ); 00127 00128 /** 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. 00129 * \sa followCamera 00130 */ 00131 void enableFollowCamera( bool enabled ) { m_followCamera = enabled; } 00132 00133 /** Return the value of "followCamera" 00134 * \sa enableFollowCamera 00135 */ 00136 bool followCamera() const { return m_followCamera; } 00137 00138 /** Returns the first object with a given name, or NULL (an empty smart pointer) if not found. 00139 */ 00140 CRenderizablePtr getByName( const std::string &str, const std::string &viewportName = std::string("main") ); 00141 00142 /** Returns the i'th object of a given class (or of a descendant class), or NULL (an empty smart pointer) if not found. 00143 * Example: 00144 * \code 00145 CSpherePtr obs = myscene.getByClass<CSphere>(); 00146 * \endcode 00147 * By default (ith=0), the first observation is returned. 00148 */ 00149 template <typename T> 00150 typename T::SmartPtr getByClass( const size_t &ith = 0 ) const 00151 { 00152 MRPT_START; 00153 for (TListViewports::const_iterator it = m_viewports.begin();it!=m_viewports.end();++it) 00154 { 00155 typename T::SmartPtr o = (*it)->getByClass<T>(ith); 00156 if (o.present()) return o; 00157 } 00158 return typename T::SmartPtr(); // Not found: return empty smart pointer 00159 MRPT_END; 00160 } 00161 00162 00163 /** Removes the given object from the scene (it also deletes the object to free its memory). 00164 */ 00165 void removeObject( const CRenderizablePtr &obj, const std::string &viewportName = std::string("main") ); 00166 00167 /** Initializes all textures in the scene (See opengl::CTexturedPlane::loadTextureInOpenGL) 00168 */ 00169 void initializeAllTextures(); 00170 00171 /** Retrieves a list of all objects in text form. 00172 */ 00173 void dumpListOfObjects( utils::CStringList &lst ); 00174 00175 /** Saves the scene to a 3Dscene file, loadable by the application SceneViewer3D 00176 * \sa loadFromFile 00177 * \return false on any error. 00178 */ 00179 bool saveToFile(const std::string &fil) const; 00180 00181 /** Loads the scene from a 3Dscene file, the format used by the application SceneViewer3D. 00182 * \sa saveToFile 00183 * \return false on any error. 00184 */ 00185 bool loadFromFile(const std::string &fil); 00186 00187 /** Traces a ray 00188 */ 00189 bool traceRay(const mrpt::poses::CPose3D&o,double &dist) const; 00190 00191 00192 /** Recursive depth-first visit all objects in all viewports of the scene, calling the user-supplied function 00193 * The passed function must accept only one argument of type "const mrpt::opengl::CRenderizablePtr &" 00194 */ 00195 template <typename FUNCTOR> 00196 void visitAllObjects( FUNCTOR functor) const 00197 { 00198 MRPT_START 00199 for (TListViewports::const_iterator it = m_viewports.begin();it!=m_viewports.end();++it) 00200 for (COpenGLViewport::const_iterator itO = (*it)->begin();itO!=(*it)->end();++itO) 00201 internal_visitAllObjects(functor, *itO); 00202 MRPT_END 00203 } 00204 00205 /** Recursive depth-first visit all objects in all viewports of the scene, calling the user-supplied function 00206 * The passed function must accept a first argument of type "const mrpt::opengl::CRenderizablePtr &" 00207 * and a second one of type EXTRA_PARAM 00208 */ 00209 template <typename FUNCTOR,typename EXTRA_PARAM> 00210 inline void visitAllObjects( FUNCTOR functor, const EXTRA_PARAM &userParam) const { 00211 visitAllObjects( std::bind2nd(functor,userParam) ); 00212 } 00213 00214 protected: 00215 bool m_followCamera; 00216 00217 typedef std::vector<COpenGLViewportPtr> TListViewports; 00218 00219 TListViewports m_viewports; //!< The list of viewports, indexed by name. 00220 00221 00222 template <typename FUNCTOR> 00223 static void internal_visitAllObjects(FUNCTOR functor, const CRenderizablePtr &o) 00224 { 00225 (*functor)(o); 00226 if (IS_CLASS(o,CSetOfObjects)) 00227 { 00228 CSetOfObjectsPtr obj = CSetOfObjectsPtr(o); 00229 for (CSetOfObjects::const_iterator it=obj->begin();it!=obj->end();++it) 00230 internal_visitAllObjects(functor,*it); 00231 } 00232 } 00233 00234 }; 00235 /** 00236 * Inserts an openGL object into a scene. Allows call chaining. 00237 * \sa mrpt::opengl::COpenGLScene::insert 00238 */ 00239 inline COpenGLScenePtr &operator<<(COpenGLScenePtr &s,const CRenderizablePtr &r) { 00240 s->insert(r); 00241 return s; 00242 } 00243 /** 00244 * Inserts any iterable collection of openGL objects into a scene, allowing call chaining. 00245 * \sa mrpt::opengl::COpenGLScene::insert 00246 */ 00247 template <class T> inline COpenGLScenePtr &operator<<(COpenGLScenePtr &s,const std::vector<T> &v) { 00248 s->insert(v.begin(),v.end()); 00249 return s; 00250 } 00251 } // end namespace 00252 00253 } // End of namespace 00254 00255 00256 #endif
Page generated by Doxygen 1.7.1 for MRPT 0.9.4 SVN: at Mon Jan 10 23:33:19 UTC 2011 |