c++ - 我的 3d OpenGL 对象围绕世界原点旋转,而不是本地空间原点。我做错了什么或误解了什么?

标签 c++ opengl 3d rotation glm-math

我已经被这个问题困扰了两天了,我不知道还能去哪里看看。我正在使用 OpenGL 渲染两个 3D 立方体,并尝试对这些场景中的每个立方体应用局部旋转,以响应我按下按钮。

我的立方体在 3d 空间中旋转,但它们都围绕世界空间原点旋转,而不是围绕它们自己的本地原点旋转。

(第二个视频) https://www.youtube.com/watch?v=3mrK4_cCvUw

经过网上搜索,合适的MVP计算公式如下:

auto const model = TranslationMatrix * RotationMatrix * ScaleMatrix;
auto const modelview = projection * view * model;

我的每个立方体都有自己的“模型”,其定义如下:

struct model
{
    glm::vec3 translation;
    glm::quat rotation;
    glm::vec3 scale = glm::vec3{1.0f};
};

当我按下键盘上的按钮时,我会创建一个代表新角度的四元数,并将其与之前的旋转四元数相乘,从而就地更新它。

该函数如下所示:

template<typename TData>
void rotate_entity(TData &data, ecst::entity_id const eid, float const angle,
  glm::vec3 const& axis) const
{
    auto &m = data.get(ct::model, eid);
    auto const q = glm::angleAxis(glm::degrees(angle), axis);
    m.rotation = q * m.rotation;

    // I'm a bit unsure on this last line above, I've also tried the following without fully understanding the difference
    // m.rotation = m.rotation * q;
}

轴由用户提供,如下所示:

// inside user-input handling function
float constexpr ANGLE = 0.2f;

...

// y-rotation
case SDLK_u: {
    auto constexpr ROTATION_VECTOR = glm::vec3{0.0f, 1.0f, 0.0f};
    rotate_entities(data, ANGLE, ROTATION_VECTOR);
    break;
}
case SDLK_i: {
    auto constexpr ROTATION_VECTOR = glm::vec3{0.0f, -1.0f, 0.0f};
    rotate_entities(data, ANGLE, ROTATION_VECTOR);
    break;
}

我的 GLSL 顶点着色器与我在示例代码中找到的内容非常直接:

// attributes input to the vertex shader
in vec4 a_position; // position value

// output of the vertex shader - input to fragment
// shader
out vec3 v_uv;

uniform mat4 u_mvmatrix;

void main()
{
  gl_Position = u_mvmatrix * a_position;
  v_uv = vec3(a_position.x, a_position.y, a_position.z);
}

在我的绘制代码中,我用来计算每个立方体 MVP 的确切代码是:

...
auto const& model = shape.model();

auto const tmatrix = glm::translate(glm::mat4{}, model.translation);
auto const rmatrix = glm::toMat4(model.rotation);
auto const smatrix = glm::scale(glm::mat4{}, model.scale);
auto const mmatrix = tmatrix * rmatrix * smatrix;
auto const mvmatrix = projection * view * mmatrix;

// simple wrapper that does logging and forwards to glUniformMatrix4fv()
p.set_uniform_matrix_4fv(logger, "u_mvmatrix", mvmatrix);

在我的程序的早期,我像这样计算 View /投影矩阵:

auto const windowheight = static_cast<GLfloat>(hw.h);
auto const windowwidth = static_cast<GLfloat>(hw.w);
auto projection = glm::perspective(60.0f, (windowwidth / windowheight), 0.1f, 100.0f);
auto view = glm::lookAt(
  glm::vec3(0.0f, 0.0f, 1.0f), // camera position
  glm::vec3(0.0f, 0.0f, -1.0f),  // look at origin
  glm::vec3(0.0f, 1.0f, 0.0f)); // "up" vector

我的立方体在世界空间中的位置位于 Z 轴上,因此它们应该是可见的:

cube0.set_world_position(0.0f, 0.0f, 0.0f, 1.0f);
cube1.set_world_position(-0.7f, 0.7f, 0.0f, 1.0f);

// I call set_world_position() exactly once before my game enter's it's main loop.
// I never call this again, it just modifies the vertex used as the center of the shape.
// It doesn't modify the model matrix at all.
// I call it once before my game enter's it's game loop, and I never modify it after that.

所以,我的问题是,更新对象旋转的适当方法是什么?

我应该直接在对象的“模型”中存储四元数吗?

我应该将翻译和缩放存储为单独的 vec3 吗?

有更简单的方法吗?我一直在阅读和重新阅读我能找到的所有内容,但我没有看到有人以同样的方式这样做。

本教程在细节上有点简短,特别是如何将旋转应用于现有旋转(我相信这只是将四元数相乘,这就是我在上面的rotate_entity(...) 中所做的事情)。 http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-17-quaternions/ https://github.com/opengl-tutorials/ogl/blob/master/tutorial17_rotations/tutorial17.cpp#L306-L311

将生成的“MVP”矩阵存储为我的“模型”并在 MVP 矩阵上应用 glm::transform/glm::scale/glm::rotate 操作是否更有意义直接地? (我之前尝试过最后一个选项,但我不知道如何让它发挥作用)。

谢谢!

编辑:更好的链接

最佳答案

通常,您不想修改模型的各个顶点在 CPU 上的位置。这就是顶点程序的全部目的。模型矩阵的目的是在顶点程序中将模型定位在世界中。

要绕其中心旋转模型,您需要先将中心移动到原点,然后旋转模型,然后将中心移动到最终位置。假设您有一个从 (0,0,0) 延伸到 (1,1,1) 的立方体。您需要:

  1. 将立方体平移 (-0.5, -0.5, -0.5)
  2. 旋转角度
  3. 将立方体平移 (0.5, 0.5, 0.5)
  4. 将立方体平移到场景中它所属的位置

您可以将最后 2 个转换合并为一个转换,当然,您可以将所有这些转换合并为一个矩阵,即模型矩阵。

关于c++ - 我的 3d OpenGL 对象围绕世界原点旋转,而不是本地空间原点。我做错了什么或误解了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41134820/

相关文章:

OpenGL 恐龙模型错误 : gluTessCallback issue

c++ - 在 Windows 上编译+分发 Linux 代码

c++ - 在拖动窗口或按住菜单按钮期间,如何阻止 Windows 阻止程序?

c++ - const 正确性和返回值 - C++

algorithm - body 在某个封闭空间中可以移动的所有可能方向

javascript - 如何在 Three.js 中从一系列 3D 点创建 3D 多边形?

c++ - 给定两个值时,输入循环循环两次

c++ - 在 GLM 中加载纹理

opengl - OpenGL统一缓冲区std140布局,驱动程序错误或我误解了规范?

OpenGL 备忘单?