Presentation is loading. Please wait.

Presentation is loading. Please wait.

InteractionHofstra University1 Graphics Programming Input and Interaction.

Similar presentations


Presentation on theme: "InteractionHofstra University1 Graphics Programming Input and Interaction."— Presentation transcript:

1 InteractionHofstra University1 Graphics Programming Input and Interaction

2 InteractionHofstra University2 Interaction Early 60’s, Ivan Sutherland’s Project Sketchpad Basic Paradigm: User see an image, reacts with an interactive device, image changes in response to input,...

3 InteractionHofstra University3 Interaction OpenGL does not support interaction directly Increase portability – work in a variety of environments Windowing and input functions left out of API – emphasis on rendering Use toolkits, GLUT

4 InteractionHofstra University4 Physical Input Devices Pointing Device – indicates position on screen Keyboard Device – return character codes to a program

5 InteractionHofstra University5 Logical Input Devices Major Characteristics: What measurements the device returns to the program When the device returns those measurements

6 InteractionHofstra University6 Application Input Measure - what the device returns to the program Trigger – signal send to the computer Three distinct modes defined by the relationship between the measure process and the trigger. Request Mode, Sample Mode, Event Mode

7 InteractionHofstra University7 Request Mode The measure of the device is not returned to the program until the device is triggered

8 InteractionHofstra University8 Sample Mode Measure is returned immediately after the function is called in the user program (device sample). No trigger needed Useful in apps where the program guides the user

9 InteractionHofstra University9 Event Mode Can handle multiple inputs When device triggered an event is generated Identifier for device placed in the event queue Event queue process is independent of the application, asynchronous A callback function associated with a specific type of event

10 InteractionHofstra University10 Event-Driven Programming Callback functions implement how the application program responds to events Examples, each of the events below needs a callback function to handle it. Pointing Events Window Events Keyboard Events Display & Idle Events

11 InteractionHofstra University11 Pointing Device Almost always a mouse Move event – when mouse is moved with button depressed; Passive Move Event – move without pressing button Mouse Event – mouse button is depressed or released glutMouseFunc (mouse);

12 InteractionHofstra University12 Pointing Device – Mouse Callback void mouse(int btn, int state, int x, int y) { if (btn==GLUT_RIGHT_BUTTON && state==GLUT_DOWN) exit(0); } Depressing the right Button terminates the program

13 InteractionHofstra University13 Register callbacks int main(int argc, char** argv) { glutInit(&argc,argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutCreateWindow("square"); myinit (); glutReshapeFunc (myReshape); glutMouseFunc (mouse); glutMotionFunc(drawSquare); glutDisplayFunc(display); glutMainLoop(); } Called whenever the window is resized

14 InteractionHofstra University14 Window Events (Resizing – Dragging) Redraw all the objects? How do we handle aspect ratio? Change size of attributes and primitives?

15 InteractionHofstra University15 Square Program Example This program illustrates the use of the glut library for interfacing with a Window System The program opens a window, clears it to black,then draws a box at the location of the mouse each time the left button is clicked. The right button exits the program The program also reacts correctly when the window is moved or resized by clearing the new window to black

16 InteractionHofstra University16 Global Variables Size of window Viewport position and size Size of clipping window /* globals */ GLsizei wh = 500, ww = 500; /* initial window size */ GLfloat size = 3.0; /* half side length of square */

17 InteractionHofstra University17 Window Event – Reshape Example: change clipping wndw to match screen wndw, viewport matches new aspect ratio, whole screen wndw void myReshape(GLsizei w, GLsizei h) { /* adjust clipping box */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, (GLdouble)w, 0.0, (GLdouble)h, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); /* adjust viewport and clear */ glViewport(0,0,w,h); glClearColor (0.0, 0.0, 0.0, 1.0); glClear(GL_COLOR_BUFFER_BIT); glFlush(); /* set global size for use by drawing routine */ ww = w; wh = h; } reshaping routine called with glutReshapeFunc (myReshape); whenever window is resized or moved

18 InteractionHofstra University18 Pointing Device – Motion Callback Example: whenever mouse dragged and button down draw random color square at the mouse position void drawSquare(int x, int y) { y=wh-y; glColor3ub( (char) rand()%256, (char) rand()%256, (char) rand()%256); glBegin(GL_POLYGON); glVertex2f(x+size, y+size); glVertex2f(x-size, y+size); glVertex2f(x-size, y-size); glVertex2f(x+size, y-size); glEnd(); glFlush(); } Called with glutMotionFunc(drawSquare); if button held down

19 InteractionHofstra University19 Keyboard Events void keyboard(unsigned char key, int x, int y) { if (key=='q' || key=='Q') exit(0); } glutKeyboardFunc(keyboard);

20 InteractionHofstra University20 Window Management id=glutCreateWindow(“second window”); glutSetWindow(id);

21 InteractionHofstra University21 Menus Pop-up menus Common Steps: Define the entries Link the menu to a mouse button Define callback function

