0

我在 64x64x64 立方体内绘制了一堆点,但不知道如何移动原点,以便围绕立方体的中心进行旋转。我的顶点数据存储在一维数组中,从 {(0,0,0), (0,0,1), ..., (63, 63, 63)} 开始。

这是我当前设置每帧矩阵的代码:

// Set ProjectionMatrix
projectionMatrix = glm::perspective(90.0f, (GLfloat)width / (GLfloat) height,0.1f, 1000.f);
glUniformMatrix4fv(location_projectionMatrix, 1, GL_FALSE, glm::value_ptr(projectionMatrix));

// Set ModelViewMatrix
glm::mat4 identity = glm::mat4(1.0);
glm::mat4 viewTranslate = glm::translate(identity, glm::vec3(0.0f, 0.0f, -translate_z));
glm::mat4 viewRotateX   = glm::rotate(viewTranslate, rotate_x, glm::vec3(1.0f, 0.0f, 0.0f));
glm::mat4 viewRotateY   = glm::rotate(viewRotateX, rotate_y, glm::vec3(0.0f, 1.0f, 0.0f));
modelViewMatrix = viewRotateY;
glUniformMatrix4fv(location_modelViewMatrix, 1, GL_FALSE, glm::value_ptr(modelViewMatrix));

我试图将立方体平移 -32 到 x 和 y 方向,但没有成功。

4

1 回答 1

2

矩阵运算是不可交换的。这意味着先平移后旋转与先旋转后平移不同。通常顺序是:缩放->旋转->平移。所以试试这个:

glm::mat4 viewRotateX   = glm::rotate(identity, rotate_x, glm::vec3(1.0f, 0.0f, 0.0f));
glm::mat4 viewRotateY   = glm::rotate(viewRotateX, rotate_y, glm::vec3(0.0f, 1.0f, 0.0f));
glm::mat4 viewTranslate = glm::translate(viewRotateY   , glm::vec3(0.0f, 0.0f, -translate_z));
于 2013-01-11T10:15:02.747 回答