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/opengl/opengl_fonts.h> 00034 #include <mrpt/utils/CImage.h> 00035 00036 /*--------------------------------------------------------------- 00037 Class 00038 ---------------------------------------------------------------*/ 00039 namespace mrpt 00040 { 00041 namespace utils 00042 { 00043 class CImage; 00044 class CImageFloat; 00045 } 00046 00047 namespace gui 00048 { 00049 using namespace mrpt::utils; 00050 00051 class C3DWindowDialog; 00052 class CMyGLCanvas_DisplayWindow3D; 00053 00054 DEFINE_MRPT_OBJECT_PRE_CUSTOM_BASE_LINKAGE(CDisplayWindow3D, mrpt::gui::CBaseGUIWindow, GUI_IMPEXP) 00055 00056 /** A graphical user interface (GUI) for efficiently rendering 3D scenes in real-time. 00057 * This class always contains internally an instance of opengl::COpenGLScene, which 00058 * the objects, viewports, etc. to be rendered. 00059 * 00060 * Images can be grabbed automatically to disk for easy creation of videos. 00061 * See CDisplayWindow3D::grabImagesStart (and for creating videos, mrpt::utils::CVideoFileWriter). 00062 * 00063 * 00064 * Since the 3D rendering is performed in a detached thread, especial care must be taken 00065 * when updating the 3D scene to be rendered. The process involves an internal critical section 00066 * and it must always consist of these steps: 00067 * 00068 \code 00069 CDisplayWindow3D win("My window"); 00070 00071 // Adquire the scene: 00072 opengl::COpenGLScenePtr &ptrScene = win.get3DSceneAndLock(); 00073 00074 // Modify the scene: 00075 ptrScene->... 00076 // or replace by another scene: 00077 ptrScene = otherScene; 00078 00079 // Unlock it, so the window can use it for redraw: 00080 win.unlockAccess3DScene(); 00081 00082 // Update window, if required 00083 win.forceRepaint(); 00084 \endcode 00085 * 00086 * An alternative way of updating the scene is by creating, before locking the 3D window, a new object 00087 * of class COpenGLScene, then locking the window only for replacing the smart pointer. This may be 00088 * advantageous is generating the 3D scene takes a long time, since while the window 00089 * is locked it will not be responsive to the user input or window redraw. 00090 * 00091 * The window can also display a set of 2D text messages overlapped to the 3D scene. 00092 * See CDisplayWindow3D::add2DTextMessage 00093 * 00094 * For a list of supported events with the observer/observable pattern, see the discussion in mrpt::gui::CBaseGUIWindow. 00095 * 00096 * 00097 * \sa The example /samples/display3D, the <a href="http://www.mrpt.org/Tutorial_3D_Scenes">tutorial on the wiki</a>. 00098 */ 00099 class GUI_IMPEXP CDisplayWindow3D : public mrpt::gui::CBaseGUIWindow 00100 { 00101 // This must be added to any CObject derived class: 00102 DEFINE_MRPT_OBJECT( CDisplayWindow3D ) 00103 00104 friend class C3DWindowDialog; 00105 friend class CMyGLCanvas_DisplayWindow3D; 00106 00107 00108 float m_minRange,m_maxRange,m_FOV; 00109 00110 00111 /** Internal OpenGL object (see general discussion in about usage of this object) 00112 */ 00113 opengl::COpenGLScenePtr m_3Dscene; 00114 00115 /** Critical section for accesing m_3Dscene 00116 */ 00117 synch::CCriticalSection m_csAccess3DScene; 00118 00119 /** Throws an exception on initialization error 00120 */ 00121 void createOpenGLContext(); 00122 00123 void_ptr_noncopy m_DisplayDeviceContext; 00124 void_ptr_noncopy m_GLRenderingContext; 00125 00126 std::string m_grab_imgs_prefix; 00127 unsigned int m_grab_imgs_idx; 00128 00129 bool m_is_capturing_imgs; 00130 CImagePtr m_last_captured_img; 00131 synch::CCriticalSection m_last_captured_img_cs; 00132 00133 void doRender(); 00134 00135 mrpt::system::TTimeStamp m_lastFullScreen; 00136 00137 public: 00138 /** Constructor 00139 */ 00140 CDisplayWindow3D( 00141 const std::string &windowCaption = std::string(), 00142 unsigned int initialWindowWidth = 400, 00143 unsigned int initialWindowHeight = 300 ); 00144 00145 /** Class factory returning a smart pointer */ 00146 static CDisplayWindow3DPtr Create( 00147 const std::string &windowCaption = std::string(), 00148 unsigned int initialWindowWidth = 400, 00149 unsigned int initialWindowHeight = 300 ) 00150 { 00151 return CDisplayWindow3DPtr(new CDisplayWindow3D(windowCaption,initialWindowWidth,initialWindowHeight)); 00152 } 00153 00154 /** Destructor 00155 */ 00156 virtual ~CDisplayWindow3D(); 00157 00158 /** Gets a reference to the smart shared pointer that holds the internal scene (carefuly read introduction in gui::CDisplayWindow3D before use!) 00159 * This also locks the critical section for accesing the scene, thus the window will not be repainted until it is unlocked. 00160 */ 00161 opengl::COpenGLScenePtr & get3DSceneAndLock( ); 00162 00163 /** Unlocks the access to the internal 3D scene. 00164 * Typically user will want to call forceRepaint after updating the scene. 00165 */ 00166 void unlockAccess3DScene(); 00167 00168 /** Repaints the window. 00169 * forceRepaint, repaint and updateWindow are all aliases of the same method. 00170 */ 00171 void forceRepaint(); 00172 00173 /** Repaints the window. 00174 * forceRepaint, repaint and updateWindow are all aliases of the same method. 00175 */ 00176 void repaint() { forceRepaint(); } 00177 00178 /** Repaints the window. 00179 * forceRepaint, repaint and updateWindow are all aliases of the same method. 00180 */ 00181 void updateWindow() { forceRepaint(); } 00182 00183 /** Return the camera min range (z) (used for gluPerspective). 00184 */ 00185 float getMinRange() const { return m_minRange; }; 00186 00187 /** Return the camera max range (z) (used for gluPerspective). 00188 */ 00189 float getMaxRange() const { return m_maxRange; }; 00190 00191 /** Return the camera field of view (in degrees) (used for gluPerspective). 00192 */ 00193 float getFOV() const { return m_FOV; }; 00194 00195 /** Changes the camera min range (z) (used for gluPerspective). 00196 * The window is not updated with this method, call "forceRepaint" to update the 3D view. 00197 */ 00198 void setMinRange(float v) { m_minRange=v; }; 00199 00200 /** Changes the camera max range (z) (used for gluPerspective). 00201 * The window is not updated with this method, call "forceRepaint" to update the 3D view. 00202 */ 00203 void setMaxRange(float v) { m_maxRange=v; }; 00204 00205 /** Changes the camera field of view (in degrees) (used for gluPerspective). 00206 * The window is not updated with this method, call "forceRepaint" to update the 3D view. 00207 */ 00208 void setFOV(float v) { m_FOV=v; }; 00209 00210 /** Resizes the window, stretching the image to fit into the display area. 00211 */ 00212 void resize( unsigned int width, unsigned int height ); 00213 00214 /** Changes the position of the window on the screen. 00215 */ 00216 void setPos( int x, int y ); 00217 00218 /** Changes the window title. 00219 */ 00220 void setWindowTitle( const std::string &str ); 00221 00222 /** Changes the camera parameters programatically 00223 */ 00224 void setCameraElevationDeg( float deg ); 00225 00226 /** Changes the camera parameters programatically 00227 */ 00228 void setCameraAzimuthDeg( float deg ); 00229 00230 /** Changes the camera parameters programatically 00231 */ 00232 void setCameraPointingToPoint( float x,float y, float z ); 00233 00234 /** Changes the camera parameters programatically 00235 */ 00236 void setCameraZoom( float zoom ); 00237 00238 /** Sets the camera as projective, or orthogonal. */ 00239 void setCameraProjective( bool isProjective ); 00240 00241 00242 /** Get camera parameters programatically */ 00243 float getCameraElevationDeg() const; 00244 00245 /** Get camera parameters programatically */ 00246 float getCameraAzimuthDeg() const; 00247 00248 /** Get camera parameters programatically */ 00249 void getCameraPointingToPoint( float &x,float &y, float &z ) const; 00250 00251 /** Get camera parameters programatically */ 00252 float getCameraZoom() const; 00253 00254 /** Sets the camera as projective, or orthogonal. */ 00255 bool isCameraProjective() const; 00256 00257 /** If set to true (default = false), the mouse-based scene navigation will be disabled and the camera position will be determined by the opengl viewports in the 3D scene. 00258 */ 00259 void useCameraFromScene(bool useIt = true); 00260 00261 /** Gets the last x,y pixel coordinates of the mouse. \return False if the window is closed. */ 00262 bool getLastMousePosition(int &x, int &y) const; 00263 00264 /** Set cursor style to default (cursorIsCross=false) or to a cross (cursorIsCross=true) */ 00265 void setCursorCross(bool cursorIsCross); 00266 00267 /** Start to save rendered images to disk. 00268 * Images will be saved independently as png files, depending on 00269 * the template path passed to this method. For example: 00270 * 00271 * path_prefix: "./video_" 00272 * 00273 * Will generate "./video_000001.png", etc. 00274 * 00275 * \sa grabImagesStop 00276 */ 00277 void grabImagesStart( const std::string &grab_imgs_prefix = std::string("video_") ); 00278 00279 /** Stops image grabbing started by grabImagesStart 00280 * \sa grabImagesStart 00281 */ 00282 void grabImagesStop(); 00283 00284 /** Enables the grabbing of CImage objects from screenshots of the window. 00285 * \sa getLastWindowImage 00286 */ 00287 void captureImagesStart(); 00288 00289 /** Stop image grabbing 00290 * \sa captureImagesStart 00291 */ 00292 void captureImagesStop(); 00293 00294 /** Retrieve the last captured image from the window. 00295 * You MUST CALL FIRST captureImagesStart to enable image grabbing. 00296 * \sa captureImagesStart, getLastWindowImagePtr 00297 */ 00298 void getLastWindowImage( mrpt::utils::CImage &out_img) const; 00299 00300 /** Retrieve the last captured image from the window, as a smart pointer. 00301 * This method is more efficient than getLastWindowImage since only a copy of the pointer is performed, while 00302 * getLastWindowImage would copy the entire image. 00303 * 00304 * You MUST CALL FIRST captureImagesStart to enable image grabbing. 00305 * \sa captureImagesStart, getLastWindowImage 00306 */ 00307 mrpt::utils::CImagePtr getLastWindowImagePtr() const; 00308 00309 /** Increments by one the image counter and return the next image file name (Users normally don't want to call this method). 00310 * \sa grabImagesStart 00311 */ 00312 std::string grabImageGetNextFile(); 00313 00314 bool isCapturingImgs() const { return m_is_capturing_imgs; } 00315 00316 00317 /** Add 2D text messages overlapped to the 3D rendered scene. The string will remain displayed in the 3D window 00318 * until it's changed with subsequent calls to this same method, or all the texts are cleared with clearTextMessages(). 00319 * 00320 * \param x The X position, interpreted as absolute pixels from the left if X>=1, absolute pixels from the left if X<0 or as a width factor if in the range [0,1[. 00321 * \param y The Y position, interpreted as absolute pixels from the bottom if Y>=1, absolute pixels from the top if Y<0 or as a height factor if in the range [0,1[. 00322 * \param text The text string to display. 00323 * \param color The text color. For example: TColorf(1.0,1.0,1.0) 00324 * \param unique_index An "index" for this text message, so that subsequent calls with the same index will overwrite this text message instead of creating new ones. 00325 * 00326 * You'll need to refresh the display manually with forceRepaint(). 00327 * 00328 * \sa clearTextMessages 00329 */ 00330 void addTextMessage( 00331 const double x, 00332 const double y, 00333 const std::string &text, 00334 const mrpt::utils::TColorf &color = mrpt::utils::TColorf(1.0,1.0,1.0), 00335 const size_t unique_index = 0, 00336 const TOpenGLFont font = MRPT_GLUT_BITMAP_TIMES_ROMAN_24 00337 ); 00338 00339 /** Clear all text messages created with addTextMessage(). 00340 * You'll need to refresh the display manually with forceRepaint(). 00341 * \sa addTextMessage 00342 */ 00343 void clearTextMessages(); 00344 00345 }; // End of class def. 00346 } // End of namespace 00347 } // End of namespace 00348 00349 #endif
| Page generated by Doxygen 1.7.1 for MRPT 0.9.1 SVN:exported at Fri Aug 6 10:31:35 UTC 2010 |
