gpt4 book ai didi

c++ - GLM 翻译问题

转载 作者:行者123 更新时间:2023-11-28 01:27:42 24 4
gpt4 key购买 nike

我有一个模型,如果我有一个变换矩阵 glm::mat4 并且向上 vector 是 glm::vec4 up(matrix[1 ]); 因此,如果我想按值 up*=d; 沿向上 vector 移动模型,则 matrix=glm::translate(matrix,up); ,结果矩阵不会向右移动模型,例如,如果向上 vector id (0,0.707106769,0.707106769) “模型绕 X 轴旋转 45d”,我想要将其移动 5 个单位,因此平移 vector 为 (0,3.535533845,3.535533845),然后在平移后位置分量仅在 Y 方向发生变化,因此仅沿 Y 轴移动。

最佳答案

GLM 的 translate 源代码:

template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER mat<4, 4, T, Q> translate(mat<4, 4, T, Q> const& m, vec<3, T, Q> const& v)
{
mat<4, 4, T, Q> Result(m);
Result[3] = m[0] * v[0] + m[1] * v[1] + m[2] * v[2] + m[3];
return Result;
}

应用平移的效果由现有矩阵的旋转分量(左上角的 3x3 子矩阵,或者如果底行是 0 0 0 1 的前 3 列)修改,即:

glm::translate prepends, rather than appends, the translation.

换句话说,上面的代码等价于:

// create an identity matrix and apply the translation
glm::mat4 translation = glm::translate(glm::mat4(1.f), up);

// post-multiply (i.e. the applied translation comes FIRST)
matrix = matrix * translation;

可以通过以下方式实现您想要的效果:

1)

// pre-multiply (i.e. the applied translation comes AFTER)
matrix = translation * matrix;

或者等效地,在模型的本地基础上构建翻译:

2)

// local up vector (Y-axis)
glm::vec3 local_up(0.f, 1.f, 0.f);
local_up *= d;

// apply using translate as before
matrix = glm::translate(matrix, local_up);

关于c++ - GLM 翻译问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53068777/

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