Input and Interaction Chapter 3. CS 480/680Chapter 3 -- Input and Interaction2 n Introduction: -We now turn to the development of interactive graphics.

Slides:



Advertisements
Similar presentations
OpenGL CMSC340 3D Character Design & Animation. What is OpenGL? A low-level graphics library specification designed for use with the C and C++ provides…
Advertisements

Better Interactive Programs
1 Computer Graphics Chapter 2 Input Devices. RM[2]-2 Input Devices Logical Input Devices  Categorized based on functional characteristics.  Each device.
CS 4731 Lecture 2: Intro to 2D, 3D, OpenGL and GLUT (Part I) Emmanuel Agu.
Chapter 3: Input and Interaction Instructor: Shih-Shinh Huang 1.
CSC461 Lecture 10: Widgets and Picking Objectives Introduce menus in GLUT Picking –Select objects from the display –Three methods Put things together.
OpenGL (I). What is OpenGL (OGL)? OGL is a 3D graphics & modeling library Can also use it to draw 2D objects.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Better Interactive Programs Ed Angel Professor of Computer Science, Electrical and Computer.
InteractionHofstra University1 Graphics Programming Input and Interaction.
31/1/2006Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)1 CSC345: Advanced Graphics & Virtual Environments Lecture 3: Introduction.
InteractionHofstra University1 Graphics Programming Input and Interaction.
1 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 GLUT Callback Functions.
Based on slides created by Edward Angel
CSC461 Lecture 9: GLUT Callbacks Objectives Introduce double buffering for smooth animations Programming event input with GLUT.
CS 480/680 Computer Graphics Programming with Open GL Part 8: Working with Callbacks Dr. Frederick C Harris, Jr. Fall 2011.
Using Graphics Libraries Lecture 3 Mon, Sep 1, 2003.
CSC461 Lecture 11: Interactive Programs Contents and Objectives Picking Writing modes – XOR/Copy Rubberbanding Display list.
CGMB214: Introduction to Computer Graphics
Introduction to OpenGL and GLUT GLUT. What is OpenGL? An application programming interface (API) A (low-level) Graphics rendering API Generate high-quality.
1 Working with Callbacks Yuanfeng Zhou Shandong University.
More on Drawing in OpenGL: Examples CSC 2141 Introduction to Computer Graphics.
WORKING WITH CALLBACKS Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico Angel: Interactive.
Fundamentals of Computer Graphics Part 3 prof.ing.Václav Skala, CSc. University of West Bohemia Plzeň, Czech Republic ©2002 Prepared with Angel,E.: Interactive.
Interaction with Graphics System
Lecture 5: Interaction 1  Principles of Interactive Graphics  CMSCD2012  Dr David England, Room 711,  ex 2271 
School of Computer Science University of Seoul. 1. Interaction 2. Input Devices 3. Clients and Servers 4. Display Lists 5. Programming Event-Driven Input.
Input and Interaction Chapter 3. CS 480/680 2Chapter 3 -- Input and Interaction Introduction: Introduction: We now turn to the development of interactive.
1Computer Graphics Input and Interaction Lecture 8 John Shearer Culture Lab – space 2
CAP 4703 Computer Graphic Methods Prof. Roy Levow Lecture 3.
Input and Interaction Lecture No. 4.
Lecture 3 OpenGL.
1 Input and Interaction. 2 Input Devices Physical input devices Keyboard devices and pointing devices Logical input devices.
Computer Graphics I, Fall 2010 Input and Interaction.
CD2012 Principles of Interactive Graphics Lecture 01 Introduction Abir Hussain (Rome: 6.33,Tel , Web:
Computing & Information Sciences Kansas State University Lecture 20 of 42CIS 636/736: (Introduction to) Computer Graphics Lecture 21 of 42 William H. Hsu.
CS 480/680 Computer Graphics Programming with Open GL Part 7: Input and Interaction Dr. Frederick C Harris, Jr. Fall 2011.
1 Better Interactive Programs. 2 Objectives Learn to build more sophisticated interactive programs using ­Picking Select objects from the display Three.
Interactive Computer Graphics CS 418 MP1: Dancing I TA: Zhicheng Yan Sushma S Kini Mary Pietrowicz Slides Taken from: “An Interactive Introduction to OpenGL.
Introduction to OpenGL and GLUT. What’s OpenGL? An Application Programming Interface (API) A low-level graphics programming API – Contains over 250 functions.
Program 2 due 02/01  Be sure to document your program  program level doc  your name  what the program does  each function  describe the arguments.
1 Input and Interaction. 2 Objectives Introduce the basic input devices ­Physical Devices ­Logical Devices ­Input Modes Event-driven input Introduce double.
Programming in the Simple Raster Graphics Package (SRGP) Chapter 2.
Computer Graphics: Programming, Problem Solving, and Visual Communication Steve Cunningham California State University Stanislaus and Grinnell College.
Computer Graphics I, Fall 2010 Working with Callbacks.
Input and Interaction Yuanfeng Zhou Shandong University Software College 1.
1 Graphics CSCI 343, Fall 2015 Lecture 6 Viewing, Animation, User Interface.
University of New Mexico
NoufNaief.net TA: Nouf Al-harbi.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
12/22/ :38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Input and Interaction Ed Angel Professor of Computer Science, Electrical and Computer Engineering,
31/1/2006Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)1 CSC345: Advanced Graphics & Virtual Environments Lecture 2: Introduction.
1 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Working with Callbacks.
Computer Graphics (Fall 2003) COMS 4160, Lecture 5: OpenGL 1 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
Review GLUT Callback Functions
Computer Graphics - Chapter 3 Input and Interaction
Working with Callbacks
Better Interactive Programs
CSC461 Lecture 8: Input Devices
Introduction to Computer Graphics with WebGL
Input and Interaction Chapter 3.
Working with Callbacks
Better Interactive Programs
Introduction to OpenGL
Isaac Gang University of Mary Hardin-Baylor
Fundamentals of Computer Graphics Part 3
Input and Interaction Ed Angel
University of New Mexico
Input and Interaction Ed Angel
Input and Interaction Ed Angel Professor Emeritus of Computer Science,
Presentation transcript:

