The mouse callback function is responsible for handling mouse button clicks, both press and release. For our example, only left button events are handled by the callback function (keep in mind that
the right button triggers the popup menu). GLUT passes to our function an integer identifying the button pressed (or released), the state of the button (up or down) and the x and y device location of the mouse when the button event occurred.
We use the mouse in our example to select a geometric object. Thus, when the left button is down, we find the selected object, if any. Since the x, y location of the mouse are in device window (screen) coordinates, we convert them to projection window coordinates to find the selected object. If the mouse is over one of our objects, we then
change the mouse cursor, saving the current one for restoring when the
button is released.
In our example, we provide the user with the ability to translate the selected object to a new location. This is accomplished with dragging the mouse. Since we want dragging to occur only when the button is in the down state, we set the boolean variable translating to true to indicate such. Of course, when the user releases the button, we set translating to false. We also set the cursor back to its original when the button is releaased.