22
Lecture 10: Input, Interaction & callbacks CITS 3003 Graphics & Animation Slides: E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012

Lecture 10: Input, Interaction & callbacksteaching.csse.uwa.edu.au/.../10_Input-interaction-and-callbacks.pdf · Objectives •Introduce the basic input devices –Physical Devices

Embed Size (px)

Citation preview

Page 1: Lecture 10: Input, Interaction & callbacksteaching.csse.uwa.edu.au/.../10_Input-interaction-and-callbacks.pdf · Objectives •Introduce the basic input devices –Physical Devices

Lecture 10: Input, Interaction & callbacks

CITS 3003 Graphics & Animation Slides: E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012

Page 2: Lecture 10: Input, Interaction & callbacksteaching.csse.uwa.edu.au/.../10_Input-interaction-and-callbacks.pdf · Objectives •Introduce the basic input devices –Physical Devices

Objectives

• Introduce the basic input devices

– Physical Devices

– Logical Devices

– Input Modes

• Event-driven input

• Programming event input with GLUT

• An introduction to callbacks

2

Page 3: Lecture 10: Input, Interaction & callbacksteaching.csse.uwa.edu.au/.../10_Input-interaction-and-callbacks.pdf · Objectives •Introduce the basic input devices –Physical Devices

Project Sketchpad

• Ivan Sutherland (MIT 1963) established the basic interactive paradigm that characterizes interactive computer graphics:

– User sees an object on the display

– User points to (picks) the object with an input device (light pen, mouse, trackball)

– Object changes (moves, rotates, morphs)

– Repeat

3

Page 4: Lecture 10: Input, Interaction & callbacksteaching.csse.uwa.edu.au/.../10_Input-interaction-and-callbacks.pdf · Objectives •Introduce the basic input devices –Physical Devices

Input Devices

• User inputs control the program through input devices

• Input devices can be viewed as

o Physical devices: described by their physical properties, e.g., mouse, keyboard, trackball, etc.

o Logical devices: characterized by how they influence the application program, e.g., what is returned to the program via the API

• 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.

4

Page 5: Lecture 10: Input, Interaction & callbacksteaching.csse.uwa.edu.au/.../10_Input-interaction-and-callbacks.pdf · Objectives •Introduce the basic input devices –Physical Devices

Physical Devices

mouse trackball light pen

data tablet joy stick space ball

5

Page 6: Lecture 10: Input, Interaction & callbacksteaching.csse.uwa.edu.au/.../10_Input-interaction-and-callbacks.pdf · Objectives •Introduce the basic input devices –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? o Can’t tell from the code o Could be keyboard, file, output from another

program

• The code provides logical input o A number (an int) is returned to the program

regardless of the physical device

6

Page 7: Lecture 10: Input, Interaction & callbacksteaching.csse.uwa.edu.au/.../10_Input-interaction-and-callbacks.pdf · Objectives •Introduce the basic input devices –Physical Devices

Logical Input Devices (cont.)

• Older APIs (GKS, PHIGS, not OpenGL) defined six specific types of logical input:

– Locator: return a position, e.g., clicked at by a mouse pointer

– Choice: return one of n discrete items, e.g., a menu item

– String: return strings of characters, e.g., via key presses

– Stroke: return array of positions

– Valuator: return floating point number

– Pick: return ID of an object

• OpenGL and GLUT provide functions to handle all these

7

Page 8: Lecture 10: Input, Interaction & callbacksteaching.csse.uwa.edu.au/.../10_Input-interaction-and-callbacks.pdf · Objectives •Introduce the basic input devices –Physical Devices

Incremental (Relative) Devices

• 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 Must integrate these inputs to obtain an

absolute position • Rotation of cylinders in mouse • Roll of trackball • Difficult to obtain absolute position • Can get variable sensitivity

8

Page 9: Lecture 10: Input, Interaction & callbacksteaching.csse.uwa.edu.au/.../10_Input-interaction-and-callbacks.pdf · Objectives •Introduce the basic input devices –Physical Devices

Input Modes

• Input devices contain a trigger which can be used to send a signal to the operating system

o Button on mouse

o Pressing or releasing a key

• When triggered, input devices return information (their measure) to the system

o Mouse returns position information

o Keyboard returns ASCII code

9

Page 10: Lecture 10: Input, Interaction & callbacksteaching.csse.uwa.edu.au/.../10_Input-interaction-and-callbacks.pdf · Objectives •Introduce the basic input devices –Physical Devices

