LWJGL中鼠标和纹理的坐标系不匹配

时间:2012-07-10 03:07:59

标签: java lwjgl

我不确定如何扩展这个,除了说我的LWJGL看起来似乎有不同的鼠标坐标系和绘制纹理。似乎Textures有通常的Java2D方式将(0,0)放在左上角,而鼠标则采用更为明智的方式将原点放在左下角。我已经检查了我的代码,但是我没有看到任何修改我读取它们的位置和使用它们的位置之间的值。它引发了我一个循环,我无法弄明白。

我会发布所有涉及鼠标输入和纹理绘画的代码供你们查看。

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();
}

我把这行代码放入,因为它暂时修复了坐标系问题。但是,我希望我的代码尽可能独立,所以我希望尽可能多地依赖其他类。

这是触摸鼠标输入的唯一方法,据我所知,它们都没有改变任何东西,所以这里一切都很好。现在对于Texture方法:

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);
}

我会说实话,我对这种方法中真正发生的事情没有最微弱的线索但是我能够把它放在一起并让它发挥作用。我的猜测是这就是问题所在。

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

说实话,说实话。纹理和鼠标输入都在左上角的(0,0)处工作,而且我100%肯定。您是否尝试过记录鼠标x和y?