4 OpenGLInteraction
4 OpenGLInteraction
2
Event Types
3
Callback Functions
Programming interface for event-driven input
5
GLUT Event Loop
6
The display/draw callback
The display callback is executed whenever GLUT
determines that the window should be refreshed (i.e.
redraw the scene), for example:
When the window is first opened
When the window is reshaped
When a window is exposed
When the user program decides it wants to change the
display
In main.c
- glutDisplayFunc(mydisplay) identifies the function
to be executed
Every GLUT program must have a display callback
7
Posting Redisplays
8
Animating a Display
9
Double Buffering
Display refreshes at 60 ~ 75 Hz
Rendering could be “faster” than the refresh period
Too fast leads to
Frames not shown
Too slow leads to
New and old frame mixed
Flickering
Solution:
Double (or multiple) Buffering
Actually more complex than this – see posted article
Double Buffering
Instead of one color buffer, we use two
Front Buffer: one that is displayed but not written to
Back Buffer: one that is written to but not displayed
Program then requests a double buffer in main.c
- glutInitDisplayMode(GL_RGB | GL_DOUBLE)
At the end of the display callback buffers are swapped
void mydisplay()
{
glClear(GL_COLOR_BUFFER_BIT|….)
.
/* draw graphics here */
.
glutSwapBuffers()
}
11
Using the idle callback
The idle callback is executed whenever there are no events
in the event queue
- glutIdleFunc(myidle)
Useful for animations
12
Using a timer for animation
If we want to run animations at a specified frame rate, use a timer
Idle function tries to redisplay frames as fast as hardware will run –
application dependent
GLUT provides a system timer, allows you to specify a callback function to
be run at specified intervals
glutTimerFunc(20, animationHandler, 0);
void animation Handler(int param)
{
/* change something */
t += dt
glutPostRedisplay();
glutTimerFunc(20, animationHandler, 0);
}
13
Using global variables
The form of all GLUT callbacks is fixed
void mydisplay()
void mymouse(GLint button, GLint state, GLint x, GLint y)
void mydisplay()
{
// draw something that depends on t
}
14
The mouse callback
glutMouseFunc(mymouse);
Returns:
which button caused event (GLUT_LEFT_BUTTON,
GLUT_MIDDLE_BUTTON, GLUT_RIGHT_BUTTON)
state of that button (GLUT_UP, GLUT_DOWN)
position in window
15
Positioning
The position in the screen window is usually measured in pixels with the
origin at the top-left corner
Consequence of refresh done from top to bottom
Programmer sets up a world coordinate system
Programmer must convert screen pixel coordinates of mouse to world
coordinates
(0,0)
h
w
16
Obtaining the window size
• glGetIntv
• glGetFloatv
to obtain any value that is part of the state
17
Using mouse motion callback
We can draw objects continuously as long as a mouse
button is depressed (and hence move them around with the
mouse) by using the motion callback
glutMotionFunc(drawSquare)
18
Using the keyboard
glutKeyboardFunc(mykey);
void mykey(unsigned char key, int x, int y) {…}
● Returns ASCII code of key depressed and mouse location
void mykey()
{
if(key == ‘Q’ | key == ‘q’)
exit(0);
}
19
Special and Modifier Keys
GLUT defines the special keys in glut.h
Function key 1: GLUT_KEY_F1
Up arrow key: GLUT_KEY_UP
◼ if(key == ‘GLUT_KEY_F1’ ……
Can also check of one of the modifiers
GLUT_ACTIVE_SHIFT
GLUT_ACTIVE_CTRL
GLUT_ACTIVE_ALT
is depressed by:
glutGetModifiers()
Allows emulation of three-button mouse with one- or two-
button mice
20
Reshaping the window
21
The Reshape callback
glutReshapeFunc(myreshape);
void myreshape( int w, int h) {…}
22
Example Reshape for 2D Application
●This reshape preserves shapes by making the viewport and world window
have the same aspect ratio
void myReshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION); /* switch matrix mode */
glLoadIdentity();
if (w <= h)
gluOrtho2D(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w,
2.0 * (GLfloat) h / (GLfloat) w);
else gluOrtho2D(-2.0 * (GLfloat) w / (GLfloat) h, 2.0 *
(GLfloat) w / (GLfloat) h, -2.0, 2.0);
glMatrixMode(GL_MODELVIEW); /* return to modelview mode */
}
23
Toolkits and Widgets (GUIs)
Most window systems provide a toolkit or library of
functions for building user interfaces that use special types
of windows called widgets
Widget sets include tools such as
Menus
Slidebars
Dials
Input boxes
But toolkits tend to be platform dependent
GLUT provides only a few widgets including menus
24
Menus
25
Defining a simple menu
In main.c
menu_id = glutCreateMenu(mymenu);
glutAddmenuEntry(“clear Screen”, 1);
gluAddMenuEntry(“exit”, 2);
clear screen
glutAttachMenu(GLUT_RIGHT_BUTTON);
exit
26
Menu actions
● Menu callback
void mymenu(int id)
{
if(id == 1) glClear();
if(id == 2) exit(0);
}
●Subwindows
●Multiple Windows
●Timers
●Portable fonts
- glutBitmapCharacter
- glutStrokeCharacter
29