0
\$\begingroup\$

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.glLoadIdentity();
GL11.glOrtho(0, graphics.window().width(), graphics.window().height(), 0, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();

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?

\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

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.

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

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.

\$\endgroup\$
4
  • \$\begingroup\$ This isn't helpful, because you are not answering his question. \$\endgroup\$
    – Bálint
    Nov 25, 2016 at 9:06
  • \$\begingroup\$ 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. \$\endgroup\$
    – Tripl3Fac3
    Dec 22, 2016 at 5:10
  • \$\begingroup\$ I have edited to be a correct answer. Do note that you can transform in the Projection Matrix. \$\endgroup\$
    – Tripl3Fac3
    Dec 22, 2016 at 5:26
  • \$\begingroup\$ Yes you can, but not like how he does it \$\endgroup\$
    – Bálint
    Dec 22, 2016 at 14:18

You must log in to answer this question.

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