Title: Introduction to the Mouse
1Introduction to the Mouse
- Glenn G. ChappellCHAPPELLG_at_member.ams.org
- U. of Alaska Fairbanks
- CS 381 Lecture Notes
- Wednesday, September 24, 2003
2ReviewDisplay 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.
3ReviewText 1/2
- In CG, text comes in two varieties
- Bitmap
- Outline/stroke
- Both are available, in a limited form, in GLUT.
4ReviewText 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.
5The MouseOverview
- 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 GLUTs reshape callback.
6Reshape CallbackWhat 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?
7Reshape CallbackSetting 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.
8Reshape CallbackSetting 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 well 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)
9Reshape CallbackWorking 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)
10Mouse-Related CallbacksMouse 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
11Mouse-Related CallbacksMouse Function
- The mouse function is called when a mouse button
is pressed or released. - Registered withglutMouseFunc(mouse) // can use
different name - Declared asvoid 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).
12Mouse-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 withglutMotionFunc(motion) // can
use different name - Declared asvoid 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).
13Mouse-Related CallbacksConverting 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.
14Mouse-Related CallbacksConverting Coordinates
2/2
- EXAMPLES
- Suppose we didgluOrtho2D(0.0, 1.0, 0.0,
1.0)How do we convert between GLUT mouse
coordinates and OpenGL coordinates? - Given mouse _at_ 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.
Usedouble(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.
15Mouse-Related CallbacksOther 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 dont 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.
16Mouse-Related CallbacksExample
- 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.