gpt4 book ai didi

c++ - 矩阵和 vector 乘法使用GLM返回错误的结果

转载 作者:行者123 更新时间:2023-12-01 14:39:23 25 4
gpt4 key购买 nike

我有恒定 c 和vec4 x =(x_1,x_2,x_3,x_4)的等式:

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

其中sum(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 ?)
)

但是,结果中的y坐标(vec3)根本不等于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)

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

最佳答案

OpenGL矩阵和GLM矩阵按列主顺序存储。 vector 必须从右边乘以矩阵:
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;

关于c++ - 矩阵和 vector 乘法使用GLM返回错误的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61958276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com