# Visualization Library

A lightweight C++ OpenGL middleware for 2D/3D graphics

[Home] [Tutorials] [All Classes] [Grouped Classes]

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# D:/VL/BuildSystem/VL_DISTRO/Visualization_Library_SDK/src/vlGLUT/GLUTWindow.cpp

Go to the documentation of this file.

    00001 /**************************************************************************************/
    00002 /*                                                                                    */
    00003 /*  Visualization Library                                                             */
    00004 /*  http://www.visualizationlibrary.com                                               */
    00005 /*                                                                                    */
    00006 /*  Copyright (c) 2005-2010, Michele Bosi                                             */
    00007 /*  All rights reserved.                                                              */
    00008 /*                                                                                    */
    00009 /*  Redistribution and use in source and binary forms, with or without modification,  */
    00010 /*  are permitted provided that the following conditions are met:                     */
    00011 /*                                                                                    */
    00012 /*  - Redistributions of source code must retain the above copyright notice, this     */
    00013 /*  list of conditions and the following disclaimer.                                  */
    00014 /*                                                                                    */
    00015 /*  - Redistributions in binary form must reproduce the above copyright notice, this  */
    00016 /*  list of conditions and the following disclaimer in the documentation and/or       */
    00017 /*  other materials provided with the distribution.                                   */
    00018 /*                                                                                    */
    00019 /*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND   */
    00020 /*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED     */
    00021 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE            */
    00022 /*  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR  */
    00023 /*  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    */
    00024 /*  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;      */
    00025 /*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON    */
    00026 /*  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           */
    00027 /*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS     */
    00028 /*  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                      */
    00029 /*                                                                                    */
    00030 /**************************************************************************************/
    00031
    00032 #include <vlGLUT/GLUTWindow.hpp>
    00033 #include <vlGraphics/Applet.hpp>
    00034 #include <vlCore/VisualizationLibrary.hpp>
    00035 #include <vlCore/Log.hpp>
    00036 #include <vlCore/Say.hpp>
    00037
    00038 using namespace vlGLUT;
    00039
    00040 //-----------------------------------------------------------------------------
    00041 std::map< int, GLUTWindow* > GLUTWindow::mWinMap;
    00042 //-----------------------------------------------------------------------------
    00043 GLUTWindow::GLUTWindow()
    00044 {
    00045   mInited = false;
    00046   mHandle = 0;
    00047 }
    00048 //-----------------------------------------------------------------------------
    00049 GLUTWindow::GLUTWindow(const vl::String& title, const vl::OpenGLContextFormat& info, int x, int y, int width, int height)
    00050 {
    00051   mInited = false;
    00052   mHandle = 0;
    00053
    00054   initGLUTWindow(title, info, x, y, width, height);
    00055 }
    00056 //-----------------------------------------------------------------------------
    00057 bool GLUTWindow::initGLUTWindow(const vl::String& title, const vl::OpenGLContextFormat& info, int x, int y, int width, int height)
    00058 {
    00059   setOpenGLContextInfo(info);
    00060
    00061   int flags = GLUT_RGB;
    00062   if (info.rgbaBits().a()) flags |= GLUT_ALPHA;
    00063   if (info.accumRGBABits().r()|info.accumRGBABits().g()|info.accumRGBABits().b()|info.accumRGBABits().a()) flags |= GLUT_ACCUM;
    00064   if (info.doubleBuffer()) flags |= GLUT_DOUBLE;
    00065   if (info.depthBufferBits()) flags |= GLUT_DEPTH;
    00066   if (info.stencilBufferBits()) flags |= GLUT_STENCIL;
    00067   if (info.multisample()) flags |= GLUT_MULTISAMPLE;
    00068   if (info.stereo()) flags |= GLUT_STEREO;
    00069
    00070   #if defined(WIN32)
    00071     if (info.fullscreen())
    00072     {
    00073       DEVMODE devmode;
    00074       EnumDisplaySettings(NULL,ENUM_CURRENT_SETTINGS,&devmode);
    00075       width  = devmode.dmPelsWidth;
    00076       height = devmode.dmPelsHeight;
    00077     }
    00078   #endif
    00079   mFullscreen = info.fullscreen();
    00080   glutInitDisplayMode( flags );
    00081   glutInitWindowSize( width, height );
    00082   glutInitWindowPosition( x, y );
    00083
    00084   mHandle = glutCreateWindow( title.toStdString().c_str() ) ;
    00085   if (info.fullscreen())
    00086     glutFullScreen();
    00087
    00088   glutSetWindow( handle() );
    00089
    00090   initGLContext();
    00091   // dispatchInitEvent();
    00092
    00093   setVSyncEnabled(info.vSync());
    00094
    00095   glutSetIconTitle(title.toStdString().c_str());
    00096   mWinMap[handle()] = this;
    00097
    00098   glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF);
    00099   initKeymap();
    00100
    00101   // install callbacks
    00102   glutDisplayFunc( glut_display_func );
    00103   glutReshapeFunc( glut_reshape_func );
    00104   glutKeyboardFunc( glut_keyboard_func );
    00105   glutKeyboardUpFunc( glut_keyboard_up_func );
    00106   glutMouseFunc( glut_mouse_func );
    00107   glutMotionFunc( glut_motion_func );
    00108   glutPassiveMotionFunc( glut_passive_motion_func );
    00109   glutVisibilityFunc( glut_visibility_func );
    00110   glutSpecialFunc( glut_special_func );
    00111   glutSpecialUpFunc( glut_special_up_func );
    00112 #if !defined(__APPLE__)
    00113   glutMouseWheelFunc( glut_mouse_wheel_func );
    00114 #endif
    00115
    00116   glutReshapeWindow(width, height);
    00117
    00118   // glutEntryFunc( glut_entry_func );
    00119   glutWMCloseFunc( glut_wmclose_func );
    00120 #if !defined(__APPLE__)
    00121   glutCloseFunc( glut_close_func );
    00122 #endif
    00123
    00124   // used for continuous update
    00125   glutIdleFunc(  glut_idle_func  );
    00126
    00127   // GLUT does not set the appropriate current window when calling these two, so we cannot use them.
    00128   // glutTimerFunc( millisecs, glut_timer_func, value );
    00129
    00130   return true;
    00131
    00132   /*** GLUT functions that would be nice to support ***/
    00133
    00135   // * Initialization functions, see fglut_init.c
    00136   // */
    00137   //glutInit( int* pargc, char** argv );
    00138   //glutInitWindowPosition( int x, int y );
    00139   //glutInitWindowSize( int width, int height );
    00140   //glutInitDisplayMode( unsigned int displayMode );
    00141   //glutInitDisplayString( const char* displayMode );
    00142
    00144   // * Process loop function, see freeglut_main.c
    00145   // */
    00146   //glutMainLoop( void );
    00147
    00149   // * Window management functions, see freeglut_window.c
    00150   // */
    00151   //glutCreateWindow( const char* title );
    00152   //glutCreateSubWindow( int window, int x, int y, int width, int height );
    00153   //glutDestroyWindow( int window );
    00154   //glutSetWindow( int window );
    00155   //glutGetWindow( void );
    00156   //glutSetWindowTitle( const char* title );
    00157   //glutSetIconTitle( const char* title );
    00158   //glutReshapeWindow( int width, int height );
    00159   //glutPositionWindow( int x, int y );
    00160   //glutShowWindow( void );
    00161   //glutHideWindow( void );
    00162   //glutIconifyWindow( void );
    00163   //glutPushWindow( void );
    00164   //glutPopWindow( void );
    00165   //glutFullScreen( void );
    00166
    00168   // * Display-connected functions, see freeglut_display.c
    00169   // */
    00170   //glutPostWindowRedisplay( int window );
    00171   //glutPostRedisplay( void );
    00172   //glutSwapBuffers( void );
    00173
    00175   // * Mouse cursor functions, see freeglut_cursor.c
    00176   // */
    00177   //glutWarpPointer( int x, int y );
    00178   //glutSetCursor( int cursor );
    00179
    00181   // * Overlay stuff, see freeglut_overlay.c
    00182   // */
    00183   //glutEstablishOverlay( void );
    00184   //glutRemoveOverlay( void );
    00185   //glutUseLayer( GLenum layer );
    00186   //glutPostOverlayRedisplay( void );
    00187   //glutPostWindowOverlayRedisplay( int window );
    00188   //glutShowOverlay( void );
    00189   //glutHideOverlay( void );
    00190
    00192   // * Menu stuff, see freeglut_menu.c
    00193   // */
    00194   //glutCreateMenu( void (* callback)( int menu ) );
    00195   //glutDestroyMenu( int menu );
    00196   //glutGetMenu( void );
    00197   //glutSetMenu( int menu );
    00198   //glutAddMenuEntry( const char* label, int value );
    00199   //glutAddSubMenu( const char* label, int subMenu );
    00200   //glutChangeToMenuEntry( int item, const char* label, int value );
    00201   //glutChangeToSubMenu( int item, const char* label, int value );
    00202   //glutRemoveMenuItem( int item );
    00203   //glutAttachMenu( int button );
    00204   //glutDetachMenu( int button );
    00205
    00207   // * Global callback functions, see freeglut_callbacks.c
    00208   // */
    00209   //glutTimerFunc( unsigned int time, void (* callback)( int ), int value );
    00210   //glutIdleFunc( void (* callback)( void ) );
    00211
    00213   // * Window-specific callback functions, see freeglut_callbacks.c
    00214   // */
    00215   //glutKeyboardFunc( void (* callback)( unsigned char, int, int ) );
    00216   //glutSpecialFunc( void (* callback)( int, int, int ) );
    00217   //glutReshapeFunc( void (* callback)( int, int ) );
    00218   //glutVisibilityFunc( void (* callback)( int ) );
    00219   //glutDisplayFunc( void (* callback)( void ) );
    00220   //glutMouseFunc( void (* callback)( int, int, int, int ) );
    00221   //glutMotionFunc( void (* callback)( int, int ) );
    00222   //glutPassiveMotionFunc( void (* callback)( int, int ) );
    00223   //glutEntryFunc( void (* callback)( int ) );
    00224
    00225   //glutKeyboardUpFunc( void (* callback)( unsigned char, int, int ) );
    00226   //glutSpecialUpFunc( void (* callback)( int, int, int ) );
    00227   //glutJoystickFunc( void (* callback)( unsigned int, int, int, int ), int pollInterval );
    00228   //glutMenuStateFunc( void (* callback)( int ) );
    00229   //glutMenuStatusFunc( void (* callback)( int, int, int ) );
    00230   //glutOverlayDisplayFunc( void (* callback)( void ) );
    00231   //glutWindowStatusFunc( void (* callback)( int ) );
    00232
    00233   //glutSpaceballMotionFunc( void (* callback)( int, int, int ) );
    00234   //glutSpaceballRotateFunc( void (* callback)( int, int, int ) );
    00235   //glutSpaceballButtonFunc( void (* callback)( int, int ) );
    00236   //glutButtonBoxFunc( void (* callback)( int, int ) );
    00237   //glutDialsFunc( void (* callback)( int, int ) );
    00238   //glutTabletMotionFunc( void (* callback)( int, int ) );
    00239   //glutTabletButtonFunc( void (* callback)( int, int, int, int ) );
    00240
    00242   // * State setting and retrieval functions, see freeglut_state.c
    00243   // */
    00244   //glutGet( GLenum query );
    00245   //glutDeviceGet( GLenum query );
    00246   //glutGetModifiers( void );
    00247   //glutLayerGet( GLenum query );
    00248
    00250   // * Font stuff, see freeglut_font.c
    00251   // */
    00252   //glutBitmapCharacter( void* font, int character );
    00253   //glutBitmapWidth( void* font, int character );
    00254   //glutStrokeCharacter( void* font, int character );
    00255   //glutStrokeWidth( void* font, int character );
    00256   //glutBitmapLength( void* font, const unsigned char* string );
    00257   //glutStrokeLength( void* font, const unsigned char* string );
    00258
    00260   // * Game mode functions, see freeglut_gamemode.c
    00261   // */
    00262   //glutGameModeString( const char* string );
    00263   //glutEnterGameMode( void );
    00264   //glutLeaveGameMode( void );
    00265   //glutGameModeGet( GLenum query );
    00266
    00268   // * Video resize functions, see freeglut_videoresize.c
    00269   // */
    00270   //glutVideoResizeGet( GLenum query );
    00271   //glutSetupVideoResizing( void );
    00272   //glutStopVideoResizing( void );
    00273   //glutVideoResize( int x, int y, int width, int height );
    00274   //glutVideoPan( int x, int y, int width, int height );
    00275
    00277   // * Misc keyboard and joystick functions, see freeglut_misc.c
    00278   // */
    00279   //glutIgnoreKeyRepeat( int ignore );
    00280   //glutSetKeyRepeat( int repeatMode );
    00281 }
    00282 //-----------------------------------------------------------------------------
    00283 bool GLUTWindow::setFullscreen(bool fs)
    00284 {
    00285   if ( handle() )
    00286   {
    00287     int prev = glutGetWindow();
    00288     glutSetWindow( handle() );
    00289     if (fs)
    00290     {
    00291       #if defined(WIN32)
    00292         DEVMODE devmode;
    00293         EnumDisplaySettings(NULL,ENUM_CURRENT_SETTINGS,&devmode);
    00294         int width  = devmode.dmPelsWidth;
    00295         int height = devmode.dmPelsHeight;
    00296         glutPositionWindow( 0, 0 );
    00297         glutReshapeWindow( width, height );
    00298       #endif
    00299       glutFullScreen();
    00300     }
    00301     else
    00302     {
    00303       glutPositionWindow( 0, 0 );
    00304       glutReshapeWindow( 640, 480 );
    00305     }
    00306     glutSetWindow( prev );
    00307     mFullscreen = fs;
    00308     return true;
    00309   }
    00310   else
    00311     return false;
    00312 }
    00313 //-----------------------------------------------------------------------------
    00314 void GLUTWindow::setMouseVisible(bool visible)
    00315 {
    00316   mMouseVisible = visible;
    00317   if (visible)
    00318     glutSetCursor(GLUT_CURSOR_LEFT_ARROW);
    00319   else
    00320     glutSetCursor(GLUT_CURSOR_NONE);
    00321 }
    00322 //-----------------------------------------------------------------------------
    00323 void GLUTWindow::setMousePosition(int x, int y)
    00324 {
    00325   glutWarpPointer(x,y);
    00326 }
    00327 //-----------------------------------------------------------------------------
    00328 void GLUTWindow::update()
    00329 {
    00330   if ( handle() )
    00331     glutPostWindowRedisplay( handle() );
    00332 }
    00333 //-----------------------------------------------------------------------------
    00334 void GLUTWindow::makeCurrent()
    00335 {
    00336   if ( handle() )
    00337     glutSetWindow( handle() );
    00338 }
    00339 //-----------------------------------------------------------------------------
    00340 void GLUTWindow::destroyWindow()
    00341 {
    00342   // according to GLUT specs pag 44 we can do this
    00343   if ( handle() )
    00344   {
    00345     // should trigger glut_close_func
    00346     glutDestroyWindow( handle() );
    00347   }
    00348 }
    00349 //-----------------------------------------------------------------------------
    00350 void GLUTWindow::updateOverlay()
    00351 {
    00352   glutPostOverlayRedisplay();
    00353 }
    00354 //-----------------------------------------------------------------------------
    00355 void GLUTWindow::swapBuffers()
    00356 {
    00357   glutSwapBuffers();
    00358 }
    00359 //-----------------------------------------------------------------------------
    00360 void GLUTWindow::show()
    00361 {
    00362   if ( handle() )
    00363   {
    00364     int prev = glutGetWindow();
    00365     glutSetWindow( handle() );
    00366     glutShowWindow();
    00367     glutSetWindow( prev );
    00368   }
    00369 }
    00370 //-----------------------------------------------------------------------------
    00371 void GLUTWindow::hide()
    00372 {
    00373   if ( handle() )
    00374   {
    00375     int prev = glutGetWindow();
    00376     glutSetWindow( handle() );
    00377     glutHideWindow();
    00378     glutSetWindow( prev );
    00379   }
    00380 }
    00381 //-----------------------------------------------------------------------------
    00382 void GLUTWindow::getFocus()
    00383 {
    00384   if ( handle() )
    00385     glutSetWindow( handle() );
    00386 }
    00387 //-----------------------------------------------------------------------------
    00388 void GLUTWindow::setWindowTitle(const vl::String& title)
    00389 {
    00390   if ( handle() )
    00391   {
    00392     int prev = glutGetWindow();
    00393     glutSetWindow( handle() );
    00394     glutSetWindowTitle( title.toStdString().c_str() );
    00395     glutSetWindow( prev );
    00396   }
    00397 }
    00398 //-----------------------------------------------------------------------------
    00399 void GLUTWindow::setPosition(int x, int y)
    00400 {
    00401   if ( handle() )
    00402   {
    00403     int prev = glutGetWindow();
    00404     glutSetWindow( handle() );
    00405     glutPositionWindow(x, y);
    00406     glutSetWindow( prev );
    00407   }
    00408 }
    00409 //-----------------------------------------------------------------------------
    00410 void GLUTWindow::setSize(int w, int h)
    00411 {
    00412   if ( handle() )
    00413   {
    00414     int prev = glutGetWindow();
    00415     glutSetWindow( handle() );
    00416     glutReshapeWindow(w, h);
    00417     glutSetWindow( prev );
    00418   }
    00419 }
    00420 //-----------------------------------------------------------------------------
    00421 vl::ivec2 GLUTWindow::position() const
    00422 {
    00423   if ( handle() )
    00424   {
    00425     int prev = glutGetWindow();
    00426     glutSetWindow( handle() );
    00427     vl::ivec2 v( glutGet(GLUT_WINDOW_X), glutGet(GLUT_WINDOW_Y) );
    00428     glutSetWindow( prev );
    00429     return v;
    00430   }
    00431   else
    00432     return vl::ivec2(0,0);
    00433 }
    00434 //-----------------------------------------------------------------------------
    00435 vl::ivec2 GLUTWindow::size() const
    00436 {
    00437   if ( handle() )
    00438   {
    00439     int prev = glutGetWindow();
    00440     glutSetWindow( handle() );
    00441     vl::ivec2 v( glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT) );
    00442     glutSetWindow( prev );
    00443     return v;
    00444   }
    00445   else
    00446     return vl::ivec2(0,0);
    00447 }
    00448 //-----------------------------------------------------------------------------
    00449 void GLUTWindow::initKeymap()
    00450 {
    00451   mKeymap.clear();
    00452
    00453   mKeymap[27]  = vl::Key_Escape;
    00454   mKeymap[127] = vl::Key_Delete;
    00455   mKeymap[8] = vl::Key_BackSpace;
    00456   mKeymap[13] = vl::Key_Return;
    00457   // mKeymap['/'] = vl::Key_Clear;
    00458   mKeymap[' '] = vl::Key_Space;
    00459   mKeymap['`'] = vl::Key_QuoteLeft;
    00460   mKeymap['-'] = vl::Key_Minus;
    00461   mKeymap['='] = vl::Key_Equal;
    00462   mKeymap['['] = vl::Key_LeftBracket;
    00463   mKeymap[']'] = vl::Key_RightBracket;
    00464   mKeymap[';'] = vl::Key_Semicolon;
    00465   mKeymap['\''] = vl::Key_Quote;
    00466   mKeymap['\\'] = vl::Key_BackSlash;
    00467   mKeymap[','] = vl::Key_Comma;
    00468   mKeymap['.'] = vl::Key_Period;
    00469   mKeymap['/'] = vl::Key_Slash;
    00470   mKeymap['\t'] = vl::Key_Tab;
    00471   mKeymap['!'] = vl::Key_Exclam;
    00472   mKeymap['"'] = vl::Key_QuoteDbl;
    00473   mKeymap['#'] = vl::Key_Hash;
    00474   mKeymap['$'] = vl::Key_Dollar;
    00475   mKeymap['&'] = vl::Key_Ampersand;
    00476   mKeymap['('] = vl::Key_LeftParen;
    00477   mKeymap[')'] = vl::Key_RightParen;
    00478   mKeymap['*'] = vl::Key_Asterisk;
    00479   mKeymap['+'] = vl::Key_Plus;
    00480   mKeymap[':'] = vl::Key_Colon;
    00481   mKeymap['<'] = vl::Key_Less;
    00482   mKeymap['>'] = vl::Key_Greater;
    00483   mKeymap['?'] = vl::Key_Question;
    00484   mKeymap['@'] = vl::Key_At;
    00485   mKeymap['|'] = vl::Key_Caret;
    00486   mKeymap['_'] = vl::Key_Underscore;
    00487
    00488   mKeymap['q'] = vl::Key_Q;
    00489   mKeymap['w'] = vl::Key_W;
    00490   mKeymap['e'] = vl::Key_E;
    00491   mKeymap['r'] = vl::Key_R;
    00492   mKeymap['t'] = vl::Key_T;
    00493   mKeymap['y'] = vl::Key_Y;
    00494   mKeymap['u'] = vl::Key_U;
    00495   mKeymap['i'] = vl::Key_I;
    00496   mKeymap['o'] = vl::Key_O;
    00497   mKeymap['p'] = vl::Key_P;
    00498   mKeymap['a'] = vl::Key_A;
    00499   mKeymap['s'] = vl::Key_S;
    00500   mKeymap['d'] = vl::Key_D;
    00501   mKeymap['f'] = vl::Key_F;
    00502   mKeymap['g'] = vl::Key_G;
    00503   mKeymap['h'] = vl::Key_H;
    00504   mKeymap['j'] = vl::Key_J;
    00505   mKeymap['k'] = vl::Key_K;
    00506   mKeymap['l'] = vl::Key_L;
    00507   mKeymap['z'] = vl::Key_Z;
    00508   mKeymap['x'] = vl::Key_X;
    00509   mKeymap['c'] = vl::Key_C;
    00510   mKeymap['v'] = vl::Key_V;
    00511   mKeymap['b'] = vl::Key_B;
    00512   mKeymap['n'] = vl::Key_N;
    00513   mKeymap['m'] = vl::Key_M;
    00514
    00515   mKeymap['Q'] = vl::Key_Q;
    00516   mKeymap['W'] = vl::Key_W;
    00517   mKeymap['E'] = vl::Key_E;
    00518   mKeymap['R'] = vl::Key_R;
    00519   mKeymap['T'] = vl::Key_T;
    00520   mKeymap['Y'] = vl::Key_Y;
    00521   mKeymap['U'] = vl::Key_U;
    00522   mKeymap['I'] = vl::Key_I;
    00523   mKeymap['O'] = vl::Key_O;
    00524   mKeymap['P'] = vl::Key_P;
    00525   mKeymap['A'] = vl::Key_A;
    00526   mKeymap['S'] = vl::Key_S;
    00527   mKeymap['D'] = vl::Key_D;
    00528   mKeymap['F'] = vl::Key_F;
    00529   mKeymap['G'] = vl::Key_G;
    00530   mKeymap['H'] = vl::Key_H;
    00531   mKeymap['J'] = vl::Key_J;
    00532   mKeymap['K'] = vl::Key_K;
    00533   mKeymap['L'] = vl::Key_L;
    00534   mKeymap['Z'] = vl::Key_Z;
    00535   mKeymap['X'] = vl::Key_X;
    00536   mKeymap['C'] = vl::Key_C;
    00537   mKeymap['V'] = vl::Key_V;
    00538   mKeymap['B'] = vl::Key_B;
    00539   mKeymap['N'] = vl::Key_N;
    00540   mKeymap['M'] = vl::Key_M;
    00541
    00542   mKeymap['0'] = vl::Key_0;
    00543   mKeymap['1'] = vl::Key_1;
    00544   mKeymap['2'] = vl::Key_2;
    00545   mKeymap['3'] = vl::Key_3;
    00546   mKeymap['4'] = vl::Key_4;
    00547   mKeymap['5'] = vl::Key_5;
    00548   mKeymap['6'] = vl::Key_6;
    00549   mKeymap['7'] = vl::Key_7;
    00550   mKeymap['8'] = vl::Key_8;
    00551   mKeymap['9'] = vl::Key_9;
    00552 }
    00553 //-----------------------------------------------------------------------------
    00554 vl::EKey GLUTWindow::mapAsciiKey(unsigned char ascii)
    00555 {
    00556   vl::EKey key;
    00557   if(mKeymap.find(ascii) == mKeymap.end())
    00558     key = vl::Key_Unknown;
    00559   else
    00560     key = mKeymap[ascii];
    00561
    00562   return key;
    00563 }
    00564 //-----------------------------------------------------------------------------
    00565 vl::EKey GLUTWindow::mapSpecialKey(int special_key)
    00566 {
    00567   vl::EKey vl_key = vl::Key_Unknown;
    00568   switch(special_key)
    00569   {
    00570     case GLUT_KEY_F1: vl_key = vl::Key_F1; break;
    00571     case GLUT_KEY_F2: vl_key = vl::Key_F2; break;
    00572     case GLUT_KEY_F3: vl_key = vl::Key_F3; break;
    00573     case GLUT_KEY_F4: vl_key = vl::Key_F4; break;
    00574     case GLUT_KEY_F5: vl_key = vl::Key_F5; break;
    00575     case GLUT_KEY_F6: vl_key = vl::Key_F6; break;
    00576     case GLUT_KEY_F7: vl_key = vl::Key_F7; break;
    00577     case GLUT_KEY_F8: vl_key = vl::Key_F8; break;
    00578     case GLUT_KEY_F9: vl_key = vl::Key_F9; break;
    00579     case GLUT_KEY_F10: vl_key = vl::Key_F10; break;
    00580     case GLUT_KEY_F11: vl_key = vl::Key_F11; break;
    00581     case GLUT_KEY_F12: vl_key = vl::Key_F12; break;
    00582     case GLUT_KEY_LEFT: vl_key = vl::Key_Left; break;
    00583     case GLUT_KEY_UP: vl_key = vl::Key_Up; break;
    00584     case GLUT_KEY_RIGHT: vl_key = vl::Key_Right; break;
    00585     case GLUT_KEY_DOWN: vl_key = vl::Key_Down; break;
    00586     case GLUT_KEY_PAGE_UP: vl_key = vl::Key_PageUp; break;
    00587     case GLUT_KEY_PAGE_DOWN: vl_key = vl::Key_PageDown; break;
    00588     case GLUT_KEY_HOME: vl_key = vl::Key_Home; break;
    00589     case GLUT_KEY_END: vl_key = vl::Key_End; break;
    00590     case GLUT_KEY_INSERT: vl_key = vl::Key_Insert; break;
    00591     default:
    00592       vl_key = vl::Key_Unknown;
    00593   }
    00594   return vl_key;
    00595 }
    00596 //-----------------------------------------------------------------------------
    00597 void GLUTWindow::updateModifiers()
    00598 {
    00599   keyRelease(vl::Key_Shift);
    00600   keyRelease(vl::Key_Alt);
    00601   keyRelease(vl::Key_Ctrl);
    00602   int modifiers = glutGetModifiers();
    00603   if (modifiers & GLUT_ACTIVE_SHIFT)
    00604     keyPress(vl::Key_Shift);
    00605   if (modifiers & GLUT_ACTIVE_CTRL)
    00606     keyPress(vl::Key_Ctrl);
    00607   if (modifiers & GLUT_ACTIVE_ALT)
    00608     keyPress(vl::Key_Alt);
    00609 }
    00610 //-----------------------------------------------------------------------------
    00611 void GLUTWindow::glut_keyboard_func(unsigned char ch, int, int)
    00612 {
    00613   int cur_win = glutGetWindow();
    00614   GLUTWindow* win = mWinMap[cur_win];
    00615   VL_CHECK(win);
    00616   win->updateModifiers();
    00617   win->dispatchKeyPressEvent(ch, win->mapAsciiKey(ch) );
    00618 }
    00619 //-----------------------------------------------------------------------------
    00620 void GLUTWindow::glut_keyboard_up_func(unsigned char ch, int, int)
    00621 {
    00622   int cur_win = glutGetWindow();
    00623   GLUTWindow* win = mWinMap[cur_win];
    00624   VL_CHECK(win);
    00625   win->updateModifiers();
    00626   win->dispatchKeyReleaseEvent(ch, win->mapAsciiKey(ch) );
    00627 }
    00628 //-----------------------------------------------------------------------------
    00629 void GLUTWindow::glut_special_func(int key, int, int)
    00630 {
    00631   int cur_win = glutGetWindow();
    00632   GLUTWindow* win = mWinMap[cur_win];
    00633   VL_CHECK(win);
    00634   win->updateModifiers();
    00635   win->dispatchKeyPressEvent(0, mapSpecialKey(key) );
    00636 }
    00637 //-----------------------------------------------------------------------------
    00638 void GLUTWindow::glut_special_up_func(int key, int, int)
    00639 {
    00640   int cur_win = glutGetWindow();
    00641   GLUTWindow* win = mWinMap[cur_win];
    00642   VL_CHECK(win);
    00643   win->updateModifiers();
    00644   win->dispatchKeyReleaseEvent(0, mapSpecialKey(key) );
    00645 }
    00646 //-----------------------------------------------------------------------------
    00647 void GLUTWindow::glut_mouse_func(int button, int state, int x, int y)
    00648 {
    00649   int cur_win = glutGetWindow();
    00650   GLUTWindow* win = mWinMap[cur_win];
    00651   VL_CHECK(win);
    00652   win->updateModifiers();
    00653
    00654   vl::EMouseButton btn = vl::UnknownButton;
    00655   switch(button)
    00656   {
    00657     case GLUT_LEFT_BUTTON: btn = vl::LeftButton; break;
    00658     case GLUT_MIDDLE_BUTTON: btn = vl::MiddleButton; break;
    00659     case GLUT_RIGHT_BUTTON: btn = vl::RightButton; break;
    00660   }
    00661
    00662   if (state == GLUT_DOWN)
    00663     win->dispatchMouseDownEvent(btn, x, y);
    00664   else
    00665   if (state == GLUT_UP)
    00666     win->dispatchMouseUpEvent(btn, x, y);
    00667 }
    00668 //-----------------------------------------------------------------------------
    00669 void GLUTWindow::glut_motion_func(int x, int y)
    00670 {
    00671   int cur_win = glutGetWindow();
    00672   GLUTWindow* win = mWinMap[cur_win];
    00673   VL_CHECK(win);
    00674
    00675   // win->updateModifiers();
    00676
    00677   win->dispatchMouseMoveEvent(x, y);
    00678 }
    00679 //-----------------------------------------------------------------------------
    00680 void GLUTWindow::glut_passive_motion_func(int x, int y)
    00681 {
    00682   int cur_win = glutGetWindow();
    00683   GLUTWindow* win = mWinMap[cur_win];
    00684   VL_CHECK(win);
    00685
    00686   // !!!
    00687   if (!win->mInited)
    00688     return;
    00689
    00690   // win->updateModifiers();
    00691
    00692   win->dispatchMouseMoveEvent(x, y);
    00693 }
    00694 //-----------------------------------------------------------------------------
    00695 void GLUTWindow::glut_mouse_wheel_func(int, int rotation, int, int)
    00696 {
    00697   int cur_win = glutGetWindow();
    00698   GLUTWindow* win = mWinMap[cur_win];
    00699   VL_CHECK(win);
    00700   win->updateModifiers();
    00701
    00702   win->dispatchMouseWheelEvent(rotation);
    00703 }
    00704 //-----------------------------------------------------------------------------
    00705 void GLUTWindow::glut_visibility_func(int visibility)
    00706 {
    00707   int cur_win = glutGetWindow();
    00708   GLUTWindow* win = mWinMap[cur_win];
    00709   VL_CHECK(win);
    00710   // win->updateModifiers(); // cannot be called from here
    00711
    00712   win->dispatchVisibilityEvent(visibility == GLUT_VISIBLE);
    00713 }
    00714 //-----------------------------------------------------------------------------
    00715 void GLUTWindow::glut_reshape_func(int w, int h)
    00716 {
    00717   int cur_win = glutGetWindow();
    00718   GLUTWindow* win = mWinMap[cur_win];
    00719   VL_CHECK(win);
    00720
    00721   win->renderTarget()->setWidth(w);
    00722   win->renderTarget()->setHeight(h);
    00723
    00724   if (win->mInited == false)
    00725   {
    00726     win->mInited = true;
    00727     win->dispatchInitEvent();
    00728   }
    00729
    00730   win->dispatchResizeEvent(w, h);
    00731 }
    00732 //-----------------------------------------------------------------------------
    00733 void GLUTWindow::glut_display_func()
    00734 {
    00735   int cur_win = glutGetWindow();
    00736   GLUTWindow* win = mWinMap[cur_win];
    00737   VL_CHECK(win);
    00738   // win->updateModifiers(); // cannot be called from here;
    00739
    00740   win->dispatchRunEvent();
    00741
    00742   //if (win->continuousUpdate())
    00743   //  win->update();
    00744 }
    00745 //-----------------------------------------------------------------------------
    00746 void GLUTWindow::glut_close_func()
    00747 {
    00748   int cur_win = glutGetWindow();
    00749   if (mWinMap.find(cur_win) != mWinMap.end())
    00750   {
    00751     GLUTWindow* win = mWinMap[cur_win];
    00752     VL_CHECK(win);
    00753     // win->updateModifiers() cannot be called from here
    00754     win->dispatchDestroyEvent();
    00755     win->mHandle = 0;
    00756     win->mInited = false;
    00757     win->mKeymap.clear();
    00758     mWinMap.erase( cur_win );
    00759   }
    00760 }
    00761 //-----------------------------------------------------------------------------
    00762 void GLUTWindow::glut_wmclose_func()
    00763 {
    00764   glut_close_func();
    00765 }
    00766 //-----------------------------------------------------------------------------
    00767 void GLUTWindow::glut_idle_func()
    00768 {
    00769   bool sleep = true;
    00770   for(std::map< int, GLUTWindow* >::iterator it = mWinMap.begin(); it != mWinMap.end(); ++it)
    00771   {
    00772     if (it->second->continuousUpdate())
    00773     {
    00774       it->second->update();
    00775       sleep = false;
    00776     }
    00777   }
    00778   #ifdef WIN32
    00779     if (sleep)
    00780     {
    00781       Sleep(10);
    00782     }
    00783   #endif
    00784 }
    00785 //-----------------------------------------------------------------------------
    00786 void vlGLUT::atexit_visualization_library_shutdown()
    00787 {
    00788   // Workaround for GLUT bug calling the exit function once per window opened +1
    00789   // if one explicitly pushes the X button of one of the windows.
    00790   static bool alread_called = false;
    00791   if (alread_called)
    00792   {
    00793     return;
    00794   }
    00795   else
    00796   {
    00797     vl::VisualizationLibrary::shutdown();
    00798     alread_called = true;
    00799     return;
    00800   }
    00801 }
    00802 //-----------------------------------------------------------------------------

Visualization Library v2011.05.1142 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu May 26 2011 22:51:22.
Permission is granted to use this page to write and publish articles regarding Visualization Library.