07_Graphical Input (Updated)
07_Graphical Input (Updated)
6.53
Input
Computer Graphics
CPIT285
OBJECTIVES
⮚Introduce the basic input devices
Physical Devices
Logical Devices
Input Modes
⮚Event-driven input
⮚Introduce double buffering for smooth animations
⮚Programming event input with GLUT
⮚Learn to build interactive programs using GLUT callbacks
Mouse
Keyboard
Reshape
⮚Introduce menus in GLUT
2
PROJECT SKETCHPAD
Ivan Sutherland (MIT 1963) established the basic interactive paradigm that
characterizes interactive computer graphics:
• User points to (picks) the object with an input device (light pen, mouse, trackball)
• Repeat
mous
trackbal light
e
l pen
Graphical input is more varied than input to standard programs which is usually numbers,
characters, or bits
⮚Two older APIs (GKS, PHIGS) defined six types of logical input
Locator: return a position
Pick: return ID of an object
Keyboard: return strings of characters
Stroke: return array of positions
Valuator: return floating point number
Choice: return one of n items
9
INPUT MODES
Input devices contain a trigger which can be used to send a signal to the operating system
Button on mouse
Pressing or releasing a key
When triggered, input devices return information (their measure) to the system
Mouse returns position information
Keyboard returns ASCII code
Most systems have more than one input device, each of which can be triggered at
an arbitrary time by a user
Each trigger generates an event whose measure is put in an event queue which can
be examined by the user program
mouse callback
function
Recall that the last line in main.c for a program using GLUT must be
glutMainLoop();
GLUT checks to see if the flag is set at the end of the event loop.
⮚ If sets then the display callback function is executed
void myidle() {
/* change something */
t += dt
glutPostRedisplay();
}
Void mydisplay() {
glClear();
/* draw something that depends on t */
glutSwapBuffers();
}
float t; /*global */
void mydisplay()
{
/* draw something that depends on t
}
glutMouseFunc(mymouse)
void mymouse(GLint button, GLint state, GLint x, GLint y)
Returns
which button (GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON, GLUT_RIGHT_BUTTON)
caused event
state of that button (GLUT_UP, GLUT_DOWN)
Position in window
(0,0)
h
24
OBTAINING THE WINDOW SIZE
glGetIntv
glGetFloatv
In our original programs, there was no way to terminate them through OpenGL
We can use the simple mouse callback
In the next example, we draw a small square at the location of the mouse each time the left
mouse button is clicked
This example does not use the display callback but one is required by GLUT; We can use the
empty display callback function
mydisplay(){}
We can draw squares (or anything else) continuously as long as a mouse button is
depressed by using the motion callback
glutMotionFunc(drawSquare)
We can draw squares without depressing a button using the passive motion callback
glutPassiveMotionFunc(drawSquare)
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);
}
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
Slide bars
Dials
Input boxes
Three steps
• Define entries for the menu
• Define action for each menu item
• Action carried out if entry selected
• Attach menu to a mouse button
33
DEFINING A SIMPLE MENU
⮚In main.c
menu_id = glutCreateMenu(mymenu);
glutAddmenuEntry(“clear Screen”, 1);
gluAddMenuEntry(“exit”, 2);
clear
glutAttachMenu(GLUT_RIGHT_BUTTON); screen
exi
t
34
MENU ACTIONS
Menu callback
entry in parent
menu
35
OTHER FUNCTIONS IN GLUT
Dynamic Windows
- Create and destroy during execution
Subwindows
Multiple Windows
Changing callbacks during execution
Timers
Portable fonts
glutBitmapCharacter
glutStrokeCharacter
36
THANK YOU