c++ - 了解 GLM 4x4 矩阵函数

标签 c++ math matrix glm-math

我在尝试复制 GLM 矩阵函数的某些元素以完成我的 Matrix4 类作业时遇到困难。采用此旋转功能。

    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 是一个 vector )

    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> ,其代码 is available here , 还有 defines a [] access operator返回类型 T & , 因此当你写 m[a][b]你说“给我列 a 和其中的元素 b”。

tvec4<T,P>还有defines a binary * operator , 所以把整个 vector 乘以一些类型的标量是有意义的 U ,即 multiplying each element of the vector by that scalar .

所以为了回答你的问题,Result[1]不是 Result[1][1]而是Result[1][1..4] (即使那不是正确的 C++)。

关于c++ - 了解 GLM 4x4 矩阵函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27860630/

相关文章:

c - PETSc - MatLUFactor - 不支持此对象类型的此操作

math - 在 TBN 矩阵中,法线、切线和双切线向量总是垂直吗?

c++ - VS 2017程序无法识别 "scoped_lock"

C++ 服务器不显示数据包

c++ - Arduino C++ 计算触发点

java - 从 Euler ZXZ 旋转转换为固定轴 XYZ 旋转

math - 回归公式中矩阵维数不匹配

c++ - 没有 mutex.h 的 VC++ 中的自旋锁同步

java - 需要公式 - 如果可能的话

r - R中的平行距离矩阵