2010-12-23 87 views
1

我正在根据鼠标位置旋转我的相机。但是,只有在鼠标左键或右键关闭的情况下,我才会激活此功能。这个代码的问题是我必须释放并再次按下该程序才能注意到我移动了鼠标。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; 
} 

回答

0

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

+0

我不是你的意思,你能告诉我一些简单的伪代码描绘任何机会,完全肯定它? – snackbar 2010-12-24 00:07:33

1

您可以结合glutMouseFunc,glutMotionFunc和glutPassiveMotionFunc来实现它。

(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部分: https://www.opengl.org/archives/resources/faq/technical/glut.htm