首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GLUT鼠标位置未更新

GLUT鼠标位置未更新
EN

Stack Overflow用户
提问于 2010-12-24 03:01:31
回答 2查看 2.4K关注 0票数 2

我根据鼠标的位置旋转我的相机。但我希望这只在鼠标左键或右键按下时激活。这段代码的问题是,我必须松开并再次按下,让程序注意到我移动了鼠标。

当使用键盘键并移动鼠标时,它起作用了。

尝试glutPostRedisplay,但我不确定它是否是我需要的或如何使用它。

代码语言:javascript
复制
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;  
}
EN

回答 2

Stack Overflow用户

发布于 2014-10-26 16:30:27

您可以结合使用glutMouseFuncglutMotionFuncglutPassiveMotionFunc来实现它。

(1)仅当按下鼠标的按钮时,glutMotionFunc才会在任何时刻告诉您光标的位置( (x, y) )。另一方面,当没有按钮被按下时,glutPassiveMotionFunc会告诉你(x, y)。(有关更多详细信息,请查看glut specification )。

(2)组合这些功能

首先,准备onLeftButton(int x, int y)onRightButton(int x, int y)来分别处理按下左键和按右键的事件,如下所示:

代码语言:javascript
复制
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

代码语言:javascript
复制
glutMouseFunc(onMouse);

onMouse函数中,它将如下所示:

代码语言:javascript
复制
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部分

票数 4
EN

Stack Overflow用户

发布于 2010-12-24 05:16:14

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4521656

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档