22 InteractionHofstra University22 Registering a menu callback and defining a menu glutCreateMenu(demo_menu); glutAddMenuEntry(“quit”, 1); glutAddMenuEntry(“increase square size”, 2); glutAddMenuEntry(“decrease square size”, 3); glutAttachMenu(GLUT_RIGHT_BUTTON); demo_menu is the callback function for the menu The menu appears on right-button mouse click identifier passed to callback

23 InteractionHofstra University23 Menus void demo_menu(int id) { if (id==1) exit (0); if (id==2) size = 2* size; else if (size > 1) size = size/2; glutPostRedisplay( ); }

24 InteractionHofstra University24 Hierarchical Menus top_menu size_menu

25 InteractionHofstra University25 Hierarchical Menus GLint sub_menu = glutCreateMenu(size_menu); glutAddMenuEntry(“increase square size”, 1); glutAddMenuEntry(“decrease square size”, 2); glutCreateMenu(top_menu); glutAddMenuEntry(“quit”, 1); glutAddSubMenu(“Resize”, sub_menu); glutAttachMenu(GLUT_RIGHT_BUTTON); top_menu and size_menu are callback functions for the top- and the sub-menus, respectively

26 InteractionHofstra University26 Picking Picking – identify an object on the display Don’t return an x,y position, pick an “object” NOT an easy process Two methods: selection and bounding rectangles.

27 InteractionHofstra University27 Selection Adjusting the clipping region and viewport to keep track of which primitives in a small clipping region are rendered into a region near the cursor. Primitives are entered into a hit list to be examined by the user program later on.

28 InteractionHofstra University28 Animation: Using idle callback and double buffering Example Rotating Cube Program We want to create animation We continuously keep changing the value of

29 InteractionHofstra University29 Spin the square: globals #define PI 3.141592 GLdouble theta = 0; // rotation angle GLsizei wnd_w = 500; // display window width GLsizei wnd_h = 500; // display window height GLfloat spin_speed = PI/180; // incerement for angle in rad

30 InteractionHofstra University30 Spin square: prototypes void help(); //print instructions void myinit(); // OpenGL init void display(); // display callback void myreshape(GLsizei, GLsizei); // reshape callback void update_angle(); // idle callback void control_speed(GLubyte, GLint, GLint); // keybrd callback

31 InteractionHofstra University31 Spin square: reshape callback /* keep the viewport aspect ratio 1, so square does not get distorted */ void myreshape(GLsizei w, GLsizei h) { wnd_w = w; wnd_h = h; if (h < w) w=h; // adjust viewport (to preserve aspect ratio 1), and clear. glViewport(0, 0, w, w); glutPostRedisplay(); }

32 InteractionHofstra University32 Spin square: keyboard callback // keyboard callback increase speed, decrease speed or quit void control_speed(GLubyte key, GLint x, GLint y) { switch (key) { case('q'): case('Q'): exit (0); break; case('s'): case('S'): spin_speed *=2; break; case('d'): case( 'D'): spin_speed /= 2; } glutPostRedisplay( ); }

33 InteractionHofstra University33 Spin square: idle callback // idle callback void update_angle(void) { theta += spin_speed; if (theta>2*PI) theta-= 2*PI; glutPostRedisplay(); } glutIdleFunc(update_angle);

34 InteractionHofstra University34 Double Buffering A complex display may not be drawn in a single refresh cycle Double Buffering solves the problem Assume two frame buffers: front buffer and back buffer Swap these from the application program invoking a display callback

35 InteractionHofstra University35 Double Buffering void display(void) { /* display callback, clear frame buffer and z buffer, */ /* rotate cube and draw, swap buffers */ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);... glRotatef(theta[2], 0.0, 0.0, 1.0); colorcube(); glFlush(); glutSwapBuffers(); } glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); Updating back buffer and swapping

36 InteractionHofstra University36 Spin square: double buffering void display() { glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_POLYGON); glColor3f(1.0, 0.0, 0.0); glVertex2d(cos(theta), sin(theta)); glColor3f(0.0, 0.1, 0.0); glVertex2d(-sin(theta), cos(theta)); glColor3f(1.0, 0.0, 1.0); glVertex2d(-cos(theta), -sin(theta)); glColor3f(1.0, 1.0, 0.0); glVertex2d(sin(theta), -cos(theta)); glEnd(); glutSwapBuffers(); }

37 InteractionHofstra University37 Spin square: register callbacks int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); glutInitWindowSize(wnd_w,wnd_h); glutInitWindowPosition(0,0); glutCreateWindow("spinning square, speed control with menu"); myinit(); glutDisplayFunc(display); glutReshapeFunc(myreshape); glutIdleFunc(update_angle); glutKeyboardFunc(control_speed); glutMainLoop(); return 0; }

38 InteractionHofstra University38 Good Interactive Programs Smooth display Interactive devices Variety of methods to input data Easy-to-use interface Feedback to user (help) Human consideration (HCI)


Download ppt "InteractionHofstra University1 Graphics Programming Input and Interaction."

Similar presentations


Ads by Google