Input, Interaction, and Intro to Callbacks

Topic 8 – Input, Interaction, & Intro. to Callbacks
Topic 8 – Input, Interaction, & Intro. to Callbacks
Objectives
Topic 8:
• Introduce the basic input devices
Input, Interaction, &
o Physical Devices
o Logical Devices
o Input Modes
Introduction to callbacks
• Event-driven input
• Programming event input with GLUT
• An introduction to callbacks
CITS 3003 Graphics & Animation
Slides: E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012
2
1
Topic 8 – Input, Interaction, & Intro. to Callbacks
Topic 8 – Input, Interaction, & Intro. to Callbacks
Project Sketchpad
Input Devices
• User inputs control the program through input
devices
• Input devices can be viewed as
• Ivan Sutherland (MIT 1963) established
the basic interactive paradigm that
characterizes interactive computer
graphics:
o Physical devices: described by their physical properties,
e.g., mouse, keyboard, trackball, etc.
o User sees an object on the display
o User points to (picks) the object with an input
device (light pen, mouse, trackball)
o Object changes (moves, rotates, morphs)
o Repeat
o Logical devices: characterized by how they influence the
application program, e.g., what is returned to the program
via the API
•
•
3
An 𝑥, 𝑦 position on the screen?
An object identifier for a menu item chosen?
Both are performed by the same physical device (the
mouse, in this case) but what is returned to the program
is different.
1
Topic 8 – Input, Interaction, & Intro. to Callbacks
Topic 8 – Input, Interaction, & Intro. to Callbacks
Physical Devices
Logical Input Devices
• Consider the C and C++ code
o C++: cin >> x;
o C: scanf(”%d”, &x);
• What is the input physical device?
mouse
trackball
o Can’t tell from the code
o Could be keyboard, file, output from another
program
light pen
• The code provides logical input
data tablet
joy stick
o A number (an int) is returned to the
program regardless of the physical device
space ball
5
Topic 8 – Input, Interaction, & Intro. to Callbacks
Topic 8 – Input, Interaction, & Intro. to Callbacks
Incremental (Relative) Devices
Logical Input Devices (cont.)
• Older APIs (GKS, PHIGS, not OpenGL) defined
six specific types of logical input:
• Devices such as the data tablet return a
position directly to the operating system
• Devices such as the mouse, trackball,
and joy stick return incremental inputs
(or velocities) to the operating system
o Locator: return a position, e.g., clicked at by a mouse pointer
o Choice: return one of n discrete items, e.g., a menu item
o String: return strings of characters, e.g., via key
presses
o Stroke: return array of positions
o Must integrate these inputs to obtain an
absolute position
o Valuator: return floating point number
o Pick: return ID of an object
• OpenGL and GLUT provide functions to handle
all these
6
•
•
•
•
7
Rotation of cylinders in mouse
Roll of trackball
Difficult to obtain absolute position
Can get variable sensitivity
8
2
Topic 8 – Input, Interaction, & Intro. to Callbacks
Topic 8 – Input, Interaction, & Intro. to Callbacks
Input Modes
Input Modes (cont.)
• Input devices contain a trigger which can
be used to send a signal to the operating
system
• Input modes concern how and when
input is obtained
• There are two types of input modes:
o Button on mouse
o Pressing or releasing a key
o Request mode
o Event mode
• When triggered, input devices return
information (their measure) to the
system
o Mouse returns position information
o Keyboard returns ASCII code
9
Topic 8 – Input, Interaction, & Intro. to Callbacks
10
Topic 8 – Input, Interaction, & Intro. to Callbacks
Request Mode
Event Mode
• For request mode input, the input value is
provided to program only when user triggers the
device
• A typical example is keyboard input:
o The application program request a keyboard input
from the user
o The user can type, erase (backspace), correct. The
application program hangs there until the enter
(return) key (the trigger) is depressed
11
• Request mode is not suitable for programs
that need to interact with the user.
• Most systems have more than one input
device, each of which can be triggered at an
arbitrary time by a user
12
3
Topic 8 – Input, Interaction, & Intro. to Callbacks
Topic 8 – Input, Interaction, & Intro. to Callbacks
Event Types in event mode input
Event Mode (cont.)
• In event mode input, the program specifies a
number of input events that are of interest.
The program gets into an event handling loop
to deal with the events when they occur
• Each trigger generates an event whose
measure is put in an event queue which can
be examined by the user program
• Window event: resize, expose, iconify
• Mouse event: click one or more buttons
• Motion event: this refers to the mouse move
event (when the cursor is inside the window
of the application program)
• Keyboard: press or release a key
• Idle: non-event
o Define what should be done if no other event is in
the event queue
13
Topic 8 – Input, Interaction, & Intro. to Callbacks
14
Topic 8 – Input, Interaction, & Intro. to Callbacks
Callbacks
GLUT callbacks
• GLUT recognizes a subset of the events
recognized by any particular window
system (Windows, X, Macintosh)
• We can program to handle event mode
input
• We can define a callback function for
each type of event that the graphics
system recognizes
• This user-supplied function is executed
when the event occurs
• GLUT example:
glutMouseFunc(mymouse)
mouse callback function
o
o
o
o
o
o
o
15
glutDisplayFunc
glutMouseFunc
glutReshapeFunc
glutKeyboardFunc
glutIdleFunc
glutMotionFunc
glutPassiveMotionFunc
16
4
Topic 8 – Input, Interaction, & Intro. to Callbacks
Topic 8 – Input, Interaction, & Intro. to Callbacks
GLUT Event Loop
The display callback
• The display callback is executed whenever
GLUT determines that the window should be
refreshed, for example
• Recall that the last line in main.c for a
program using GLUT must be
glutMainLoop();
o
o
o
o
which puts the program in an infinite event
loop
• In each pass through the event loop, GLUT
1. looks at the events in the queue
2. for each event in the queue, GLUT executes the
appropriate callback function if one is defined
3. if no callback is defined for the event, the event is
ignored
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()
o glutDisplayFunc(mydisplay) identifies the
function to be executed
o Every GLUT program must have a display callback
17
Topic 8 – Input, Interaction, & Intro. to Callbacks
18
Topic 8 – Input, Interaction, & Intro. to Callbacks
References
Posting redisplays
• Many events may invoke the display callback
function
“Interactive Computer Graphics – A Top-Down
Approach with Shader-Based OpenGL” by Edward
Angel and Dave Shreiner, 6th Ed, 2012
• Sec. 1.2.4-1.2.7 Input Devices, Physical Input
Devices, Logical Devices, Input Modes
• Sec. 2.1 The Sierpinski Gasket; immediate mode
graphics vs retained mode graphics
• C++ code in the Chapter02 to Chapter04 folders
o Can lead to multiple executions of the display callback
on a single pass through the event loop
• We can avoid this problem by instead using
glutPostRedisplay();
which sets a flag.
• GLUT checks to see if the flag is set at the end
of the event loop
• If set then the display callback function is
executed
19
Angel and Shreiner: Interactive Computer Graphics6E © Addison-Wesley 2012
20
5