Input and Interaction Chapter 3

CS 480/680Chapter 3 -- Input and Interaction2 n Introduction: -We now turn to the development of interactive graphics programs. -Our discussion has three main parts First, we consider the variety of devices available for interactionFirst, we consider the variety of devices available for interaction We then consider client-server networks and client- server graphicsWe then consider client-server networks and client- server graphics Finally we develop a paint program that demonstrates the important features of interactive graphics programming.Finally we develop a paint program that demonstrates the important features of interactive graphics programming.

CS 480/680Chapter 3 -- Input and Interaction3 1. Interaction One of the most important advances in computer technology was enabling users to interact with computer displaysOne of the most important advances in computer technology was enabling users to interact with computer displays Ivan Sutherland’s Project Sketchpad launched the present era of interactive computer graphicsIvan Sutherland’s Project Sketchpad launched the present era of interactive computer graphics –Since 1963 when this work was published there have been many advances in both hardware and software, but the viewpoint ideas he introduced still dominate computer graphics.

CS 480/680Chapter 3 -- Input and Interaction4 OpenGL does not support interaction directlyOpenGL does not support interaction directly –windowing and input functions were left out of the API for portability reasons. We can avoid potential difficulties by using a simple library, or toolkit, as we did in Chapter 2.We can avoid potential difficulties by using a simple library, or toolkit, as we did in Chapter 2. –Our use of the GLUT toolkit will enable us to avoid the complexities inherent in the interactions among the windowing system Our outline for this chapter looks like this:Our outline for this chapter looks like this: –We start by describing several interactive devices and how to interact with them –We then put these devices in the setting of a client- server network. –Then, we introduce an API for minimal interaction. –Finally, we shall generate sample programs.

CS 480/680Chapter 3 -- Input and Interaction5 2. Input Devices -From the physical perspective, each device has properties that make it more suitable for certain tasks than for others. -There are two primary types of physical devices: Pointing DevicesPointing Devices Keyboard DevicesKeyboard Devices

CS 480/680Chapter 3 -- Input and Interaction6 n 2.1 Physical Input Devices The pointing device allows the user to indicate a position on the screen,The pointing device allows the user to indicate a position on the screen, –and almost always incorporates one or more buttons The keyboard device is almost always a physical keyboard,The keyboard device is almost always a physical keyboard, –but can be generalized to include any device that returns character codes to a program

CS 480/680Chapter 3 -- Input and Interaction7 Pointing devices can be broken into two subcategoriesPointing devices can be broken into two subcategories –relative-position devices –absolute- position devices

