00001 /* +---------------------------------------------------------------------------+ 00002 | The Mobile Robot Programming Toolkit (MRPT) C++ library | 00003 | | 00004 | http://mrpt.sourceforge.net/ | 00005 | | 00006 | Copyright (C) 2005-2009 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. 00037 */ 00038 namespace opengl 00039 { 00040 // This must be added to any CSerializable derived class: 00041 DEFINE_SERIALIZABLE_PRE( COpenGLScene ) 00042 00043 /** This class allows the user to create, load, save, and render 3D scenes using OpenGL primitives. 00044 * The class can be understood as a program to be run over OpenGL, containing a sequence of viewport definitions, 00045 * rendering primitives, etc... 00046 * 00047 * In MRPT 0.5.5 this class has been re-structured to contain from 1 to any number of <b>Viewports</b>, each one 00048 * associated a set of OpenGL objects and, optionally, a preferred camera position. Both orthogonal (2D/3D) and projection 00049 * camera models can be used for each viewport independently, greatly increasing the possibilities of rendered scenes. 00050 * 00051 * An object of COpenGLScene always contains at least one viewport (utils::COpenGLViewport), named "main". Optionally, any 00052 * number of other viewports may exist. Viewports are referenced by their names, case-sensitive strings. Each viewport contains 00053 * a different 3D scene (i.e. they render different objects), though a mechanism exist to share the same 3D scene by a number of 00054 * viewports so memory is not wasted replicating the same objects (see COpenGLViewport::setCloneView ). 00055 * 00056 * The main rendering method, COpenGLScene::render(), assumes a viewport has been set-up for the entire target window. That 00057 * method will internally make the required calls to opengl for creating the additional viewports. Note that only the depth 00058 * buffer is cleared by default for each (non-main) viewport, to allow transparencies. This can be disabled by the approppriate 00059 * member in COpenGLViewport. 00060 * 00061 * An object COpenGLScene can be saved to a ".3Dscene" file using CFileOutputStream, for posterior visualization from 00062 * the standalone application <a href="http://babel.isa.uma.es/mrpt/index.php/Application:SceneViewer">SceneViewer</a>. 00063 * It can be also displayed in real-time using gui::CDisplayWindow3D. 00064 */ 00065 class MRPTDLLIMPEXP COpenGLScene : public mrpt::utils::CSerializable 00066 { 00067 DEFINE_SERIALIZABLE( COpenGLScene ) 00068 public: 00069 /** Constructor 00070 */ 00071 COpenGLScene(); 00072 00073 /** Destructor: 00074 */ 00075 virtual ~COpenGLScene(); 00076 00077 /** Copy operator: 00078 */ 00079 COpenGLScene & operator =( const COpenGLScene &obj ); 00080 00081 /** Copy constructor: 00082 */ 00083 COpenGLScene( const COpenGLScene &obj ); 00084 00085 /** 00086 * Inserts a set of objects into the scene, in the given viewport ("main" by default). Any iterable object will be accepted. 00087 * \sa createViewport,getViewport 00088 */ 00089 template<class T> inline void insertCollection(const T &objs,const std::string &vpn=std::string("main")) { 00090 insert(objs.begin(),objs.end(),vpn); 00091 } 00092 /** Insert a new object into the scene, in the given viewport (by default, into the "main" viewport). 00093 * The viewport must be created previously, an exception will be raised if the given name does not correspond to 00094 * an existing viewport. 00095 * \sa createViewport, getViewport 00096 */ 00097 void insert( const CRenderizablePtr &newObject, const std::string &viewportName=std::string("main")); 00098 00099 /** 00100 * Inserts a set of objects into the scene, in the given viewport ("main" by default). 00101 * \sa createViewport,getViewport 00102 */ 00103 template<class T_it> inline void insert(const T_it &begin,const T_it &end,const std::string &vpn=std::string("main")) { 00104 for (T_it it=begin;it!=end;it++) insert(*it,vpn); 00105 } 00106 00107 /**Creates a new viewport, adding it to the scene and returning a pointer to the new object. 00108 * 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. 00109 * The first, default viewport, is named "main". 00110 */ 00111 COpenGLViewportPtr createViewport( const std::string &viewportName ); 00112 00113 /** Returns the viewport with the given name, or NULL if it does not exist 00114 */ 00115 COpenGLViewportPtr getViewport( const std::string &viewportName ) const; 00116 00117 /** Render this scene. 00118 */ 00119 void render() const; 00120 00121 size_t viewportsCount() const { return m_viewports.size(); } 00122 00123 /** Clear the list of objects and viewports in the scene, deleting objects' memory, and leaving just the default viewport with the default values. 00124 */ 00125 void clear( bool createMainViewport = true ); 00126 00127 /** 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. 00128 * \sa followCamera 00129 */ 00130 void enableFollowCamera( bool enabled ) { m_followCamera = enabled; } 00131 00132 /** Return the value of "followCamera" 00133 * \sa enableFollowCamera 00134 */ 00135 bool followCamera() const { return m_followCamera; } 00136 00137 /** Returns the first object with a given name, or NULL (an empty smart pointer) if not found. 00138 */ 00139 CRenderizablePtr getByName( const std::string &str, const std::string &viewportName = std::string("main") ); 00140 00141 /** Returns the i'th object of a given class (or of a descendant class), or NULL (an empty smart pointer) if not found. 00142 * Example: 00143 * \code 00144 CSpherePtr obs = myscene.getByClass<CSphere>(); 00145 * \endcode 00146 * By default (ith=0), the first observation is returned. 00147 */ 00148 template <typename T> 00149 typename T::SmartPtr getByClass( const size_t &ith = 0 ) const 00150 { 00151 MRPT_TRY_START; 00152 for (TListViewports::const_iterator it = m_viewports.begin();it!=m_viewports.end();++it) 00153 { 00154 typename T::SmartPtr o = (*it)->getByClass<T>(ith); 00155 if (o.present()) return o; 00156 } 00157 return typename T::SmartPtr(); // Not found: return empty smart pointer 00158 MRPT_TRY_END; 00159 } 00160 00161 00162 /** Removes the given object from the scene (it also deletes the object to free its memory). 00163 */ 00164 void removeObject( const CRenderizablePtr &obj, const std::string &viewportName = std::string("main") ); 00165 00166 /** Initializes all textures in the scene (See opengl::CTexturedPlane::loadTextureInOpenGL) 00167 */ 00168 void initializeAllTextures(); 00169 00170 /** Retrieves a list of all objects in text form. 00171 */ 00172 void dumpListOfObjects( utils::CStringList &lst ); 00173 00174 /** Saves the scene to a 3Dscene file, loadable by the application SceneViewer3D 00175 * \sa loadFromFile 00176 * \return false on any error. 00177 */ 00178 bool saveToFile(const std::string &fil) const; 00179 00180 /** Loads the scene from a 3Dscene file, the format used by the application SceneViewer3D. 00181 * \sa saveToFile 00182 * \return false on any error. 00183 */ 00184 bool loadFromFile(const std::string &fil); 00185 00186 /** Traces a ray 00187 */ 00188 bool traceRay(const mrpt::poses::CPose3D&o,double &dist) const; 00189 00190 protected: 00191 bool m_followCamera; 00192 00193 typedef std::vector<COpenGLViewportPtr> TListViewports; 00194 00195 /** The list of viewports, indexed by name. 00196 */ 00197 TListViewports m_viewports; 00198 }; 00199 /** 00200 * Inserts an openGL object into a scene. Allows call chaining. 00201 * \sa mrpt::opengl::COpenGLScene::insert 00202 */ 00203 inline COpenGLScenePtr &operator<<(COpenGLScenePtr &s,const CRenderizablePtr &r) { 00204 s->insert(r); 00205 return s; 00206 } 00207 /** 00208 * Inserts any iterable collection of openGL objects into a scene, allowing call chaining. 00209 * \sa mrpt::opengl::COpenGLScene::insert 00210 */ 00211 template <class T> inline COpenGLScenePtr &operator<<(COpenGLScenePtr &s,const std::vector<T> &v) { 00212 s->insert(v.begin(),v.end()); 00213 return s; 00214 } 00215 } // end namespace 00216 00217 } // End of namespace 00218 00219 00220 #endif
Page generated by Doxygen 1.5.9 for MRPT 0.7.1 SVN: at Mon Aug 17 22:20:53 EDT 2009 |