了解 GLM 4x4 矩阵函数
Understanding the GLM 4x4 Matrix functions
我在尝试复制它的一些数学以完成我的 Matrix4 class 作业时无法理解 GLM 矩阵函数的某个元素。采取这个旋转功能。
tmat4x4<T, P> const & m,
T angle,
tvec3<T, P> const & v
T const a = angle;
T const c = cos(a);
T const s = sin(a);
tvec3<T, P> axis(normalize(v));
tvec3<T, P> temp((T(1) - c) * axis);
tmat4x4<T, P> Rotate(uninitialize);
Rotate[0][0] = c + temp[0] * axis[0];
Rotate[0][1] = 0 + temp[0] * axis[1] + s * axis[2];
Rotate[0][2] = 0 + temp[0] * axis[2] - s * axis[1];
Rotate[1][0] = 0 + temp[1] * axis[0] - s * axis[2];
Rotate[1][1] = c + temp[1] * axis[1];
Rotate[1][2] = 0 + temp[1] * axis[2] + s * axis[0];
Rotate[2][0] = 0 + temp[2] * axis[0] + s * axis[1];
Rotate[2][1] = 0 + temp[2] * axis[1] - s * axis[0];
Rotate[2][2] = c + temp[2] * axis[2];
tmat4x4<T, P> Result(uninitialize);
Result[0] = m[0] * Rotate[0][0] + m[1] * Rotate[0][1] + m[2] * Rotate[0][2];
Result[1] = m[0] * Rotate[1][0] + m[1] * Rotate[1][1] + m[2] * Rotate[1][2];
Result[2] = m[0] * Rotate[2][0] + m[1] * Rotate[2][1] + m[2] * Rotate[2][2];
Result[3] = m[3];
或者 Translate 函数(v 是向量)
tmat4x4<T, P> const & m,
tvec3<T, P> const & v
Result[3] = m[0] * v[0] + m[1] * v[1] + m[2] * v[2] + m[3];
我难以理解的部分是访问矩阵 Result[1] 或 m[0] 的哪一部分。结果 [1] = 结果 [1][1] 吗?它用于许多功能,这是我遇到麻烦的这些功能的最后一部分。
他们如何处理使用单个数字访问二维数组中的元素,以及该单个数字访问的是什么元素?
定义模板 tmat4x4<T,P>
、类型 T
和精度 P
、that is available here 的代码回答了您的问题。
如您所见,第 60 行,tmat4x4
的实际数据内容定义为 4 个 col_type
元素的数组,并访问 m[i]
(第 96 行定义为返回col_type &
) returns the full i-th column.
col_type
类型定义为 tvec4<T,P>
,returns 类型 T &
的代码 is available here, and also defines a []
access operator,因此当您编写 m[a][b]
你说 "give me the column a, and therein the element b".
tvec4<T,P>
也 defines a binary *
operator, so it makes sense to take the whole vector and multiply it by some scalar of type U
, which is multiplying each element of the vector by that scalar.
因此,为了回答您的问题,Result[1]
不是 Result[1][1]
而是 Result[1][1..4]
(即使那不是正确的 C++)。
有两个函数名为 tmat4x4<T, P>::operator[]
。一个 return 类型 tmat4x4<T>::col_type&
,另一个 return 类型 tmat4x4<T>::col_type const&
。所以 m[1]
不表示矩阵的单个元素;相反,它表示矩阵的整个列(四个元素),您可以对其执行数学列向量运算。
我在尝试复制它的一些数学以完成我的 Matrix4 class 作业时无法理解 GLM 矩阵函数的某个元素。采取这个旋转功能。
tmat4x4<T, P> const & m,
T angle,
tvec3<T, P> const & v
T const a = angle;
T const c = cos(a);
T const s = sin(a);
tvec3<T, P> axis(normalize(v));
tvec3<T, P> temp((T(1) - c) * axis);
tmat4x4<T, P> Rotate(uninitialize);
Rotate[0][0] = c + temp[0] * axis[0];
Rotate[0][1] = 0 + temp[0] * axis[1] + s * axis[2];
Rotate[0][2] = 0 + temp[0] * axis[2] - s * axis[1];
Rotate[1][0] = 0 + temp[1] * axis[0] - s * axis[2];
Rotate[1][1] = c + temp[1] * axis[1];
Rotate[1][2] = 0 + temp[1] * axis[2] + s * axis[0];
Rotate[2][0] = 0 + temp[2] * axis[0] + s * axis[1];
Rotate[2][1] = 0 + temp[2] * axis[1] - s * axis[0];
Rotate[2][2] = c + temp[2] * axis[2];
tmat4x4<T, P> Result(uninitialize);
Result[0] = m[0] * Rotate[0][0] + m[1] * Rotate[0][1] + m[2] * Rotate[0][2];
Result[1] = m[0] * Rotate[1][0] + m[1] * Rotate[1][1] + m[2] * Rotate[1][2];
Result[2] = m[0] * Rotate[2][0] + m[1] * Rotate[2][1] + m[2] * Rotate[2][2];
Result[3] = m[3];
或者 Translate 函数(v 是向量)
tmat4x4<T, P> const & m,
tvec3<T, P> const & v
Result[3] = m[0] * v[0] + m[1] * v[1] + m[2] * v[2] + m[3];
我难以理解的部分是访问矩阵 Result[1] 或 m[0] 的哪一部分。结果 [1] = 结果 [1][1] 吗?它用于许多功能,这是我遇到麻烦的这些功能的最后一部分。
他们如何处理使用单个数字访问二维数组中的元素,以及该单个数字访问的是什么元素?
定义模板 tmat4x4<T,P>
、类型 T
和精度 P
、that is available here 的代码回答了您的问题。
如您所见,第 60 行,tmat4x4
的实际数据内容定义为 4 个 col_type
元素的数组,并访问 m[i]
(第 96 行定义为返回col_type &
) returns the full i-th column.
col_type
类型定义为 tvec4<T,P>
,returns 类型 T &
的代码 is available here, and also defines a []
access operator,因此当您编写 m[a][b]
你说 "give me the column a, and therein the element b".
tvec4<T,P>
也 defines a binary *
operator, so it makes sense to take the whole vector and multiply it by some scalar of type U
, which is multiplying each element of the vector by that scalar.
因此,为了回答您的问题,Result[1]
不是 Result[1][1]
而是 Result[1][1..4]
(即使那不是正确的 C++)。
有两个函数名为 tmat4x4<T, P>::operator[]
。一个 return 类型 tmat4x4<T>::col_type&
,另一个 return 类型 tmat4x4<T>::col_type const&
。所以 m[1]
不表示矩阵的单个元素;相反,它表示矩阵的整个列(四个元素),您可以对其执行数学列向量运算。