00001 /* +---------------------------------------------------------------------------+ 00002 | The Mobile Robot Programming Toolkit (MRPT) C++ library | 00003 | | 00004 | http://mrpt.sourceforge.net/ | 00005 | | 00006 | Copyright (C) 2005-2010 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 min/max clip depth distances for rendering (default: 0.1 - 10000) 00114 * \sa getViewportClipDistances 00115 */ 00116 void setViewportClipDistances(const double clip_min, const double clip_max); 00117 00118 /** Get the current min/max clip depth distances for rendering (default: 0.1 - 10000) 00119 * \sa setViewportClipDistances 00120 */ 00121 void getViewportClipDistances(double &clip_min, double &clip_max) const; 00122 00123 /** Set the border size ("frame") of the viewport (default=0). 00124 */ 00125 void setBorderSize( unsigned int lineWidth ) { m_borderWidth = lineWidth; } 00126 00127 /** Return whether the viewport will be rendered transparent over previous viewports. 00128 */ 00129 bool isTransparent() { return m_isTransparent; } 00130 00131 /** Set the transparency, that is, whether the viewport will be rendered transparent over previous viewports (default=false). 00132 */ 00133 void setTransparent( bool trans ) { m_isTransparent=trans; } 00134 00135 virtual ~COpenGLViewport(); //!< Destructor: clears all objects. 00136 00137 /** Returns the first object with a given name, or NULL if not found. 00138 */ 00139 CRenderizablePtr getByName( const std::string &str ); 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 = view.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_START; 00152 size_t foundCount = 0; 00153 const mrpt::utils::TRuntimeClassId* class_ID = T::classinfo; 00154 for (CListOpenGLObjects::const_iterator it = m_objects.begin();it!=m_objects.end();++it) 00155 if ( (*it).present() && (*it)->GetRuntimeClass()->derivedFrom( class_ID ) ) 00156 if (foundCount++ == ith) 00157 return typename T::SmartPtr(*it); 00158 00159 // If not found directly, search recursively: 00160 for (CListOpenGLObjects::const_iterator it=m_objects.begin();it!=m_objects.end();++it) 00161 { 00162 if ( (*it).present() && (*it)->GetRuntimeClass() == CLASS_ID_NAMESPACE(CSetOfObjects,mrpt::opengl)) 00163 { 00164 typename T::SmartPtr o = CSetOfObjectsPtr(*it)->getByClass<T>(ith); 00165 if (o.present()) return o; 00166 } 00167 } 00168 return typename T::SmartPtr(); // Not found: return empty smart pointer 00169 MRPT_END; 00170 } 00171 00172 00173 00174 /** Removes the given object from the scene (it also deletes the object to free its memory). 00175 */ 00176 void removeObject( const CRenderizablePtr & obj ); 00177 00178 /** Number of objects contained. */ 00179 size_t size() const { return m_objects.size(); } 00180 00181 opengl::CCamera& getCamera() { return m_camera;} //!< Get a reference to the camera associated with this viewport. 00182 00183 const opengl::CCamera & getCamera() const { return m_camera;} //!< Get a reference to the camera associated with this viewport. 00184 00185 protected: 00186 /** Constructor, invoked from COpenGLScene only. 00187 */ 00188 COpenGLViewport( COpenGLScene *parent=NULL, const std::string &name=std::string("") ); 00189 00190 /** Initializes all textures in the scene (See opengl::CTexturedPlane::loadTextureInOpenGL) 00191 */ 00192 void initializeAllTextures(); 00193 00194 /** Retrieves a list of all objects in text form. 00195 */ 00196 void dumpListOfObjects( utils::CStringList &lst ); 00197 00198 /** Render the objects in this viewport (called from COpenGLScene only) */ 00199 void render( const int &render_width, const int &render_height ) const; 00200 00201 /** The camera associated to the viewport */ 00202 opengl::CCamera m_camera; 00203 utils::safe_ptr<COpenGLScene> m_parent; //!< The scene that contains this viewport. 00204 bool m_isCloned; //!< Set by setCloneView 00205 bool m_isClonedCamera; //!< Set by setCloneCamera 00206 std::string m_clonedViewport; //!< Only if m_isCloned=true 00207 std::string m_name; //!< The viewport's name 00208 bool m_isTransparent; //!< Whether to clear color buffer. 00209 uint32_t m_borderWidth; //!< Default=0, the border around the viewport. 00210 double m_view_x, m_view_y,m_view_width,m_view_height; //!< The viewport position [0,1] 00211 double m_clip_min,m_clip_max; //!< The min/max clip depth distances (default: 0.1 - 10000) 00212 00213 /** The list of objects that comprise the 3D scene. 00214 * Objects are automatically deleted when calling "clear" or in the destructor. 00215 */ 00216 opengl::CListOpenGLObjects m_objects; 00217 00218 }; 00219 /** 00220 * Inserts an openGL object into a viewport. Allows call chaining. 00221 * \sa mrpt::opengl::COpenGLViewport::insert 00222 */ 00223 inline COpenGLViewportPtr &operator<<(COpenGLViewportPtr &s,const CRenderizablePtr &r) { 00224 s->insert(r); 00225 return s; 00226 } 00227 /** 00228 * Inserts any iterable set of openGL objects into a viewport. Allows call chaining. 00229 * \sa mrpt::opengl::COpenGLViewport::insert 00230 */ 00231 inline COpenGLViewportPtr &operator<<(COpenGLViewportPtr &s,const std::vector<CRenderizablePtr> &v) { 00232 for (std::vector<CRenderizablePtr>::const_iterator it=v.begin();it!=v.end();++it) s->insert(*it); 00233 return s; 00234 } 00235 00236 } // end namespace 00237 00238 } // End of namespace 00239 00240 00241 #endif
| Page generated by Doxygen 1.6.2 for MRPT 0.8.1 SVN:exported at Mon Feb 15 22:01:07 UTC 2010 |
