1 91.427 computer graphics i, fall 2010 input and interaction
TRANSCRIPT
![Page 1: 1 91.427 Computer Graphics I, Fall 2010 Input and Interaction](https://reader031.vdocuments.site/reader031/viewer/2022032016/56649ebf5503460f94bca8e9/html5/thumbnails/1.jpg)
191.427 Computer Graphics I, Fall 2010
Input and Interaction
![Page 2: 1 91.427 Computer Graphics I, Fall 2010 Input and Interaction](https://reader031.vdocuments.site/reader031/viewer/2022032016/56649ebf5503460f94bca8e9/html5/thumbnails/2.jpg)
291.427 Computer Graphics I, Fall 2010
Objectives
•Introduce basic input devices Physical Devices Logical Devices Input Modes
•Event-driven input•Introduce double buffering for smooth animations
•Programming event input with GLUT
![Page 3: 1 91.427 Computer Graphics I, Fall 2010 Input and Interaction](https://reader031.vdocuments.site/reader031/viewer/2022032016/56649ebf5503460f94bca8e9/html5/thumbnails/3.jpg)
391.427 Computer Graphics I, Fall 2010
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) Repeat
![Page 4: 1 91.427 Computer Graphics I, Fall 2010 Input and Interaction](https://reader031.vdocuments.site/reader031/viewer/2022032016/56649ebf5503460f94bca8e9/html5/thumbnails/4.jpg)
491.427 Computer Graphics I, Fall 2010
Graphical Input
•Devices can be described either by Physical properties
• Mouse• Keyboard• Trackball
Logical Properties• What returned to program via API
– position– object identifier
•Modes How & when input obtained
• Request or event
![Page 5: 1 91.427 Computer Graphics I, Fall 2010 Input and Interaction](https://reader031.vdocuments.site/reader031/viewer/2022032016/56649ebf5503460f94bca8e9/html5/thumbnails/5.jpg)
591.427 Computer Graphics I, Fall 2010
Physical Devices
mouse trackball light pen
data tablet joy stick space ball
![Page 6: 1 91.427 Computer Graphics I, Fall 2010 Input and Interaction](https://reader031.vdocuments.site/reader031/viewer/2022032016/56649ebf5503460f94bca8e9/html5/thumbnails/6.jpg)
691.427 Computer Graphics I, Fall 2010
Incremental (Relative) Devices
•data tablet return position directly to OS
•mouse, trackball, 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• Can get variable sensitivity
![Page 7: 1 91.427 Computer Graphics I, Fall 2010 Input and Interaction](https://reader031.vdocuments.site/reader031/viewer/2022032016/56649ebf5503460f94bca8e9/html5/thumbnails/7.jpg)
791.427 Computer Graphics I, Fall 2010
Logical Devices
•Consider the C and C++ code C++: cin >> x; C: scanf (“%d”, &x);
•What is the input device? Can’t tell from code Could be keyboard, file, output from another program
•Code provides logical input Number (int) returned to program regardless of physical device
![Page 8: 1 91.427 Computer Graphics I, Fall 2010 Input and Interaction](https://reader031.vdocuments.site/reader031/viewer/2022032016/56649ebf5503460f94bca8e9/html5/thumbnails/8.jpg)
891.427 Computer Graphics I, Fall 2010
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 position Pick: return ID of object Keyboard: return strings of characters Stroke: return array of positions Valuator: return floating point number Choice: return one of n items
![Page 9: 1 91.427 Computer Graphics I, Fall 2010 Input and Interaction](https://reader031.vdocuments.site/reader031/viewer/2022032016/56649ebf5503460f94bca8e9/html5/thumbnails/9.jpg)
991.427 Computer Graphics I, Fall 2010
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
![Page 10: 1 91.427 Computer Graphics I, Fall 2010 Input and Interaction](https://reader031.vdocuments.site/reader031/viewer/2022032016/56649ebf5503460f94bca8e9/html5/thumbnails/10.jpg)
1091.427 Computer Graphics I, Fall 2010
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 Keyboard returns ASCII code
![Page 11: 1 91.427 Computer Graphics I, Fall 2010 Input and Interaction](https://reader031.vdocuments.site/reader031/viewer/2022032016/56649ebf5503460f94bca8e9/html5/thumbnails/11.jpg)
1191.427 Computer Graphics I, Fall 2010
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
![Page 12: 1 91.427 Computer Graphics I, Fall 2010 Input and Interaction](https://reader031.vdocuments.site/reader031/viewer/2022032016/56649ebf5503460f94bca8e9/html5/thumbnails/12.jpg)
1291.427 Computer Graphics I, Fall 2010
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
![Page 13: 1 91.427 Computer Graphics I, Fall 2010 Input and Interaction](https://reader031.vdocuments.site/reader031/viewer/2022032016/56649ebf5503460f94bca8e9/html5/thumbnails/13.jpg)
1391.427 Computer Graphics I, Fall 2010
Event Types
•Window: resize, expose, iconify•Mouse: click one or more buttons•Motion: move mouse•Keyboard: press / release key•Idle: nonevent
Define what should be done if no event in queue
![Page 14: 1 91.427 Computer Graphics I, Fall 2010 Input and Interaction](https://reader031.vdocuments.site/reader031/viewer/2022032016/56649ebf5503460f94bca8e9/html5/thumbnails/14.jpg)
1491.427 Computer Graphics I, Fall 2010
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: glutMouseFunc(mymouse)
mouse callback function
![Page 15: 1 91.427 Computer Graphics I, Fall 2010 Input and Interaction](https://reader031.vdocuments.site/reader031/viewer/2022032016/56649ebf5503460f94bca8e9/html5/thumbnails/15.jpg)
1591.427 Computer Graphics I, Fall 2010
GLUT callbacks
GLUT recognizes subset of events recognized by any particular window system (Windows, X, Macintosh)glutDisplayFuncglutMouseFuncglutReshapeFuncglutKeyboardFuncglutIdleFuncglutMotionFunc, glutPassiveMotionFunc
![Page 16: 1 91.427 Computer Graphics I, Fall 2010 Input and Interaction](https://reader031.vdocuments.site/reader031/viewer/2022032016/56649ebf5503460f94bca8e9/html5/thumbnails/16.jpg)
1691.427 Computer Graphics I, Fall 2010
GLUT Event Loop
•Recall that last line in main.c for program using GLUT must beglutMainLoop();
==> 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 defined•if no callback defined for event
– event ignored
![Page 17: 1 91.427 Computer Graphics I, Fall 2010 Input and Interaction](https://reader031.vdocuments.site/reader031/viewer/2022032016/56649ebf5503460f94bca8e9/html5/thumbnails/17.jpg)
1791.427 Computer Graphics I, Fall 2010
The display callback
•Display callback executed whenever GLUT determines that window should be refreshed
•For example, when window
•first opened•window reshaped•exposed
user program decides it wants to change display•In main.c
glutDisplayFunc(mydisplay) identifies function to be executed
Every GLUT program must have display callback
![Page 18: 1 91.427 Computer Graphics I, Fall 2010 Input and Interaction](https://reader031.vdocuments.site/reader031/viewer/2022032016/56649ebf5503460f94bca8e9/html5/thumbnails/18.jpg)
1891.427 Computer Graphics I, Fall 2010
Posting redisplays
•Many events invoke display callback function multiple executions of display callback on single
pass through event loop
•Can avoid this problem by usingglutPostRedisplay();
==> sets flag •GLUT checks to see if flag set at end of event loop
•If set ==> display callback function executed
![Page 19: 1 91.427 Computer Graphics I, Fall 2010 Input and Interaction](https://reader031.vdocuments.site/reader031/viewer/2022032016/56649ebf5503460f94bca8e9/html5/thumbnails/19.jpg)
1991.427 Computer Graphics I, Fall 2010
Animating a Display
•When redraw display through display callback, usually start by clearing windowglClear()
then draw altered display•Problem: drawing of information in frame buffer decoupled from display of contents
Graphics systems use dual ported memory
•Hence can see partially drawn display See program single_double.c for example with
rotating cube
![Page 20: 1 91.427 Computer Graphics I, Fall 2010 Input and Interaction](https://reader031.vdocuments.site/reader031/viewer/2022032016/56649ebf5503460f94bca8e9/html5/thumbnails/20.jpg)
2091.427 Computer Graphics I, Fall 2010
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.cglutInitDisplayMode(GL_RGB | GL_DOUBLE) At end of display callback buffers are swappedvoid mydisplay()
{glClear(GL_COLOR_BUFFER_BIT|….)
./* draw graphics here */.
glutSwapBuffers()}
![Page 21: 1 91.427 Computer Graphics I, Fall 2010 Input and Interaction](https://reader031.vdocuments.site/reader031/viewer/2022032016/56649ebf5503460f94bca8e9/html5/thumbnails/21.jpg)
2191.427 Computer Graphics I, Fall 2010
Using the idle callback
•Idle callback executed whenr no events in event queueglutIdleFunc(myidle) Useful for animationsvoid myidle() {/* change something */
t += dtglutPostRedisplay();
}
Void mydisplay() {glClear();
/* draw something that depends on t */glutSwapBuffers();
}
![Page 22: 1 91.427 Computer Graphics I, Fall 2010 Input and Interaction](https://reader031.vdocuments.site/reader031/viewer/2022032016/56649ebf5503460f94bca8e9/html5/thumbnails/22.jpg)
2291.427 Computer Graphics I, Fall 2010
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}