How can I rotate multiple quads separately? (Pygam

2019-07-25 13:08发布

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()

1条回答
我命由我不由天
2楼-- · 2019-07-25 13:49

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, and GL_TEXTURE - see glMatrixMode.
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 matrix

If you want to do an individual matrix transformation to an object, then you have to:

  • Push the current matrix to the stack by glPushMatrix
  • Do the transformations like glTranslate, glRotate, glScale or even glLoadMatrix which loads a completely new matrix. For the sak of completeness, there als exists glLoadIdentity and glMultMatrix.
  • Draw the object.
  • Pop the top most matrix 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.

glPushMatrix()
glTranslated(250, 100, 0)
glRotated(5, 0, 0, 1)
makeQuad()
glPopMatrix()

If you want to animate the quads separately, then i recommend to use the 2 control variables a1 and a2, which control the angle of rotation and are incremented ongoing in the main loop:

a1, a2 = 0, 0

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(250, 100, 0)
    glRotated(a1, 0, 0, 1)
    makeQuad()
    glPopMatrix()

    glPushMatrix()
    glTranslated(100, 250, 0)
    glRotated(a2, 0, 0, 1)
    makeQuad()
    glPopMatrix()

    a1 += 1
    a2 += 2

    pygame.display.flip()

Preview:

查看更多
登录 后发表回答