2010-12-23 105 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