Input Modes (cont.)

• Input modes concern how and when input is obtained

• There are two types of input modes: o Request mode

o Event mode

10

Page 11: Lecture 10: Input, Interaction & callbacksteaching.csse.uwa.edu.au/.../10_Input-interaction-and-callbacks.pdf · Objectives •Introduce the basic input devices –Physical Devices

Request 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

Page 12: Lecture 10: Input, Interaction & callbacksteaching.csse.uwa.edu.au/.../10_Input-interaction-and-callbacks.pdf · Objectives •Introduce the basic input devices –Physical Devices

Event Mode

• 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

Page 13: Lecture 10: Input, Interaction & callbacksteaching.csse.uwa.edu.au/.../10_Input-interaction-and-callbacks.pdf · Objectives •Introduce the basic input devices –Physical Devices

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

13

Page 14: Lecture 10: Input, Interaction & callbacksteaching.csse.uwa.edu.au/.../10_Input-interaction-and-callbacks.pdf · Objectives •Introduce the basic input devices –Physical Devices

Event Types in event mode input

• 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

14

Page 15: Lecture 10: Input, Interaction & callbacksteaching.csse.uwa.edu.au/.../10_Input-interaction-and-callbacks.pdf · Objectives •Introduce the basic input devices –Physical Devices

Callbacks

• 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 15

Page 16: Lecture 10: Input, Interaction & callbacksteaching.csse.uwa.edu.au/.../10_Input-interaction-and-callbacks.pdf · Objectives •Introduce the basic input devices –Physical Devices

GLUT callbacks

• GLUT recognizes a subset of the events recognized by any particular window system (Windows, X, Macintosh) o glutDisplayFunc

o glutCreateMenu

o glutMouseFunc

o glutReshapeFunc

o glutKeyboardFunc

o glutIdleFunc

o glutMotionFunc

o glutPassiveMotionFunc

16

Page 17: Lecture 10: Input, Interaction & callbacksteaching.csse.uwa.edu.au/.../10_Input-interaction-and-callbacks.pdf · Objectives •Introduce the basic input devices –Physical Devices

glutMouseFunc % usage

void glutMouseFunc(void (*func)(int button, int state, int x, int y));

Example:

glutMouseFunc(myMouseFun); % defined in the main()

% the function definition

void myMouseFun(int button, int state, int x, int y)

{

% this is where you write code for what you want to do % when a mouse “event” happens

} 17

Page 18: Lecture 10: Input, Interaction & callbacksteaching.csse.uwa.edu.au/.../10_Input-interaction-and-callbacks.pdf · Objectives •Introduce the basic input devices –Physical Devices

GLUT Event Loop

• Recall that the last line in main.c for a program using GLUT must be

glutMainLoop();

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

18

Page 19: Lecture 10: Input, Interaction & callbacksteaching.csse.uwa.edu.au/.../10_Input-interaction-and-callbacks.pdf · Objectives •Introduce the basic input devices –Physical Devices

The display callback

• The display callback is executed whenever GLUT determines that the window should be refreshed, for example o When the window is first opened

o When the window is reshaped

o When a window is exposed

o 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

19

Page 20: Lecture 10: Input, Interaction & callbacksteaching.csse.uwa.edu.au/.../10_Input-interaction-and-callbacks.pdf · Objectives •Introduce the basic input devices –Physical Devices

Posting redisplays

• Many events may invoke the display callback function 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

20

Page 21: Lecture 10: Input, Interaction & callbacksteaching.csse.uwa.edu.au/.../10_Input-interaction-and-callbacks.pdf · Objectives •Introduce the basic input devices –Physical Devices

Mid-Semester Test

• When – Tuesday 10 April during the lecture 12:00-12:45PM

• Where – In ENCM [G04] i.e. where Tuesday lectures are held

• What is included – Everything from Lecture 1 to Lecture 10

• What to bring to the test – A pen to write with (no pencils, no books, no notes)

• See unit website for sample test from 2016 – http://teaching.csse.uwa.edu.au/units/CITS3003/index.php?fna

me=lectures

Page 22: Lecture 10: Input, Interaction & callbacksteaching.csse.uwa.edu.au/.../10_Input-interaction-and-callbacks.pdf · Objectives •Introduce the basic input devices –Physical Devices

Further Reading

“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

22