Visualization Library 2.0.0

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

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
GLUTWindow.cpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2020, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #include <vlGLUT/GLUTWindow.hpp>
33 #include <vlGraphics/Applet.hpp>
35 #include <vlCore/Log.hpp>
36 #include <vlCore/Say.hpp>
37 #include <vlCore/Time.hpp>
38 
39 using namespace vlGLUT;
40 
41 //-----------------------------------------------------------------------------
42 std::map< int, GLUTWindow* > GLUTWindow::mWinMap;
43 //-----------------------------------------------------------------------------
45 {
46  mInited = false;
47  mHandle = 0;
48 }
49 //-----------------------------------------------------------------------------
50 GLUTWindow::GLUTWindow(const vl::String& title, const vl::OpenGLContextFormat& info, int x, int y, int width, int height)
51 {
52  mInited = false;
53  mHandle = 0;
54 
55  initGLUTWindow(title, info, x, y, width, height);
56 }
57 //-----------------------------------------------------------------------------
58 bool GLUTWindow::initGLUTWindow(const vl::String& title, const vl::OpenGLContextFormat& info, int x, int y, int width, int height)
59 {
61 
62  int flags = GLUT_RGB;
63  if (info.rgbaBits().a()) flags |= GLUT_ALPHA;
64  if (info.accumRGBABits().r()|info.accumRGBABits().g()|info.accumRGBABits().b()|info.accumRGBABits().a()) flags |= GLUT_ACCUM;
65  if (info.doubleBuffer()) flags |= GLUT_DOUBLE;
66  if (info.depthBufferBits()) flags |= GLUT_DEPTH;
67  if (info.stencilBufferBits()) flags |= GLUT_STENCIL;
68  if (info.multisample()) flags |= GLUT_MULTISAMPLE;
69  if (info.stereo()) flags |= GLUT_STEREO;
70 
71  #if defined(WIN32)
72  if (info.fullscreen())
73  {
74  DEVMODE devmode;
75  EnumDisplaySettings(NULL,ENUM_CURRENT_SETTINGS,&devmode);
76  width = devmode.dmPelsWidth;
77  height = devmode.dmPelsHeight;
78  }
79  #endif
80  mFullscreen = info.fullscreen();
81 
82  switch( info.openGLProfile() )
83  {
85  glutInitContextProfile ( GLUT_COMPATIBILITY_PROFILE );
86  if ( info.majVersion() ) {
87  glutInitContextVersion( info.majVersion(), info.minVersion() );
88  }
89  break;
90  case vl::GLP_Core:
91  glutInitContextProfile ( GLUT_CORE_PROFILE );
92  if ( info.majVersion() ) {
93  glutInitContextVersion( info.majVersion(), info.minVersion() );
94  }
95  break;
96  case vl::GLP_Default:
97  // do nothing
98  break;
99  }
100 
101  glutInitDisplayMode( flags );
102  glutInitWindowSize( width, height );
103  glutInitWindowPosition( x, y );
104 
105  mHandle = glutCreateWindow( title.toStdString().c_str() ) ;
106  if (info.fullscreen())
107  glutFullScreen();
108 
109  glutSetWindow( handle() );
110 
111  initGLContext();
112  // dispatchInitEvent();
113 
114  setVSyncEnabled(info.vSync());
115 
116  glutSetIconTitle(title.toStdString().c_str());
117  mWinMap[handle()] = this;
118 
119  glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF);
120  initKeymap();
121 
122  // install callbacks
123  glutDisplayFunc( glut_display_func );
124  glutReshapeFunc( glut_reshape_func );
125  glutKeyboardFunc( glut_keyboard_func );
126  glutKeyboardUpFunc( glut_keyboard_up_func );
127  glutMouseFunc( glut_mouse_func );
128  glutMotionFunc( glut_motion_func );
129  glutPassiveMotionFunc( glut_passive_motion_func );
130  glutVisibilityFunc( glut_visibility_func );
131  glutSpecialFunc( glut_special_func );
132  glutSpecialUpFunc( glut_special_up_func );
133 #if !defined(__APPLE__)
134  glutMouseWheelFunc( glut_mouse_wheel_func );
135 #endif
136 
137  glutReshapeWindow(width, height);
138 
139  // glutEntryFunc( glut_entry_func );
140  glutWMCloseFunc( glut_wmclose_func );
141 #if !defined(__APPLE__)
142  glutCloseFunc( glut_close_func );
143 #endif
144 
145  // used for continuous update
146  glutIdleFunc( glut_idle_func );
147 
148  // GLUT does not set the appropriate current window when calling these two, so we cannot use them.
149  // glutTimerFunc( millisecs, glut_timer_func, value );
150 
151  return true;
152 
153  /*** GLUT functions that would be nice to support ***/
154 
156  // * Initialization functions, see fglut_init.c
157  // */
158  //glutInit( int* pargc, char** argv );
159  //glutInitWindowPosition( int x, int y );
160  //glutInitWindowSize( int width, int height );
161  //glutInitDisplayMode( unsigned int displayMode );
162  //glutInitDisplayString( const char* displayMode );
163 
165  // * Process loop function, see freeglut_main.c
166  // */
167  //glutMainLoop( void );
168 
170  // * Window management functions, see freeglut_window.c
171  // */
172  //glutCreateWindow( const char* title );
173  //glutCreateSubWindow( int window, int x, int y, int width, int height );
174  //glutDestroyWindow( int window );
175  //glutSetWindow( int window );
176  //glutGetWindow( void );
177  //glutSetWindowTitle( const char* title );
178  //glutSetIconTitle( const char* title );
179  //glutReshapeWindow( int width, int height );
180  //glutPositionWindow( int x, int y );
181  //glutShowWindow( void );
182  //glutHideWindow( void );
183  //glutIconifyWindow( void );
184  //glutPushWindow( void );
185  //glutPopWindow( void );
186  //glutFullScreen( void );
187 
189  // * Display-connected functions, see freeglut_display.c
190  // */
191  //glutPostWindowRedisplay( int window );
192  //glutPostRedisplay( void );
193  //glutSwapBuffers( void );
194 
196  // * Mouse cursor functions, see freeglut_cursor.c
197  // */
198  //glutWarpPointer( int x, int y );
199  //glutSetCursor( int cursor );
200 
202  // * Overlay stuff, see freeglut_overlay.c
203  // */
204  //glutEstablishOverlay( void );
205  //glutRemoveOverlay( void );
206  //glutUseLayer( GLenum layer );
207  //glutPostOverlayRedisplay( void );
208  //glutPostWindowOverlayRedisplay( int window );
209  //glutShowOverlay( void );
210  //glutHideOverlay( void );
211 
213  // * Menu stuff, see freeglut_menu.c
214  // */
215  //glutCreateMenu( void (* callback)( int menu ) );
216  //glutDestroyMenu( int menu );
217  //glutGetMenu( void );
218  //glutSetMenu( int menu );
219  //glutAddMenuEntry( const char* label, int value );
220  //glutAddSubMenu( const char* label, int subMenu );
221  //glutChangeToMenuEntry( int item, const char* label, int value );
222  //glutChangeToSubMenu( int item, const char* label, int value );
223  //glutRemoveMenuItem( int item );
224  //glutAttachMenu( int button );
225  //glutDetachMenu( int button );
226 
228  // * Global callback functions, see freeglut_callbacks.c
229  // */
230  //glutTimerFunc( unsigned int time, void (* callback)( int ), int value );
231  //glutIdleFunc( void (* callback)( void ) );
232 
234  // * Window-specific callback functions, see freeglut_callbacks.c
235  // */
236  //glutKeyboardFunc( void (* callback)( unsigned char, int, int ) );
237  //glutSpecialFunc( void (* callback)( int, int, int ) );
238  //glutReshapeFunc( void (* callback)( int, int ) );
239  //glutVisibilityFunc( void (* callback)( int ) );
240  //glutDisplayFunc( void (* callback)( void ) );
241  //glutMouseFunc( void (* callback)( int, int, int, int ) );
242  //glutMotionFunc( void (* callback)( int, int ) );
243  //glutPassiveMotionFunc( void (* callback)( int, int ) );
244  //glutEntryFunc( void (* callback)( int ) );
245 
246  //glutKeyboardUpFunc( void (* callback)( unsigned char, int, int ) );
247  //glutSpecialUpFunc( void (* callback)( int, int, int ) );
248  //glutJoystickFunc( void (* callback)( unsigned int, int, int, int ), int pollInterval );
249  //glutMenuStateFunc( void (* callback)( int ) );
250  //glutMenuStatusFunc( void (* callback)( int, int, int ) );
251  //glutOverlayDisplayFunc( void (* callback)( void ) );
252  //glutWindowStatusFunc( void (* callback)( int ) );
253 
254  //glutSpaceballMotionFunc( void (* callback)( int, int, int ) );
255  //glutSpaceballRotateFunc( void (* callback)( int, int, int ) );
256  //glutSpaceballButtonFunc( void (* callback)( int, int ) );
257  //glutButtonBoxFunc( void (* callback)( int, int ) );
258  //glutDialsFunc( void (* callback)( int, int ) );
259  //glutTabletMotionFunc( void (* callback)( int, int ) );
260  //glutTabletButtonFunc( void (* callback)( int, int, int, int ) );
261 
263  // * State setting and retrieval functions, see freeglut_state.c
264  // */
265  //glutGet( GLenum query );
266  //glutDeviceGet( GLenum query );
267  //glutGetModifiers( void );
268  //glutLayerGet( GLenum query );
269 
271  // * Font stuff, see freeglut_font.c
272  // */
273  //glutBitmapCharacter( void* font, int character );
274  //glutBitmapWidth( void* font, int character );
275  //glutStrokeCharacter( void* font, int character );
276  //glutStrokeWidth( void* font, int character );
277  //glutBitmapLength( void* font, const unsigned char* string );
278  //glutStrokeLength( void* font, const unsigned char* string );
279 
281  // * Game mode functions, see freeglut_gamemode.c
282  // */
283  //glutGameModeString( const char* string );
284  //glutEnterGameMode( void );
285  //glutLeaveGameMode( void );
286  //glutGameModeGet( GLenum query );
287 
289  // * Video resize functions, see freeglut_videoresize.c
290  // */
291  //glutVideoResizeGet( GLenum query );
292  //glutSetupVideoResizing( void );
293  //glutStopVideoResizing( void );
294  //glutVideoResize( int x, int y, int width, int height );
295  //glutVideoPan( int x, int y, int width, int height );
296 
298  // * Misc keyboard and joystick functions, see freeglut_misc.c
299  // */
300  //glutIgnoreKeyRepeat( int ignore );
301  //glutSetKeyRepeat( int repeatMode );
302 }
303 //-----------------------------------------------------------------------------
305 {
306  if ( handle() )
307  {
308  int prev = glutGetWindow();
309  glutSetWindow( handle() );
310  if (fs)
311  {
312  #if defined(WIN32)
313  DEVMODE devmode;
314  EnumDisplaySettings(NULL,ENUM_CURRENT_SETTINGS,&devmode);
315  int width = devmode.dmPelsWidth;
316  int height = devmode.dmPelsHeight;
317  glutPositionWindow( 0, 0 );
318  glutReshapeWindow( width, height );
319  #endif
320  glutFullScreen();
321  }
322  else
323  {
324  glutPositionWindow( 0, 0 );
325  glutReshapeWindow( 640, 480 );
326  }
327  glutSetWindow( prev );
328  mFullscreen = fs;
329  return true;
330  }
331  else
332  return false;
333 }
334 //-----------------------------------------------------------------------------
335 void GLUTWindow::setMouseVisible(bool visible)
336 {
337  mMouseVisible = visible;
338  if (visible)
339  glutSetCursor(GLUT_CURSOR_LEFT_ARROW);
340  else
341  glutSetCursor(GLUT_CURSOR_NONE);
342 }
343 //-----------------------------------------------------------------------------
345 {
346  glutWarpPointer(x,y);
347 }
348 //-----------------------------------------------------------------------------
350 {
351  if ( handle() )
352  glutPostWindowRedisplay( handle() );
353 }
354 //-----------------------------------------------------------------------------
356 {
357  if ( handle() )
358  glutSetWindow( handle() );
359 }
360 //-----------------------------------------------------------------------------
362 {
363  // according to GLUT specs pag 44 we can do this
364  if ( handle() )
365  {
366  // should trigger glut_close_func
367  glutDestroyWindow( handle() );
368  }
369 }
370 //-----------------------------------------------------------------------------
372 {
373  glutPostOverlayRedisplay();
374 }
375 //-----------------------------------------------------------------------------
377 {
378  glutSwapBuffers();
379 }
380 //-----------------------------------------------------------------------------
382 {
383  if ( handle() )
384  {
385  int prev = glutGetWindow();
386  glutSetWindow( handle() );
387  glutShowWindow();
388  glutSetWindow( prev );
389  }
390 }
391 //-----------------------------------------------------------------------------
393 {
394  if ( handle() )
395  {
396  int prev = glutGetWindow();
397  glutSetWindow( handle() );
398  glutHideWindow();
399  glutSetWindow( prev );
400  }
401 }
402 //-----------------------------------------------------------------------------
404 {
405  if ( handle() )
406  glutSetWindow( handle() );
407 }
408 //-----------------------------------------------------------------------------
410 {
411  if ( handle() )
412  {
413  int prev = glutGetWindow();
414  glutSetWindow( handle() );
415  glutSetWindowTitle( title.toStdString().c_str() );
416  glutSetWindow( prev );
417  }
418 }
419 //-----------------------------------------------------------------------------
420 void GLUTWindow::setPosition(int x, int y)
421 {
422  if ( handle() )
423  {
424  int prev = glutGetWindow();
425  glutSetWindow( handle() );
426  glutPositionWindow(x, y);
427  glutSetWindow( prev );
428  }
429 }
430 //-----------------------------------------------------------------------------
431 void GLUTWindow::setSize(int w, int h)
432 {
433  if ( handle() )
434  {
435  int prev = glutGetWindow();
436  glutSetWindow( handle() );
437  glutReshapeWindow(w, h);
438  glutSetWindow( prev );
439  }
440 }
441 //-----------------------------------------------------------------------------
443 {
444  if ( handle() )
445  {
446  int prev = glutGetWindow();
447  glutSetWindow( handle() );
448  vl::ivec2 v( glutGet(GLUT_WINDOW_X), glutGet(GLUT_WINDOW_Y) );
449  glutSetWindow( prev );
450  return v;
451  }
452  else
453  return vl::ivec2(0,0);
454 }
455 //-----------------------------------------------------------------------------
457 {
458  if ( handle() )
459  {
460  int prev = glutGetWindow();
461  glutSetWindow( handle() );
462  vl::ivec2 v( glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT) );
463  glutSetWindow( prev );
464  return v;
465  }
466  else
467  return vl::ivec2(0,0);
468 }
469 //-----------------------------------------------------------------------------
471 {
472  mKeymap.clear();
473 
474  mKeymap[27] = vl::Key_Escape;
475  mKeymap[127] = vl::Key_Delete;
477  mKeymap[13] = vl::Key_Return;
478  // mKeymap['/'] = vl::Key_Clear;
479  mKeymap[' '] = vl::Key_Space;
480  mKeymap['`'] = vl::Key_QuoteLeft;
481  mKeymap['-'] = vl::Key_Minus;
482  mKeymap['='] = vl::Key_Equal;
485  mKeymap[';'] = vl::Key_Semicolon;
486  mKeymap['\''] = vl::Key_Quote;
487  mKeymap['\\'] = vl::Key_BackSlash;
488  mKeymap[','] = vl::Key_Comma;
489  mKeymap['.'] = vl::Key_Period;
490  mKeymap['/'] = vl::Key_Slash;
491  mKeymap['\t'] = vl::Key_Tab;
492  mKeymap['!'] = vl::Key_Exclam;
493  mKeymap['"'] = vl::Key_QuoteDbl;
494  mKeymap['#'] = vl::Key_Hash;
495  mKeymap['$'] = vl::Key_Dollar;
496  mKeymap['&'] = vl::Key_Ampersand;
497  mKeymap['('] = vl::Key_LeftParen;
499  mKeymap['*'] = vl::Key_Asterisk;
500  mKeymap['+'] = vl::Key_Plus;
501  mKeymap[':'] = vl::Key_Colon;
502  mKeymap['<'] = vl::Key_Less;
503  mKeymap['>'] = vl::Key_Greater;
504  mKeymap['?'] = vl::Key_Question;
505  mKeymap['@'] = vl::Key_At;
506  mKeymap['|'] = vl::Key_Caret;
508 
509  mKeymap['q'] = vl::Key_Q;
510  mKeymap['w'] = vl::Key_W;
511  mKeymap['e'] = vl::Key_E;
512  mKeymap['r'] = vl::Key_R;
513  mKeymap['t'] = vl::Key_T;
514  mKeymap['y'] = vl::Key_Y;
515  mKeymap['u'] = vl::Key_U;
516  mKeymap['i'] = vl::Key_I;
517  mKeymap['o'] = vl::Key_O;
518  mKeymap['p'] = vl::Key_P;
519  mKeymap['a'] = vl::Key_A;
520  mKeymap['s'] = vl::Key_S;
521  mKeymap['d'] = vl::Key_D;
522  mKeymap['f'] = vl::Key_F;
523  mKeymap['g'] = vl::Key_G;
524  mKeymap['h'] = vl::Key_H;
525  mKeymap['j'] = vl::Key_J;
526  mKeymap['k'] = vl::Key_K;
527  mKeymap['l'] = vl::Key_L;
528  mKeymap['z'] = vl::Key_Z;
529  mKeymap['x'] = vl::Key_X;
530  mKeymap['c'] = vl::Key_C;
531  mKeymap['v'] = vl::Key_V;
532  mKeymap['b'] = vl::Key_B;
533  mKeymap['n'] = vl::Key_N;
534  mKeymap['m'] = vl::Key_M;
535 
536  mKeymap['Q'] = vl::Key_Q;
537  mKeymap['W'] = vl::Key_W;
538  mKeymap['E'] = vl::Key_E;
539  mKeymap['R'] = vl::Key_R;
540  mKeymap['T'] = vl::Key_T;
541  mKeymap['Y'] = vl::Key_Y;
542  mKeymap['U'] = vl::Key_U;
543  mKeymap['I'] = vl::Key_I;
544  mKeymap['O'] = vl::Key_O;
545  mKeymap['P'] = vl::Key_P;
546  mKeymap['A'] = vl::Key_A;
547  mKeymap['S'] = vl::Key_S;
548  mKeymap['D'] = vl::Key_D;
549  mKeymap['F'] = vl::Key_F;
550  mKeymap['G'] = vl::Key_G;
551  mKeymap['H'] = vl::Key_H;
552  mKeymap['J'] = vl::Key_J;
553  mKeymap['K'] = vl::Key_K;
554  mKeymap['L'] = vl::Key_L;
555  mKeymap['Z'] = vl::Key_Z;
556  mKeymap['X'] = vl::Key_X;
557  mKeymap['C'] = vl::Key_C;
558  mKeymap['V'] = vl::Key_V;
559  mKeymap['B'] = vl::Key_B;
560  mKeymap['N'] = vl::Key_N;
561  mKeymap['M'] = vl::Key_M;
562 
563  mKeymap['0'] = vl::Key_0;
564  mKeymap['1'] = vl::Key_1;
565  mKeymap['2'] = vl::Key_2;
566  mKeymap['3'] = vl::Key_3;
567  mKeymap['4'] = vl::Key_4;
568  mKeymap['5'] = vl::Key_5;
569  mKeymap['6'] = vl::Key_6;
570  mKeymap['7'] = vl::Key_7;
571  mKeymap['8'] = vl::Key_8;
572  mKeymap['9'] = vl::Key_9;
573 }
574 //-----------------------------------------------------------------------------
575 vl::EKey GLUTWindow::mapAsciiKey(unsigned char ascii)
576 {
577  vl::EKey key;
578  if(mKeymap.find(ascii) == mKeymap.end())
579  key = vl::Key_Unknown;
580  else
581  key = mKeymap[ascii];
582 
583  return key;
584 }
585 //-----------------------------------------------------------------------------
587 {
588  vl::EKey vl_key = vl::Key_Unknown;
589  switch(special_key)
590  {
591  case GLUT_KEY_F1: vl_key = vl::Key_F1; break;
592  case GLUT_KEY_F2: vl_key = vl::Key_F2; break;
593  case GLUT_KEY_F3: vl_key = vl::Key_F3; break;
594  case GLUT_KEY_F4: vl_key = vl::Key_F4; break;
595  case GLUT_KEY_F5: vl_key = vl::Key_F5; break;
596  case GLUT_KEY_F6: vl_key = vl::Key_F6; break;
597  case GLUT_KEY_F7: vl_key = vl::Key_F7; break;
598  case GLUT_KEY_F8: vl_key = vl::Key_F8; break;
599  case GLUT_KEY_F9: vl_key = vl::Key_F9; break;
600  case GLUT_KEY_F10: vl_key = vl::Key_F10; break;
601  case GLUT_KEY_F11: vl_key = vl::Key_F11; break;
602  case GLUT_KEY_F12: vl_key = vl::Key_F12; break;
603  case GLUT_KEY_LEFT: vl_key = vl::Key_Left; break;
604  case GLUT_KEY_UP: vl_key = vl::Key_Up; break;
605  case GLUT_KEY_RIGHT: vl_key = vl::Key_Right; break;
606  case GLUT_KEY_DOWN: vl_key = vl::Key_Down; break;
607  case GLUT_KEY_PAGE_UP: vl_key = vl::Key_PageUp; break;
608  case GLUT_KEY_PAGE_DOWN: vl_key = vl::Key_PageDown; break;
609  case GLUT_KEY_HOME: vl_key = vl::Key_Home; break;
610  case GLUT_KEY_END: vl_key = vl::Key_End; break;
611  case GLUT_KEY_INSERT: vl_key = vl::Key_Insert; break;
612  default:
613  vl_key = vl::Key_Unknown;
614  }
615  return vl_key;
616 }
617 //-----------------------------------------------------------------------------
619 {
623  int modifiers = glutGetModifiers();
624  if (modifiers & GLUT_ACTIVE_SHIFT)
626  if (modifiers & GLUT_ACTIVE_CTRL)
628  if (modifiers & GLUT_ACTIVE_ALT)
630 }
631 //-----------------------------------------------------------------------------
632 void GLUTWindow::glut_keyboard_func(unsigned char ch, int, int)
633 {
634  int cur_win = glutGetWindow();
635  GLUTWindow* win = mWinMap[cur_win];
636  VL_CHECK(win);
637  win->updateModifiers();
638  win->dispatchKeyPressEvent(ch, win->mapAsciiKey(ch) );
639 }
640 //-----------------------------------------------------------------------------
641 void GLUTWindow::glut_keyboard_up_func(unsigned char ch, int, int)
642 {
643  int cur_win = glutGetWindow();
644  GLUTWindow* win = mWinMap[cur_win];
645  VL_CHECK(win);
646  win->updateModifiers();
647  win->dispatchKeyReleaseEvent(ch, win->mapAsciiKey(ch) );
648 }
649 //-----------------------------------------------------------------------------
650 void GLUTWindow::glut_special_func(int key, int, int)
651 {
652  int cur_win = glutGetWindow();
653  GLUTWindow* win = mWinMap[cur_win];
654  VL_CHECK(win);
655  win->updateModifiers();
656  win->dispatchKeyPressEvent(0, mapSpecialKey(key) );
657 }
658 //-----------------------------------------------------------------------------
659 void GLUTWindow::glut_special_up_func(int key, int, int)
660 {
661  int cur_win = glutGetWindow();
662  GLUTWindow* win = mWinMap[cur_win];
663  VL_CHECK(win);
664  win->updateModifiers();
665  win->dispatchKeyReleaseEvent(0, mapSpecialKey(key) );
666 }
667 //-----------------------------------------------------------------------------
668 void GLUTWindow::glut_mouse_func(int button, int state, int x, int y)
669 {
670  int cur_win = glutGetWindow();
671  GLUTWindow* win = mWinMap[cur_win];
672  VL_CHECK(win);
673  win->updateModifiers();
674 
676  switch(button)
677  {
678  case GLUT_LEFT_BUTTON: btn = vl::LeftButton; break;
679  case GLUT_MIDDLE_BUTTON: btn = vl::MiddleButton; break;
680  case GLUT_RIGHT_BUTTON: btn = vl::RightButton; break;
681  }
682 
683  if (state == GLUT_DOWN)
684  win->dispatchMouseDownEvent(btn, x, y);
685  else
686  if (state == GLUT_UP)
687  win->dispatchMouseUpEvent(btn, x, y);
688 }
689 //-----------------------------------------------------------------------------
691 {
692  int cur_win = glutGetWindow();
693  GLUTWindow* win = mWinMap[cur_win];
694  VL_CHECK(win);
695 
696  // win->updateModifiers();
697 
698  win->dispatchMouseMoveEvent(x, y);
699 }
700 //-----------------------------------------------------------------------------
702 {
703  int cur_win = glutGetWindow();
704  GLUTWindow* win = mWinMap[cur_win];
705  VL_CHECK(win);
706 
707  // !!!
708  if (!win->mInited)
709  return;
710 
711  // win->updateModifiers();
712 
713  win->dispatchMouseMoveEvent(x, y);
714 }
715 //-----------------------------------------------------------------------------
716 void GLUTWindow::glut_mouse_wheel_func(int, int rotation, int, int)
717 {
718  int cur_win = glutGetWindow();
719  GLUTWindow* win = mWinMap[cur_win];
720  VL_CHECK(win);
721  win->updateModifiers();
722 
723  win->dispatchMouseWheelEvent(rotation);
724 }
725 //-----------------------------------------------------------------------------
727 {
728  int cur_win = glutGetWindow();
729  GLUTWindow* win = mWinMap[cur_win];
730  VL_CHECK(win);
731  // win->updateModifiers(); // cannot be called from here
732 
733  win->dispatchVisibilityEvent(visibility == GLUT_VISIBLE);
734 }
735 //-----------------------------------------------------------------------------
737 {
738  int cur_win = glutGetWindow();
739  GLUTWindow* win = mWinMap[cur_win];
740  VL_CHECK(win);
741 
742  win->framebuffer()->setWidth(w);
743  win->framebuffer()->setHeight(h);
744 
745  if (win->mInited == false)
746  {
747  win->mInited = true;
748  win->dispatchInitEvent();
749  }
750 
751  win->dispatchResizeEvent(w, h);
752 }
753 //-----------------------------------------------------------------------------
755 {
756  int cur_win = glutGetWindow();
757  GLUTWindow* win = mWinMap[cur_win];
758  VL_CHECK(win);
759  // win->updateModifiers(); // cannot be called from here;
760 
761  win->dispatchUpdateEvent();
762 
763  //if (win->continuousUpdate())
764  // win->update();
765 }
766 //-----------------------------------------------------------------------------
768 {
769  int cur_win = glutGetWindow();
770  if (mWinMap.find(cur_win) != mWinMap.end())
771  {
772  GLUTWindow* win = mWinMap[cur_win];
773  VL_CHECK(win);
774  // win->updateModifiers() cannot be called from here
775  win->dispatchDestroyEvent();
776  win->mHandle = 0;
777  win->mInited = false;
778  win->mKeymap.clear();
779  mWinMap.erase( cur_win );
780  }
781 }
782 //-----------------------------------------------------------------------------
784 {
785  glut_close_func();
786 }
787 //-----------------------------------------------------------------------------
789 {
790  bool sleep = true;
791  for(std::map< int, GLUTWindow* >::iterator it = mWinMap.begin(); it != mWinMap.end(); ++it)
792  {
793  if (it->second->continuousUpdate())
794  {
795  it->second->update();
796  sleep = false;
797  }
798  }
799  if (sleep)
800  {
801  vl::Time::sleep(10);
802  }
803 }
804 //-----------------------------------------------------------------------------
806 {
807  // Workaround for GLUT bug calling the exit function once per window opened +1
808  // if one explicitly pushes the X button of one of the windows.
809  static bool alread_called = false;
810  if (alread_called)
811  {
812  return;
813  }
814  else
815  {
817  alread_called = true;
818  return;
819  }
820 }
821 //-----------------------------------------------------------------------------
void setPosition(int x, int y)
If the OpenGL context is a widget this function sets its position.
Definition: GLUTWindow.cpp:420
static void glut_motion_func(int x, int y)
Definition: GLUTWindow.cpp:690
void dispatchKeyReleaseEvent(unsigned short unicode_ch, EKey key)
Dispatches the UIEventListener::keyReleaseEvent() notification to the subscribed UIEventListener obje...
Vector2< int > ivec2
A 2 components vector with int precision.
Definition: Vector2.hpp:278
static void glut_visibility_func(int visibility)
Definition: GLUTWindow.cpp:726
void dispatchInitEvent()
Dispatches the UIEventListener::initEvent() notification to the subscribed UIEventListener objects...
vl::ivec2 position() const
If the OpenGL context is a widget this function returns its position.
Definition: GLUTWindow.cpp:442
void keyRelease(EKey key)
Removes the specified key from the set of currently active keys - For internal use only...
void setVSyncEnabled(bool enable)
If the OpenGL context is a widget this function enabled/disables double buffer swapping to the monito...
static void glut_close_func()
Definition: GLUTWindow.cpp:767
void setWidth(int width)
The width of a render target.
Definition: Framebuffer.hpp:78
static void glut_wmclose_func()
Definition: GLUTWindow.cpp:783
bool initGLUTWindow(const vl::String &title, const vl::OpenGLContextFormat &info, int x, int y, int width, int height)
Initializes and shows a GLUT window.
Definition: GLUTWindow.cpp:58
void dispatchMouseDownEvent(EMouseButton button, int x, int y)
Dispatches the UIEventListener::mouseDownEvent() notification to the subscribed UIEventListener objec...
void makeCurrent()
Sets the OpenGL context as current for the calling thread.
Definition: GLUTWindow.cpp:355
Framebuffer * framebuffer()
The default render target (always returns leftFramebuffer()).
The GLUTWindow class implements an OpenGLContext using the GLUT API.
Definition: GLUTWindow.hpp:59
static void glut_reshape_func(int w, int h)
Definition: GLUTWindow.cpp:736
The String class implements an advanced UTF16 (Unicode BMP) string manipulation engine.
Definition: String.hpp:62
const T_Scalar & r() const
Definition: Vector4.hpp:111
void update()
If the OpenGLContext is a widget this function requests a redraw and generates an updateEvent()...
Definition: GLUTWindow.cpp:349
static VLMAIN_EXPORT void shutdown()
Releases all the resources acquired by VLCore and VLGraphics.
vl::EKey mapAsciiKey(unsigned char ascii)
Definition: GLUTWindow.cpp:575
static vl::EKey mapSpecialKey(int special_key)
Definition: GLUTWindow.cpp:586
void dispatchVisibilityEvent(bool visible)
Dispatches the UIEventListener::visibilityEvent() notification to the subscribed UIEventListener obje...
static void glut_idle_func()
Definition: GLUTWindow.cpp:788
void dispatchKeyPressEvent(unsigned short unicode_ch, EKey key)
Dispatches the UIEventListener::keyPressEvent() notification to the subscribed UIEventListener object...
void dispatchResizeEvent(int w, int h)
Dispatches the UIEventListener::resizeEvent() notification to the subscribed UIEventListener objects...
static void glut_display_func()
Definition: GLUTWindow.cpp:754
void keyPress(EKey key)
Inserts the specified key in the set of currently active keys - For internal use only.
bool setFullscreen(bool fs)
If the OpenGL context is a widget this function requests a maximization to fullscreen.
Definition: GLUTWindow.cpp:304
const T_Scalar & g() const
Definition: Vector4.hpp:112
static void glut_special_func(int key, int x, int y)
Definition: GLUTWindow.cpp:650
void dispatchUpdateEvent()
Dispatches the UIEventListener::updateEvent() notification to the subscribed UIEventListener objects...
The OpenGLContextFormat class encapsulates the settings of an OpenGL rendering context.
const ivec4 & rgbaBits() const
void setOpenGLContextInfo(const OpenGLContextFormat &info)
Sets the OpenGLContextFormat associated to an OpenGLContext.
void hide()
If the OpenGL context is a widget this function makes it invisible to the user.
Definition: GLUTWindow.cpp:392
int height() const
Returns the height in pixels of an OpenGLContext.
const ivec4 & accumRGBABits() const
static void glut_special_up_func(int key, int x, int y)
Definition: GLUTWindow.cpp:659
int width() const
Returns the width in pixels of an OpenGLContext.
std::map< unsigned char, vl::EKey > mKeymap
Definition: GLUTWindow.hpp:159
EMouseButton
static void glut_mouse_func(int button, int state, int x, int y)
Definition: GLUTWindow.cpp:668
const T_Scalar & b() const
Definition: Vector4.hpp:113
void dispatchMouseWheelEvent(int n)
Dispatches the UIEventListener::mouseWheelEvent() notification to the subscribed UIEventListener obje...
static void glut_keyboard_up_func(unsigned char ch, int x, int y)
Definition: GLUTWindow.cpp:641
int handle() const
Definition: GLUTWindow.hpp:109
#define NULL
Definition: OpenGLDefs.hpp:81
VLGLUT_EXPORT void atexit_visualization_library_shutdown()
Definition: GLUTWindow.cpp:805
EOpenGLProfile openGLProfile() const
void setWindowTitle(const vl::String &title)
If the OpenGL context is a top window this function sets its title.
Definition: GLUTWindow.cpp:409
void swapBuffers()
Swaps the back and front buffers to present the last rendering.
Definition: GLUTWindow.cpp:376
virtual void setMouseVisible(bool visible)
If the OpenGL context is a widget this function sets whether the mouse is visible over it or not...
Definition: GLUTWindow.cpp:335
vl::ivec2 size() const
Definition: GLUTWindow.cpp:456
void dispatchDestroyEvent()
Dispatches the UIEventListener::destroyEvent() notification to the subscribed UIEventListener(s), calls destroyAllOpenGLResources() and eraseAllEventListeners() This event must be issued just before the actual GL context is destroyed.
void getFocus()
If the OpenGL context is a widget this function requests the mouse focus on it.
Definition: GLUTWindow.cpp:403
static void glut_keyboard_func(unsigned char ch, int x, int y)
Definition: GLUTWindow.cpp:632
void dispatchMouseMoveEvent(int x, int y)
Dispatches the UIEventListener::mouseMoveEvent() notification to the subscribed UIEventListener objec...
static void glut_passive_motion_func(int x, int y)
Definition: GLUTWindow.cpp:701
void setHeight(int height)
The height of a render target.
Definition: Framebuffer.hpp:81
void show()
If the OpenGL context is a widget this function makes it visible to the user.
Definition: GLUTWindow.cpp:381
The GLUT bindings namespace.
std::string toStdString() const
Returns a UTF8 encoded std::string.
Definition: String.cpp:1156
static void sleep(unsigned int milliseconds)
Definition: Time.cpp:144
void setSize(int w, int h)
If the OpenGL context is a widget this function sets its size.
Definition: GLUTWindow.cpp:431
bool initGLContext(bool log=true)
Initializes the supported OpenGL extensions.
#define VL_CHECK(expr)
Definition: checks.hpp:73
static std::map< int, GLUTWindow *> mWinMap
Definition: GLUTWindow.hpp:163
virtual void setMousePosition(int x, int y)
If the OpenGL context is a widget this function sets the mouse position.
Definition: GLUTWindow.cpp:344
static void glut_mouse_wheel_func(int a, int rotation, int c, int d)
Definition: GLUTWindow.cpp:716
const T_Scalar & a() const
Definition: Vector4.hpp:114
void dispatchMouseUpEvent(EMouseButton button, int x, int y)
Dispatches the UIEventListener::mouseUpEvent() notification to the subscribed UIEventListener objects...