CS 480/680Chapter 3 -- Input and Interaction8 Other Pointing DevicesOther Pointing Devices –Joystick –SpaceBalls

CS 480/680Chapter 3 -- Input and Interaction9 n 2.2 Logical Devices Two major characteristics describe the logical behavior of an input deviceTwo major characteristics describe the logical behavior of an input device –What measurements the device returns to the user program –When the device returns those measurements. Some API’s such as PHIGS and GKS consider six classes of logical input devices.Some API’s such as PHIGS and GKS consider six classes of logical input devices. –OpenGL does not follow this approach, but we will look at these classes briefly and see how OpenGL provides similar functionality

CS 480/680Chapter 3 -- Input and Interaction String a device that provides ASCII strings to the user program.a device that provides ASCII strings to the user program. Most windowing systems and OpenGL do not distinguish between a logical string device and the keyboard.Most windowing systems and OpenGL do not distinguish between a logical string device and the keyboard. -2. Locator a device that provides a position in world coordinates to the user program.a device that provides a position in world coordinates to the user program. –It is usually implemented via a pointing device, such as a mouse or a trackball. In OpenGL we usually have to do the conversion from screen coordinates to world coordinates within our own programsIn OpenGL we usually have to do the conversion from screen coordinates to world coordinates within our own programs

CS 480/680Chapter 3 -- Input and Interaction Pick a device that returns the identifier of an object to the user program.a device that returns the identifier of an object to the user program. –It is usually implemented with the same device as the locator, but has a separate software interface In OpenGL, we can use a process called selection to accomplish picking.In OpenGL, we can use a process called selection to accomplish picking. -4. Choice a device that allows the user to select one of a discrete number of options.a device that allows the user to select one of a discrete number of options. In OpenGL we can use various widgets provided by the windowing system.In OpenGL we can use various widgets provided by the windowing system.

CS 480/680Chapter 3 -- Input and Interaction Dial Dials provide analog input to the user programDials provide analog input to the user program Here again widgets provide this facility through graphical devices, such as slidebarsHere again widgets provide this facility through graphical devices, such as slidebars -6. Stroke A device that returns an array of locations.A device that returns an array of locations. Although we can think of a stroke as similar to multiple locators, it is often implemented such that an action, such as pushing down a mouse button, starts the transfer of data into the specified array, and a second action, such as the releasing of the button, ends the transferAlthough we can think of a stroke as similar to multiple locators, it is often implemented such that an action, such as pushing down a mouse button, starts the transfer of data into the specified array, and a second action, such as the releasing of the button, ends the transfer

CS 480/680Chapter 3 -- Input and Interaction13 n 2.3 Measure and Trigger -The manner by which physical and logical input devices provide input to an application program can be described in terms of two entities: Measure -- what is returnedMeasure -- what is returned Trigger -- when it is returnedTrigger -- when it is returned Measure can also include status informationMeasure can also include status information

CS 480/680Chapter 3 -- Input and Interaction14 n 2.4 Input Modes -We can obtain the measure of a device in three distinct modes. Each mode is defined by the relationship between the measure process and the trigger.Each mode is defined by the relationship between the measure process and the trigger. The three modes are:The three modes are: –Request Mode –Sample-Mode –Event Mode

CS 480/680Chapter 3 -- Input and Interaction15 -Request Mode: The measure of the device is not returned to the program until the device is triggered.The measure of the device is not returned to the program until the device is triggered. This mode is standard in non-graphical applications such as typical C programsThis mode is standard in non-graphical applications such as typical C programs –input is buffered until the trigger is hit –for scanf( ) the trigger is the “enter” key.

CS 480/680Chapter 3 -- Input and Interaction16 -Sample Mode: –The input is immediate –As soon as the function call in the user program is encountered, the measure is returned. –Both request mode and sample mode are useful for situations where the program guides the user, but are not useful in applications where the user controls the flow of the program. (Flight Simulator)

CS 480/680Chapter 3 -- Input and Interaction17 -Event Mode: –Each time a device is triggered, an event is generated and placed into an event queue. From here there are two approaches 1) The program can then get events off the queue, and “service” them.1) The program can then get events off the queue, and “service” them. 2) associate a “callback” function with a specific type of event and when that event comes to the front of the queue, that function is automatically executed.2) associate a “callback” function with a specific type of event and when that event comes to the front of the queue, that function is automatically executed.

