我根据鼠标的位置旋转我的相机。但我希望这只在鼠标左键或右键按下时激活。这段代码的问题是,我必须松开并再次按下,让程序注意到我移动了鼠标。
当使用键盘键并移动鼠标时,它起作用了。
尝试glutPostRedisplay,但我不确定它是否是我需要的或如何使用它。
void processMouse(int button, int state, int x, int y) {
if (state == GLUT_DOWN) {
if (button == GLUT_LEFT_BUTTON) {mouseM=true;} if (button == GLUT_RIGHT_BUTTON) {mouseN=true;}
} if (state == GLUT_UP){ if (button == GLUT_LEFT_BUTTON){mouseM=false;} if (button == GLUT_RIGHT_BUTTON) {mouseN=false;} }
}
void mouseMove(int x, int y){
if (x < 0) angleX = 0.0; else if (x > w) angleX = 180.0; else //angleX = 5.0 * ((float) x)/w; angleX = (x-320)/50; angleZ = angleX; angleY= (y-240)/50;
}发布于 2014-10-26 16:30:27
您可以结合使用glutMouseFunc、glutMotionFunc和glutPassiveMotionFunc来实现它。
(1)仅当按下鼠标的按钮时,glutMotionFunc才会在任何时刻告诉您光标的位置( (x, y) )。另一方面,当没有按钮被按下时,glutPassiveMotionFunc会告诉你(x, y)。(有关更多详细信息,请查看glut specification )。
(2)组合这些功能
首先,准备onLeftButton(int x, int y)和onRightButton(int x, int y)来分别处理按下左键和按右键的事件,如下所示:
void onLeftButton(int x, int y){
//change variables for your glRotatef function for example
//(x, y) is the current coordinate and
//(preMouseX, preMouseY) is the previous coordinate of your cursor.
//and axisX is the degree for rotation along x axis. Similar as axisY.
axisX += (y - preMouseY);
axisY += (x - preMouseX);
...
}
void onRightButton(int x, int y){
//do something you want...
}其次,为glutMouseFunc准备一个函数,例如onMouse:
glutMouseFunc(onMouse);在onMouse函数中,它将如下所示:
void onMouse(int button, int state, int x, int y)
{
if(state == GLUT_DOWN){
if(button == GLUT_RIGHT_BUTTON)
glutMotionFunc(onRightButton);
else if(button == GLUT_LEFT_BUTTON)
glutMotionFunc(onLeftButton);
}
}完成这些操作后,只有当按下并按住left/right按钮时,您才能在任何时刻获得光标的(x, y)。
有关如何组合这些函数的更多信息,您可以在this site查看3.030部分
发布于 2010-12-24 05:16:14
我认为你需要把你的glutMouseFunc和glutMotionFunc结合起来。在前者中设定鼠标按钮状态,并根据按钮状态在glutMotionFunc中更新旋转。
https://stackoverflow.com/questions/4521656
复制相似问题