PDA

View Full Version : I am a bit confused about GLSL matrix multiplication.

nikoniko
06-24-2017, 08:08 PM
I am a bit confused about matrices and matrix multiplication in GLSL. It is not clear to me where the entries go.

Let's say I have a vec2 v = vec2(1.0f, 2.0f); and a matrix mat2 m = mat2(1.0f, 2.0f, 0.0f, 1.0f); I can multiply those two together v = m * v;

I think GLSL interprets vectors as column vectors, which means that the matrices are filled column after column. So the multiplication above is actually 2402. Also, this means that a mat2x3 is actually would appear as a 3x2 matrix in linear algebra, and can be multiplied from the left by a vec2.

Edit: Moreover, we can multiply matnxm times matkxn, in that order.

Is this the right interpretation?

GClements
06-25-2017, 03:47 AM
I think GLSL interprets vectors as column vectors, which means that the matrices are filled column after column. So the multiplication above is actually 2402.

Correct. Matrix constructor arguments are in column-major order.

Also, this means that a mat2x3 is actually would appear as a 3x2 matrix in linear algebra,

Correct. GLSL terminology is backwards; matMxN has M columns and N rows, whereas long-standing mathematical convention is that a "M × N" or "M by N" matrix has M rows and N columns.

and can be multiplied from the left by a vec2.

Correct. A matrix-vector multiplication can have the vector on either side; it will be treated as a row vector if it's on the left and a column vector if it's on the right.

Edit: Moreover, we can multiply matnxm times matkxn, in that order.

Correct.