View Full Version : Mouse movement rotation
Hello, I need help with something rela quick. I have designed and modeled a bezier patch, which is stored in a display list. The porblem is, however, that I need to be able to rotate the patch when the mouse button is pressed down. How do I do this? I am using GLUT, but the documentation seems cryptic at best. Any help or code examples would be very appreciated. Thanks.
To respond to a mouse message in glut you need to use the function glutMouseFunc(mouse). Where mouse is a function to handle what mouse event is called. Example:
void mouse(int button, int state, int x, int y)
if (state == GLUT_DOWN)
/*Add your code here
[This message has been edited by Gill (edited 02-09-2001).]
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:
2nd you need to rotate the object when the mouse button is pressed and the mouse is moved on the screen:
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.
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.
Powered by vBulletin® Version 4.2.3 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.