Reputation: 374
I have a cube object that I am constantly rotating, but I ran into an issue. If I move the object away from (0,0,0), the object begins to rotate in a strange way. I have no idea why and how to fix this.
This is what my object looks like:
and this is how I rotate the cube:
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()
and I call a rotate method on the cube that adds the passed in values to the position:
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
Can anyone help with this.
Upvotes: 1
Views: 229
Reputation: 211166
The Fixed Function Pipeline matrix operations like glTranslate
and glRotate
specify a new matrix an multiply the current matrix by the new matrix.
Matrix multiplication is not commutative. Hence there is a difference, whether you call glTranslate
and then glRotate
or glRotate
followed by glTranslate
.
When glRotate
is followed by glTranslate
, the translated object rotates around the origin:
When glTranslate
is followed by glRotate
, then the object rotates and the rotated object is translated:
If you translate the mesh, by adding an offset to the vertices, this corresponds to the later. But if you want to rotate the mesh around the origin of the mesh, then you have to translate the rotated model (translate * rotate
).
Use glTranslate
to "move" the mesh:
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()
Upvotes: 1