• No results found

Input and Interaction

N/A
N/A
Protected

Academic year: 2021

Share "Input and Interaction"

Copied!
22
0
0

Loading.... (view fulltext now)

Full text

(1)
(2)

Objectives

•Introduce basic input devices

­ Physical Devices

­ Logical Devices

­ Input Modes

•Event-driven input

•Introduce double buffering for smooth animations

(3)

Project Sketchpad

•Ivan Sutherland (MIT 1963)

•Established basic interactive paradigm •Characterizes interactive computer

graphics:

­ User sees object on display

­ User points to (picks) object with input device (light pen, mouse, trackball)

­ Object changes (moves, rotates, morphs)

(4)

Graphical Input

•Devices can be described either by

­ Physical properties

•Mouse

•Keyboard

•Trackball

­ Logical Properties

•What is returned to program via API – A position

– An object identifier

•Modes

­ How and when input is obtained

(5)

Physical Devices

mouse trackball light pen

(6)

Incremental (Relative) Devices

•Devices such as data tablet return position directly to OS

•Devices such as mouse, trackball, and joy stick return incremental inputs (or velocities) to OS

­ Must integrate these inputs to obtain absolute position

•Rotation of cylinders in mouse

•Roll of trackball

•Difficult to obtain absolute position

(7)

Logical Devices

•Consider the C and C++ code

­ C++: cin >> x;

­ C: scanf (“%d”, &x);

•What is the input device?

­ Can’t tell from the code

­ Could be keyboard, file, output from another program

•Code provides logical input

­ Number (int) returned to program regardless of physical device

(8)

Graphical Logical Devices

•Graphical input more varied than input to standard programs

­ 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

(9)

X Window Input

•X Window System introduced client-server model for network of workstations

­ Client: OpenGL program

­ Graphics Server: bitmap display with pointing device and keyboard

(10)

Input Modes

•Input devices contain trigger

­ Can be used to send signal to OS

­ Button on mouse

­ Press or release key

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

­ Mouse returns position information

(11)

Request Mode

•Input provided to program only when user triggers device

•Typical of keyboard input

­ Can erase (backspace), edit, correct until enter (return) key (the trigger) depressed

(12)

Event Mode

•Most systems have > 1 input device •Each can be triggered at arbitrary

time by user

•Each trigger generates event

•==> measure put in event queue

•==> can be examined by user program

(13)

Event Types

•Window: resize, expose, iconify •Mouse: click one or more buttons •Motion: move mouse

•Keyboard: press or release a key •Idle: nonevent

­ Define what should be done if no other event is in queue

(14)

Callbacks

•Programming interface for event-driven input

•Define callback function for each type of event graphics system recognizes •This user-supplied function executed

when event occurs •GLUT example:

(15)

GLUT callbacks

GLUT recognizes subset of events

recognized by any particular window system (Windows, X, Macintosh)

-glutDisplayFunc -glutMouseFunc -glutReshapeFunc -glutKeyboardFunc -glutIdleFunc -glutMotionFunc, glutPassiveMotionFunc

(16)

GLUT Event Loop

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

glutMainLoop();

==> put program in infinite event loop •In each pass through event loop, GLUT

­ looks at events in queue

­ for each event in queue, GLUT executes appropriate callback function

­ if one is defined

(17)

The display callback

•Display callback executed whenever GLUT determines that window should be refreshed •For example

­ When window first opened

­ When window reshaped

­ When window exposed

­ When user program decides it wants to change display •In main.c

-glutDisplayFunc(mydisplay) identifies function to be

executed

(18)

Posting redisplays

•Many events may invoke display callback function

­ Can lead to multiple executions of display callback on single pass through event loop

•Can avoid this problem by instead using glutPostRedisplay();

==> sets a flag

•GLUT checks to see if flag set at end of event loop

•If set ==> display callback function is executed

(19)

Animating a Display

•When redraw display through display

callback, usually start by clearing window -glClear()

then draw altered display

•Problem: drawing of information in frame buffer is decoupled from display of its

contents

­ Graphics systems use dual ported memory

•Hence we can see partially drawn display

­ See program single_double.c for example with rotating cube

(20)

Double Buffering

•Instead of one color buffer, use two

­ Front Buffer: displayed but not written to

­ Back Buffer: written to but not displayed

•Program then requests double buffer in main.c -glutInitDisplayMode(GL_RGB | GL_DOUBLE)

­ At end of display callback buffers are swapped

void mydisplay() {

glClear(GL_COLOR_BUFFER_BIT|….) .

/* draw graphics here */ .

(21)

Using the idle callback

•Idle callback executed whenever there are no events in event queue

-glutIdleFunc(myidle)

­ Useful for animations

void myidle() { /* change something */ t += dt glutPostRedisplay(); } Void mydisplay() { glClear();

/* draw something that depends on t */ glutSwapBuffers();

(22)

Using globals

•Form of all GLUT callbacks fixed

­ void mydisplay()

­ void mymouse(GLint button, GLint state,

GLint x, GLint y)

•Must use globals to pass information to callbacks

float t; /*global */ void mydisplay()

{

/* draw something that depends on t }

References

Related documents