Presentation is loading. Please wait.

Presentation is loading. Please wait.

Interaction with Graphics System

Similar presentations


Presentation on theme: "Interaction with Graphics System"— Presentation transcript:

1 Interaction with Graphics System
User Display React to Change Change Image Input Device

2 Computer Graphics Conceptual Model
API Output Devices Application Program Application Model Graphics System Input Devices Function Calls or Protocol Data

3 Physical vs. Logical Input Devices
Application Program Logical Device Software Driver Physical Device Mouse, Keyboard Trackball, etc.

4 What is it?

5 Logical Input Devices String Provides ASCII strings to user program
Usually implemented as physical keyboard Locator Inputs position in defined coordinate system Absolute vs. relative Direct vs. indirect Choice Choice among a discrete number of options Implemented by menus or control button widget Valuator (or Dial) Provides analog input to user program Bounded vs. unbounded Often implemented as slidebar widget

6 Logical Input Devices, contd.
Pick Returns identifier of an object to user program Can be implemented by locator and choice Stroke Returns an array of locations

7 Measure vs. Trigger The measure of a device is the value returned to the user program. String device: ASCII string Locator: Location coordinates Choice: Identifier of option chosen etc. Trigger of a device is a physical signal that the user can use to signal the computer that the measure is available. String device (keyboard): Return key Locator (mouse): mouse button(s)

8 Request Mode Input 1. Program requests measure of a device and blocks.
Trigger Process Measure Process Program Trigger Measure 1. Program requests measure of a device and blocks. 2. Measure process maintains current measure. 3. Trigger process signals measure process to return measure. request_locator(device_id, &measure);

9 Sample Mode Input 1. Program requests measure of a device and blocks.
Process Program Measure 1. Program requests measure of a device and blocks. 2. Measure process immediately returns current measure. sample_locator(device_id, &measure);

10 Event Mode Input Trigger Process Measure Process Event Queue Program
Device & Measure Await Event Queue Events Program Event

