The mouse callback function is responsible for handling mouse button events, both press and release. GLUT passes to the registered callback function an integer identifying the button pressed (or released), the state of the button (up or down) and the x and y device position of the mouse when the button event occurred. We structured our program so that these values are passed from the registered mouse function in main to this mouse function. Also, we are only interested in left button events in this example.

The user of our example is able to rotate the model about the x and y coordinate axes. The user accomplishes such by moving the mouse over the window. Note that the registered motion function is called whenever the mouse moves independent of the state of the mouse buttons. Since we want to acknowledge mouse movement only when the left button is in the down state, we set the Boolean variable dragging to true to indicate such. Of course, when the user releases the button, we set dragging back to false.

Our method for computing the change in the angles of rotation is based on the distance the mouse moves relative to its position when the left button is pressed. Thus, we store the x and y mouse coordinates in the variables startX and startY and use these values in our mouseMotion function. Further, we store for each direction the continuously changing angle of rotation while the mouse is in motion and apply it to the current angle of rotation. For the X angle of rotation, the variables are respectively updatedAngleX and currentAngleX. Similar variables are used for the Y angle of rotation. Last, the updated angles become the current angles when the mouse button is released.