Help end child hunger

The Mouse

 
Prev: The Code So Far Next: Moving the Camera III
 

In the previous section we saw how to add interactivity to an OpenGL application using GLUT’s Keyboard functionality. Now its time to explore the mouse. GLUT’s mouse interface provides a lot of options for adding mouse interactivy, namely detecting clicks and mouse motion.

Detecting Mouse Clicks

As in the keyboard version, GLUT provides a way for you to register the function that will be responsible for processing events generated by mouse clicks. The name of this function is glutMouseFunc,and it is commonly called in the initialization phase of the application. The syntax is as follows:


void glutMouseFunc(void (*func)(int button, int state, int x, int y));

Parameters:

  • func – The name of the function that will handle mouse click events

As we can see from the signature of glutMouseFunc, the function that will handle the mouse click events must have four parameters.

The first relates to which button was pressed, or released. This argument can have one of three values:

  • GLUT_LEFT_BUTTON
  • GLUT_MIDDLE_BUTTON
  • GLUT_RIGHT_BUTTON

The second argument relates to the state of the button when the callback was generated, i.e. pressed or released. The possible values are:

  • GLUT_DOWN
  • GLUT_UP

When a callback is generated with the state GLUT_DOWN, the application can assume that a GLUT_UP will come afterwards even if the mouse moves outside the window.

The remaining two parameters provide the (x,y) coordinates of the mouse relatively to the upper left corner of the client area of the window.

Detecting Motion

GLUT provides mouse motion detection capabilities to an application. There are two types of motion that GLUT handles: active and passive motion. Active motion occurs when the mouse is moved and a button is pressed. Passive motion is when the mouse is moving but no buttons are pressed. If an application is tracking motion, an event will be generated per frame during the period that the mouse is moving.

As usual you must register with GLUT the function that will be responsible for handling the motion events. GLUT allows us to specify two different functions: one for tracking passive motion, and another to track active motion.

The signatures for the GLUT functions are as follows:


void glutMotionFunc(void (*func) (int x,int y));
void glutPassiveMotionFunc(void (*func) (int x, int y));

Parameters:

  • func – the function that will be responsible for the respective type of motion.

The parameters for the motion processing function are the (x,y) coordinates of the mouse relatively to the upper left corner of the window’s client area.

Detecting when the mouse enters or leaves the window

GLUT is also able to detect when the mouse leaves or enters the window area. A callback function can be registered to handle these two events. The GLUT function to register this callback is glutEntryFunc and the syntax is as follows:


void glutEntryFunc(void (*func)(int state));

Parameters:

  • func – the function that will handle these events.

The parameter of the function that will handle these events tells us if the mouse has entered of left the window region. GLUT defines two constants that can be used in the application:

  • GLUT_LEFT
  • GLUT_ENTERED

Note: This doesn’t work exactly as it says in Microsoft Windows, this is because in Microsoft’s OS the focus is changed with a mouse click. Although you can change this is your own system using some tools from Microsoft, others are likely to have the standard setting so its probably better if you don’t use this feature in Microsoft Windows to detect when the mouse enters/leaves the window.

 

Prev: The Code So Far Next: Moving the Camera III
 

  11 Responses to “The Mouse”

  1. Hi

    Is there a way to handle the scroll wheel movements, like scroll up and scroll down??
    Thank you

  2. This a good and well simplified tutorial. Thanks.
    Please I need information about how I can add buttons to my GUI, and also make establish interactivity between the mouse click and the GLUT models in the GUI

  3. Hi

    thanks for this useful information

    i have a problem with the mouse.i want to change the (x,y) of the mouse and make them change according to lower left corner , how i do that ?

    • Hi Narek,

      In the example provided you can use the mouse to look sideways. I presume that you want to be able to look up and down as well. In order to do that you have to use two angles, an horizontal angle ( as in the example) and a vertical angle. Then with spherical coordinates you can convert those two angles to Cartesian coordinates to compute the look at point.

      • Hi

        Thanks for the Help

        You see all my coordinates are Cartesian coordinates but when i click on the display window it doesnt draw what i want because its coordinates are according to upper left , what i want is to convert my mouse to Cartesian (lower left corner )

        • Oh, sorry. All you have to do is to compute the following yFromBottom = viewportHeight – mouseY, where mouseY is the mouse position assuming the origin is at the top left.

          • Just to be sure that i understand u well

            yFromBottom : y from lower left corner

            viewportHeight : the y coordinate of my display window

            mouseY : the y coordinate of upper left corner

            right ?

          • ViewportHeight is the height of the viewport, the fourth parameter of glViewport, mouseY is the mouse Y coordinate as given by GLUT .

  4. thanks for giving information about how to use all the functions…i do need a help with the mouse function…i need to display an object if the mouse is clicked inside a small square box only (drawn already) and not otherwise…how do i specify the location for x and y…

Leave a Reply to ARF Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: