2D Camera in LWJGL 3

I am trying to implement a simple 2D camera in LWJGL3.

The camera has an orthographic projection and can move in 2D space.

This is run once at game start-up:

GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glOrtho(0, graphics.window().width(), graphics.window().height(), 0, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);


This is run as part of the draw loop:

GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glPushMatrix();
GL11.glTranslatef(-position.x(), -position.y(), 0f);

// Draw stuff

GL11.glPopMatrix();


Everything looks fine until I move the view, after which everything seems to be closer to the top left (0, 0) than it should be. It seems to be off by a scalar of the window size - the factor is greater in x than y.

How have I misunderstood the OpenGL commands?

You put the translation data in the projection matrix. Don't do this, that matrix is strictly for converting 3d to 2d, put the camera stuff in the modelview matrix.

The OpenGL fixed pipeline has two different matrices for the camera.
* ModelView where you transform it (position, rotation, scale - Note that you have you should apply them in this order!)
* Projection where you apply the view matrix (orthographic, frustum, perspective)

My advise would be to use shaders for this, but you can stick to the fixed pipeline.

As Bálint already stated, the projection matrix is not for transformation.

However, if you want to load the identity matrix for the ModelView every call, you could transform it in the Projection matrix.
Do note that you shouldn't call the Push and Pop matrix in the Projection matrix.

• This isn't helpful, because you are not answering his question. Nov 25 '16 at 9:06
• I'm sorry, I wrote this without reading it properly, he indeed shouldn't put transformation matrices into the projection matrix. Although, my advise of using shaders still stands. If you're however using linux you can use the default OpenGL fixed pipeline since not every machine is able to update the graphics drivers. However, when possibile, use shaders. Dec 22 '16 at 5:10
• I have edited to be a correct answer. Do note that you can transform in the Projection Matrix. Dec 22 '16 at 5:26
• Yes you can, but not like how he does it Dec 22 '16 at 14:18