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 #include <mrpt/math/lightweight_geom_data.h> 00036 00037 namespace mrpt 00038 { 00039 namespace utils { class CStringList; } 00040 00041 using namespace mrpt::math; 00042 00043 /** The namespace for 3D scene representation and rendering. 00044 */ 00045 namespace opengl 00046 { 00047 class COpenGLScene; 00048 class CRenderizable; 00049 00050 // This must be added to any CSerializable derived class: 00051 DEFINE_SERIALIZABLE_PRE( COpenGLViewport ) 00052 00053 /** A viewport within a COpenGLScene, containing a set of OpenGL objects to render. 00054 * This class has protected constuctor, thus it cannot be created by users. Use COpenGLScene::createViewport instead. 00055 * Refer to opengl::COpenGLScene for further details. 00056 */ 00057 class MRPTDLLIMPEXP COpenGLViewport : public mrpt::utils::CSerializable 00058 { 00059 DEFINE_SERIALIZABLE( COpenGLViewport ) 00060 00061 friend class COpenGLScene; 00062 public: 00063 /** 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. 00064 * By default, only the objects are cloned, not the camera. See 00065 * \sa resetCloneView 00066 */ 00067 void setCloneView( const std::string &clonedViewport ); 00068 00069 /** Reset the viewport to normal mode: rendering its own objects. 00070 * \sa setCloneView 00071 */ 00072 void resetCloneView() { m_isCloned=false;m_isClonedCamera=false; } 00073 00074 /** If set to true, and setCloneView() has been called, this viewport will be rendered using the camera of the cloned viewport. 00075 */ 00076 void setCloneCamera(bool enable) { m_isClonedCamera = enable; } 00077 00078 00079 /** Delete all internal obejcts 00080 * \sa insert 00081 */ 00082 void clear(); 00083 00084 /** Insert a new object into the list. 00085 * The object MUST NOT be deleted, it will be deleted automatically by this object when not required anymore. 00086 */ 00087 void insert( const CRenderizablePtr &newObject ); 00088 00089 /** Returns the name of the viewport */ 00090 std::string getName() { return m_name; } 00091 00092 /** Change the viewport position and dimension on the rendering window. 00093 * Coordinates here are alwasys in the range [0,1], relative to the actual 00094 * window sizes (i.e. width=1 means the entire width of the rendering window). 00095 * \note (x,y) specify the lower left corner of the viewport rectangle. 00096 * \sa getViewportPosition 00097 */ 00098 void setViewportPosition( 00099 const double &x, 00100 const double &y, 00101 const double &width, 00102 const double &height ); 00103 00104 /** Get the current viewport position and dimension on the rendering window. 00105 * Coordinates here are alwasys in the range [0,1], relative to the actual 00106 * window sizes (i.e. width=1 means the entire width of the rendering window). 00107 * \note (x,y) specify the lower left corner of the viewport rectangle. 00108 * \sa setViewportPosition 00109 */ 00110 void getViewportPosition( 00111 double &x, 00112 double &y, 00113 double &width, 00114 double &height ); 00115 00116 /** Set the min/max clip depth distances for rendering (default: 0.1 - 10000) 00117 * \sa getViewportClipDistances 00118 */ 00119 void setViewportClipDistances(const double clip_min, const double clip_max); 00120 00121 /** Get the current min/max clip depth distances for rendering (default: 0.1 - 10000) 00122 * \sa setViewportClipDistances 00123 */ 00124 void getViewportClipDistances(double &clip_min, double &clip_max) const; 00125 00126 /** Set the border size ("frame") of the viewport (default=0). 00127 */ 00128 void setBorderSize( unsigned int lineWidth ) { m_borderWidth = lineWidth; } 00129 00130 /** Return whether the viewport will be rendered transparent over previous viewports. 00131 */ 00132 bool isTransparent() { return m_isTransparent; } 00133 00134 /** Set the transparency, that is, whether the viewport will be rendered transparent over previous viewports (default=false). 00135 */ 00136 void setTransparent( bool trans ) { m_isTransparent=trans; } 00137 00138 virtual ~COpenGLViewport(); //!< Destructor: clears all objects. 00139 00140 /** Compute the 3D ray corresponding to a given pixel; this can be used to allow the user to pick and select 3D objects by clicking onto the 2D image. 00141 * \param x_coord Horizontal coordinate with the usual meaning (0:left of the viewport, W-1: right border). 00142 * \param y_coord Horizontal coordinate with the usual meaning (0:top of the viewport, H-1: right border). 00143 * \param out_cameraPose If not NULL, will have the camera 3D pose as a mrpt::poses::CPose3D. See also 00144 * \note (x,y) refer to VIEWPORT coordinates. Take into account this when viewports do not extend to the whole window size. 00145 * \note x and y are double instead of integers to allow sub-pixel precision. 00146 * \sa getCurrentCameraPose 00147 */ 00148 void get3DRayForPixelCoord( const double x_coord, const double y_coord, mrpt::math::TLine3D &out_ray, mrpt::poses::CPose3D *out_cameraPose=NULL ) const; 00149 00150 /** Compute the current 3D camera pose. 00151 * \sa get3DRayForPixelCoord 00152 */ 00153 void getCurrentCameraPose( mrpt::poses::CPose3D &out_cameraPose ) const; 00154 00155 /** Returns the first object with a given name, or NULL if not found. 00156 */ 00157 CRenderizablePtr getByName( const std::string &str ); 00158 00159 /** Returns the i'th object of a given class (or of a descendant class), or NULL (an empty smart pointer) if not found. 00160 * Example: 00161 * \code 00162 CSpherePtr obs = view.getByClass<CSphere>(); 00163 * \endcode 00164 * By default (ith=0), the first observation is returned. 00165 */ 00166 template <typename T> 00167 typename T::SmartPtr getByClass( const size_t &ith = 0 ) const 00168 { 00169 MRPT_START; 00170 size_t foundCount = 0; 00171 const mrpt::utils::TRuntimeClassId* class_ID = T::classinfo; 00172 for (CListOpenGLObjects::const_iterator it = m_objects.begin();it!=m_objects.end();++it) 00173 if ( (*it).present() && (*it)->GetRuntimeClass()->derivedFrom( class_ID ) ) 00174 if (foundCount++ == ith) 00175 return typename T::SmartPtr(*it); 00176 00177 // If not found directly, search recursively: 00178 for (CListOpenGLObjects::const_iterator it=m_objects.begin();it!=m_objects.end();++it) 00179 { 00180 if ( (*it).present() && (*it)->GetRuntimeClass() == CLASS_ID_NAMESPACE(CSetOfObjects,mrpt::opengl)) 00181 { 00182 typename T::SmartPtr o = CSetOfObjectsPtr(*it)->getByClass<T>(ith); 00183 if (o.present()) return o; 00184 } 00185 } 00186 return typename T::SmartPtr(); // Not found: return empty smart pointer 00187 MRPT_END; 00188 } 00189 00190 00191 00192 /** Removes the given object from the scene (it also deletes the object to free its memory). 00193 */ 00194 void removeObject( const CRenderizablePtr & obj ); 00195 00196 /** Number of objects contained. */ 00197 size_t size() const { return m_objects.size(); } 00198 00199 opengl::CCamera& getCamera() { return m_camera;} //!< Get a reference to the camera associated with this viewport. 00200 00201 const opengl::CCamera & getCamera() const { return m_camera;} //!< Get a reference to the camera associated with this viewport. 00202 00203 protected: 00204 /** Constructor, invoked from COpenGLScene only. 00205 */ 00206 COpenGLViewport( COpenGLScene *parent=NULL, const std::string &name=std::string("") ); 00207 00208 /** Initializes all textures in the scene (See opengl::CTexturedPlane::loadTextureInOpenGL) 00209 */ 00210 void initializeAllTextures(); 00211 00212 /** Retrieves a list of all objects in text form. 00213 */ 00214 void dumpListOfObjects( utils::CStringList &lst ); 00215 00216 /** Render the objects in this viewport (called from COpenGLScene only) */ 00217 void render( const int render_width, const int render_height ) const; 00218 00219 /** The camera associated to the viewport */ 00220 opengl::CCamera m_camera; 00221 utils::safe_ptr<COpenGLScene> m_parent; //!< The scene that contains this viewport. 00222 bool m_isCloned; //!< Set by setCloneView 00223 bool m_isClonedCamera; //!< Set by setCloneCamera 00224 std::string m_clonedViewport; //!< Only if m_isCloned=true 00225 std::string m_name; //!< The viewport's name 00226 bool m_isTransparent; //!< Whether to clear color buffer. 00227 uint32_t m_borderWidth; //!< Default=0, the border around the viewport. 00228 double m_view_x, m_view_y,m_view_width,m_view_height; //!< The viewport position [0,1] 00229 double m_clip_min,m_clip_max; //!< The min/max clip depth distances (default: 0.1 - 10000) 00230 00231 struct TLastProjectiveMatrixInfo 00232 { 00233 TLastProjectiveMatrixInfo() : is_projective(true),eye(0,0,0),pointing(0,0,0),up(0,0,0), FOV(30), viewport_width(640), viewport_height(480), azimuth(0), elev(0) 00234 {} 00235 bool is_projective; // true: projective, false: ortho 00236 TPoint3D eye; //!< The camera is here. 00237 TPoint3D pointing; //!< The camera points to here 00238 TPoint3D up; //!< Up vector of the camera. 00239 float FOV; //!< FOV in degrees. 00240 size_t viewport_width, viewport_height; //!< In pixels. This may be smaller than the total render window. 00241 float azimuth, elev; //!< Camera elev & azimuth, in radians. 00242 }; 00243 mutable TLastProjectiveMatrixInfo m_lastProjMat; //!< Info updated with each "render()" and used in "get3DRayForPixelCoord" 00244 00245 /** The list of objects that comprise the 3D scene. 00246 * Objects are automatically deleted when calling "clear" or in the destructor. 00247 */ 00248 opengl::CListOpenGLObjects m_objects; 00249 00250 }; 00251 /** 00252 * Inserts an openGL object into a viewport. Allows call chaining. 00253 * \sa mrpt::opengl::COpenGLViewport::insert 00254 */ 00255 inline COpenGLViewportPtr &operator<<(COpenGLViewportPtr &s,const CRenderizablePtr &r) { 00256 s->insert(r); 00257 return s; 00258 } 00259 /** 00260 * Inserts any iterable set of openGL objects into a viewport. Allows call chaining. 00261 * \sa mrpt::opengl::COpenGLViewport::insert 00262 */ 00263 inline COpenGLViewportPtr &operator<<(COpenGLViewportPtr &s,const std::vector<CRenderizablePtr> &v) { 00264 for (std::vector<CRenderizablePtr>::const_iterator it=v.begin();it!=v.end();++it) s->insert(*it); 00265 return s; 00266 } 00267 00268 } // end namespace 00269 00270 } // End of namespace 00271 00272 00273 #endif
| Page generated by Doxygen 1.7.1 for MRPT 0.8.1 SVN:exported at Sun Sep 19 14:53:45 UTC 2010 |
