JOML-CI / JOML

A Java math library for OpenGL rendering calculations
MIT License
715 stars 102 forks source link

rotation matrix scaling #338

Closed TheoW03 closed 1 year ago

TheoW03 commented 1 year ago

I dont know if this is a code or a math issue, but when I use a rotation matrix like this

        Matrix4f m = new Matrix4f();
        m.identity();
        m.rotate(45,1,0,1);

it scales my render image

   vec4 l = rot*vec4(position, 1.0);
    gl_Position = l;

(this is the shader code for it because I am sending it to the GPU, rot is the Rotation matrix "m" in my java code)

I so far been only able to reproduce the bug when I do combinations of rotation angles.

httpdigest commented 1 year ago

The axis must be a (normalize) unit vector, as mentioned by the JavaDoc of this method: https://javadoc.io/doc/org.joml/joml/latest/org/joml/Matrix4f.html#rotate(float,float,float,float)

The axis described by the three components needs to be a unit vector.

Your axis vector (1, 0, 1) is not a unit vector.

This is in order to save one additional Math.sqrt() and division which would otherwise be necessary in order to normalize the axis. So, in this case, the caller must ensure that the axis is normalized.

And in addition, your angle 45 (which supposedly is meant to be 45 degrees) is not interpreted as degress. All angle parameters in all JOML methods are always in radians not degrees. See also: https://github.com/JOML-CI/JOML/wiki/Common-Pitfalls#angles-are-always-in-radians

Additionally additionally, calling identity() on a newly instantiated matrix is unnecessary, as newly constructed matrix objects are always identity by default.