Presentation is loading. Please wait.

Presentation is loading. Please wait.

Events and Coordinates Lecture 5 Fri, Sep 5, 2003.

Similar presentations


Presentation on theme: "Events and Coordinates Lecture 5 Fri, Sep 5, 2003."— Presentation transcript:

1 Events and Coordinates Lecture 5 Fri, Sep 5, 2003

2 Mouse and Keyboard Interaction Through the callback functions, we may process mouse clicks and keystrokes. This will be our only form of input to our programs.

3 Processing Mouse Clicks The mouse function has prototype void mouse(int button, int state, int x, int y); Values of button GLUT_BUTTON_LEFT GLUT_BUTTON_RIGHT Values of state GLUT_UP GLUT_DOWN

4 Processing Mouse Clicks x and y are the x and y screen coordinates of the mouse when the key was pressed, measured in pixels. y is measured from the top of the window down. x is measured from the left of the window across.

5 Processing Mouse Clicks Skeleton code for mouse(). void mouse(int button, int state, int x, int y) { // Filter out other mouse events if (button == GLUT_BUTTON_LEFT && state == GLUT_DOWN) { // Do something } glutPostRedisplay(); }

6 Processing Keystrokes The keyboard function has prototype void keyboard(unsigned char key, int x, int y); key is any ASCII character on the keyboard. x and y are the x and y screen coordinates of the mouse when the key was pressed.

7 Processing Keystrokes Skeleton code for keyboard(). void keyboard(unsigned char key, int x, int y) { // Switch on designated keys only switch (key) { case ‘a’: // Do something default: break; } glutPostRedisplay(); }

8 Processing Special Keystrokes The special function has prototype void special(int key, int x, int y); key is nearly any non-ASCII character on the keyboard. Values of key GLUT_KEY_LEFT – left arrow key, etc. GLUT_KEY_F1 – F1 function key, etc. GLUT_KEY_HOME – home key, etc.

9 Processing Special Keystrokes x and y are the x and y screen coordinates of the mouse when the key was pressed.

10 Processing Special Keystrokes Skeleton code for special(). void special(int key, int x, int y) { // Switch on designated keys only switch (key) { case GLUT_KEY_LEFT: // Do something default: break; } glutPostRedisplay(); }

11 Example: Drawing an Octagon DrawOctagon.cpp

12 Chapter 3 More Drawing Tools

13 World Coordinates The world coordinate system is the coordinate system of the model itself, expressed in world units. It is established by calling gluOrtho2D(). gluOrtho2D(xmin, xmax, ymin, ymax).

14 Screen Coordinates The screen (or window) coordinate system is the coordinate system of the screen (or window), expressed in pixels. It is established by calling glViewport(). glViewport(left, bottom, width, height).

15 Changing Coordinate Systems We might need to convert from one coordinate system to another When we go from screen coordinates (e.g., a mouse click) to world coordinates. When we resize the window.

16 Changing Coordinate Systems ab d c (X, Y) rs u v (x, y) Screen coordinates World coordinates

17 Change of Coordinates The points (X, Y) and (x, y) occupy the same relative positions in their respective rectangles. Therefore, (x – r)/(s – r) = (X – a)/(b – a) and so x = X(s – r)/(b – a) + (br – as)/(b – a). Similarly for y.

18 Example: Change of Coordinates Given the statements express x and y in terms of X and Y. x = X/40 – 8, y = Y/40 – 6. gluOrtho2D(-8, 8, -6, 6); glViewport(0, 0, 640, 480);

19 Change of Coordinates Furthermore, since the mouse() function measures the y-coordinate from the top down, we must make an additional adjustment. Normally we will replace y by screenHeight – y. In the last example, we now have x = X/40 – 8, y = (screenHeight – Y)/40 – 6.

20 Change of Coordinates If we want to convert world coordinates into screen coordinates, this will require the inverse transformation. In the last example, we find X = 40x + 320, Y = screenHeight – (40y + 240).


Download ppt "Events and Coordinates Lecture 5 Fri, Sep 5, 2003."

Similar presentations


Ads by Google