I have all the basic rendering already set up, with translation, scaling etc. What matrix(s) and/or vector(s) do I need to multiply/add with my existing code to rotate an object.

Preferably around a point given by a vector2fa, also only in degrees because quaternions aren't implemented and its only 2D not 3D.

Snippet of the vertex shader:

gl_Position = transformation * vec4(position, 1.0) + translation

Where transformation is done in java as (Note not actual code as * is replaced with .mul()):

Matrix4f transformation = ortho * scale

And translation is given by a method:

Vector4f translation = new Vector4f(x , y , 0f, 0f).mul(gc.getOrtho());

All the code listed before works as expected. It is just lacking the rotation feature mentioned at the top.

How can I add rotation to my code?


If you use JOML with LWJGL3 then you can use the Matrix3x2f class to do 2D transformations, like so:

Matrix3x2f transformation = new Matrix3x2f()
  .view(-10, +10, -10, +10) // <- like ortho2D in Matrix4f
  .translate(x, y)  // <- multiply translation
  .rotate(radians); // <- multiply rotation
FloatBuffer buffer = <16xfloat>;
GL20.glMatrixUniform4fv(transformationLoc, false, transformation.get(buffer));

and your vertex shader then becomes (I am assuming that all you need is a vec2 as a position as well):

gl_Position = transformation * vec4(position, 0.0, 1.0)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.