使用 GLM 的矩阵和向量乘法 returns 错误结果

Matrix and vector multiplication returns wrong result using GLM

我有这个等式,常数 c 和 vec4 x=(x_1, x_2, x_3, x_4):

x_1*c + x_2*c + x_3*c + x_4*c

其中总和(x_i) = 1

也就是说,结果应该是c:

=(x_1 + x_2 + x_3 + x_4) * c

= 1 * c = c

所以我有一个 vec4 和一个 matrix4*3 乘法,如下所示:

(x_1, x_2, x_3, x_4) * ((? C ?), (? C ?), (? C ?) (? C ?) )

然而,结果(vec3)中的y坐标根本不等于c。

这是怎么回事,如何解决?

float constant = 10.0f;
glm::vec3 v1 = glm::vec3(48.0f, constant, 18.0f);
glm::vec3 v2 = glm::vec3(56.0f, constant, 18.0f);
glm::vec3 v3 = glm::vec3(56.0f, constant, 12.0f);
glm::vec3 v4 = glm::vec3(52.0f, constant, 8.0f);
glm::mat4x3 M = glm::mat4x3(v1, v2, v3, v4);
glm::vec4 sumTo1 = glm::vec4(0.2f, 0.4f, 0.1f, 0.3f);
glm::vec3 result = sumTo1 * M;
cout << "sumTo1=" << glm::to_string(sumTo1) << endl;
cout << "M=" << glm::to_string(M) << endl;
cout << "result=" << glm::to_string(result) << endl;

输出:

sumTo1=vec4(0.200000, 0.400000, 0.100000, 0.300000)
M=mat4x3((48.000000, 10.000000, 18.000000), (56.000000, 10.000000, 18.000000), (56.000000, 10.000000, 12.000000), (52.000000, 10.000000, 8.000000))
result=vec3(15.400001, 17.000000, 16.400000)

据我所知,向量已经被视为行向量。

OpenGL 矩阵和 GLM 矩阵按列主要顺序存储。向量必须从右边乘以矩阵:

glm::vec3 result = sumTo1 * M;

glm::vec3 result = M * sumTo1;

GLSL Programming/Vector and Matrix Operations
OpenGL Shading Language 4.60 Specification - 5.10. Vector and Matrix Operations

The exceptions are matrix multiplied by vector, vector multiplied by matrix, and matrix multiplied by matrix. These do not operate component-wise, but rather perform the correct linear algebraic multiply.

vec3 v, u;
mat3 m;
u = v * m;

is equivalent to

u.x = dot(v, m[0]); // m[0] is the left column of m
u.y = dot(v, m[1]); // dot(a,b) is the inner (dot) product of a and b
u.z = dot(v, m[2]);

And

u = m * v;

is equivalent to

u.x = m[0].x * v.x + m[1].x * v.y + m[2].x * v.z;
u.y = m[0].y * v.x + m[1].y * v.y + m[2].y * v.z;
u.z = m[0].z * v.x + m[1].z * v.y + m[2].z * v.z;