Consider two GLSL vertex shaders:
#version 450
in vec4 vertex;
uniform mat4 transformationMatrix;
void main() {
gl_Position = transformationMatrix * vertex;
}
and
#version 450
in vec4 vertex;
uniform mat4 transformationMatrix;
void main() {
gl_Position = vertex * transformationMatrix;
}
From my testing, both of these compile without errors.
Is there any difference between the order in which the mat4 is multiplied with the vec4?
If so, what exactly is the difference?
For a vec4 vertex
and a mat4 matrix
, vertex * matrix
is equivalent to transpose(matrix) * vertex
.
See GLSL Programming/Vector and Matrix Operations:
Furthermore, the *-operator can be used for matrix-vector products of the corresponding dimension, e.g.:
vec2 v = vec2(10., 20.); mat2 m = mat2(1., 2., 3., 4.); vec2 w = m * v; // = vec2(1. * 10. + 3. * 20., 2. * 10. + 4. * 20.)
Note that the vector has to be multiplied to the matrix from the right.
If a vector is multiplied to a matrix from the left, the result corresponds to multiplying a row vector from the left to the matrix. This corresponds to multiplying a column vector to the transposed matrix from the right:
Thus, multiplying a vector from the left to a matrix corresponds to multiplying it from the right to the transposed matrix:vec2 v = vec2(10., 20.); mat2 m = mat2(1., 2., 3., 4.); vec2 w = v * m; // = vec2(1. * 10. + 2. * 20., 3. * 10. + 4. * 20.)