python - PyOpenGl Pygame 窗口在运行时卡住

标签 python pygame pyopengl

我是第一次使用 PyOpenGl,并观看了有关它的 YouTube 教程。这是视频:https://www.youtube.com/watch?v=R4n4NyDG2hI&t=1464s

然而,当我尝试代码时,pygame 打开并移动一帧,然后它卡住并且不会停止加载。我不确定这是因为我使用的系统还是我使用的 python 版本。

我有一台 11-6 英寸的 2012 MacBook Air,我使用的是 python 2.7.15。我使用 python 2 而不是 python 3 的原因是因为当我尝试 pip3 安装 PyOpenGl 时,它给了我一个错误。

import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
verticies = (
        (1, -1, -1),
        (1, 1, -1),
        (-1, 1, -1),
        (-1, -1, -1),
        (1, -1, 1),
        (1, 1, 1),
        (-1, -1, 1),
        (-1, 1, 1),
        )
edges = (
        (0,1),
        (0,3),
        (0,4),
        (2,1),
        (2,3),
        (2,7),
        (6,3),
        (6,4),
        (6,7),
        (5,1),
        (5,4),
        (5,7),
        )
def Cube():
        global edges
        glBegin(GL_LINES)
        for edges in edges:
                for vertex in edges:
                        glVertex3fv(verticies[vertex])
        glEnd()
def main():
        pygame.init()
        display = (800,600)
        pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
        gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
        glTranslatef(0.0, 0.0, -5)
        glRotatef(20, 3, 1, 1)
        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)
                Cube()
                pygame.display.flip()
                pygame.time.wait(10)
main()

发生的另一件事是,当我运行程序时,IDLE 给我这个错误信息:

Traceback (most recent call last):
  File "/Users/SplatM4n/Desktop/First3DGraphics.py", line 73, in                 <module>
    main()
  File "/Users/SplatM4n/Desktop/First3DGraphics.py", line 66, in main
    Cube()
  File "/Users/SplatM4n/Desktop/First3DGraphics.py", line 44, in Cube
    for vertex in edges:
TypeError: 'int' object is not iterable

我也觉得这是问题的一部分,所以请提供任何帮助。

谢谢,SplatM4n

最佳答案

tutorial video所示,必须在主循环内应用旋转矩阵。

glTranslatef(0.0, 0.0, -5)
    while True:
        clock.tick(60)
        for event in pygame.event.get():
                if event.type == pygame.QUIT:
                        pygame.quit()
                        quit()

        glRotatef(1, 3, 1, 1)

请注意,glRotatef不仅生成旋转矩阵。它还将当前矩阵乘以新的旋转矩阵。这导致立方体在每一帧中连续且渐进地旋转 1 度。
如果 glRotatef 在循环外完成,则只会对立方体应用一次旋转。在那之后,场景似乎被卡住了。

查看示例,其中我将更改应用于您的原始应用程序:

import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
verticies = ((1, -1, -1), (1, 1, -1), (-1, 1, -1), (-1, -1, -1),
             (1, -1, 1), (1, 1, 1), (-1, -1, 1), (-1, 1, 1))
edges = ((0,1), (0,3), (0,4), (2,1),(2,3), (2,7), (6,3), (6,4),(6,7), (5,1), (5,4), (5,7))


def Cube():
    global edges
    glBegin(GL_LINES)
    for edge in edges:
        for vertex in edge:
            glVertex3fv(verticies[vertex])
    glEnd()

def main():
    pygame.init()
    display = (800,600)
    pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
    gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
    glTranslatef(0.0, 0.0, -5)
    while True:
        for event in pygame.event.get():
                if event.type == pygame.QUIT:
                        pygame.quit()
                        quit()

        glRotatef(1, 3, 1, 1) # <--------------- rotate inside the main loop

        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
        Cube()
        pygame.display.flip()
        pygame.time.wait(10)
main()

关于python - PyOpenGl Pygame 窗口在运行时卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54683378/

相关文章:

python - Pygame OpenGL 3D 立方体滞后

python - 运行和终止一个程序(Windows下的Python)

python - Django:openpyxl 将工作簿保存为附件

python - pygame中Tiled和pytmx的透明图 block 问题

python - Pygame alpha 值

python - PyOpenGl 还是 pyglet?

python - Pygame OpenGL 3D 立方体滞后

python - 聚合来自多个模块的多个测试用例以在 PyDev TestRunner 中运行

python - 从具有多个页面结果的网站抓取网页

python - 近距离 3D 显示困惑