I'd like to rotate my quads roughly around they own axis. My first thought was to use glPushMatrix() and glPophMatrix(), but doesn't work. Regardless of that I believe this is the good way.
Code:
import time, pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
def makeQuad():
glBegin(GL_QUADS)
glVertex2f(0, 0)
glVertex2f(0, 50)
glVertex2f(50, 50)
glVertex2f(50, 0)
glEnd()
def main():
pygame.init()
display = (800,800)
pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
gluOrtho2D(0, 800, 0, 800)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glPushMatrix()
glTranslated(150, 0, 0)
makeQuad()
glTranslated(100, 250, 0)
makeQuad()
glPopMatrix()
pygame.display.flip()
main()
Note, that drawing by
glBegin
/glEnd
sequences and the fixed function pipeline matrix stack, is deprecated since decades. Read about Fixed Function Pipeline and see Vertex Specification and Shader for a state of the art way of rendering.Anyway, OpenGL has different matrix modes, which are
GL_MODELVIEW
,GL_PROJECTION
, andGL_TEXTURE
- seeglMatrixMode
.Ech matrix mode provides a current matrix and a matrix stack.
glPushMatrix
/glPopMatrix
push the current matrix to the stack and pop the top mast matrix form the stack and store it to the current matrixIf you want to do an individual matrix transformation to an object, then you have to:
glPushMatrix
glTranslate
,glRotate
,glScale
or evenglLoadMatrix
which loads a completely new matrix. For the sak of completeness, there als existsglLoadIdentity
andglMultMatrix
.glPopMatrix
, to restore the current matrix and the matrix stack. Now the current matrix and the matrix stack seem to be so, as the above transformations never would have happened.e.g.
If you want to animate the quads separately, then i recommend to use the 2 control variables
a1
anda2
, which control the angle of rotation and are incremented ongoing in the main loop:Preview: