2015-09-23 176 views
1

我有一个通用的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); 

当我应用旋转并因此围绕原点旋转场景时,如何存储绝对平移以补偿它?或者换句话说,当我有累积转化时,我该如何在原点周围旋转世界?

回答

1

要翻译点(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来获取当前场景。