CSC 307 1.
Graphics Programming
Graphics Programming
GLUT
Budditha Hettige
Department of Statistics and Computer Science
Events in OpenGL
Event
Example
GLUT Callback functions
OpenGL Callback
Function
Event-driven: Programs that use windows
Keypress KeyDown
KeyUp
glutKeyboardFunc
Mouse
leftButtonDown
leftButtonUp
glutMouseFunc
Motion
With mouse press
Without
glutMotionFunc
glutPassiveMotionFunc
Window
Moving
Resizing
glutReshapeFunc
Events key press, mouse button press and
release, window resize, etc.
System
Idle
Timer
glutIdleFunc
glutTimerFunc
Your OpenGL program will be in infinite loop
Software What to draw
Input/Output
Wait until an event happens and then execute
some pre-defined functions according to the users
input
glutDisplayFunc
3
GLUT Callback Functions
Rendering Callback
Callback function : Routine to call when an
event happens
Callback function where all our drawing is done
Every GLUT program must have a display callback
Window resize or redraw
User input (mouse, keyboard)
Animation (render many frames)
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();
}
Register callbacks with GLUT
glutDisplayFunc( my_display_func );
glutIdleFunc( my_idle_func );
glutKeyboardFunc( my_key_events_func );
glutMouseFunc ( my_mouse_events_func );
Idle Callback Function
User Input Callbacks
Use for animation and continuous update
Can use glutTimerFunc or timed callbacks for animations
glutIdleFunc( idle );
Process user input (Keyboard/ Mouse)
glutKeyboardFunc( my_key_events ); // for keyboard
glutMouseFunc( my_mouse ); // for mouse
glutKeyboardFunc(void (*func)(unsigned char key, int x, int y))
glutIdleFunc(void (*func)(void)).
glutIdleFunc(MyidleFun);
glutKeyboardFunc sets the keyboard callback for the current window
void my_key_events (char key, int x, int y )
{
switch ( key )
{
case q :
case Q :
exit ( EXIT_SUCCESS);
break;
case r :
case R :
rotate = GL_TRUE;
break;
...
}
}
Function Name
void MyidleFun( void )
{
/* change something */
t += dt;
glutPostRedisplay();
}
7
glutSpecialFunc
glutSpecialFunc Example
The new special callback function
sets the special keyboard callback for the
current window
void SpecialKeys(int key, int x, int y)
{
if(key == GLUT_KEY_UP)
// Some functions
if(key == GLUT_KEY_DOWN)
// Some functions
if(key == GLUT_KEY_LEFT)
if(key == GLUT_KEY_RIGHT)
// Some functions
glutPostRedisplay();
}
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.
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE |
GLUT_RGB);
glutInitWindowSize(800, 600);
glutSpecialFunc(SpecialKeys);
glutDisplayFunc(RenderScene);
glutMainLoop();
return 0;
}
Example
Mouse Callback
Captures mouse press and
glutMouseFunc( my_mouse );
Insert a keyboard function to display the
simple polygon with following optons
Left
Right
Up
Down
F1
F2
F3
F5
10
Increase the width
Descries the width
Increase the height
Descries the height
Change the color as red
Change the color as blue
Change the color white
Set the default values
release events
void myMouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON)
{
}
}
Button
State
11
GLUT LEFT BUTTON, GLUT MIDDLE BUTTON, or
GLUT RIGHT BUTTON.
GLUT_DOWN GLUT_UP
12
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;
}
glutReshapeFunc(void (*func)(int w, int h))
indicates what action should be taken when the window is resized
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
glutTimerFunc registers a timer callback to be
triggered in a specified number of milliseconds
GLUT supports simple cascading pop-up menus
designed to let a user select various modes
within a program
pop-up menu facility with an attempt to create a
full-featured user interface
It is illegal to create, destroy, change, add, or
remove menu items while a menu are in use
Wait ms, value pass to call back function
void glutTimerFunc(unsigned int msecs, void
(*func)(int value), int value);
void TimerFunction(int value)
{
}
Main
glutTimerFunc(33, TimerFunction, 1);
15
16
Menu Functions
Menu Functions
glutCreateMenu
glutRemoveMenuItem
glutRemoveMenuItem remove the specified menu
item.
void glutRemoveMenuItem(int entry);
glutCreateMenu creates a new pop-up menu.
int glutCreateMenu(void (*func)(int value));
glutAddSubMenu
glutAttachMenu
glutAddSubMenu adds a sub-menu trigger to the
bottom of the current menu.
void glutAddSubMenu(char *name, int menu);
glutAddMenuEntry
attaches a mouse button for the current window to
the identifier of the current menu;
void glutAttachMenu(int button);
glutDetachMenu
detaches an attached mouse button from the current
window.
void glutDetachMenu(int button);
glutAddMenuEntry adds a menu entry to the
bottom of the current menu
void glutAddMenuEntry(char *name, int value);
17
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);
glutCreateWindow creates a top-level window.
int glutCreateWindow(char *name);
glutCreateSubWindow
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutCreateSubWindow creates a subwindow.
int glutCreateSubWindow(int win, int x, int y, int width, int
height);
void ProcessMenu(int value)
{
if(value == 0)
{
// TASK FOR 0 VALUE
}
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
glutDestroyWindow destroys the specified window.
void glutDestroyWindow(int win);
glutPostRedisplay
glutPositionWindow requests a change to the
position of the current window.
void glutPositionWindow(int x, int y);
glutReshapeWindow
glutPostRedisplay marks the current window as
needing to be redisplayed.
void glutPostRedisplay(void);
glutReshapeWindow requests a change to the size of
the current window.
void glutReshapeWindow(int width, int height);
glutSwapBuffers
glutFullScreen
glutSwapBuffers swaps the buffers of the current
window if double buffered.
void glutSwapBuffers(void);
glutFullScreen requests that the current window be
made full screen.
void glutFullScreen(void);
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
glutSetCursor changes the cursor image of the current
window.
void glutSetCursor(int cursor);
glutSetCursor(GLUT_CURSOR_NONE);
glutShowWindow, glutHideWindow, glutIconifyWindow
glutShowWindow, glutHideWindow, and glutIconifyWindow
change the display status of the current window.
void glutShowWindow(void);
void glutHideWindow(void);
void glutIconifyWindow(void);
23
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