2015-04-21 145 views
0

我試圖讀取鼠標座標,當我點擊屏幕上的一個點,並用點標記它,但它沒有工作。閱讀鼠標座標點擊OpenGL

 float cx = 0, cy = 0; 

glPushMatrix(); 
    glPointSize(6.0f); 
    //Draw the points 
    glBegin(GL_POINTS); 
     cx = (((x * (maxx + minx)))/width) + minx; 
     cy = (((-1 * pow((y - height), width) * (maxy - miny))/height) + miny; 

     glVertex2f(cx , cy); 
    glEnd(); 


glPopMatrix(); 

這是一個大學運動,公式得到的屏幕座標是:

Formula to get the coordinate from screen

Px和Px爲座標mouseFunc傳遞給這個函數。 w和h是屏幕的寬度和高度(我從重塑得到它) maxx,maxy,minx,miny ...是座標座標

因此,我的代碼有什麼問題?

鼠標功能(如果鼠標點擊的工作,我已經測試它):

void mouse(int button, int state, int x, int y){ 
switch(button){ 
     case GLUT_LEFT_BUTTON: 
      if(state == GLUT_DOWN) 
         exerciseThree(x, y); 
     break;   
}  

glutPostRedisplay(); 
} 
+0

你還可以發佈你獲得鼠標座標的部分嗎? –

+0

爲什麼代碼調用'pow()'系統函數? – user3629249

+0

@ user3629249並且只有一個參數,就像書面的那樣,該行應該引發語法錯誤! – duskwuff

回答

3

你還沒有實現你正確引用的公式。嘗試

cx = x * (maxx - minx)/width + minx; 
cy = (height - y) * (maxy - miny)/height + miny; 

兩個顯著差異在第一個表達式爲(maxx - minx),和你的誤讀第二行和上面的思路除數w是第二線的電力。

+0

謝謝天氣風向標! ;) –