2012-07-10 57 views
0

我不確定如何擴展這個,而不是說我的LWJGL似乎有不同的鼠標座標系和繪製紋理。看起來紋理採用通常的Java2D方法將(0,0)放在左上角,而鼠標則以更合理的方式在左下角放置原點。我檢查了一下我的代碼,但是我沒有看到任何修改我讀取它們的位置和使用它們的位置之間的值。它引發了我一個循環,我不能弄明白。LWJGL中鼠標和紋理不匹配的座標系

我會發布所有涉及鼠標輸入和紋理繪圖的代碼,供大家查看。

private static void pollHelpers() 
{ 
    while(Mouse.next()) 
    { 
     InputHelper.acceptMouseInput(Mouse.getEventButton(), 
     Mouse.getEventX(), Mouse.getEventY()); 
    } 
    while (Keyboard.next()) {   
     if (Keyboard.getEventKeyState()) { 
      InputHelper.acceptKeyboardInput(Keyboard.getEventKey(), true); 
     } else { 
      InputHelper.acceptKeyboardInput(Keyboard.getEventKey(), false); 
     } 
    } 
} 

public static void acceptMouseInput(int mouseButton, int x, int y) 
{ 
    for(InputHelper ie: InputHelper.instances) 
    { 
     if(ie.checkRectangle(x, y)) 
     { 
      ie.sendMouseInputToParent(mouseButton); 
     } 
    } 
} 

private void sendMouseInputToParent(int mouseButton) 
{ 
    parent.onClick(mouseButton); 
} 

public boolean checkRectangle(int x, int y) 
{ 
    //y = InputManager.HEIGHT - y; See below for explanation 

    return x > parent.getX() && x < parent.getX() + parent.getWidth() && 
    y > parent.getY() && y < parent.getY() + parent.getHeight(); 
} 

我把這行代碼放進去,因爲它暫時修正了座標系問題。但是,我希望我的代碼儘可能獨立,所以我想盡可能多地去除對其他類的依賴。

這些是觸摸鼠標輸入的唯一方法,並且據我所知,他們都沒有改變任何東西,所以這裏都很好。現在的紋理方法:

public void draw() 
{ 
    if(!clicked || deactivatedImage == null) 
    { 
     activatedImage.bind(); 

     glBegin(GL_QUADS); 
     { 
      DrawHelper.drawGLQuad(activatedImage, x, y, width, height); 
     } 
     glEnd(); 
    } else { 
     deactivatedImage.bind(); 

     glBegin(GL_QUADS); 
     { 
      DrawHelper.drawGLQuad(deactivatedImage, x, y, width, 
            height); 
     } 
     glEnd(); 
    } 

} 

public static void drawGLQuad(Texture texture, float x, float y, float width, float 
      height) 
{ 
    glTexCoord2f(x, y); 
    glVertex2f(x, y); 

    glTexCoord2f(x, y + texture.getHeight()); 
    glVertex2f(x, y + height); 

    glTexCoord2f(x + texture.getWidth(), y + texture.getHeight()); 
    glVertex2f(x + width, y +height); 

    glTexCoord2f(x + texture.getWidth(), y); 
    glVertex2f(x + width, y); 
} 

我會說實話,我沒有最微弱的線索,什麼這個方法真的發生了,但我能夠把它連同我自己,並得到它工作。我的猜測是這是問題所在。

感謝您的幫助!

回答

0

說實話,這看起來沒錯。紋理和鼠標輸入從左上角的(0,0)開始工作,我對此100%確定。你有沒有試過記錄鼠標x和y?

+0

我剛剛做到了。鼠標在左上角的(0,MAX)和左下角的(0,0)處讀數。我在pollHelpers()方法中放置了println()語句,這是我在第一次讀取鼠標位置之前將其發送給其他類的地方。 – Braains 2012-07-10 03:17:27

+0

你在哪一部分代碼中加入了日誌? – reagan 2012-07-10 03:19:44

+0

我把它放在pollHelpers()方法中。快速的谷歌搜索表明它應該以這種方式工作,我相信這是具有無與倫比的座標系統的紋理。與投影有關的東西,但我不知道在不影響我的課程獨立性的情況下進行簡單的修復。 – Braains 2012-07-10 03:21:07