User 12692182
User 12692182

Reputation: 981

glTranslatef gets messed up after glRotatef

I'm a newbie to PyOpenGL. I made a simple code using it that displays a cube, and you can navigate around it using the keyboard. However, I noticed that after a glRotatef command gets called, it changes your perspective, not the positionment of the objects. However, glTranslatef does not work on perspective, and rather a literal coordinate system. In other words, after calling a glRotatef command by 90*, a glTranslatef command that would have moved you forward now moves you to the left. Is there a function like glTranslatef, only it changes with the rotation so you don't get weird motions, or some sort of workaround you can do, to change the values you pass to glTranslatef based on the rotation?

Upvotes: 2

Views: 284

Answers (1)

Rabbid76
Rabbid76

Reputation: 210878

Operations like glTranslate and glRotate define a (translation respectively rotation) matrix and multiply the current matrix by the new matrix.
The matrix multiplication is not commutative. If you want to rotate the model an translate the model independent of the rotation, then you have to do the rotation before the translation respectively multiply the translation matrix by the rotation matrix: (OpenGL matrix multiplications have to be read from the right to the left):

modelmatrix = translation * rotation

Respectively glTranslate before glRotate:

glTranslate(x, y, z)
glRotate(angle, ax, ay, az)

Upvotes: 2

Related Questions