Events and Coordinates Lecture 5 Fri, Sep 5, 2003.
-
Upload
madlyn-fletcher -
Category
Documents
-
view
213 -
download
0
Transcript of Events and Coordinates Lecture 5 Fri, Sep 5, 2003.
Events and Coordinates
Lecture 5Fri, Sep 5, 2003
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.
Processing Mouse Clicks
The mouse function has prototypevoid 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
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.
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();}
Processing Keystrokes
The keyboard function has prototypevoid 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.
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();}
Processing Special Keystrokes
The special function has prototypevoid 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.
Processing Special Keystrokes
x and y are the x and y screen coordinates of the mouse when the key was pressed.
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();}
Example: Drawing an Octagon
DrawOctagon.cpp
Chapter 3
More Drawing Tools
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).
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).
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.
Changing Coordinate Systems
a b
d
c
(X, Y)
r s
u
v
(x, y)
Screencoordinates
Worldcoordinates
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.
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);
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.
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).