【问题标题】:Rotating 3D model in opengl在opengl中旋转3D模型
【发布时间】:2021-06-24 03:58:54
【问题描述】:

我正在尝试根据 9DoF 参数为我的模型在场景中分配一个位置。其中,轮换让我很难受。如果我将 x 和 z 旋转保持为零,那么 roty 似乎工作正常并且模型仅围绕 y 轴旋转。但是,只要我给 x 和 y 旋转任何值,模型就会在进行 y 旋转时围绕其他两个轴移动。

下面是我用来给出 rotxrotyrotz 值的代码。我觉得矩阵乘法有一些问题。因为 0 * x + roty * y + 0 * z = roty 除了任何东西 * x + roty * y + 任何东西 * z = 任何东西 .

model = glm::translate(model, glm::vec3(transX, transY, transZ));
model = glm::rotate(model, toRadians * rotX, glm::vec3(1, 0, 0));
model = glm::rotate(model, toRadians * rotY, glm::vec3(0, 1, 0));
model = glm::rotate(model, toRadians * rotZ, glm::vec3(0, 0, 1));

【问题讨论】:

标签: c++ opengl 3d rendering glm-math


【解决方案1】:

为了定位一个对象,首先应用旋转,然后才考虑围绕另一个对象平移/旋转:

 model = glm::rotate(model, toRadians * rotX, glm::vec3(1, 0, 0));
 model = glm::rotate(model, toRadians * rotY, glm::vec3(0, 1, 0));
 model = glm::rotate(model, toRadians * rotZ, glm::vec3(0, 0, 1));
 model = glm::translate(model, glm::vec3(transX, transY, transZ));

【讨论】:

  • “先应用旋转,”。如果要先旋转,命令glm::rotate必须是代码的最后一行,因为glm::roatate会创建一个旋转矩阵并将输入矩阵与旋转矩阵相乘。旋转优先意味着translate * rotate。见OpenGL move object and keep transformation
猜你喜欢
  • 2013-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-26
  • 2014-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多