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 public: 00136 /** Constructor 00137 */ 00138 CDisplayWindow3D( 00139 const std::string &windowCaption = std::string(), 00140 unsigned int initialWindowWidth = 400, 00141 unsigned int initialWindowHeight = 300 ); 00142 00143 /** Class factory returning a smart pointer */ 00144 static CDisplayWindow3DPtr Create( 00145 const std::string &windowCaption = std::string(), 00146 unsigned int initialWindowWidth = 400, 00147 unsigned int initialWindowHeight = 300 ) 00148 { 00149 return CDisplayWindow3DPtr(new CDisplayWindow3D(windowCaption,initialWindowWidth,initialWindowHeight)); 00150 } 00151 00152 /** Destructor 00153 */ 00154 virtual ~CDisplayWindow3D(); 00155 00156 /** Gets a reference to the smart shared pointer that holds the internal scene (carefuly read introduction in gui::CDisplayWindow3D before use!) 00157 * This also locks the critical section for accesing the scene, thus the window will not be repainted until it is unlocked. 00158 */ 00159 opengl::COpenGLScenePtr & get3DSceneAndLock( ); 00160 00161 /** Unlocks the access to the internal 3D scene. 00162 * Typically user will want to call forceRepaint after updating the scene. 00163 */ 00164 void unlockAccess3DScene(); 00165 00166 /** Repaints the window. 00167 * forceRepaint, repaint and updateWindow are all aliases of the same method. 00168 */ 00169 void forceRepaint(); 00170 00171 /** Repaints the window. 00172 * forceRepaint, repaint and updateWindow are all aliases of the same method. 00173 */ 00174 void repaint() { forceRepaint(); } 00175 00176 /** Repaints the window. 00177 * forceRepaint, repaint and updateWindow are all aliases of the same method. 00178 */ 00179 void updateWindow() { forceRepaint(); } 00180 00181 /** Return the camera min range (z) (used for gluPerspective). 00182 */ 00183 float getMinRange() const { return m_minRange; }; 00184 00185 /** Return the camera max range (z) (used for gluPerspective). 00186 */ 00187 float getMaxRange() const { return m_maxRange; }; 00188 00189 /** Return the camera field of view (in degrees) (used for gluPerspective). 00190 */ 00191 float getFOV() const { return m_FOV; }; 00192 00193 /** Changes the camera min range (z) (used for gluPerspective). 00194 * The window is not updated with this method, call "forceRepaint" to update the 3D view. 00195 */ 00196 void setMinRange(float v) { m_minRange=v; }; 00197 00198 /** Changes the camera max range (z) (used for gluPerspective). 00199 * The window is not updated with this method, call "forceRepaint" to update the 3D view. 00200 */ 00201 void setMaxRange(float v) { m_maxRange=v; }; 00202 00203 /** Changes the camera field of view (in degrees) (used for gluPerspective). 00204 * The window is not updated with this method, call "forceRepaint" to update the 3D view. 00205 */ 00206 void setFOV(float v) { m_FOV=v; }; 00207 00208 /** Resizes the window, stretching the image to fit into the display area. 00209 */ 00210 void resize( unsigned int width, unsigned int height ); 00211 00212 /** Changes the position of the window on the screen. 00213 */ 00214 void setPos( int x, int y ); 00215 00216 /** Changes the window title. 00217 */ 00218 void setWindowTitle( const std::string &str ); 00219 00220 /** Changes the camera parameters programatically 00221 */ 00222 void setCameraElevationDeg( float deg ); 00223 00224 /** Changes the camera parameters programatically 00225 */ 00226 void setCameraAzimuthDeg( float deg ); 00227 00228 /** Changes the camera parameters programatically 00229 */ 00230 void setCameraPointingToPoint( float x,float y, float z ); 00231 00232 /** Changes the camera parameters programatically 00233 */ 00234 void setCameraZoom( float zoom ); 00235 00236 /** Sets the camera as projective, or orthogonal. */ 00237 void setCameraProjective( bool isProjective ); 00238 00239 00240 /** Get camera parameters programatically */ 00241 float getCameraElevationDeg() const; 00242 00243 /** Get camera parameters programatically */ 00244 float getCameraAzimuthDeg() const; 00245 00246 /** Get camera parameters programatically */ 00247 void getCameraPointingToPoint( float &x,float &y, float &z ) const; 00248 00249 /** Get camera parameters programatically */ 00250 float getCameraZoom() const; 00251 00252 /** Sets the camera as projective, or orthogonal. */ 00253 bool isCameraProjective() const; 00254 00255 /** 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. 00256 */ 00257 void useCameraFromScene(bool useIt = true); 00258 00259 /** Gets the last x,y pixel coordinates of the mouse. \return False if the window is closed. */ 00260 bool getLastMousePosition(int &x, int &y) const; 00261 00262 /** Set cursor style to default (cursorIsCross=false) or to a cross (cursorIsCross=true) */ 00263 void setCursorCross(bool cursorIsCross); 00264 00265 /** Start to save rendered images to disk. 00266 * Images will be saved independently as png files, depending on 00267 * the template path passed to this method. For example: 00268 * 00269 * path_prefix: "./video_" 00270 * 00271 * Will generate "./video_000001.png", etc. 00272 * 00273 * \sa grabImagesStop 00274 */ 00275 void grabImagesStart( const std::string &grab_imgs_prefix = std::string("video_") ); 00276 00277 /** Stops image grabbing started by grabImagesStart 00278 * \sa grabImagesStart 00279 */ 00280 void grabImagesStop(); 00281 00282 /** Enables the grabbing of CImage objects from screenshots of the window. 00283 * \sa getLastWindowImage 00284 */ 00285 void captureImagesStart(); 00286 00287 /** Stop image grabbing 00288 * \sa captureImagesStart 00289 */ 00290 void captureImagesStop(); 00291 00292 /** Retrieve the last captured image from the window. 00293 * You MUST CALL FIRST captureImagesStart to enable image grabbing. 00294 * \sa captureImagesStart, getLastWindowImagePtr 00295 */ 00296 void getLastWindowImage( mrpt::utils::CImage &out_img) const; 00297 00298 /** Retrieve the last captured image from the window, as a smart pointer. 00299 * This method is more efficient than getLastWindowImage since only a copy of the pointer is performed, while 00300 * getLastWindowImage would copy the entire image. 00301 * 00302 * You MUST CALL FIRST captureImagesStart to enable image grabbing. 00303 * \sa captureImagesStart, getLastWindowImage 00304 */ 00305 mrpt::utils::CImagePtr getLastWindowImagePtr() const; 00306 00307 /** Increments by one the image counter and return the next image file name (Users normally don't want to call this method). 00308 * \sa grabImagesStart 00309 */ 00310 std::string grabImageGetNextFile(); 00311 00312 bool isCapturingImgs() const { return m_is_capturing_imgs; } 00313 00314 00315 /** Add 2D text messages overlapped to the 3D rendered scene. The string will remain displayed in the 3D window 00316 * until it's changed with subsequent calls to this same method, or all the texts are cleared with clearTextMessages(). 00317 * 00318 * \param x_frac The X position, in the range [0,1] (left,right, of the 3D viewport). 00319 * \param y_frac The Y position, in the range [0,1] (bottom,top, of the 3D viewport). 00320 * \param text The text string to display. 00321 * \param color The text color. For example: TColorf(1.0,1.0,1.0) 00322 * \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. 00323 * 00324 * You'll need to refresh the display manually with forceRepaint(). 00325 * 00326 * \sa clearTextMessages 00327 */ 00328 void addTextMessage( 00329 const double x_frac, 00330 const double y_frac, 00331 const std::string &text, 00332 const mrpt::utils::TColorf &color = mrpt::utils::TColorf(1.0,1.0,1.0), 00333 const size_t unique_index = 0, 00334 const TOpenGLFont font = MRPT_GLUT_BITMAP_TIMES_ROMAN_24 00335 ); 00336 00337 /** Clear all text messages created with addTextMessage(). 00338 * You'll need to refresh the display manually with forceRepaint(). 00339 * \sa addTextMessage 00340 */ 00341 void clearTextMessages(); 00342 00343 }; // End of class def. 00344 } // End of namespace 00345 } // End of namespace 00346 00347 #endif
| Page generated by Doxygen 1.6.1 for MRPT 0.9.0 SVN: at Mon Jun 7 06:47:58 UTC 2010 |
