关于opengl:mat4x4与vec4的乘积顺序有什么区别?

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

考虑两个GLSL顶点着色器:

1
2
3
4
5
6
7
8
#version 450

in vec4 vertex;
uniform mat4 transformationMatrix;

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

1
2
3
4
5
6
7
8
#version 450

in vec4 vertex;
uniform mat4 transformationMatrix;

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

根据我的测试,这两个文件都可以正确编译。

mat4与vec4的乘积顺序之间是否有区别?

如果是这样,到底有什么区别?


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

请参阅GLSL编程/向量和矩阵运算:

Furthermore, the *-operator can be used for matrix-vector products of the corresponding dimension, e.g.:

1
2
3
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:

1
2
3
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.)