CS 480/680Chapter 3 -- Input and Interaction18 3. Clients and Servers n Def: Server -A Server is something that can perform tasks for clients. n Clients and Servers can be distributed over a network, or contained entirely within a computational unit. -OpenGL programs are clients that use the graphics server

CS 480/680Chapter 3 -- Input and Interaction19 4. Display Lists -We saw in Chapter 1 the history of graphics hardware

CS 480/680Chapter 3 -- Input and Interaction20 -Today: The display processor has become the graphics server, and the user program has become a client.The display processor has become the graphics server, and the user program has become a client. The issue is not the refresh rate, but the amount of traffic between the client and the serverThe issue is not the refresh rate, but the amount of traffic between the client and the server Now we can send graphical entities to a display in one of two ways:Now we can send graphical entities to a display in one of two ways: –immediate mode –retained-mode

CS 480/680Chapter 3 -- Input and Interaction21 -Immediate Mode: As soon as the program executes a statement that defines a primitive, that primitive is sent to the server for display and no memory of it is retained.As soon as the program executes a statement that defines a primitive, that primitive is sent to the server for display and no memory of it is retained. To redisplay it, you must redefine it and resend itTo redisplay it, you must redefine it and resend it For complex objects in highly interactive applications, this process can cause considerable quantity of data to pass from the client to the serverFor complex objects in highly interactive applications, this process can cause considerable quantity of data to pass from the client to the server

CS 480/680Chapter 3 -- Input and Interaction22 -Retained Mode: We define an object once,We define an object once, –then put its description into a display list The display list is stored in the server and redisplayed by a simple function call issued from the client to the server.The display list is stored in the server and redisplayed by a simple function call issued from the client to the server. Advantages:Advantages: –makes good use of hardware (compute and graphics)(compute and graphics) Disadvantages:Disadvantages: –memory on the server –overhead of creating the lists

CS 480/680Chapter 3 -- Input and Interaction23 n 4.1 Definition and Execution of Display Lists -Display lists have much in common with ordinary files. There must be a mechanism to define/create them as well as and add-to/manipulate them.There must be a mechanism to define/create them as well as and add-to/manipulate them. OpenGL has a small set of functions to manipulate display lists and placed only a few restrictions on display- list contents.OpenGL has a small set of functions to manipulate display lists and placed only a few restrictions on display- list contents. We will look at several examples to show their use.We will look at several examples to show their use.

CS 480/680Chapter 3 -- Input and Interaction24 -Display lists are defined similarly to geometric primitives Each display list must have a unique identifierEach display list must have a unique identifier #define BOX 1#define BOX 1 // GL_COMPILE says // GL_COMPILE says glNewList(BOX, GL_COMPILE); // send to serverglNewList(BOX, GL_COMPILE); // send to server glBegin(GL_POLYGON); // but don’t display glBegin(GL_POLYGON); // but don’t display glColor3f(1.0,0.0,0.0); glColor3f(1.0,0.0,0.0); glVertex2f(-1.0, -1.0); glVertex2f(-1.0, -1.0); glEnd(); glEnd(); glEndList();glEndList();

CS 480/680Chapter 3 -- Input and Interaction25 If we want an immediate display of the contents, we can use the flagIf we want an immediate display of the contents, we can use the flag –GL_COMPIL_AND_EXECUTE Each time that we wish to draw the box on the server, we execute the following:Each time that we wish to draw the box on the server, we execute the following: –glCallList(BOX); Because the call to set the drawing color to Red will still be in effect, you usually see display lists with the following at the beginning:Because the call to set the drawing color to Red will still be in effect, you usually see display lists with the following at the beginning: glPushAttrib(GL_ALL_ATTRIB_BITS);glPushAttrib(GL_ALL_ATTRIB_BITS); glPushMatrix();glPushMatrix(); –and the following at the end glPopAttrib();glPopAttrib(); glPopMatrix();glPopMatrix();

CS 480/680Chapter 3 -- Input and Interaction26 5. Programming Event-Driven Input In this section, we develop event-driven input through a number of simple examples that use the callback mechanism we introduced earlier.In this section, we develop event-driven input through a number of simple examples that use the callback mechanism we introduced earlier. We will examine various events that are recognized by the window system and write callback functions that govern how the application responds to those events.We will examine various events that are recognized by the window system and write callback functions that govern how the application responds to those events.

