Events in Opengl Events in Opengl: Graphics Programming Graphics Programming
Events in Opengl Events in Opengl: Graphics Programming Graphics Programming
Graphics Programming
Graphics Programming
GLUT
Budditha Hettige
Department of Statistics and Computer Science
Events in OpenGL
Event
Example
Keypress KeyDown
KeyUp
glutKeyboardFunc
Mouse
leftButtonDown
leftButtonUp
glutMouseFunc
Motion
glutMotionFunc
glutPassiveMotionFunc
Window
Moving
Resizing
glutReshapeFunc
System
Idle
Timer
glutIdleFunc
glutTimerFunc
Input/Output
Wait until an event happens and then execute
some pre-defined functions according to the users
input
glutDisplayFunc
3
Rendering Callback
glutDisplayFunc( my_display_func );
/* this part is in main.c */
void my_display_func (void )
{
glClear( GL_COLOR_BUFFER_BIT );
glBegin( GL_TRIANGLE );
glVertex3fv( v[0] );
glVertex3fv( v[1] );
glVertex3fv( v[2] );
glEnd();
glFlush();
}
glutDisplayFunc( my_display_func );
glutIdleFunc( my_idle_func );
glutKeyboardFunc( my_key_events_func );
glutMouseFunc ( my_mouse_events_func );
glutIdleFunc( idle );
glutIdleFunc(void (*func)(void)).
glutIdleFunc(MyidleFun);
Function Name
glutSpecialFunc
glutSpecialFunc Example
GLUT_KEY_F1
GLUT_KEY_F2
GLUT_KEY_F3
GLUT_KEY_F4
GLUT_KEY_F5
GLUT_KEY_LEFT
GLUT_KEY_UP
GLUT_KEY_RIGHT
F1 function key.
F2 function key.
F3 function key.
F4 function key.
F5 function key.
Left directional key.
Up directional key.
Right directional key.
Example
Mouse Callback
Captures mouse press and
glutMouseFunc( my_mouse );
Left
Right
Up
Down
F1
F2
F3
F5
10
release events
}
}
Button
State
11
Reshape
Example
void mouse(int button, int state, int x, int y)
{
switch (button)
int main(int argc, char** argv)
{
{
glutInit(&argc, argv);
case GLUT_LEFT_BUTTON:
glutInitDisplayMode(GLUT_DOUBLE |
if (state == GLUT_DOWN)
GLUT_RGB);
glutIdleFunc(spinDisplay);
glutInitWindowSize(250, 250);
break;
glutInitWindowPosition(100, 100);
case GLUT_MIDDLE_BUTTON:
glutCreateWindow(argv[0]);
if (state == GLUT_DOWN)
init();
glutIdleFunc(NULL);
glutDisplayFunc(display);
break;
glutReshapeFunc(reshape);
default: break;
glutMouseFunc(mouse);
}
glutMainLoop();
}
return 0;
}
Example
glutReshapeFunc(resize);
void resize(GLsizei w, GLsizei h)
{
if (h==0)
h=1;
// avoid div by 0
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w<=h)
glOrtho(0, 250, 0, 250*h/w, 1.0, -1.0);
else
glOrtho(0, 250*w/h, 0, 250, 1.0, -1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
13
Timer Function
14
OpenGL Menu
16
Menu Functions
Menu Functions
glutCreateMenu
glutRemoveMenuItem
glutRemoveMenuItem remove the specified menu
item.
void glutRemoveMenuItem(int entry);
glutAddSubMenu
glutAttachMenu
glutAddMenuEntry
glutDetachMenu
detaches an attached mouse button from the current
window.
void glutDetachMenu(int button);
Menu Example
18
Window Management
GLUT supports two types of windows: top-level
windows and subwindows
glutCreateWindow
glutCreateMenu(ProcessMenu);
glutAddMenuEntry("Save Image",0);
glutAddMenuEntry("Draw Pixels",1);
glutAddMenuEntry("Flip Pixels",2);
glutAddMenuEntry("Zoom Pixels",3);
glutCreateSubWindow
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutSetWindow, glutGetWindow
glutSetWindow sets the current window;
glutGetWindow returns the identifier of the current
window.
void glutSetWindow(int win);
int glutGetWindow(void);
glutPostRedisplay();
}
19
20
Window Management
Window Management
glutDestroyWindow
glutPositionWindow
glutPostRedisplay
glutReshapeWindow
glutSwapBuffers
glutFullScreen
21
Window Management
22
Window Management
glutPopWindow, glutPushWindow
glutPopWindow and glutPushWindow change the stacking order
of the current window relative to its siblings.
void glutPopWindow(void);
void glutPushWindow(void);
glutSetWindowTitle, glutSetIconTitle
glutSetWindowTitle and glutSetIconTitlechange thewindow or
icon title respectively of the current top-level window.
void glutSetWindowTitle(char *name);
void glutSetIconTitle(char *name);
glutSetCursor
GLUT_CURSOR_RIGHT_ARROW
GLUT_CURSOR_LEFT
GLUT_CURSOR_INFO
GLUT_CURSOR_DESTROY.
GLUT_CURSOR_HELP
GLUT_CURSOR_CYCLE
GLUT_CURSOR_SPRAY
GLUT_CURSOR_WAIT
GLUT_CURSOR_NONE
GLUT_CURSOR_INHERIT
24