Visualization Library

A lightweight C++ OpenGL middleware for 2D/3D graphics
[Home] [Tutorials] [All Classes] [Grouped Classes]

X:/dropbox/visualizationlibrary/src/vlGLUT/GLUTWindow.cpp

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

Visualization Library 2011.09.1160 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu May 2 2013 13:40:34.
Permission is granted to use this page to write and publish articles regarding Visualization Library.