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_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 namespace opengl 00042 { 00043 using namespace mrpt::math; 00044 00045 class COpenGLScene; 00046 class CRenderizable; 00047 00048 // This must be added to any CSerializable derived class: 00049 DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE_LINKAGE( COpenGLViewport, mrpt::utils::CSerializable, OPENGL_IMPEXP ) 00050 00051 /** A viewport within a COpenGLScene, containing a set of OpenGL objects to render. 00052 * This class has protected constuctor, thus it cannot be created by users. Use COpenGLScene::createViewport instead. 00053 * A viewport has these "operation modes": 00054 * - Normal (default): It renders the contained objects. 00055 * - Cloned: It clones the objects from another viewport. See \a setCloneView() 00056 * - Image mode: It renders an image (e.g. from a video stream) efficiently using a textued quad. See \a setImageView(). 00057 * 00058 * In any case, the viewport can be resized to only fit a part of the entire parent viewport. 00059 * There will be always at least one viewport in a COpenGLScene named "main". 00060 * 00061 * Refer to mrpt::opengl::COpenGLScene for further details. 00062 */ 00063 class OPENGL_IMPEXP COpenGLViewport : public mrpt::utils::CSerializable 00064 { 00065 DEFINE_SERIALIZABLE( COpenGLViewport ) 00066 friend class COpenGLScene; 00067 public: 00068 // ------------------------------------------------------------------- 00069 /** @name Set the viewport "modes" 00070 @{ */ 00071 00072 /** Set this viewport as a clone of some other viewport, given its name - as a side effect, current list of internal OpenGL objects is cleared. 00073 * By default, only the objects are cloned, not the camera. See 00074 * \sa resetCloneView 00075 */ 00076 void setCloneView( const std::string &clonedViewport ); 00077 00078 /** Set this viewport into "image view"-mode, where an image is efficiently drawn (fitting the viewport area) using an OpenGL textured quad. 00079 * Call this method with the new image to update the displayed image (but recall to first lock the parent openglscene's critical section, then do the update, then release the lock, and then issue a window repaint). 00080 * Internally, the texture is drawn using a mrpt::opengl::CTexturedPlane 00081 * The viewport can be reverted to behave like a normal viewport by calling setNormalMode() 00082 * \sa setImageView_fast 00083 */ 00084 void setImageView(const mrpt::utils::CImage &img); 00085 00086 /** Just like \a setImageView but moves the internal image memory instead of making a copy, so it's faster but empties the input image. 00087 * \sa setImageView 00088 */ 00089 void setImageView_fast(mrpt::utils::CImage &img); 00090 00091 /** Reset the viewport to normal mode: rendering its own objects. 00092 * \sa setCloneView, setNormalMode 00093 */ 00094 inline void resetCloneView() { setNormalMode(); } 00095 00096 /** If set to true, and setCloneView() has been called, this viewport will be rendered using the camera of the cloned viewport. 00097 */ 00098 inline void setCloneCamera(bool enable) { m_isClonedCamera = enable; } 00099 00100 /** Resets the viewport to a normal 3D viewport \sa setCloneView, setImageView */ 00101 void setNormalMode(); 00102 00103 /** @} */ // end of Set the "viewport mode" 00104 // ------------------------------------------------------ 00105 00106 00107 // ------------------------------------------------------------------- 00108 /** @name Change or read viewport properties (except "viewport modes") 00109 @{ */ 00110 00111 /** Returns the name of the viewport */ 00112 inline std::string getName() { return m_name; } 00113 00114 /** Change the viewport position and dimension on the rendering window. 00115 * X & Y coordinates here can have two interpretations: 00116 * - If in the range [0,1], they are factors with respect to the actual window sizes (i.e. width=1 means the entire width of the rendering window). 00117 * - If >1, they are interpreted as pixels. 00118 * 00119 * width & height can be interpreted as: 00120 * - If >1, they are the size of the viewport in that dimension, in pixels. 00121 * - If in [0,1], they are the size of the viewport in that dimension, in a factor of the width/height. 00122 * - If in [-1,0[, the size is computed such as the right/top border ends up in the given coordinate, interpreted as a factor (e.g. -1: up to the end of the viewport, -0.5: up to the middle of it). 00123 * - If <-1 the size is computed such as the right/top border ends up in the given absolute coordinate (e.g. -200: up to the row/column 200px). 00124 * 00125 * \note (x,y) specify the lower left corner of the viewport rectangle. 00126 * \sa getViewportPosition 00127 */ 00128 void setViewportPosition( 00129 const double x, 00130 const double y, 00131 const double width, 00132 const double height ); 00133 00134 /** Get the current viewport position and dimension on the rendering window. 00135 * X & Y coordinates here can have two interpretations: 00136 * - If in the range [0,1], they are factors with respect to the actual window sizes (i.e. width=1 means the entire width of the rendering window). 00137 * - If >1, they are interpreted as pixels. 00138 * \note (x,y) specify the lower left corner of the viewport rectangle. 00139 * \sa setViewportPosition 00140 */ 00141 void getViewportPosition( 00142 double &x, 00143 double &y, 00144 double &width, 00145 double &height ); 00146 00147 /** Set the min/max clip depth distances of the rendering frustum (default: 0.1 - 10000) 00148 * \sa getViewportClipDistances 00149 */ 00150 void setViewportClipDistances(const double clip_min, const double clip_max); 00151 00152 /** Get the current min/max clip depth distances of the rendering frustum (default: 0.1 - 10000) 00153 * \sa setViewportClipDistances 00154 */ 00155 void getViewportClipDistances(double &clip_min, double &clip_max) const; 00156 00157 /** Set the border size ("frame") of the viewport (default=0). 00158 */ 00159 inline void setBorderSize( unsigned int lineWidth ) { m_borderWidth = lineWidth; } 00160 00161 /** Return whether the viewport will be rendered transparent over previous viewports. 00162 */ 00163 inline bool isTransparent() { return m_isTransparent; } 00164 00165 /** Set the transparency, that is, whether the viewport will be rendered transparent over previous viewports (default=false). 00166 */ 00167 inline void setTransparent( bool trans ) { m_isTransparent=trans; } 00168 00169 /** Set a background color different from that of the parent GUI window */ 00170 inline void setCustomBackgroundColor( const TColorf &color ) { m_custom_backgb_color = true; m_background_color = color; } 00171 00172 inline bool hasCustomBackgroundColor() const { return m_custom_backgb_color; } 00173 00174 inline TColorf getCustomBackgroundColor() const { return m_background_color; } 00175 00176 /** 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. 00177 * \param x_coord Horizontal coordinate with the usual meaning (0:left of the viewport, W-1: right border). 00178 * \param y_coord Horizontal coordinate with the usual meaning (0:top of the viewport, H-1: right border). 00179 * \param out_cameraPose If not NULL, will have the camera 3D pose as a mrpt::poses::CPose3D. See also 00180 * \note (x,y) refer to VIEWPORT coordinates. Take into account this when viewports do not extend to the whole window size. 00181 * \note x and y are double instead of integers to allow sub-pixel precision. 00182 * \sa getCurrentCameraPose 00183 */ 00184 void get3DRayForPixelCoord( const double x_coord, const double y_coord, mrpt::math::TLine3D &out_ray, mrpt::poses::CPose3D *out_cameraPose=NULL ) const; 00185 00186 /** @} */ // end of Change or read viewport properties 00187 // ------------------------------------------------------ 00188 00189 00190 // ------------------------------------------------------------------- 00191 /** @name Contained objects set/get/search 00192 @{ */ 00193 00194 typedef CListOpenGLObjects::const_iterator const_iterator; 00195 typedef CListOpenGLObjects::iterator iterator; 00196 00197 inline const_iterator begin() const { return m_objects.begin(); } 00198 inline const_iterator end() const { return m_objects.end(); } 00199 inline iterator begin() { return m_objects.begin(); } 00200 inline iterator end() { return m_objects.end(); } 00201 00202 /** Delete all internal obejcts 00203 * \sa insert */ 00204 void clear(); 00205 00206 /** Insert a new object into the list. 00207 * The object MUST NOT be deleted, it will be deleted automatically by this object when not required anymore. 00208 */ 00209 void insert( const CRenderizablePtr &newObject ); 00210 00211 /** Compute the current 3D camera pose. 00212 * \sa get3DRayForPixelCoord 00213 */ 00214 void getCurrentCameraPose( mrpt::poses::CPose3D &out_cameraPose ) const; 00215 00216 /** Returns the first object with a given name, or NULL if not found. 00217 */ 00218 CRenderizablePtr getByName( const std::string &str ); 00219 00220 /** Returns the i'th object of a given class (or of a descendant class), or NULL (an empty smart pointer) if not found. 00221 * Example: 00222 * \code 00223 CSpherePtr obs = view.getByClass<CSphere>(); 00224 * \endcode 00225 * By default (ith=0), the first observation is returned. 00226 */ 00227 template <typename T> 00228 typename T::SmartPtr getByClass( const size_t &ith = 0 ) const 00229 { 00230 MRPT_START; 00231 size_t foundCount = 0; 00232 const mrpt::utils::TRuntimeClassId* class_ID = T::classinfo; 00233 for (CListOpenGLObjects::const_iterator it = m_objects.begin();it!=m_objects.end();++it) 00234 if ( (*it).present() && (*it)->GetRuntimeClass()->derivedFrom( class_ID ) ) 00235 if (foundCount++ == ith) 00236 return typename T::SmartPtr(*it); 00237 00238 // If not found directly, search recursively: 00239 for (CListOpenGLObjects::const_iterator it=m_objects.begin();it!=m_objects.end();++it) 00240 { 00241 if ( (*it).present() && (*it)->GetRuntimeClass() == CLASS_ID_NAMESPACE(CSetOfObjects,mrpt::opengl)) 00242 { 00243 typename T::SmartPtr o = CSetOfObjectsPtr(*it)->getByClass<T>(ith); 00244 if (o.present()) return o; 00245 } 00246 } 00247 return typename T::SmartPtr(); // Not found: return empty smart pointer 00248 MRPT_END; 00249 } 00250 00251 /** Removes the given object from the scene (it also deletes the object to free its memory). 00252 */ 00253 void removeObject( const CRenderizablePtr & obj ); 00254 00255 /** Number of objects contained. */ 00256 inline size_t size() const { return m_objects.size(); } 00257 00258 inline bool empty() const { return m_objects.empty(); } 00259 00260 00261 opengl::CCamera& getCamera() { return m_camera;} //!< Get a reference to the camera associated with this viewport. 00262 00263 const opengl::CCamera & getCamera() const { return m_camera;} //!< Get a reference to the camera associated with this viewport. 00264 00265 /** @} */ // end of Contained objects set/get/search 00266 // ------------------------------------------------------ 00267 00268 00269 virtual ~COpenGLViewport(); //!< Destructor: clears all objects. 00270 00271 00272 protected: 00273 /** Constructor, invoked from COpenGLScene only. 00274 */ 00275 COpenGLViewport( COpenGLScene *parent=NULL, const std::string &name=std::string("") ); 00276 00277 /** Initializes all textures in the scene (See opengl::CTexturedPlane::loadTextureInOpenGL) 00278 */ 00279 void initializeAllTextures(); 00280 00281 /** Retrieves a list of all objects in text form. 00282 */ 00283 void dumpListOfObjects( utils::CStringList &lst ); 00284 00285 /** Render the objects in this viewport (called from COpenGLScene only) */ 00286 void render( const int render_width, const int render_height ) const; 00287 00288 /** The camera associated to the viewport */ 00289 opengl::CCamera m_camera; 00290 utils::safe_ptr<COpenGLScene> m_parent; //!< The scene that contains this viewport. 00291 bool m_isCloned; //!< Set by setCloneView 00292 bool m_isClonedCamera; //!< Set by setCloneCamera 00293 std::string m_clonedViewport; //!< Only if m_isCloned=true 00294 std::string m_name; //!< The viewport's name 00295 bool m_isTransparent; //!< Whether to clear color buffer. 00296 uint32_t m_borderWidth; //!< Default=0, the border around the viewport. 00297 double m_view_x, m_view_y,m_view_width,m_view_height; //!< The viewport position [0,1] 00298 double m_clip_min,m_clip_max; //!< The min/max clip depth distances (default: 0.1 - 10000) 00299 bool m_custom_backgb_color; 00300 TColorf m_background_color; //!< used only if m_custom_backgb_color 00301 bool m_isImageView; //!< Set by setImageView 00302 CRenderizablePtr m_imageview_quad ; //!< A mrpt::opengl::CTexturedPlane used after setImageView() is called 00303 00304 struct TLastProjectiveMatrixInfo 00305 { 00306 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), zoom(1) 00307 {} 00308 bool is_projective; // true: projective, false: ortho 00309 TPoint3D eye; //!< The camera is here. 00310 TPoint3D pointing; //!< The camera points to here 00311 TPoint3D up; //!< Up vector of the camera. 00312 float FOV; //!< FOV in degrees. 00313 size_t viewport_width, viewport_height; //!< In pixels. This may be smaller than the total render window. 00314 float azimuth, elev; //!< Camera elev & azimuth, in radians. 00315 float zoom; 00316 }; 00317 mutable TLastProjectiveMatrixInfo m_lastProjMat; //!< Info updated with each "render()" and used in "get3DRayForPixelCoord" 00318 00319 /** The list of objects that comprise the 3D scene. 00320 * Objects are automatically deleted when calling "clear" or in the destructor. 00321 */ 00322 opengl::CListOpenGLObjects m_objects; 00323 00324 void internal_setImageView_fast(const mrpt::utils::CImage &img, bool is_fast); 00325 00326 }; 00327 /** 00328 * Inserts an openGL object into a viewport. Allows call chaining. 00329 * \sa mrpt::opengl::COpenGLViewport::insert 00330 */ 00331 inline COpenGLViewportPtr &operator<<(COpenGLViewportPtr &s,const CRenderizablePtr &r) { 00332 s->insert(r); 00333 return s; 00334 } 00335 /** 00336 * Inserts any iterable set of openGL objects into a viewport. Allows call chaining. 00337 * \sa mrpt::opengl::COpenGLViewport::insert 00338 */ 00339 inline COpenGLViewportPtr &operator<<(COpenGLViewportPtr &s,const std::vector<CRenderizablePtr> &v) { 00340 for (std::vector<CRenderizablePtr>::const_iterator it=v.begin();it!=v.end();++it) s->insert(*it); 00341 return s; 00342 } 00343 00344 } // end namespace 00345 00346 } // End of namespace 00347 00348 00349 #endif
Page generated by Doxygen 1.7.2 for MRPT 0.9.4 SVN: at Mon Jan 10 22:30:30 UTC 2011 |