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 CDisplayWindow3D_H 00029 #define CDisplayWindow3D_H 00030 00031 #include <mrpt/gui/CBaseGUIWindow.h> 00032 #include <mrpt/opengl.h> 00033 #include <mrpt/utils/CImage.h> 00034 00035 /*--------------------------------------------------------------- 00036 Class 00037 ---------------------------------------------------------------*/ 00038 namespace mrpt 00039 { 00040 namespace utils 00041 { 00042 class CImage; 00043 class CImageFloat; 00044 } 00045 00046 namespace gui 00047 { 00048 using namespace mrpt::utils; 00049 00050 class C3DWindowDialog; 00051 class CMyGLCanvas_DisplayWindow3D; 00052 00053 DEFINE_SERIALIZABLE_PRE(CDisplayWindow3D) 00054 00055 /** A graphical user interface (GUI) for efficiently rendering 3D scenes in real-time. 00056 * This class always contains internally an instance of opengl::COpenGLScene, which 00057 * the objects, viewports, etc. to be rendered. 00058 * 00059 * Since MRPT 0.6.2, images can be grabbed automatically to disk for easy creation of videos. 00060 * See CDisplayWindow3D::grabImagesStart 00061 * 00062 * 00063 * Since the 3D rendering is performed in a detached thread, especial care must be taken 00064 * when updating the 3D scene to be rendered. The process involves an internal critical section 00065 * and it must always consist of these steps: 00066 * 00067 \code 00068 CDisplayWindow3D win("My window"); 00069 00070 // Adquire the scene: 00071 opengl::COpenGLScenePtr &ptrScene = win.get3DSceneAndLock(); 00072 00073 // Modify the scene: 00074 ptrScene->... 00075 // or replace by another scene: 00076 ptrScene = otherScene; 00077 00078 // Unlock it, so the window can use it for redraw: 00079 win.unlockAccess3DScene(); 00080 00081 // Update window, if required 00082 win.forceRepaint(); 00083 \endcode 00084 * 00085 * An alternative way of updating the scene is by creating, before locking the 3D window, a new object 00086 * of class COpenGLScene, then locking the window only for replacing the smart pointer. This may be 00087 * advantageous is generating the 3D scene takes a long time, since while the window 00088 * is locked it will not be responsive to the user input or window redraw. 00089 * 00090 * \sa The example /samples/display3D, the <a href="http://babel.isa.uma.es/mrpt/index.php/Tutorial_3D_Scenes">tutorial on the wiki</a>. 00091 */ 00092 class MRPTDLLIMPEXP CDisplayWindow3D : public mrpt::utils::CSerializable, public mrpt::gui::CBaseGUIWindow 00093 { 00094 // This must be added to any CSerializable derived class: 00095 DEFINE_SERIALIZABLE( CDisplayWindow3D ) 00096 00097 friend class C3DWindowDialog; 00098 friend class CMyGLCanvas_DisplayWindow3D; 00099 00100 00101 float m_minRange,m_maxRange,m_FOV; 00102 00103 00104 /** Internal OpenGL object (see general discussion in about usage of this object) 00105 */ 00106 opengl::COpenGLScenePtr m_3Dscene; 00107 00108 /** Critical section for accesing m_3Dscene 00109 */ 00110 synch::CCriticalSection m_csAccess3DScene; 00111 00112 /** Throws an exception on initialization error 00113 */ 00114 void createOpenGLContext(); 00115 00116 void_ptr_noncopy m_DisplayDeviceContext; 00117 void_ptr_noncopy m_GLRenderingContext; 00118 00119 std::string m_grab_imgs_prefix; 00120 unsigned int m_grab_imgs_idx; 00121 00122 bool m_is_capturing_imgs; 00123 CImagePtr m_last_captured_img; 00124 synch::CCriticalSection m_last_captured_img_cs; 00125 00126 void doRender(); 00127 00128 public: 00129 /** Constructor 00130 */ 00131 CDisplayWindow3D( 00132 const std::string &windowCaption = std::string(), 00133 unsigned int initialWindowWidth = 400, 00134 unsigned int initialWindowHeight = 300 ); 00135 00136 /** Class factory returning a smart pointer */ 00137 static CDisplayWindow3DPtr Create( 00138 const std::string &windowCaption = std::string(), 00139 unsigned int initialWindowWidth = 400, 00140 unsigned int initialWindowHeight = 300 ) 00141 { 00142 return CDisplayWindow3DPtr(new CDisplayWindow3D(windowCaption,initialWindowWidth,initialWindowHeight)); 00143 } 00144 00145 /** Destructor 00146 */ 00147 virtual ~CDisplayWindow3D(); 00148 00149 /** Gets a reference to the smart shared pointer that holds the internal scene (carefuly read introduction in gui::CDisplayWindow3D before use!) 00150 * This also locks the critical section for accesing the scene, thus the window will not be repainted until it is unlocked. 00151 */ 00152 opengl::COpenGLScenePtr & get3DSceneAndLock( ); 00153 00154 /** Unlocks the access to the internal 3D scene. 00155 * Typically user will want to call forceRepaint after updating the scene. 00156 */ 00157 void unlockAccess3DScene(); 00158 00159 /** Repaints the window. 00160 * forceRepaint, repaint and updateWindow are all aliases of the same method. 00161 */ 00162 void forceRepaint(); 00163 00164 /** Repaints the window. 00165 * forceRepaint, repaint and updateWindow are all aliases of the same method. 00166 */ 00167 void repaint() { forceRepaint(); } 00168 00169 /** Repaints the window. 00170 * forceRepaint, repaint and updateWindow are all aliases of the same method. 00171 */ 00172 void updateWindow() { forceRepaint(); } 00173 00174 /** Return the camera min range (z) (used for gluPerspective). 00175 */ 00176 float getMinRange() const { return m_minRange; }; 00177 00178 /** Return the camera max range (z) (used for gluPerspective). 00179 */ 00180 float getMaxRange() const { return m_maxRange; }; 00181 00182 /** Return the camera field of view (in degrees) (used for gluPerspective). 00183 */ 00184 float getFOV() const { return m_FOV; }; 00185 00186 /** Changes the camera min range (z) (used for gluPerspective). 00187 * The window is not updated with this method, call "forceRepaint" to update the 3D view. 00188 */ 00189 void setMinRange(float v) { m_minRange=v; }; 00190 00191 /** Changes the camera max range (z) (used for gluPerspective). 00192 * The window is not updated with this method, call "forceRepaint" to update the 3D view. 00193 */ 00194 void setMaxRange(float v) { m_maxRange=v; }; 00195 00196 /** Changes the camera field of view (in degrees) (used for gluPerspective). 00197 * The window is not updated with this method, call "forceRepaint" to update the 3D view. 00198 */ 00199 void setFOV(float v) { m_FOV=v; }; 00200 00201 /** Resizes the window, stretching the image to fit into the display area. 00202 */ 00203 void resize( unsigned int width, unsigned int height ); 00204 00205 /** Changes the position of the window on the screen. 00206 */ 00207 void setPos( int x, int y ); 00208 00209 /** Changes the window title. 00210 */ 00211 void setWindowTitle( const std::string &str ); 00212 00213 /** Changes the camera parameters programatically 00214 */ 00215 void setCameraElevationDeg( float deg ); 00216 00217 /** Changes the camera parameters programatically 00218 */ 00219 void setCameraAzimuthDeg( float deg ); 00220 00221 /** Changes the camera parameters programatically 00222 */ 00223 void setCameraPointingToPoint( float x,float y, float z ); 00224 00225 /** Changes the camera parameters programatically 00226 */ 00227 void setCameraZoom( float zoom ); 00228 00229 /** Sets the camera as projective, or orthogonal. */ 00230 void setCameraProjective( bool isProjective ); 00231 00232 00233 /** Get camera parameters programatically */ 00234 float getCameraElevationDeg() const; 00235 00236 /** Get camera parameters programatically */ 00237 float getCameraAzimuthDeg() const; 00238 00239 /** Get camera parameters programatically */ 00240 void getCameraPointingToPoint( float &x,float &y, float &z ) const; 00241 00242 /** Get camera parameters programatically */ 00243 float getCameraZoom() const; 00244 00245 /** Sets the camera as projective, or orthogonal. */ 00246 bool isCameraProjective() const; 00247 00248 00249 /** Start to save rendered images to disk. 00250 * Images will be saved independently as png files, depending on 00251 * the template path passed to this method. For example: 00252 * 00253 * path_prefix: "./video_" 00254 * 00255 * Will generate "./video_000001.png", etc. 00256 * 00257 * \sa grabImagesStop 00258 */ 00259 void grabImagesStart( const std::string &grab_imgs_prefix = std::string("video_") ); 00260 00261 /** Stops image grabbing started by grabImagesStart 00262 * \sa grabImagesStart 00263 */ 00264 void grabImagesStop(); 00265 00266 /** Enables the grabbing of CImage objects from screenshots of the window. 00267 * \sa getLastWindowImage 00268 */ 00269 void captureImagesStart(); 00270 00271 /** Stop image grabbing 00272 * \sa captureImagesStart 00273 */ 00274 void captureImagesStop(); 00275 00276 /** Retrieve the last captured image from the window. 00277 * You MUST CALL FIRST captureImagesStart to enable image grabbing. 00278 * \sa captureImagesStart, getLastWindowImagePtr 00279 */ 00280 void getLastWindowImage( mrpt::utils::CImage &out_img) const; 00281 00282 /** Retrieve the last captured image from the window, as a smart pointer. 00283 * This method is more efficient than getLastWindowImage since only a copy of the pointer is performed, while 00284 * getLastWindowImage would copy the entire image. 00285 * 00286 * You MUST CALL FIRST captureImagesStart to enable image grabbing. 00287 * \sa captureImagesStart, getLastWindowImage 00288 */ 00289 mrpt::utils::CImagePtr getLastWindowImagePtr() const; 00290 00291 /** Increments by one the image counter and return the next image file name (Users normally don't want to call this method). 00292 * \sa grabImagesStart 00293 */ 00294 std::string grabImageGetNextFile(); 00295 00296 bool isCapturingImgs() const { return m_is_capturing_imgs; } 00297 00298 }; // End of class def. 00299 } // End of namespace 00300 } // End of namespace 00301 00302 #endif
| Page generated by Doxygen 1.6.2 for MRPT 0.8.1 SVN:exported at Mon Feb 15 22:01:07 UTC 2010 |
