Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to the Mouse

Similar presentations


Presentation on theme: "Introduction to the Mouse"— Presentation transcript:

1 Introduction to the Mouse
Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Wednesday, September 24, 2003

2 Review: Display Lists A display list (or call list) is a way for OpenGL to save commands for later playback. Display lists are compiled and then executed. Display list storage & format is implementation-dependent and handled internally by OpenGL. We compile a display list in four steps: Generate a “name” (actually an integer) for the list. (glGenLists). Tell OpenGL that we are creating a display list (glNewList). Make the OpenGL function calls that we want to store in the display list. Only OpenGL commands are stored in the list! Tell OpenGL that we are finished creating the list (glEndList). We execute a display list with a single function call. We pass its name to glCallList. 24 Sep 2003 CS 381

3 Review: Text [1/2] In CG, text comes in two varieties:
Bitmap Outline/stroke Both are available, in a limited form, in GLUT. 24 Sep 2003 CS 381

4 Review: Text [2/2] GLUT bitmap text is made using the OpenGL bitmap primitive glBitmap. Bitmaps (and other raster images) are drawn at the raster position. The left-hand point of the baseline is drawn at this position. The baseline is often the bottom of the image. We can move it up, to allow for descenders in text. Set the raster position using glRasterPos*. Parameters of glRasterPos* are handled like those of glVertex*. We draw a character at the current raster position using glutBitmapCharacter. The raster position is advanced. 24 Sep 2003 CS 381

5 The Mouse: Overview We now begin a discussion of event-driven programming using the mouse (or other 2-D pointing device). We will cover: Basic mouse handling via “mouse” & “motion” callbacks. Today Mouse-based pop-up menus. Friday “Picking”. Starting on Monday. But first: Mouse-handling is heavily dependent on pixel coordinates, so we need to know how to deal with these. Therefore, we discuss GLUT’s “reshape” callback. 24 Sep 2003 CS 381

6 Reshape Callback: What It Does
The GLUT reshape callback function is called: When the window is first created, before any other callback. Later, whenever the window size/shape changes. Not when the window is merely moved. GLUT has a built-in reshape function. But it might not do what you want. Registering a callback replaces the built-in function with your own. Reshape has 2 parameters: New width of window, in pixels. New height of window, in pixels. If you need information on the window size somewhere else, save it in the reshape function. Maybe you can get it by other means, but why bother? 24 Sep 2003 CS 381

7 Reshape Callback: Setting the Viewport
The first thing you usually do in a reshape function is void reshape(int w, int h) { glViewport(0, 0, w, h); Function glViewport sets the viewport. Parameters: Left side (pixels from left side of window). Bottom (pixels from bottom of window). Width (in pixels). Height (in pixels). So the above call sets the viewport to the entire windows. 24 Sep 2003 CS 381

8 Reshape Callback: Setting the Projection
The next thing you do in a reshape function is usually to set the projection. For now, gluOrtho2D is generally what you want to use. Later we’ll want perspective. Recall: You can think of this as setting up a coordinate system in the viewport. glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(left, right, bottom, top); glMatrixMode(GL_MODELVIEW); 24 Sep 2003 CS 381

9 Reshape Callback: Working in Pixels
Suppose you want the coordinate system to be in pixels? This might be convenient when dealing with the mouse. void reshape(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0, w, 0, h); glMatrixMode(GL_MODELVIEW); } 24 Sep 2003 CS 381

10 Mouse-Related Callbacks: Mouse & Motion Functions
GLUT has two main mouse-handling callbacks. The mouse function. This is called when any of the mouse buttons (GLUT assumes 3) is pressed or released. … unless the mouse event is intended for some other program. The motion function. This is called (possibly repeatedly) whenever the mouse moves while any of the buttons is pressed. An exception: GLUT includes mouse-controlled pop-up menus. A menu can be “attached” to a mouse button, so that the menu pops up when the button is pressed. If this is done, then the above callbacks are not called when this button is down. We will discuss menus on Friday. Now the details … 24 Sep 2003 CS 381

11 Mouse-Related Callbacks: Mouse Function
The mouse function is called when a mouse button is pressed or released. Registered with glutMouseFunc(mouse); // can use different name Declared as void mouse(int button, int state, int x, int y) button will be one of GLUT_LEFT_BUTTON GLUT_MIDDLE_BUTTON (GLUT started under Unix/X-Windows) GLUT_RIGHT_BUTTON state will be one of GLUT_DOWN (button press) GLUT_UP (button release) x and y are the mouse position, in pixels. x is from left side of window, y is from top (not bottom). 24 Sep 2003 CS 381

12 Mouse-Related Callbacks: Motion Function
The motion function is called if the mouse is moved while a mouse button is down. If the mouse moves again, motion function is called again. Motion functions are useful for dragging and drawing. Registered with glutMotionFunc(motion); // can use different name Declared as void motion(int x, int y) x and y are the mouse position, in pixels. x is from left side of window, y is from top (not bottom). Guaranteed sequence of events when dragging: First the mouse function is called (button down). Then the motion function may be called (zero or more times). Lastly, the mouse function is called (button up). 24 Sep 2003 CS 381

13 Mouse-Related Callbacks: Converting Coordinates [1/2]
GLUT gives mouse coordinates in pixels from the upper-left corner of the window. OpenGL deals with coordinates set by the projection. May not be in pixels. Based at the lower-left corner of the viewport. 24 Sep 2003 CS 381

14 Mouse-Related Callbacks: Converting Coordinates [2/2]
EXAMPLES Suppose we did gluOrtho2D(0.0, 1.0, 0.0, 1.0); How do we convert between GLUT mouse coordinates and OpenGL coordinates? Given x, y; window size w, h. In OpenGL coordinates: x/w, 1-y/h. Note: The above will not work right in a C++ program, since it will do integer division. Use: double(x)/w, 1-double(y)/h. Same question for the coordinates in pixels, as in the last “reshape” slide. In OpenGL coordinates: x, h-y. 24 Sep 2003 CS 381

15 Mouse-Related Callbacks: Other Callbacks
The keyboard and special functions are given the mouse position. Use it if you want. You might treat the keyboard as a large collection of “mouse buttons”. Unconventional … Also, you don’t get mouse-up events. There is a “passive motion” callback. It is called when the mouse moves, and no button is down. Otherwise, it works just like the motion function, except, of course, for the guaranteed sequence of events. 24 Sep 2003 CS 381

16 Mouse-Related Callbacks: Example
Write a program the uses the mouse and motion callbacks. We will do this on Friday. For now, see simplemouse.cpp, on the web page. 24 Sep 2003 CS 381


Download ppt "Introduction to the Mouse"

Similar presentations


Ads by Google