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_COpenGLViewport_H 00029 #define opengl_COpenGLViewport_H 00030 00031 #include <mrpt/utils/CSerializable.h> 00032 #include <mrpt/utils/safe_pointers.h> 00033 #include <mrpt/opengl/CCamera.h> 00034 #include <mrpt/opengl/CSetOfObjects.h> 00035 00036 namespace mrpt 00037 { 00038 namespace utils { class CStringList; } 00039 00040 /** The namespace for 3D scene representation and rendering. 00041 */ 00042 namespace opengl 00043 { 00044 class COpenGLScene; 00045 class CRenderizable; 00046 00047 // This must be added to any CSerializable derived class: 00048 DEFINE_SERIALIZABLE_PRE( COpenGLViewport ) 00049 00050 /** A viewport within a COpenGLScene, containing a set of OpenGL objects to render. 00051 * This class has protected constuctor, thus it cannot be created by users. Use COpenGLScene::createViewport instead. 00052 * Refer to opengl::COpenGLScene for further details. 00053 */ 00054 class MRPTDLLIMPEXP COpenGLViewport : public mrpt::utils::CSerializable 00055 { 00056 DEFINE_SERIALIZABLE( COpenGLViewport ) 00057 00058 friend class COpenGLScene; 00059 public: 00060 /** Set this view as a clone of some other viewport, given its name - as a side effect, current list of internal OpenGL objects is cleared. 00061 * By default, only the objects are cloned, not the camera. See 00062 * \sa resetCloneView 00063 */ 00064 void setCloneView( const std::string &clonedViewport ); 00065 00066 /** Reset the viewport to normal mode: rendering its own objects. 00067 * \sa setCloneView 00068 */ 00069 void resetCloneView() { m_isCloned=false;m_isClonedCamera=false; } 00070 00071 /** If set to true, and setCloneView() has been called, this viewport will be rendered using the camera of the cloned viewport. 00072 */ 00073 void setCloneCamera(bool enable) { m_isClonedCamera = enable; } 00074 00075 00076 /** Delete all internal obejcts 00077 * \sa insert 00078 */ 00079 void clear(); 00080 00081 /** Insert a new object into the list. 00082 * The object MUST NOT be deleted, it will be deleted automatically by this object when not required anymore. 00083 */ 00084 void insert( const CRenderizablePtr &newObject ); 00085 00086 /** Returns the name of the viewport */ 00087 std::string getName() { return m_name; } 00088 00089 /** Change the viewport position and dimension on the rendering window. 00090 * Coordinates here are alwasys in the range [0,1], relative to the actual 00091 * window sizes (i.e. width=1 means the entire width of the rendering window). 00092 * \note (x,y) specify the lower left corner of the viewport rectangle. 00093 * \sa getViewportPosition 00094 */ 00095 void setViewportPosition( 00096 const double &x, 00097 const double &y, 00098 const double &width, 00099 const double &height ); 00100 00101 /** Get the current viewport position and dimension on the rendering window. 00102 * Coordinates here are alwasys in the range [0,1], relative to the actual 00103 * window sizes (i.e. width=1 means the entire width of the rendering window). 00104 * \note (x,y) specify the lower left corner of the viewport rectangle. 00105 * \sa setViewportPosition 00106 */ 00107 void getViewportPosition( 00108 double &x, 00109 double &y, 00110 double &width, 00111 double &height ); 00112 00113 /** Set the border size ("frame") of the viewport (default=0). 00114 */ 00115 void setBorderSize( unsigned int lineWidth ) { m_borderWidth = lineWidth; } 00116 00117 /** Return whether the viewport will be rendered transparent over previous viewports. 00118 */ 00119 bool isTransparent() { return m_isTransparent; } 00120 00121 /** Set the transparency, that is, whether the viewport will be rendered transparent over previous viewports (default=false). 00122 */ 00123 void setTransparent( bool trans ) { m_isTransparent=trans; } 00124 00125 virtual ~COpenGLViewport(); //!< Destructor: clears all objects. 00126 00127 /** Returns the first object with a given name, or NULL if not found. 00128 */ 00129 CRenderizablePtr getByName( const std::string &str ); 00130 00131 /** Returns the i'th object of a given class (or of a descendant class), or NULL (an empty smart pointer) if not found. 00132 * Example: 00133 * \code 00134 CSpherePtr obs = view.getByClass<CSphere>(); 00135 * \endcode 00136 * By default (ith=0), the first observation is returned. 00137 */ 00138 template <typename T> 00139 typename T::SmartPtr getByClass( const size_t &ith = 0 ) const 00140 { 00141 MRPT_TRY_START; 00142 size_t foundCount = 0; 00143 const mrpt::utils::TRuntimeClassId* class_ID = T::classinfo; 00144 for (CListOpenGLObjects::const_iterator it = m_objects.begin();it!=m_objects.end();++it) 00145 if ( (*it).present() && (*it)->GetRuntimeClass()->derivedFrom( class_ID ) ) 00146 if (foundCount++ == ith) 00147 return typename T::SmartPtr(*it); 00148 00149 // If not found directly, search recursively: 00150 for (CListOpenGLObjects::const_iterator it=m_objects.begin();it!=m_objects.end();++it) 00151 { 00152 if ( (*it).present() && (*it)->GetRuntimeClass() == CLASS_ID_NAMESPACE(CSetOfObjects,mrpt::opengl)) 00153 { 00154 typename T::SmartPtr o = CSetOfObjectsPtr(*it)->getByClass<T>(ith); 00155 if (o.present()) return o; 00156 } 00157 } 00158 return typename T::SmartPtr(); // Not found: return empty smart pointer 00159 MRPT_TRY_END; 00160 } 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 ); 00167 00168 /** Number of objects contained. */ 00169 size_t size() const { return m_objects.size(); } 00170 00171 opengl::CCamera& getCamera() { return m_camera;} //!< Get a reference to the camera associated with this viewport. 00172 00173 const opengl::CCamera & getCamera() const { return m_camera;} //!< Get a reference to the camera associated with this viewport. 00174 00175 protected: 00176 /** Constructor, invoked from COpenGLScene only. 00177 */ 00178 COpenGLViewport( COpenGLScene *parent=NULL, const std::string &name=std::string("") ); 00179 00180 /** Initializes all textures in the scene (See opengl::CTexturedPlane::loadTextureInOpenGL) 00181 */ 00182 void initializeAllTextures(); 00183 00184 /** Retrieves a list of all objects in text form. 00185 */ 00186 void dumpListOfObjects( utils::CStringList &lst ); 00187 00188 /** Render the objects in this viewport (called from COpenGLScene only) */ 00189 void render( const int &render_width, const int &render_height ) const; 00190 00191 /** The camera associated to the viewport */ 00192 opengl::CCamera m_camera; 00193 00194 utils::safe_ptr<COpenGLScene> m_parent; //!< The scene that contains this viewport. 00195 00196 bool m_isCloned; //!< Set by setCloneView 00197 bool m_isClonedCamera; //!< Set by setCloneCamera 00198 std::string m_clonedViewport; //!< Only if m_isCloned=true 00199 00200 std::string m_name; //!< The viewport's name 00201 00202 bool m_isTransparent; //!< Whether to clear color buffer. 00203 00204 uint32_t m_borderWidth; //!< Default=0, the border around the viewport. 00205 00206 double m_view_x, m_view_y,m_view_width,m_view_height; //!< The viewport position [0,1] 00207 00208 /** The list of objects that comprise the 3D scene. 00209 * Objects are automatically deleted when calling "clear" or in the destructor. 00210 */ 00211 opengl::CListOpenGLObjects m_objects; 00212 00213 }; 00214 /** 00215 * Inserts an openGL object into a viewport. Allows call chaining. 00216 * \sa mrpt::opengl::COpenGLViewport::insert 00217 */ 00218 inline COpenGLViewportPtr &operator<<(COpenGLViewportPtr &s,const CRenderizablePtr &r) { 00219 s->insert(r); 00220 return s; 00221 } 00222 /** 00223 * Inserts any iterable set of openGL objects into a viewport. Allows call chaining. 00224 * \sa mrpt::opengl::COpenGLViewport::insert 00225 */ 00226 inline COpenGLViewportPtr &operator<<(COpenGLViewportPtr &s,const std::vector<CRenderizablePtr> &v) { 00227 for (std::vector<CRenderizablePtr>::const_iterator it=v.begin();it!=v.end();++it) s->insert(*it); 00228 return s; 00229 } 00230 00231 } // end namespace 00232 00233 } // End of namespace 00234 00235 00236 #endif
Page generated by Doxygen 1.5.9 for MRPT 0.7.1 SVN: at Mon Aug 17 22:32:05 EDT 2009 |