GLUT 鼠标位置未更新

发布于 2024-10-08 23:24:33 字数 724 浏览 3 评论 0原文

我根据鼠标位置旋转相机。但我希望仅当鼠标左键或右键按下时才激活此功能。这段代码的问题是我必须释放并再次按下才能让程序注意到我已经移动了鼠标。

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

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

I'm rotating my camera depending on mouse position. But I want this active only when either left or right mouse button is down. The problem with this code is that I have to release and press again for the program to notice that I've moved the mouse.

When using the keyboard keys and moving the mouse it worked.

Trying to glutPostRedisplay but I'm not sure if it is what I need or how to use it.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷途知返 2024-10-15 23:24:33

您可以结合 glutMouseFuncglutMotionFuncglutPassiveMotionFunc 来实现。

(1)glutMotionFunc 仅在按下鼠标按钮时,随时告诉您(x, y) 光标。另一方面,当没有按下任何按钮时,glutPassiveMotionFunc 会告诉您(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...
}

其次,为 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)

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

You can combine glutMouseFunc, glutMotionFunc and glutPassiveMotionFunc to achieve it.

(1)glutMotionFunc tells you (x, y) of your cursor at any moment only when button(s) of your mouse is(are) pressed. On the other hand, glutPassiveMotionFunc tells you (x, y) when no button(s) is(are) pressed. (Check the glut specification for more details).

(2) To combine these functions

First, prepare onLeftButton(int x, int y) and onRightButton(int x, int y) to handle left-button-pressed and right-button-pressed events respectively, like this:

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...
}

Second, prepare a function for glutMouseFunc, say onMouse, for example:

glutMouseFunc(onMouse);

And within the onMouse function, it'll be like this:

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

After doing those things, you can get (x, y) of your cursor at any moment only when the left/right button is pressed and held.

For more information about how to combine those functions, you may check the 3.030 part at this site

情深缘浅 2024-10-15 23:24:33

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

I think you need to combine your glutMouseFunc with a glutMotionFunc. Set your mouse button state in the former and update your rotation in glutMotionFunc depending on the button state.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文