11 Event Driven Interaction-Blocking
Initialize, including generating the initial image; Activate input devices in event mode; do { wait for user-triggered event on any device; switch (which device caused event) { case DEVICE_1: collect DEVICE_1 measure, process, respond; case DEVICE_2: collect DEVICE_2 measure, process, respond; ... } while (user does not request exit);

12 Event Driven Interaction-NonBlocking
Initialize, including generating the initial image; Activate input devices in event mode; do { if (there is an event on the event queue) switch (which device caused event) { case DEVICE_1: collect DEVICE_1 data, process, respond; case DEVICE_2: collect DEVICE_2 data, process, respond; ... } Do background processing; while (user does not request exit);

13 GL Library Organization
Window OS Application GL GLU GLUT

14 GLUT Event Processing Event in Queue No Call Idle Function Yes Call
Function n Pop Event From Queue Call Function n Call Function n Call Function n Call Function n Call Function n Switch on Event Type

15 Callbacks NB: Function parameters must match measures contained
Event Type “Registered” Function Event A m1, m2 function_A(m1, m2) Event B function_B( ) Event C function_C( ) Event D Event E Event F No Event Idle function( ) NB: Function parameters must match measures contained in associated event.

16 Display Event Trigger: GLUT determines that the window needs to be
redisplayed. A display event is generated when the window is first drawn. Callback function form: void display(); Registration: glutDisplayFunc(display); A display callback function must be registered for each window.

17 GLUT Mouse Event Trigger: Any mouse button is depressed or released.
Callback function form: void mouse_callback_func(int button, int state, int x, int y); Registration: glutMouseFunc(mouse_callback_function);

18 GLUT Defined Mouse Constants
GLUT_LEFT_BUTTON GLUT_MIDDLE_BUTTON GLUT_RIGHT_BUTTON GLUT_UP GLUT_DOWN Systems with only one mouse button can only generate a GLUT_LEFT_BUTTON callback.

19 GLUT Reshape Event Trigger: Active window is resized
Callback function form: void reshape_func(GLsizei w, GLsizei h); Registration: glutReshapeFunc(reshape_func);

20 GLUT Move Event Trigger: The mouse is moved while one or more mouse
buttons are pressed. Callback function form: void motion_func(int x, int y); Registration: glutMotionFunc(motion_func);

21 GLUT Keyboard Event Trigger: Any key is depressed.
Callback function form: void keyboard_function(unsigned char key, int x, int y); Registration: glutKeyboardFunc(keyboard_function);

22 Other Defined Events in GLUT
glutPassiveMotionFunc glutVisibilityFunc glutEntryFunc glutSpecialFunc glutSpaceballMotionFunc glutSpaceballRotateFunc glutSpaceballButtonFunc glutButtonBoxFunc glutDialsFunc glutTabletMotionFunc glutTabletButtonFunc glutMenuStatusFunc

23 Example: Simple Square Drawing Program
Open a window. Clear it to black. Draw a box at location of the mouse each time the left button is clicked. Color of box should be randomly selected from RGB space. Clear window when resized. Quit when right button is clicked.

24 Square Program Source Code Slide 1
/* 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*/ #include <GL/gl.h> #include <GL/glut.h> /* globals */ GLsizei wh = 500, ww = 500; /* initial window size */ GLfloat size = 3.0; /* half side length of square */

25 Square Program Source Code Slide 2
void drawSquare(int x, int y) { y=wh-y; glColor3ub( (char) random()%256, (char) random()%256, (char) random()%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(); }

26 Square Program Source Code Slide 3
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); /* adjust viewport and clear */ glViewport(0,0,w,h); glClearColor (0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); glFlush(); /* set global size for use by drawing routine */ ww = w; wh = h; }

27 Square Program Source Code Slide 4
void myinit(void) { glViewport(0,0,ww,wh); /* Pick 2D clipping window to match size of screen window This choice avoids having to scale object coordinates each time window is resized */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, (GLdouble) ww , 0.0, (GLdouble) wh , -1.0, 1.0); /* set clear color to black and clear window */ glClearColor (0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); glFlush(); /* callback routine for reshape event */ glutReshapeFunc(myReshape); }

28 Square Program Source Code Slide 5
void mouse(int btn, int state, int x, int y) { if(btn==GLUT_RIGHT_BUTTON&state==GLUT_DOWN) exit(); } int main(int argc, char** argv) glutInit(&argc,argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutCreateWindow("square"); myinit (); glutReshapeFunc (myReshape); glutMouseFunc (mouse); glutMotionFunc(drawSquare); glutMainLoop();

29 Output of “Square” Program

30 Implementing Choice: Menus in GLUT
Four steps: Create menu: glutCreateMenu(menu); Define menu entries: glutAddMenuEntry Attach menu to a mouse button: gluAttachMenu Define callback function: void menu(int id);

31 Creating a Menu in GLUT int glutCreateMenu(void (*func)(int value));
Menu Identifier Choice Callback Function Creates a new pop-up menu. Returns a unique integer identifier for the menu. Takes as argument a pointer to a single callback function that takes an integer argument. The integer argument of the callback is mapped to the menu choice. Sets the current menu to the newly created menu.

32 Associating a Menu with a Mouse Key
void glutAttachMenu(int button); Associates the selected button with the current menu. button is selected from the GLUT defined button constants: GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON, GLUT_RIGHT_BUTTON

33 Adding Entries to the Menu
void glutAddMenuEntry(char *name, int value); String to appear in menu entry. Value to be passed to callback function. Adds a menu entry to the bottom of the current menu.

34 Building a Sub-menu void glutAddSubMenu(char *name, int menu);
ASCII string to display in the menu item from which to cascade sub-menu. Identifier of menu to cascade from this sub-menu item.


Download ppt "Interaction with Graphics System"

Similar presentations


Ads by Google