stackgl / shader-school

:mortar_board: A workshopper for GLSL shaders and graphics programming
Other
4.28k stars 253 forks source link

vec times matrix? #65

Closed nickdesaulniers closed 9 years ago

nickdesaulniers commented 10 years ago

from 06-intro-6:

mat2 m = mat2(1, 2,
              3, 4);

vec2 v = m * vec2(1, 2);  //v = vec2(5, 8)

//Switching order of arguments is equivalent
//to transposing:
vec2 u = vec2(1, 2) * m;  //u = vec2(7, 10)

Now, I don't remember my linear algebra class much, but I thought you can't multiply vectors/matrices unless the columns of the first multiplicand are equal to the rows of the second multiplicand? So I would expect the first example to work since we would have a 2by2 x 2by1 (where the format is [num rows]by[num columns]) In my head I group the middle two 2by[2 x 2]by1 and if they're even, you can multiply them. By that logic, I would expect the second to fail since we have a 2by1 x 2by2 and 1 != 2. Am I getting mixed up by column major order, or is my memory of linear algebra just wrong (quite probable, in fact).

It might also be good to hint that matrix multiplication is not commutative to prevent things like this.

mikolalysenko commented 10 years ago

This is actually as expected, and one of the features of glsl, though it might be worth adding some extra emphasis that in this regard glsl is not consistent with standard matrix notation.