GLUT鼠标位置未更新

时间:2010-12-23 19:01:31

标签: c++ glut

我根据鼠标位置旋转相机。但是我只想在鼠标左键或右键按下时才激活它。这段代码的问题是我必须释放并再次按下程序,注意我已经移动了鼠标。

使用键盘按键并移动鼠标时,它可以正常工作。

尝试使用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;  
}

2 个答案:

答案 0 :(得分:2)

您可以合并glutMouseFuncglutMotionFuncglutPassiveMotionFunc来实现目标。

(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);
   }
}

完成这些操作后,只有按住左/右按钮,您才能随时获得光标(x, y)

有关如何组合这些功能的更多信息,您可以查看this site上的3.030部分

答案 1 :(得分:0)

我认为你需要将glutMouseFunc与glutMotionFunc结合起来。在前者中设置鼠标按钮状态,并根据按钮状态更新glutMotionFunc中的旋转。

相关问题