I have the following matrix in a text file that brought into a floatbuffer, then stored it into the Matrix4f class in LWJGL. This is the matrix in the text file
1.0, 0.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, -1.0, 0.0, 0.0,
0.0, 0.0, 0.03641997277736664, 1.0
When I add the float buffer to the matrix like this
System.out.println("------------");
for(float x : nums){
System.out.println(x);
}
System.out.println("------------");
Matrix4f matrix4f = new Matrix4f();
FloatBuffer buffer = BufferUtils.createFloatBuffer(nums.length);
buffer.put(nums);
buffer.flip();
matrix4f.load(buffer);
where nums is the float array of values. When I print out the Matrix4f class, it shows
1.0 0.0 0.0 0.0
0.0 0.0 -1.0 0.0
0.0 1.0 0.0 0.036419973
0.0 0.0 0.0 1.0
But if I use the transpose function in the Matrix4f class, it goes back to
1.0, 0.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, -1.0, 0.0, 0.0,
0.0, 0.0, 0.03641997277736664, 1.0
Why does LWJGL change the order I of the values when I create a Matrix4f? The text file holds the matrix in colum major matrix ordering, which is what I need for OpenGl. What format is LWJGL changing the matrix to? Does it make a difference, should I use the transpose function to change it back?
Okay so here is the answer:
The load function of the Matrix4f class expects the float values to be column major.
So if your matrix is like this:
11 12 13 14
21 22 23 24
31 32 33 34
41 42 43 44
The column-major order is: 11 21 31 41 12 22 ...
The more natural order of reading the file how ever is row major. And the comments showed that you did exactly that.
My suggestion is that you leave it like this. Loading the file as column major is more afford than transposing the matrix.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With