在 OpenGL C++ 中旋转子对象

Rotating child object in OpenGL c++

我正在向我的 OpenGL 应用程序添加一个具有多个 DoG 的机器人(稍后我想为这个机器人实现反向运动学)并且我需要一起移动 "connected" 的对象。

到目前为止,我在场景中添加了 3 个 STL 对象,它看起来像 this

有base,joint1,joint2

Base 是静止的,joint1 和 joint2 现在根据键盘输入围绕 Z 轴旋转,但它们都只是围绕它们的原点旋转,这对 joint1 没问题,但对于 joint2 我需要它们连接起来并且进行相应翻译。

Here是旋转后的输出。

对于对象定位和旋转,我使用常规 MVP 矩阵和 glm。

joint1M = glm::translate(joint1M, glm::vec3(-50.0f, -10.4f, 0.0f));
joint1M = glm::rotate(joint1M, glm::radians(rotation), glm::vec3(0.0f, 1.0f, 0.0f));
joint2M = glm::translate(joint2M, glm::vec3(-50.0f, 2.85f, 0.0f));
joint2M = glm::rotate(joint2M, glm::radians(rotation), glm::vec3(0.0f, 1.0f, 0.0f));

有什么方法可以在 OpenGL 中创建类似于子父 Unity 关系的东西吗?或者我应该以某种方式根据 joint1 旋转更改 joint2 的位置?

感谢评论和更多的谷歌搜索,我设法让它像这样工作:

关节 1 保持不变:

joint1M = glm::translate(joint1M, glm::vec3(-50.0f, -10.4f, 0.0f));
joint1M = glm::rotate(joint1M, glm::radians(rotation), glm::vec3(0.0f, 1.0f, 0.0f));

joint2移动到joint1的位置,旋转,移动到它应该在的地方

joint2M = glm::translate(joint2M, glm::vec3(-50.0f, -10.4f, 0.0f));
joint2M = glm::rotate(joint2M, glm::radians(rotation), glm::vec3(0.0f, 1.0f, 0.0f));
joint2M = glm::translate(joint2M, glm::vec3(0.0f, 13.25f, 0.0f));