PyOpenGL程序不显示立方体?

q5iwbnjs  于 2022-11-04  发布在  其他
关注(0)|答案(1)|浏览(137)

我正在尝试使用Python及其包pygame、numpy、PyOpenGL和PyOpenGL_accelerate绘制一个3D立方体。当我按下箭头按钮时,3D立方体也会向其他方向旋转。例如,当我按下右箭头时,它会向右旋转。
首先,我在其他电脑上试过,它运行得很好。但后来,我也在我的笔记本电脑上试过,当我运行它时,它在屏幕上没有显示任何东西。我成功地安装了所需的软件包和所有,但它仍然没有显示任何东西。
下面是我的代码:

import pygame
from pygame.locals import *

from OpenGL.GL import *
from OpenGL.GLU import *

pygame.init()

display = (800, 600)
pygame.display.set_mode(display, DOUBLEBUF|OPENGL)

pygame.display.set_caption("03 lab 1 ")

gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
glTranslatef(0, 0, -5)

angle = 1
rotatex = 0
rotatey = 0
rotatez = 0

def draw_cube():
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
    glBegin(GL_LINES)
    glVertex3f(-0.5, 0.5, 0)
    glVertex3f(-0.5, -0.5, 0)
    glVertex3f(-0.5, -0.5, 0)
    glVertex3f(0.5, -0.5, 0)

    glVertex3f(0.5, -0.5, 0)
    glVertex3f(0.5, 0.5, 0)
    glVertex3f(0.5, 0.5, 0)
    glVertex3f(-0.5, 0.5, 0)

    glVertex3f(-0.5, 0.5, 1)
    glVertex3f(-0.5, -0.5, 1)
    glVertex3f(-0.5, -0.5, 1)
    glVertex3f(0.5, -0.5, 1)

    glVertex3f(0.5, -0.5, 1)
    glVertex3f(0.5, 0.5, 1)
    glVertex3f(0.5, 0.5, 1)
    glVertex3f(-0.5, 0.5, 1)

    glVertex3f(-0.5, 0.5, 0)
    glVertex3f(-0.5, 0.5, 1)

    glVertex3f(0.5, 0.5, 0)
    glVertex3f(0.5, 0.5, 1)

    glVertex3f(-0.5, -0.5, 0)
    glVertex3f(-0.5, -0.5, 1)

    glVertex3f(0.5, -0.5, 0)
    glVertex3f(0.5, -0.5, 1)

    glEnd()

    while True:
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    rotatey -= 1
                if event.key == pygame.K_RIGHT:
                    rotatex += 1
                if event.key == pygame.K_UP:
                    rotatey += 1
                if event.key == pygame.K_DOWN:
                    rotatex -= 1

        if event.type == pygame.QUIT:
            pygame.quit()
            quit()

glRotate(angle, rotatex, rotatey,rotatez)
pygame.display.flip()
pygame.time.wait(15)
draw_cube()

这是我的输出结果。它只是显示一个空白屏幕。

col17t5w

col17t5w1#

绘制立方体后必须更新显示,在glEnd()后调用pygame.display.flip(),需要在每一帧中重新绘制立方体,并更改每一帧中的旋转矩阵,这是Indentation的问题,应用程序循环的缩进错误。

import pygame
from pygame.locals import *

from OpenGL.GL import *
from OpenGL.GLU import *

pygame.init()

display = (800, 600)
pygame.display.set_mode(display, DOUBLEBUF|OPENGL)

pygame.display.set_caption("03 lab 1 ")

gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
glTranslatef(0, 0, -5)

angle = 1
rotatex = 0
rotatey = 0
rotatez = 0

def draw_cube():
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
    glBegin(GL_LINES)
    glVertex3f(-0.5, 0.5, 0)
    glVertex3f(-0.5, -0.5, 0)
    glVertex3f(-0.5, -0.5, 0)
    glVertex3f(0.5, -0.5, 0)

    glVertex3f(0.5, -0.5, 0)
    glVertex3f(0.5, 0.5, 0)
    glVertex3f(0.5, 0.5, 0)
    glVertex3f(-0.5, 0.5, 0)

    glVertex3f(-0.5, 0.5, 1)
    glVertex3f(-0.5, -0.5, 1)
    glVertex3f(-0.5, -0.5, 1)
    glVertex3f(0.5, -0.5, 1)

    glVertex3f(0.5, -0.5, 1)
    glVertex3f(0.5, 0.5, 1)
    glVertex3f(0.5, 0.5, 1)
    glVertex3f(-0.5, 0.5, 1)

    glVertex3f(-0.5, 0.5, 0)
    glVertex3f(-0.5, 0.5, 1)

    glVertex3f(0.5, 0.5, 0)
    glVertex3f(0.5, 0.5, 1)

    glVertex3f(-0.5, -0.5, 0)
    glVertex3f(-0.5, -0.5, 1)

    glVertex3f(0.5, -0.5, 0)
    glVertex3f(0.5, -0.5, 1)

    glEnd()

# INDENTATION

# <--|

while True:
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                rotatey -= 1
            if event.key == pygame.K_RIGHT:
                rotatex += 1
            if event.key == pygame.K_UP:
                rotatey += 1
            if event.key == pygame.K_DOWN:
                rotatex -= 1

    if event.type == pygame.QUIT:
        pygame.quit()
        quit()

    # INDENTATION

# -->|

    glRotate(angle, rotatex, rotatey,rotatez)
    draw_cube()
    pygame.display.flip()
    pygame.time.wait(15)

相关问题