How to rotate 2 objects independently in pygame and pyopengl

不想你离开。 提交于 2019-12-22 13:11:30

问题


I'm trying to rotate 2 objects independently but when I run the code both objects rotate in the same direction enter code here

here i save the matrix and rotate a square under the cube

def rotate_square():
    glColor3f(1,1,1)
    glMatrixMode(GL_MODELVIEW)
    glPushMatrix()
    glRotatef(45,0,1,0)
    glBegin(GL_LINES)
    glVertex3fv(v1);
    glVertex3fv(v2);
    glVertex3fv(v1);
    glVertex3fv(v3);
    glVertex3fv(v3);
    glVertex3fv(v4);
    glVertex3fv(v2);
    glVertex3fv(v4);
    glEnd()
    glPopMatrix()

main function

def main():
    pygame.init()
    screen = pygame.display.set_mode(SCREEN_SIZE, DOUBLEBUF|OPENGL)
    resize(*SCREEN_SIZE)
    print(glGetString(GL_VERSION))
    gluLookAt(0, 0, -6, 0, 0, 0, 0, 1, 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)

Rotating the cube

        glRotatef(1,0,1,0)
        glutWireCube(2)
        rotate_square()
        pygame.display.flip()
        pygame.time.wait(10)

回答1:


Note, that drawing by glBegin/glEnd sequences, the fixed function pipeline matrix stack and fixed function pipeline per vertex light model, 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, in common an object is transformed by the model matrix, then the scene is transformed by the view matrix and the projection matrix.
Sadly at the deprecated OpenGL fixed function pipeline, the model matrix and the view matrix are concatenated and can't be handled separately that easy.

You have to transform each object by its own model matrix to rotate them independently. Create the 4*4 matrices and initialize them by the identity matrix:

model1 = [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]
model2 = [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]

To manipulate the model matrices, load them to the matrix stack by glLoadMatrix. Then manipulate them and finally read the result back by glGetFloatv(GL_MODELVIEW_MATRIX):

glPushMatrix()
glLoadMatrixf(model1)
glRotatef(1,0,1,0)
model1 = glGetFloatv(GL_MODELVIEW_MATRIX)
glPopMatrix()

If the model matrix has to be applied, it can be multiplied to the matrix stack by glMultMatrix:

glPushMatrix()
glMultMatrixf(model1)
glutWireCube(2)
glPopMatrix()

The final code may look like this:

model1 = [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]
model2 = [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]
def rotate_square():
    global model2

    glPushMatrix()
    glLoadMatrixf(model2)
    glRotatef(5,0,1,0)
    model2 = glGetFloatv(GL_MODELVIEW_MATRIX)
    glPopMatrix()

    glColor3f(1,1,1)

    glMatrixMode(GL_MODELVIEW)
    glPushMatrix()
    glMultMatrixf(model2)
    glBegin(GL_LINES)
    glVertex3fv(v1)
    glVertex3fv(v2)
    glVertex3fv(v1)
    glVertex3fv(v3)
    glVertex3fv(v3)
    glVertex3fv(v4)
    glVertex3fv(v2)
    glVertex3fv(v4)
    glEnd()
    glPopMatrix()
def main():
    global model1

    # [...]

    while True:

        # [...]

        glMatrixMode(GL_MODELVIEW)
        glPushMatrix()
        glLoadMatrixf(model1)
        glRotatef(1,0,1,0)
        model1 = glGetFloatv(GL_MODELVIEW_MATRIX)
        glPopMatrix()

        glPushMatrix()
        glMultMatrixf(model1)
        glutWireCube(2)
        glPopMatrix()

        rotate_square()

        pygame.display.flip()
        pygame.time.wait(10)


来源:https://stackoverflow.com/questions/53157171/how-to-rotate-2-objects-independently-in-pygame-and-pyopengl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!