【发布时间】:2015-09-23 16:45:32
【问题描述】:
我有一个通用的 OpenGL 3D 世界,在开始时以 (0,0,0) 为中心。我基于this code 实现了一个标准轨迹球。这将旋转实现为当前模型视图矩阵的小增量/转换,
// 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);
这部分工作得很好。但后来我想实现翻译,我也在这样做,作为模型视图矩阵的小增量,
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);
当我应用旋转并因此围绕原点旋转场景时,如何存储绝对平移以补偿它?
或者,换句话说,当我有累积变换时,我如何才能围绕原点旋转世界?
【问题讨论】: