I am a bit confused about GLSL matrix multiplication.

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 [ATTACH=CONFIG]1494[/ATTACH]. 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?

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

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.

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.


1 Like