2012-07-10 47 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