mat4x4 与 vec4 相乘的顺序有什么区别?

What is the difference between the order in which a mat4x4 is multiplied with a vec4?

考虑两个 GLSL 顶点着色器:

#version 450

in vec4 vertex;
uniform mat4 transformationMatrix;

void main() {
    gl_Position = transformationMatrix * vertex;
}

#version 450

in vec4 vertex;
uniform mat4 transformationMatrix;

void main() {
    gl_Position = vertex * transformationMatrix;
}

根据我的测试,这两个编译都没有错误。

mat4和vec4相乘的顺序有区别吗?

如果是,具体有什么区别?

对于 vec4 vertexmat4 matrixvertex * matrix 等同于 transpose(matrix) * vertex

参见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.)