3
votes

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?

1
Are you sure that your num array holds the floats in column major order? Because according to the documentation the Matrix4f.load method will load the array in column major order. - Nitram
@Nitram Yes, because if you see the println I used for debugging, it prints out pastebin.com/rNC5rRD1 (which is the correct order). Is it possible the flip function messed up the order? - jthort
The printout shows that your list is row major and not column major. Column major means that the column is the outer loop. So you fetch all rows in a column before switching to the next column. - Nitram
Well yes it does. Your list is row major, and the load function of Matrix4f expects column major. EDIT: Hey deleting the comment is mean! :P - Nitram
@Nitram Okay, now I understand why the matrix is in a different order, so should I load it how it is or use the transpose function? Which should be used so I can use this transformation matrix to multiply a vertex for transformation. Is there a way to load it from a float to the matrix in the correct order without using the transpose function to fix it? Sorry, public Wifi...! - jthort

1 Answers

4
votes

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.