My 3D models rotates around wrong axis. Everything except X-axis works. When I set angle of Y and Z to 0 then X works fine but if rotate it eg. 90 degrees around Z-axis and rotate around X after that it rotates around Z too. So when Y or Z is used X rotates around Z. So if I make my model rotate 90 degrees around Z and -90 degrees around X it stays completely still.
My function for transformation matrix. If I move matrix.translate(position);
to under rotations my model rotates in huge circle (maybe around worlds origin).
public static Matrix4f createTransformationMatrix(Vector3f position, Vector3f rotation, Vector3f scale) {
Matrix4f matrix = new Matrix4f();
matrix.scale(scale);
matrix.translate(position);
matrix.rotate((float) Math.toRadians(rotation.x), new Vector3f(1, 0, 0));
matrix.rotate((float) Math.toRadians(rotation.y), new Vector3f(0, 1, 0));
matrix.rotate((float) Math.toRadians(rotation.z), new Vector3f(0, 0, 1));
return matrix;
}
Conversion of matrix to floatbuffer so I can pass it to shaders:
public static FloatBuffer toFloatBuffer(Matrix4f matrix) {
FloatBuffer buffer = BufferUtils.createFloatBuffer(16);
buffer.put(matrix.m00());
buffer.put(matrix.m01());
buffer.put(matrix.m02());
buffer.put(matrix.m03());
buffer.put(matrix.m10());
buffer.put(matrix.m11());
buffer.put(matrix.m12());
buffer.put(matrix.m13());
buffer.put(matrix.m20());
buffer.put(matrix.m21());
buffer.put(matrix.m22());
buffer.put(matrix.m23());
buffer.put(matrix.m30());
buffer.put(matrix.m31());
buffer.put(matrix.m32());
buffer.put(matrix.m33());
buffer.flip();
return buffer;
}
Usage in shader:
gl_Position = transformationMatrix * vec4(position, 1.0) * viewMatrix * projectionMatrix;
The models' zero point has been set to its origin and I am sure that all data send to those functions are correct. I have been debugging this for while now. I am using LWJGL 3.