问题描述
我有一个不断旋转的多维数据集对象,但是遇到了一个问题。如果我将对象从(0,0)移开,则对象开始以奇怪的方式旋转。我不知道为什么以及如何解决这个问题。
这是我的对象的样子:
这就是我旋转立方体的方式:
def draw_edges(self,color):
"""Draws the cube's edges"""
glPushmatrix()
glrotate(self.rotation[3],self.rotation[0],self.rotation[1],self.rotation[2])
glBegin(GL_LInes)
for edge in self.edges:
for vertex in edge:
glColor3fv(color)
glVertex3fv(self.vertices[vertex])
glEnd()
glPopMatrix()
然后我在多维数据集上调用一个rotate方法,该方法将传入的值添加到位置:
def rotate(self,rotval,mult):
self.rotation[0] = rotval[0]
self.rotation[1] = rotval[1]
self.rotation[2] = rotval[2]
self.rotation[3] += mult
if self.rotation[3] >= 360:
self.rotation[3] = self.rotation[3] - 360
任何人都可以帮忙。
解决方法
Fixed Function Pipeline和glTranslate
之类的glRotate
矩阵运算指定了一个新矩阵,并将当前矩阵乘以新矩阵。
矩阵乘法不是commutative。因此,无论您先呼叫glTranslate
,然后呼叫glRotate
还是glRotate
,再呼叫glTranslate
,都是有区别的。
当glRotate
后跟glTranslate
时,翻译后的对象围绕原点旋转:
当glTranslate
后跟glRotate
时,对象旋转并平移旋转的对象:
如果平移网格,则通过向顶点添加偏移量,这对应于后者。但是,如果要围绕网格的原点旋转网格,则必须平移旋转的模型(translate * rotate
)。
使用glTranslate
“移动”网格:
def draw_edges(self,color):
"""Draws the cube's edges"""
glPushMatrix()
glTranslate(self.x,self.y,self.z)
glRotate(self.rotation[3],self.rotation[0],self.rotation[1],self.rotation[2])
glBegin(GL_LINES)
for edge in self.edges:
for vertex in edge:
glColor3fv(color)
glVertex3fv(self.vertices[vertex])
glEnd()
glPopMatrix()