C++ GLUT鼠标位置未更新
我正在根据鼠标位置旋转相机。但我只想在鼠标左键或右键按下时激活此选项。这段代码的问题是,我必须释放并再次按下,程序才会注意到我移动了鼠标 当使用键盘键和移动鼠标时,它起作用了 试图重新显示,但我不确定它是否是我需要的或如何使用它C++ GLUT鼠标位置未更新,c++,glut,C++,Glut,我正在根据鼠标位置旋转相机。但我只想在鼠标左键或右键按下时激活此选项。这段代码的问题是,我必须释放并再次按下,程序才会注意到我移动了鼠标 当使用键盘键和移动鼠标时,它起作用了 试图重新显示,但我不确定它是否是我需要的或如何使用它 void processMouse(int button, int state, int x, int y) { if (state == GLUT_DOWN) { if (button == GLUT_LEFT_BUTTON) {mouseM=true;
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;
}
void进程鼠标(int按钮、int状态、int x、int y){
如果(状态==GLUT\U DOWN){
if(button==GLUT\u LEFT\u button){mouseM=true;}if(button==GLUT\u RIGHT\u button){mouseN=true;}
}if(state==GLUT\u UP){if(button==GLUT\u LEFT\u button){mouseM=false;}if(button==GLUT\u RIGHT\u button){mouseN=false;}
}
无效鼠标移动(整数x,整数y){
如果(x<0)angleX=0.0;如果(x>w)angleX=180.0;否则//angleX=5.0*((浮动)x)/w;angleX=(x-320)/50;angleZ=angleX;angleY=(y-240)/50;
}
我认为您需要将glutMouseFunc与。在前者中设置鼠标按钮状态,并根据按钮状态更新glutMouseFunc中的旋转。您可以组合使用glutMouseFunc
、glutMouseFunc
和GLUTMOUSIONFUNC
来实现它
(1) glutMotionFunc
仅当按下鼠标按钮时,才会在任何时刻告诉您光标的(x,y)
。另一方面,当没有按下按钮时,glutpassiveemotionfunc
会告诉您(x,y)
。(有关更多详细信息,请查看)
(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...
}
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);
}
}
其次,为glutMouseFunc
准备一个函数,比如说onMouse
,例如:
glutMouseFunc(onMouse);
在onMouse
函数中,它将如下所示:
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...
}
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);
}
}
完成这些操作后,只有按住左/右按钮,您才能随时获取光标的(x,y)
有关如何组合这些函数的更多信息,您可以查看位于的3.030部分。我不完全确定您的意思。您是否有机会向我展示一些描述它的快速psuedo代码?Plow的意思是,鼠标有两个单独的回调函数:一个用于鼠标位置,一个用于操作(即点击按钮)您需要分别处理这两个方面