If you want your object rotate according to the distance the mouse is moved, then you will need more than the above code:
1st you need to store the location of the mouse button when it is pressed down:
glutMouseFunc(mouse_button_pressed);
2nd you need to rotate the object when the mouse button is pressed and the mouse is moved on the screen:
glutMotionFunc(mouse_down_move);
void mouse_button_pressed (int button, int state, int xmouse, int ymouse)
button = GLUT_LEFT_BUTTON or GLUT_MIDDLE_BUTTON or GLUT_RIGHT_BUTTON
state = GLUT_UP or GLUT_DOWN
so in mouse_button_pressed you say, if the button is pressed is the left (or middle or right) button, then store the values xmouse and ymouse in some variable, say, xmouse_down and ymouse_down.
void mouse_down_move (int xmouse, int ymouse)
In mouse_down_move you determine the distance the mouse was traveled (whatever direction you want) and then convert that to some angle. eg:
current_rotation_angle = xmouse - xmouse_down
(assuming moving the mouse to left and right does a rotation).
There is one subtly you have to note:
if you build your model view matrix from scratch everytime you do a tranformation, then you have to also remember the last transformation your object was in. This can be done in mouse_button_pressed:
rotation_angle_at_mouse_down = current_rotation_angle.
Then in mouse_down_move you need to make the following change:
current_rotation_angle = xmouse - xmouse_down + rotation_angle_at_mouse_down.
However, if you somehow save your model view matrix at the end of the transformation (eg glGetFloatv (GL_MODELVIEW_MATRIX, history) where history is a 1x16 array then you don’t need to save the rotation_angle_at_mouse_down.
This should be pretty comprehensive, let me know if you need more help.
Rizo
ps. make sure you redraw the screen with glutPostRedisplay. However, make sure you don’t do redundant calls to it. Think about it before you use it, also check your cpu usage once your program is done, if it stays at 100% even if you’re not moving the object, then you have redundant calls.