Download presentation
Presentation is loading. Please wait.
Published byPatricia Walters Modified over 9 years ago
1
CAP 4703 Computer Graphic Methods Prof. Roy Levow Lecture 3
2
Input and Interaction User interaction is an important aspect of many graphic applications Best supported through a standard API –Programmer avoids nasty implementaiton details GLUT provides support for OpenGL
3
Input Devices API supports logical devices –Defined by capabilities rather than physical design Different physical devices may provide same capabilities –Mouse, Trackball, possibly Light Pen
4
Types of Input Devices Pointing device –Mouse, Trackball –Data tablet reports position of stylus on tablet surfacd –Light pen reports position of pen on screen –Reports position in image Keyboard device –Character stream
5
Types of Input Devices (cont) Joystick –Reports x-y displacement –Usually treated as direction vector Space ball –Fixed ball on stick –Senses x, y, x pressure as well as 3-axis rotational pressure
6
Logical Input Devices String device –Provides character strings as input Locator Device –Provides position in world coordinates Pick device –Returns a reference to an object –Usually implemented with a locator
7
Logical Input Devices (cont) Choice device –Selection from a discrete number of objects –Usually impemented with a window widget: menu, button, etc. Dial device –Provides digitized analog input Stroke device –Returns array of locations –Often the track of a locator device
8
Measure and Trigger Measure of a device is the data it provides to the program Trigger is the action that causes the data to be sent
9
Input Modes Request mode –Program requests data from device –Requires trigger but input may not be accepted by program until a later time of the programmer’s choice Sample mode –Input is immediate –No trigger is needed
10
Input Modes (cont) Event mode –Supports asynchronous interaction –Events are queued for event handler
11
Clients and Servers Servers perform tasks for clients –Client requests –Server responds A windowed system is a server –Provides display and input services to a program using the display
12
Display Lists Display list or display file –Instructions to display processor to generate image –Stored in memory Immediate mode –Display server generates image and does not retain commands Retained mode –Display list is stored on graphics server –Can be used to regenerate images
13
Definition & Execution of Display Lists Generated like geometric objects Bounded by –glNewList(item, GL_COMPILE) –glEndList() Imaging commands are generated and sent to display server but not displayed Displayed immedaitely if second parameter is GL_COMPILE_AND_EXECUTE
14
Executing Display Lists Display list with –glCallList(item) Each time list is displayed, current model-view and projection matrices are applied, possibly changing image Display list is more efficient than regenerating image
15
Executing Display Lists (cont) Attributes changed in a display list alter state and remain in effect after list is displayed –This may be a problem Solution involves using OpenGL attribute and matrix stacks. Push current values before executing list and pop them after
16
Attribute and Matrix Stacks Save with glPushAttrib(GL_ALL_ATTRIB_BITS);glPushMatrix(); Restore with glPopAttrib();glPopMatrix(); (skipping sectin 3.4.2)
17
Event-Driven Programming Programmer provides functions to be executed when an event occurs Functions are registered with system and called when event occurs –Implementation may be based on functions with standard names as in Java callback functions registered with system as in OpenGL function has standard prototype to deliver event data
18
Mouse Event Callback Function name may be freely chosen Function prototype void mouse(int button, int state, int x, int y) Declared constants for button and state like GLUT_LEFT_BUTTONGLUT_DOWN
19
Mouse Event Callback Function registration glutMouseFunc(mouse)
20
Adding Events Example program from section 3.5.1 Example program Example program main() has two added elements –glutReshapeFunc() Called when window is reshaped –glutMouseFunc() Called when mouse button is pressed or released
21
Program Functions Mouse –Draw a square at the current location using default parameters when left button is pressed –Exit when middle button is pressed What about windows? Init –sets view –clear to black
22
Program Functions (cont) drawSquare –Draws fixed size square in random color –Note inversion of y-coordinate Translates window (mouse) to display Reshape –A window event parameters for GLsizei w, h –Resize view port and clipping to preserve aspect for square in window
23
Keyboard Events Triggered when key is pressed only Returns character and x, y mouse location when key is pressed
24
Display and Idle Callbacks glutDisplayFunc(display) –Called whenever window needs to be redisplayed glutPostRedisplay() –Redisplay only once each iteration of GLUT main loop –Avoids unnecessary redisplay
25
Menus GLUT supports pop-up menus Menu creation glutCreateMenu(demo_menu); glutAddMenuEntry("quit",1); glutAddMenuEntry("increase square size", 2); glutAddMenuEntry("decrease square size", 3); glutAttachMenu(GLUT_RIGHT_BUTTON); Callback function has single int parameter for menu item
26
GLUT Menus (cont) Also supports hierarchical menus sub_menu = glutCreateMenu(size_menu); glutAddMenuEntry("Increase square size", 2); glutAddMenuEntry("Decrease square size", 3); glutCreateMenu(top_menu); glutAddMenuEntry("Quit",1); glutAddSubMenu("Resize", sub_menu); glutAttachMenu(GLUT_RIGHT_BUTTON);
27
Picking Selection –Adjust clipping region –Add items in region to hit list Bounding rectangle or extent –Mark region –Add items in marked region to hit list Use back buffer to display each item in different color –Identify selected items
28
OpenGL Selection Mode Selection mode display allows picking –Selected item – hit –Stored on name stack Key functions –Create array for stack void glSelectBuffer(GLsizei n, void glSelectBuffer(GLsizei n, // eg 512 GLunint *buff) –Initialize name stack void glInitNames()
29
OpenGL Selection Mode Push name on stack void glPushName(GLunint name) void glPushName(GLunint name) Pop top of stack void glPopName() Replace top name void glLoadName(GLunint name) Must initialize by pushing a useless value, say 0
30
Selection Mode Values on stack at the time an item is drawn determine its name glLoadName(i);drawFigure(i); If stack contains more than one value, name has multiple components – one for each value on the stack
31
Pick Matrix Get current mouse location with glGetIntegerv (GL_VIEWPORT, viewport); // vp is GLint[4] viewport); // vp is GLint[4] Set selection rectangle with gluPickMatrix(x, y, width, height, viewport) // x,y,w,h are GLdoubles
32
Example Pick Program ..\openglbk\picksquare.c..\openglbk\picksquare.c
33
Simple Paint Program see newpaint.c newpaint.c
34
Animation glutIdleFunction() specifies a callback function to use when the main loop is idle The callback can draw an altered image each time it is called and call glutPostRedisplay() to display it To disble the animation, register NULL as the Idle Function
35
Double Buffering When redrawing an image, we want the new image to be displayed quickly enough that the redrawing operation will not be visible Double Buffering creates the new image in a second buffer and displays it only on completion of drawing
36
Double Buffering (cont) Enabled by setting –GLUT_DOUBLE IN glutInitDisplayMode() Front buffer is displayed Back buffer is the one into which we draw Swap buffers –glutSwapBuffers()
37
Flicker Clearing areas in an image can produce flicker Double buffering can help with this But we have to repeatedly draw things Can set the drawing buffer with –glDrawBuffer(which) GL_BACK, GL_FRONT_AND_BACK
38
Logic Operations Drawing can be done in copy mode, also called replacement mode –pixels are set by drawing or by performing one of 16 possible functions on 2 biits in XOR mode –new pixel value is XORed with current pixel value
39
Logic Modes OpenGL supports all 16 modes Default is GL_COPY Enable use of other modes with –glEnable(GL_COLOR_LOGIC_OP) Set with –glLogicOP() GL_COPY, GL_XOR, … Redrawing in XOR mode erases Can also change color, reverse with all 1’s
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.