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

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

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

Page 1: Events and Coordinates Lecture 5 Fri, Sep 5, 2003.

Events and Coordinates

Lecture 5Fri, Sep 5, 2003

Page 2: Events and Coordinates Lecture 5 Fri, 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.

Page 3: Events and Coordinates Lecture 5 Fri, Sep 5, 2003.

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

Page 4: Events and Coordinates Lecture 5 Fri, Sep 5, 2003.

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.

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

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();}

Page 6: Events and Coordinates Lecture 5 Fri, Sep 5, 2003.

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.

Page 7: Events and Coordinates Lecture 5 Fri, Sep 5, 2003.

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();}

Page 8: Events and Coordinates Lecture 5 Fri, Sep 5, 2003.

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.

Page 9: Events and Coordinates Lecture 5 Fri, Sep 5, 2003.

Processing Special Keystrokes

x and y are the x and y screen coordinates of the mouse when the key was pressed.

Page 10: Events and Coordinates Lecture 5 Fri, Sep 5, 2003.

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();}

Page 11: Events and Coordinates Lecture 5 Fri, Sep 5, 2003.

Example: Drawing an Octagon

DrawOctagon.cpp

Page 12: Events and Coordinates Lecture 5 Fri, Sep 5, 2003.

Chapter 3

More Drawing Tools

Page 13: Events and Coordinates Lecture 5 Fri, Sep 5, 2003.

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).

Page 14: Events and Coordinates Lecture 5 Fri, Sep 5, 2003.

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).

Page 15: Events and Coordinates Lecture 5 Fri, Sep 5, 2003.

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.

Page 16: Events and Coordinates Lecture 5 Fri, Sep 5, 2003.

Changing Coordinate Systems

a b

d

c

(X, Y)

r s

u

v

(x, y)

Screencoordinates

Worldcoordinates

Page 17: Events and Coordinates Lecture 5 Fri, Sep 5, 2003.

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.

Page 18: Events and Coordinates Lecture 5 Fri, Sep 5, 2003.

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);

Page 19: Events and Coordinates Lecture 5 Fri, Sep 5, 2003.

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.

Page 20: Events and Coordinates Lecture 5 Fri, Sep 5, 2003.

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).