Python环境下用Pygame和OpenGL绘制立方体

Python环境下用Pygame和OpenGL绘制立方体,python,opengl,pygame,pyopengl,Python,Opengl,Pygame,Pyopengl,我用python在pygame窗口中绘制OpenGL多维数据集时得到了这段代码,但每次我试图编译它时,都会在多维数据集glVertex3f(顶点[vertex])类型中显示这条代码。错误:元组索引必须是整数或切片,而不是元组 任何人谁知道如何纠正代码,使我可以使程序实现其预期的功能 import pygame from pygame.locals import * from OpenGL.GL import * from OpenGL.GLU import * ##Define the vert

我用python在pygame窗口中绘制OpenGL多维数据集时得到了这段代码,但每次我试图编译它时,都会在多维数据集glVertex3f(顶点[vertex])类型中显示这条代码。错误:元组索引必须是整数或切片,而不是元组 任何人谁知道如何纠正代码,使我可以使程序实现其预期的功能

import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
##Define the vertices. usually a cube contains 8 vertices
vertices=( (1, -1, -1),
    (1, 1, -1),
    (-1, 1, -1),
    (-1, -1, -1),
    (1, -1, 1),
    (1, 1, 1),
    (-1, -1, 1),
    (-1, 1, 1))
##define 12 edges for the body
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)
    )
##define function to draw the cube
def Cube():
    glBegin(GL_LINES)
    for edge in edges:
        for vertex in vertices:
            glVertex3f(vertices[vertex])
    glEnd
##Define main function to draw a window for the openGL
def main():
    pygame.init()
    display=(600,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)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        Cube()
        pygame.display.flip()
        pygame.time.wait(10)


main()

我可能做错了什么请帮助。

vertex
是一个三分量元组,因此不能用于订阅
glVertex3f
需要3个参数。因此,您必须解压元组或使用OpenGL指令
glVertex3fv
而不是
glVertex3f
(请参阅):

glVertex3f(顶点[顶点])

glVertex3fv(顶点)

glVertex3f(*顶点)
此外,
glEnd
后面的括号缺失:

格伦德

格伦德()
但如果要绘制线框立方体,则需要为立方体的每条边绘制线段:

def Cube():
glBegin(GLU线)
对于边中的边:
对于边中的索引:
glVertex3fv(顶点[索引])
格伦德()

错误消息中已很好地解释了该问题。您正试图用元组
顶点[(1,-1,-1)]
索引列表,这显然是行不通的


假设您试图在每个元组上调用
glVertex3f
,请将指定的行替换为
glVertex3f(顶点)
,这将导致它在顶点列表中的每个元组上运行。

注意到,感谢您的帮助没有注意到,感谢您指出这超出了确切的输出,另外,我的帖子中的
顶点也有错误的代码