glm 结合旋转和平移 [英] glm combine rotation and translation

查看:40
本文介绍了glm 结合旋转和平移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象,我首先想旋转它(围绕它自己的中心)然后将它平移到某个点.我有一个 glm::quat 保存旋转和一个 glm::vec3 保存它需要翻译的点.

I have an object which I first want to rotate (about its own center) then translate it to some point. I have a glm::quat that holds the rotation and a glm::vec3 that holds the point to which it needs to be translated.

glm::vec3 position;
glm::quat orientation;
glm::mat4 modelmatrix; <-- want to combine them both in here

modelmatrix = glm::translate(glm::toMat4(orientation),position);

然后在我的渲染函数中,我这样做了.

Then at my render function, I do.

pvm = projectionMatrix*viewMatrix*modelmatrix;
glUniformMatrix4fv(pvmMatrixUniformLocation, 1, GL_FALSE, glm::value_ptr(pvm));

...并渲染...

不幸的是,当我应用旋转时,对象只是绕原点运行(位置"离原点越远,轨道越大).

Unfortunately, the object just orbits around the origin when I apply a rotation (the farther the "position" from the origin, the larger the orbit).

当我只申请这个职位时,它翻译得很好.当我只应用旋转时,它会停留在原点并围绕其中心旋转(如预期的那样).那么为什么当我同时应用它们时会变得奇怪呢?我错过了一些基本的东西吗?

When I apply for only the position it translates fine. When I apply only the rotation it stays at the origin and rotates about its center (as expected). So why does it go weird when I apply them both? Am I missing something basic?

推荐答案

因为您以错误的顺序应用它们.通过执行 glm::translate(glm::toMat4(orientation),position),您所做的相当于:

Because you're applying them in the wrong order. By doing glm::translate(glm::toMat4(orientation),position), you are doing the equivalent of this:

glm::mat4 rot = glm::toMat4(orientation);
glm::mat4 trans = glm::translate(glm::mat4(1.0f), position);
glm::mat4 final = rot * trans;

请注意,平移在矩阵的右侧,而不是左侧.这意味着平移首先发生,然后相对于平移发生旋转.所以旋转发生在平移后的空间中.

Note that the translation is on the right side of the matrix, not the left. This means that the translation happens first, then the rotation happens relative to the translation. So rotation happens in the space after translation.

您希望轮换发生.所以把矩阵乘法的顺序倒过来.

You want the rotation to happen first. So reverse the order of the matrix multiplication.

这篇关于glm 结合旋转和平移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