CS 480/680Chapter 3 -- Input and Interaction27 n 5.1 Using the Pointing Device -Two types of events are associated with the pointing device A move eventA move event –this is generated if the mouse is moved with on of the buttons depressed A passive eventA passive event –this is generated if the mouse is moved without a button being held down. -A mouse event occurs when one of the mouse buttons is either depressed or released

CS 480/680Chapter 3 -- Input and Interaction28 -We specify the mouse callback function in main with the GLUT call glutMouseFunc(mouse_callback_func);glutMouseFunc(mouse_callback_func); -The mouse callback function must have the form void mouse_callback_func(int button, int state, int x, int y);void mouse_callback_func(int button, int state, int x, int y); -The code for this would look like void mouse_callback_function(int button, int state, int x, int y)void mouse_callback_function(int button, int state, int x, int y) { if(button==GLUT_LEFT_BUTTON && if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN) state==GLUT_DOWN) exit(); exit(); }

CS 480/680Chapter 3 -- Input and Interaction29 -Note: with this code, if any other mouse event had occurred, no response action would occur because no callbacks corresponding to the events has been defined (or registered).with this code, if any other mouse event had occurred, no response action would occur because no callbacks corresponding to the events has been defined (or registered). -Our next example is going to draw a box at the mouse location when the left button is pushedis going to draw a box at the mouse location when the left button is pushed and terminate when the middle button is pushed.and terminate when the middle button is pushed.

CS 480/680Chapter 3 -- Input and Interaction30 int main(int argc, char **argv)int main(int argc, char **argv) { glutInit(&argc, argv); glutInit(&argc, argv); glutInitDisplay(GLUT_SINGLE | GLUT_RGB); glutInitDisplay(GLUT_SINGLE | GLUT_RGB); glutCreateWindow(“square”); glutCreateWindow(“square”); myinit(); myinit(); glutReshapeFunc(myReshape); glutReshapeFunc(myReshape); glutMouseFunc(mouse); glutMouseFunc(mouse); glutDisplayFunc(display); glutDisplayFunc(display); glutMainLoop( ); glutMainLoop( ); }

CS 480/680Chapter 3 -- Input and Interaction31 -Reshape event is generated whenever the window is resized.is generated whenever the window is resized. This is typically from user interaction.This is typically from user interaction. We will discuss this later.We will discuss this later. -Display we don’t need the display function since things are only drawn when the mouse button is pushed,we don’t need the display function since things are only drawn when the mouse button is pushed, but it is required by GLUT, so our code will be:but it is required by GLUT, so our code will be: –void display ( ) { }

CS 480/680Chapter 3 -- Input and Interaction32 -Mouse void mouse(int btn, int state, int x, int y)void mouse(int btn, int state, int x, int y) { if(btn==GLUT_LEFT_BUTTON && if(btn==GLUT_LEFT_BUTTON && state=GLUT_DOWN) state=GLUT_DOWN) draw_square(x,y); draw_square(x,y); if(btn==GLUT_MIDDLE_BUTTON && if(btn==GLUT_MIDDLE_BUTTON && state==GLUT_DOWN) state==GLUT_DOWN) exit( ); exit( ); }

CS 480/680Chapter 3 -- Input and Interaction33 Glsizei wh = 500, ww = 500;Glsizei wh = 500, ww = 500; Glfloat size = 3.0;Glfloat size = 3.0; void myinit(void)void myinit(void) { glViewport(0,0,ww,wh); glViewport(0,0,ww,wh); glMatrixMode(GL_PROJECTION); glMatrixMode(GL_PROJECTION); glLoadIdentity( ); glLoadIdentity( ); gluOrtho2D(0.0, (Gldouble)ww, 0.0, (Gldouble)wh); gluOrtho2D(0.0, (Gldouble)ww, 0.0, (Gldouble)wh); glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW); glClearColor(0.0,0.0,0.0,0.0); glClearColor(0.0,0.0,0.0,0.0); glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT); glFlush( ); glFlush( ); }

