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