【问题标题】:OpenGL Rotation Around a Point Using GLMOpenGL 使用 GLM 围绕一个点旋转
【发布时间】:2014-11-16 20:05:33
【问题描述】:

我一直在阅读其他有关如何通过将枢轴平移到原点、旋转和平移来围绕 OpenGL 中的某个点旋转对象的帖子。但是,我似乎无法让它工作。我有一个立方体,正在使用 glm::perspective 和 glm::lookAt 生成投影和视图矩阵。

glm::mat4 Projection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f);
// Camera matrix
glm::mat4 View       = glm::lookAt(
                                   glm::vec3(0,3.5,-5),
                                   glm::vec3(0,0,0), 
                                   glm::vec3(0,1,0)  
                                   );
// Model matrix : an identity matrix (model will be at the origin)
glm::mat4 Model      = glm::mat4(1.0f);

然后我在 while 循环中像这样在模型矩阵上应用转换:

    if (glfwGetKeyOnce(window, GLFW_KEY_UP))
    {
        Model = translate(Model, vec3(1.0f, 0.0f, 0.0f));
        Model = rotate(Model, 90.0f, vec3(1.0f, 0.0f, 0.0f));
        Model = translate(Model, vec3(-1.0f, 0.0f, 0.0f));
    }
    mat4 MVP = Projection * View * Model;

在我的顶点着色器中,我有这个:

gl_Position =  MVP * vec4(vertexPosition_modelspace,1);

但是,这仍然只是围绕其中心旋转立方体。如果我摆脱对 glm::translate 的调用,而是通过调整顶点的 x 位置进行翻译,它可以正常工作。但我认为这不是正确的做法。我在这里想念什么?

【问题讨论】:

  • 您沿 X 轴平移,然后围绕同一轴旋转,尝试改为沿 y 轴平移

标签: c++ opengl rotation quaternions glm-math


【解决方案1】:
if (glfwGetKeyOnce(window, GLFW_KEY_UP))
{
    Model = translate(Model, vec3(1.0f, 0.0f, 0.0f));
    Model = rotate(Model, 90.0f, vec3(1.0f, 0.0f, 0.0f));
    Model = translate(Model, vec3(-1.0f, 0.0f, 0.0f));
}
mat4 MVP = Projection * View * Model;

尝试用“竖起大拇指的手”来可视化这段代码。拇指是 x 轴。先举手,然后绕一圈,最后放下手。

您很可能想绕另一个轴旋转。

请记住,还有更现代的方法可以像四元数一样进行旋转。这将为您节省大量操作。

【讨论】:

  • 除了四元数不会让你免于这个错误
猜你喜欢
  • 2019-04-06
  • 1970-01-01
  • 2023-04-03
  • 2021-10-17
  • 2010-09-22
  • 2019-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多