CS 480/680Chapter 3 -- Input and Interaction34 void drawSquare(int x, int y)void drawSquare(int x, int y) { y=wh-y; y=wh-y; glColor3ub( (char)rand()%256, (char)rand()%256, glColor3ub( (char)rand()%256, (char)rand()%256, (char)rand()%256); (char)rand()%256); glBegin(GL_POLYGON); glBegin(GL_POLYGON); glVertex2f( x+size, y+size); glVertex2f( x+size, y+size); glVertex2f( x-size, y+size); glVertex2f( x-size, y+size); glVertex2f( x-size, y-size); glVertex2f( x-size, y-size); glVertex2f( x+size, y-size); glVertex2f( x+size, y-size); glEnd( ); glEnd( ); glFlush( ); glFlush( ); } Note: The position returned by the mouse is in the window’s system (origin is in the top left), hence we have to flip itNote: The position returned by the mouse is in the window’s system (origin is in the top left), hence we have to flip it

CS 480/680Chapter 3 -- Input and Interaction35 n 5.2 Window Events -If the window size changes, we have to consider three questions: 1. Do we redraw all the objects that were in the window before it was resized?1. Do we redraw all the objects that were in the window before it was resized? 2. What do we do if the aspect ratio of the new window is different from that of the old window?2. What do we do if the aspect ratio of the new window is different from that of the old window? 3. Do we change the size or attributes of new primitives if the size of the new window is different from that of the old?3. Do we change the size or attributes of new primitives if the size of the new window is different from that of the old?

CS 480/680Chapter 3 -- Input and Interaction36 -In our example: We ensure that squares of the same size are drawn, regardless of the size or shape of the window.We ensure that squares of the same size are drawn, regardless of the size or shape of the window. –Therefore, we clear the screen each time it is resized and use the entire new window as our drawing area. The reshape event returns the height and width of the new windowThe reshape event returns the height and width of the new window –We use these to create a new OpenGL clipping window using gluOrtho2D, –As well as a new viewport with the same aspect ratio, –We then clear the window to black.

CS 480/680Chapter 3 -- Input and Interaction37 void myReshape(Glsizei w, Glsizei h)void myReshape(Glsizei w, Glsizei h) { glMatrixMode(GL_PROJECTION); glMatrixMode(GL_PROJECTION); glLoadIdentity( ); glLoadIdentity( ); gluOrtho2D(0.0, (Gldouble)ww, 0.0, (Gldouble)wh); gluOrtho2D(0.0, (Gldouble)ww, 0.0, (Gldouble)wh); glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW); glLoadIdentity( ); glLoadIdentity( ); glViewport(0,0,w,h); glViewport(0,0,w,h); glClearColor(0.0,0.0,0.0,0.0); glClearColor(0.0,0.0,0.0,0.0); glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT); glFlush( ); glFlush( ); }

CS 480/680Chapter 3 -- Input and Interaction38 n 5.3 Keyboard Events Keyboard events are generated when the mouse is in the window and one of the keys is depressed.Keyboard events are generated when the mouse is in the window and one of the keys is depressed. GLUT ignores key releasesGLUT ignores key releases –All the keyboard callbacks are registered in a single callback function such as: glutKeyboardFunc(keyboard);glutKeyboardFunc(keyboard); –For example: void keyboard(unsigned char key, int x, int y)void keyboard(unsigned char key, int x, int y) { if(key==‘q’ || key ==‘Q’) exit(); if(key==‘q’ || key ==‘Q’) exit(); }

CS 480/680Chapter 3 -- Input and Interaction39 n 5.4 The Display and Idle Callbacks glutDisplayFunc(display);glutDisplayFunc(display); –display is invoked when GLUT determines that the window should be redisplayed window initially opened,window initially opened, –We can also tell GLUT to call it with glutPostDisplay();glutPostDisplay(); idleidle –The idle callback is invoked when there are no other events. –Its default is the null function.

CS 480/680Chapter 3 -- Input and Interaction40 n 5.5 Window Management GLUT also supports both multiple windows and subwindowsGLUT also supports both multiple windows and subwindows –We can open a second top-level window by id=glutCreateWindow(“second window”);id=glutCreateWindow(“second window”); –The id allows us to select which window to work in by glutSetWindow(id);glutSetWindow(id); –We can make this window have different properties by invoking the glutInitDisplayMode before glutCreateWindow. –And each window can have its own set of callback functions, because callback specifications refer to the present window.

