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

查看:244
本文介绍了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);

然后执行渲染功能.

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

..然后渲染...

..and render...

不幸的是,当我进行旋转时,物体只是绕原点旋转(离原点的位置"越远,轨道越大).

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天全站免登陆