1 / 34

Interaction with Graphics System

Interaction with Graphics System. Graphics System. User. Display. React to Change. Change Image. Input Device. Computer Graphics Conceptual Model. API. Output Devices. Application Program. Application Model. Graphics System. Input Devices. Function Calls or Protocol. Data.

azana
Download Presentation

Interaction with Graphics System

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Computer GraphicsConceptual 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 • Can be implemented by locator and choice

  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 Request 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 Request Measure 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 Trigger Process Measure Process Event: Device & Measure Await Event Queue Program Events 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. Application GL GLU GLUT Window OS GL Library Organization

  14. GLUT Event Processing No Event in Queue 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 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 aGLUT_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 CodeSlide 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 CodeSlide 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 CodeSlide 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); glLoadIdentity(); /* 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 CodeSlide 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 CodeSlide 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.

More Related