OpenGL 链接 translations/rotations
OpenGL chained translations/rotations
我有一个通用的 OpenGL 3D 世界,开始时以 (0,0,0) 为中心。我基于 this code 实现了一个标准轨迹球。这实现了对当前模型视图矩阵 increments/transformations 的小旋转,
// 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);
当我应用旋转并因此围绕原点旋转场景时,如何存储绝对平移以补偿它?
或者,换句话说,当我有累积变换时,我怎样才能围绕原点旋转世界?
要绕点 (x,y)
平移,首先按 (x,y)
平移,然后旋转,然后按 -(x,y)
平移。
现在,如果你的世界已经被M
(某个矩阵)变换,那么在变换之前世界的原点位于M^-1 (0,0)
.
假设你的世界从原来的变换是M
,你想进行一些旋转R
,但旋转应该围绕原始原点,但旋转矩阵 R
表示为围绕点 (0,0)
的旋转(样式也是如此)。
然后 R' = M R M^-1
将生成一个新矩阵 R'
,它包括围绕 原始 (0,0)
旋转 R
。那么M' = R' M
就是表示从无到有,然后做M
,然后围绕原点做R
的矩阵。
如果您正在对某些模型进行累积变换,只需跟踪所述变换的结果,同时修改场景。
或者,存储原始场景,而不是对其进行累积变换,始终应用 M
来获取当前场景。
我有一个通用的 OpenGL 3D 世界,开始时以 (0,0,0) 为中心。我基于 this code 实现了一个标准轨迹球。这实现了对当前模型视图矩阵 increments/transformations 的小旋转,
// 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);
当我应用旋转并因此围绕原点旋转场景时,如何存储绝对平移以补偿它?
或者,换句话说,当我有累积变换时,我怎样才能围绕原点旋转世界?
要绕点 (x,y)
平移,首先按 (x,y)
平移,然后旋转,然后按 -(x,y)
平移。
现在,如果你的世界已经被M
(某个矩阵)变换,那么在变换之前世界的原点位于M^-1 (0,0)
.
假设你的世界从原来的变换是M
,你想进行一些旋转R
,但旋转应该围绕原始原点,但旋转矩阵 R
表示为围绕点 (0,0)
的旋转(样式也是如此)。
然后 R' = M R M^-1
将生成一个新矩阵 R'
,它包括围绕 原始 (0,0)
旋转 R
。那么M' = R' M
就是表示从无到有,然后做M
,然后围绕原点做R
的矩阵。
如果您正在对某些模型进行累积变换,只需跟踪所述变换的结果,同时修改场景。
或者,存储原始场景,而不是对其进行累积变换,始终应用 M
来获取当前场景。