CS 480/680Chapter 3 -- Input and Interaction41 6. Menus -GLUT provides pop-up menus that we can use with the mouse to create sophisticated interactive applications. -Using the menus involves taking a few simple steps define the entriesdefine the entries link the menu to a mouse buttonlink the menu to a mouse button define a callback for each menu entry.define a callback for each menu entry.

CS 480/680Chapter 3 -- Input and Interaction42 glutCreateMenu(demo_menu);glutCreateMenu(demo_menu); glutAddMenuEntry(“quit”,1);glutAddMenuEntry(“quit”,1); glutAddMenuEntry(“increase square size”,2);glutAddMenuEntry(“increase square size”,2); glutAddMenuEntry(“decrease square size”,3);glutAddMenuEntry(“decrease square size”,3); glutAttachMenu(GLUT_RIGHT_BUTTON);glutAttachMenu(GLUT_RIGHT_BUTTON); -Our callback function is: void demo_menu(int id)void demo_menu(int id) { if(id==1) exit( ); if(id==1) exit( ); else if(id==2)size = 2*size; else if(id==2)size = 2*size; else if (size>1) size = size/2; else if (size>1) size = size/2; glutPostRedisplay( ); glutPostRedisplay( ); }

CS 480/680Chapter 3 -- Input and Interaction43 -GLUT also supports hierarchical menus sub_menu = glutCreateMenu(size_menu);sub_menu = glutCreateMenu(size_menu); glutAddMenuEntry(“increase square size”,2);glutAddMenuEntry(“increase square size”,2); glutAddMenuEntry(“decrease square size”,3);glutAddMenuEntry(“decrease square size”,3); glutCreateMenu(top_menu);glutCreateMenu(top_menu); glutAddMenuEntry(“quit”,1);glutAddMenuEntry(“quit”,1); glutAddSubMenu(“Resize”, sub_menu);glutAddSubMenu(“Resize”, sub_menu); glutAttachMenu(GLUTRIGHTBUTTON);glutAttachMenu(GLUTRIGHTBUTTON);

CS 480/680Chapter 3 -- Input and Interaction44 7. Picking -Picking is an input operation that allows the user to identify an object on the display. Although the action uses the pointing device, the information the user wants is not a position.Although the action uses the pointing device, the information the user wants is not a position. A pick device is considerably more difficult to implement on a modern system than is a locatorA pick device is considerably more difficult to implement on a modern system than is a locator –The text gives some ideas

CS 480/680Chapter 3 -- Input and Interaction45 8. A Simple Paint Program -Any paint program should demonstrate most of the following features: ability to work with geometric objectsability to work with geometric objects ability to manipulate pixelsability to manipulate pixels provide control of attributes (color,...)provide control of attributes (color,...) include menus for controlling the applicationinclude menus for controlling the application behave correctly when the window is moved or resized.behave correctly when the window is moved or resized.

CS 480/680Chapter 3 -- Input and Interaction46

CS 480/680Chapter 3 -- Input and Interaction47 9. Animating Interactive Programs -So far our programs have been static Once a primitive was placed on the display its image did not change until the screen was cleared.Once a primitive was placed on the display its image did not change until the screen was cleared. -Suppose that we want to create a picture in which one or more objects are changing or moving, and thus their images must change.

CS 480/680Chapter 3 -- Input and Interaction48 n 9.1 The Rotating Square Consider the rotating two dimensional point whereConsider the rotating two dimensional point where –x = cos , –y = sin 

CS 480/680Chapter 3 -- Input and Interaction49 void display( )void display( ) { glClear( ); glClear( ); glBegin(GL_POLYGON); glBegin(GL_POLYGON); thetar = theta/((2* )/360.0); thetar = theta/((2* )/360.0); glVertex2f(cos(thetar), sin(thetar)); glVertex2f(cos(thetar), sin(thetar)); glVertex2f(sin(thetar), cos(thetar)); glVertex2f(sin(thetar), cos(thetar)); glVertex2f(-cos(thetar), -sin(thetar)); glVertex2f(-cos(thetar), -sin(thetar)); glVertex2f(sin(thetar), -cos(thetar)); glVertex2f(sin(thetar), -cos(thetar)); glEnd( ); glEnd( ); }

CS 480/680Chapter 3 -- Input and Interaction50 -Now suppose we want to increase theta by a fixed amount whenever nothing else is happening. glutIdleFunc(idle);glutIdleFunc(idle); void idle( )void idle( ) { theta+=2; theta+=2; if(theta >=360.0) theta-=360.0; if(theta >=360.0) theta-=360.0; glutPostRedisplay( ); glutPostRedisplay( ); }

