我有一个通用的OpenGL 3D世界,以(0,0,0)为中心开始。我实现了一个标准的轨迹球,基于this code。这实现了对当前模型视图矩阵的小增量/转换的旋转,OpenGL链接翻译/旋转
// We need to apply the rotation as the last transformation.
// 1. Get the current matrix and save it.
// 2. Set the matrix to the identity matrix (clear it).
// 3. Apply the trackball rotation.
// 4. Pre-multiply it by the saved matrix.
glGetFloatv(GL_MODELVIEW_MATRIX, (GLfloat *)objectXform);
glLoadIdentity();
glRotatef(rot_angle, rotAxis.x, rotAxis.y, rotAxis.z);
glMultMatrixf((GLfloat *)objectXform);
这部分完美地工作。但后来我wantes实施译本,我这样做也是为小增量的模型视图矩阵,
glTranslatef(-dx, -dy, 0.f);
这也按预期工作(不管世界如何旋转,平移以及用鼠标去,即模型落在鼠标后面
当我在翻译后尝试旋转时出现问题:我希望旋转是在世界中心附近,但这不会在用户翻译后发生。绝对的翻译和补偿,但显然这是行不通的。我做了如下回忆:
// Translation part, store absolute translation
m_mouseInfo.m_fTotalTranslationX -= dx;
m_mouseInfo.m_fTotalTranslationY -= dy;
glTranslatef(-dx, -dy, 0.f);
...
// Rotation, try to apply the rotation around (0,0,0)
glGetFloatv(GL_MODELVIEW_MATRIX, (GLfloat *)objectXform);
glLoadIdentity();
// Try to compensate for the translation and do the rotation aroun (0,0,0) but won't work
glTranslatef(m_mouseInfo.m_fTotalTranslationX, m_mouseInfo.m_fTotalTranslationY, 0.f);
glRotatef(rot_angle, rotAxis.x, rotAxis.y, rotAxis.z);
glTranslatef(-m_mouseInfo.m_fTotalTranslationX, -m_mouseInfo.m_fTotalTranslationY, 0.f);
glMultMatrixf((GLfloat *)objectXform);
当我应用旋转并因此围绕原点旋转场景时,如何存储绝对平移以补偿它?或者换句话说,当我有累积转化时,我该如何在原点周围旋转世界?