2010-10-26 56 views
2

我的問題與在使用OpenGL時從SFML獲取正確的鼠標座標有關。使用OpenGL在SFML中獲取正確的鼠標位置

基本上我正在Z軸上旋轉一個多邊形來查看當前的光標位置。

您還可以使用WASD鍵在屏幕上移動多邊形。

如果多邊形停留在屏幕的中心一切都很好,但當我將多邊形移動到屏幕的左上角時,會出現問題。

基本上它就像是獲取不正確的鼠標座標並超出實際鼠標光標的位置。

我用GL_LINES創建一種十字線來查看我的變量認爲鼠標光標在哪裏,並且它超出了實際位置。

爲了讓我使用這個鼠標的當前座標:

mouseX = Input.GetMouseX()-App.GetWidth()/2.f; 
mouseY = Input.GetMouseY()-App.GetHeight()/2.f; 

有誰知道我的問題可能是什麼?

爲了提供所有我可以得到的信息,下面是我的完整源代碼。 另外,如果我提供我的資源,那麼對於那些有興趣幫助的人,可以編譯它並查看我的意思,因爲我有點難以解釋。

抱歉它是如此凌亂 - 我是新來這個畢竟:)

#include <SFML/Window.hpp> 
#include <iostream> 
#include <cmath> 

const float PI = 3.14159265f; 

int main() { 
    // Angle of rotation for the polygon 
    float angle = 0.f; 

    sf::Window App(sf::VideoMode(800, 600, 32), "SFML OpenGL"); 

    glClearDepth(1.f); 
    glClearColor(0.f, 0.f, 0.f, 0.f); 

    glEnable(GL_DEPTH_TEST); 
    glDepthMask(GL_TRUE); 

    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    gluPerspective(90.f, 1.2f, 1.f, 500.f); 

    // Current position of the polygon (used in glTranslatef) 
    GLfloat currentPosX = 0.f; 
    GLfloat currentPosY = 0.f; 

    // Current position of the mouse cursor 
    float mouseX = 0.f; 
    float mouseY = 0.f; 

    const sf::Input &Input = App.GetInput(); 

    App.SetFramerateLimit(30); 

    while (App.IsOpened()) { 
     sf::Event Event; 
     while (App.GetEvent(Event)) { 
      if (Event.Type == sf::Event::Closed) { 
       App.Close(); 
      } 
      if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape)) { 
       App.Close(); 
      } 

      if (Event.Type == sf::Event::Resized) { 
       glViewport(0, 0, Event.Size.Width, Event.Size.Height); 
      } 
     } 

     App.SetActive(); 

     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

     if(Input.IsKeyDown(sf::Key::W)) { 
      currentPosY += 3.f; 
     } 
     if(Input.IsKeyDown(sf::Key::S)) { 
      currentPosY -= 3.f; 
     } 
     if(Input.IsKeyDown(sf::Key::D)) { 
      currentPosX += 3.f; 
     } 
     if(Input.IsKeyDown(sf::Key::A)) { 
      currentPosX -= 3.f; 
     } 

     // Minus half of the screen width and height 
     // because the OpenGL origin is in the middle of the screen 
     mouseX = Input.GetMouseX()-App.GetWidth()/2.f; 
     mouseY = Input.GetMouseY()-App.GetHeight()/2.f; 

     // I don't know any better way to flip the Y axis so this is what I did 
     if(mouseY >= 0) { 
      mouseY = -(mouseY); 
     } 
     else { 
      mouseY = abs(mouseY); 
     } 

     // Calculate the angle which the polygon needs to rotate at 
     angle = atan2(mouseY - currentPosY, mouseX - currentPosX)*180/PI; 

     // Print variables to console to try and figure out what I'm doing wrong 
     std::cout << mouseX << "(" << currentPosX << ")" << ", " << mouseY << "(" << currentPosY << ")" << " - " << angle << std::endl; 

     glMatrixMode(GL_MODELVIEW); 
     glLoadIdentity(); 
     glTranslatef(currentPosX, currentPosY, -200.f); 
     glRotatef(angle, 0.f, 0.f, 1.f); 

     // Polygon 
     glBegin(GL_QUADS); 

      glVertex3f(-25.f, -25.f, -50.f); 
      glVertex3f(25.f, -25.f, -50.f); 
      glVertex3f(25.f, 25.f, -50.f); 
      glVertex3f(-25.f, 25.f, -50.f); 

     glEnd(); 

     glLoadIdentity(); 
     glTranslatef(currentPosX, currentPosY, -200.f); 

     // Axis on polygon 
     glBegin(GL_LINES); 

      glVertex3f(-70.f, 0.f, -50.f); 
      glVertex3f(70.f, 0.f, -50.f); 

      glVertex3f(0.f, -70.f, -50.f); 
      glVertex3f(0.f, 70.f, -50.f); 

     glEnd(); 

     glLoadIdentity(); 
     glTranslatef(currentPosX, currentPosY, -200.f); 
     glRotatef(angle, 0.f, 0.f, 1.f); 

     // Line to indicate the direction of the polygon 
     glBegin(GL_LINES); 

      glVertex3f(0.f, 0.f, -50.f); 
      glVertex3f(50.f, 0.f, -50.f); 

     glEnd(); 

     glLoadIdentity(); 
     glTranslatef(0.f, 0.f, -200.f); 

     // Screen axis 
     glBegin(GL_LINES); 

      glVertex3f(-400.f, 0.f, -60.f); 
      glVertex3f(400.f, 0.f, -60.f); 

      glVertex3f(0.f, 300.f, -60.f); 
      glVertex3f(0.f, -300.f, -60.f); 

     glEnd(); 

     glLoadIdentity(); 
     glTranslatef(mouseX, mouseY, -200.f); 

     // Cursor position 
     glBegin(GL_LINES); 

      glVertex3f(-10.f, 0.f, -60.f); 
      glVertex3f(10.f, 0.f, -60.f); 

      glVertex3f(0.f, 10.f, -60.f); 
      glVertex3f(0.f, -10.f, -60.f); 

     glEnd(); 

     App.Display(); 
    } 

    return 0; 
} 

回答

3

Input.GetMouse?給你,比如說,窗口空間COORDS,你需要它來改造他們的模型空間。我可能是錯的,但我懶惰的解決將通過

(x, y) = (mouse_x, mouse_y)/(window_width, window_height) 
     * (viewport_width, viewport_height) 

被畫我的2D場景在第一象限,讓鼠標的位置你總是可以讓你通過相應地設置您的投影窗口的尺寸編碼簡單:

glMatrixMode(GL_PROJECTION); 
gluOrtho2D(0, App.GetWidth(), 0, App.GetHeight());