CS 480/680Chapter 3 -- Input and Interaction51 -So, what would this do? glutMouseFunc(mouse);glutMouseFunc(mouse); void mouse(int button, int state, int x, int y)void mouse(int button, int state, int x, int y) { if(button==GLUT_LEFT && state==GLUT_DOWN) if(button==GLUT_LEFT && state==GLUT_DOWN) glutIdleFunc(idle); glutIdleFunc(idle); if(button==GLUT_MIDDLE && state==GLUT_DOWN) if(button==GLUT_MIDDLE && state==GLUT_DOWN) glutIdleFunc(NULL); glutIdleFunc(NULL); }

CS 480/680Chapter 3 -- Input and Interaction52 n 9.2 Double Buffering -Why do we want double buffering? -In main we request a double buffered display by: glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE)glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE) -After we have finished drawing, we call glutSwapBuffers( );glutSwapBuffers( );

CS 480/680Chapter 3 -- Input and Interaction53 n 9.3 Other Buffering Problems -Suppose we want to add an elapsed time clock single buffer causes clock to flickersingle buffer causes clock to flicker double buffer causes drawing to flickerdouble buffer causes drawing to flicker answer is to draw the items to both buffersanswer is to draw the items to both buffers –See Chapter 9 for details

CS 480/680Chapter 3 -- Input and Interaction Design of Interactive Programs -A good interactive program should have: A smooth display (no flicker or artifacts of refresh)A smooth display (no flicker or artifacts of refresh) A variety of interactive devices on the displayA variety of interactive devices on the display A variety of methods for entering and displaying informationA variety of methods for entering and displaying information An easy to use interface that does not require substantial effort to learn.An easy to use interface that does not require substantial effort to learn. Feedback to the user.Feedback to the user. Tolerance for user errorsTolerance for user errors A design that incorporates consideration of both visual and motor properties of humans.A design that incorporates consideration of both visual and motor properties of humans.

CS 480/680Chapter 3 -- Input and Interaction55 n 10.1 Toolkits, Widgets, and the Frame Buffer The paint program used interactive tools, such as pop-up menus that were provided by GLUT.The paint program used interactive tools, such as pop-up menus that were provided by GLUT. There are many more possibilities, such as slide-bars, dials, hot areas on the screen, sound, and iconsThere are many more possibilities, such as slide-bars, dials, hot areas on the screen, sound, and icons –Usually these tools are supplied with various toolkits, although there is nothing to prevent you from writing your own. –Designing our own widget set will be much easier after pixel operations are introduced in Chapter 9 –most of these operations are described in terms of the contents of the frame buffer bit-block transfer operations (bitblt)bit-block transfer operations (bitblt)

CS 480/680Chapter 3 -- Input and Interaction56 -Rubberbanding is one of those techniques Note that before each new line segment is drawn, the previous line segment must be erased.Note that before each new line segment is drawn, the previous line segment must be erased. Usually the rubberbanding begins when the mouse button is depressed and continues until the button is released.Usually the rubberbanding begins when the mouse button is depressed and continues until the button is released. Other objects that are drawn interactively with rubberbanding include rectangles and circles.Other objects that are drawn interactively with rubberbanding include rectangles and circles.

CS 480/680Chapter 3 -- Input and Interaction Summary In this chapter, we have touched on a number of topics related to interactive computer graphicsIn this chapter, we have touched on a number of topics related to interactive computer graphics –These interactive aspects make the field exciting and fun. We handled simple interaction using GLUTWe handled simple interaction using GLUT We have been heavily influenced by the client- server perspectiveWe have been heavily influenced by the client- server perspective –This will be crucial in Chapter 8 –The overhead of setting up a program to run in this environment is small. Each program is structured the same.

CS 480/680Chapter 3 -- Input and Interaction Suggested Readings -Sutherland’s Project Sketchpad -Xerox PARC Research of the 70’s windows-icons-menus-pointing interfaceswindows-icons-menus-pointing interfaces -Human Computer Interface Foley, Van Dam, et.alFoley, Van Dam, et.al -UI Toolkits GLUT,GLUT, Scripting ones -- tcl/tkScripting ones -- tcl/tk......