如何在pygame和pyopengl中独立旋转2个对象 [英] How to rotate 2 objects independently in pygame and pyopengl

查看:152
本文介绍了如何在pygame和pyopengl中独立旋转2个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试独立旋转2个对象,但是当我运行代码时,两个对象都沿相同方向旋转,请在此处输入代码

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

主要功能

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)

推荐答案

请注意,数十年来不赞成使用glBegin/glEnd序列绘制,每个顶点光照模型的固定功能管线矩阵堆栈和固定功能管线. . 阅读有关固定功能管道的信息,并参见

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.

无论如何,通常通过模型矩阵来变换对象,然后通过视图矩阵和投影矩阵来变换场景.
可悲的是,在已弃用的OpenGL固定功能流水线中,模型矩阵和视图矩阵是连接在一起的,因此无法轻易地分别处理.

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.

您必须通过每个对象自己的模型矩阵对其进行变换,以使其独立旋转.创建4 * 4矩阵,并通过单位矩阵对其进行初始化:

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]]

要操作模型矩阵,请通过 glLoadMatrix .然后操作它们,最后通过 :

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

最终代码如下:

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)

这篇关于如何在pygame和pyopengl中独立旋转2个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