2
\$\begingroup\$

So I'm very new to using OpenGL and LWJGL, although I am proficient in Java itself. One of the first things I read in the documentation of the Display class was that it maps the origin to the bottom left as opposed to how most other programs map it to the top left.

I was ready to take this into account until I discovered that the OpenGL libraries in LWJGL still map everything to the top left. It would also appear that the two spaces (window coordinates and game/OpenGL space) do not correspond to each other. ie. if I set a quads position to 10 x 10 via a mouse click in the lower left corner of the screen, it will move to what looks more like (windowWidth - 100, windowHeight - 100) or (100,100) using a standard top left origin.

I am attempting to write a 2D game using quads to draw my sprites, and I was wondering if there was a way to get the screen and game space to correspond.

Any help would be greatly appreciated, Thanks! Peter

Just in case it helps, here's how I'm creating a window and initializing OpenGL Display

Display.setResizable(true);
Display.setDisplayMode(new DisplayMode(WIDTH,HEIGHT));
Display.setVSyncEnabled(VSYNC);
Display.setFullscreen(FULLSCREEN);

Display.create();

OpenGL

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0,Display.getWidth(),Display.getHeight(),0,1,-1);

    glMatrixMode(GL_MODELVIEW);

    glClearColor(0,0,0,1);

    glDisable(GL_DEPTH_TEST);

    glEnable(GL_BLEND);
    glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
\$\endgroup\$

2 Answers 2

5
\$\begingroup\$

I think your problem may lie here:

glOrtho(0,Display.getWidth(),Display.getHeight(),0,1,-1);

In OpenGL, coordinates go from -1 on the left of the viewport to 1 on the right, and -1 on the bottom to 1 on the top, with (0,0) in the center of the screen (and the Z axis going back to front). This is the same regardless of the shape of your viewport, and it is completely up to you to make some sense of these coordinates.

So usually you want to change this coordinate system to something that makes more sense. That is what the projection matrix is usually used for. One way to change your coordinate system is to use an orthographic projection matrix. glOrtho() offers a way to create one.

The first two parameters define where the left and right borders should go to. In your case, left is set to 0, and right is set to the display width (make sure this is what you want. If you're in a windowed application, you want to set this to the client area of your window, not the entire display width).

The next two parameters define where the bottom and top borders should go to. In your case, bottom is set to the display height, and top is set to 0. This effectively makes your origin to be set at the top left of the screen. If you want your origin to be set at the bottom left, you can invert these two parameters.

The final two parameters define where the near and far borders should go to. This is the same as before, but with the Z-axis.

Here's a nice tutorial that explains the coordinate systems in OpenGL.

\$\endgroup\$
1
\$\begingroup\$

Panda Pajama said it all, if you want to make it so you are using coords from the top down and not the bottom up change glOrtho to:

glOrtho(0, Display.getWidth(), Display.getHeight(), 0, -1, 1);

and to sort the mouse Y pos out you just do:

mousey = Display.getHeight() - Mouse.getY();

You should defo read up on all the gl functions you have in your post there so you have a clear picture of whats going on when you start drawing things to the screen, it will save you alot of trouble later on.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .