This code is a display part.
def display(self):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
gluLookAt(5.0, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)
if (self.axis == 0):
glRotatef(self.pitch, 1.0, 0.0, 0.0)
elif (self.axis == 1):
glRotatef(self.yaw, 0.0, 1.0, 0.0)
elif (self.axis == 2):
glRotatef(self.roll, 0.0, 0.0, 1.0)
self.colorcube()
glFlush()
glutSwapBuffers()
) figured it out that cube rotates when I put the code gluLookAt()
before glRotate()
and camera rotates when I put the code gluLookAt()
after glRotate()
.
but I don't know why. why does it happen?
OpenGL is a state engine. The current transformation matrix is a global state. Both functions gluLookAt
and glRotatef
define a matrix and multiply the current transformation matrix with the new defined matrix. The matrix multiplication is not commutative, the order matters:
matrix1 * matrix2 != matrix2 * matrix1
Therfore:
gluLookAt(5.0, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)
glRotatef(self.pitch, 1.0, 0.0, 0.0)
is not the same as
glRotatef(self.pitch, 1.0, 0.0, 0.0)
gluLookAt(5.0, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)
You have to care about the order.