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;
}
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以结合
glutMouseFunc
、glutMotionFunc
和glutPassiveMotionFunc
来实现。(1)
glutMotionFunc
仅在按下鼠标按钮时,随时告诉您(x, y)
光标。另一方面,当没有按下任何按钮时,glutPassiveMotionFunc
会告诉您(x, y)
。 (查看 过剩规范 了解更多详细信息)。(2) 组合这些函数
首先,准备
onLeftButton(int x, int y)
和onRightButton(int x, int y)
来处理左键按下和右键按下- 分别按下按钮事件,如下所示:其次,为
glutMouseFunc
准备一个函数,例如onMouse
:在
onMouse
函数中,它会是这样的:做完这些事情后,只要按住左/右按钮,您就可以随时获取光标的
(x, y)
。有关如何组合这些功能的更多信息,您可以查看 3.030 部分 这个网站
You can combine
glutMouseFunc
,glutMotionFunc
andglutPassiveMotionFunc
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)
andonRightButton(int x, int y)
to handle left-button-pressed and right-button-pressed events respectively, like this:Second, prepare a function for
glutMouseFunc
, sayonMouse
, for example:And within the
onMouse
function, it'll be like this: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
我认为你需要将 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.