Mismatched coordinate systems in LWJGL with Mouse and Textures
- by Braains
I'm not really sure how to expand on this other than to say that it appears that my LWJGL seems to have different coordinate systems for the Mouse and for painting Textures. It seems that Textures have the usual Java2D way of putting (0, 0) in the upper-left corner, while the Mouse goes by the more sensible way of having the origin in the lower-left corner. I've checked my code a bunch but I don't see anything modifying the values between where I read them and where I use them. It's thrown me for a loop and I can't quite figure it out.
I'll post all the code that involves the Mouse input and Texture painting for you guys to look at.
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();
}
I put this line of code in because it temporarily fixed the coordinate system problem. However, I want to have my code to be as independent as possible so I want to remove as much reliance on other classes as possible.
These are the only methods that touch the Mouse input, and as far as I can tell none of them change anything so all is good here. Now for the Texture methods:
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);
}
I'll be honest, I do not have the faintest clue as to what is really happening in this method but I was able to put it together on my own and get it to work. my guess is that this is where the problem is.
Thanks for any help!