【问题标题】:Pygame OpenGL 3D Cube Lagpyopengl中圆形物体的速度问题[重复]
【发布时间】:2019-06-04 11:24:25
【问题描述】:

我最近使用 pygame 和 pyopengl 在 python 3 中构建了一个 obj 文件加载器。它工作得很好,但是当我加载一个圆形物体时,它开始运行得很慢。我想以不错的帧速率加载多个复杂对象,但这种方法不适合。你有什么建议可以更有效地编写代码,用 pygame/pyopengl 换取不同的东西,或者(如果上述方法都不起作用)也许对对象进行下采样? (重要部分代码如下)

# verticies is a list of all verticies
# surfaces holds tuples of indexes of verticies

def body():

    glBegin(GL_TRIANGLES)
    for surface in surfaces:
        for vertex in surface:
            glVertex3fv(vertices[vertex])
    glEnd()

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

while True:
    body()

【问题讨论】:

标签: python python-3.x 3d pygame pyopengl


【解决方案1】:

改进的第一步是使用固定的功能属性。初始化时,首先创建一个带有顶点属性的数组:

vertex_list = []
for surface in surfaces:
    for vertex in surface:
        vertex_list += vertices[vertex]

vertex_array = (GLfloat * len(vertex_array))(*vertex_array)
no_of_vertices = len(vertex_list) // 3

每次绘制几何图形时都使用顶点数组。使用glVertexPointer 定义顶点数据数组。使用glEnableClientState/glDisableClientState 启用(或禁用)客户端功能。用glDrawArrays绘制几何图形:

def body():

     glVertexPointer(3, GL_FLOAT, 0, vertex_array) 
     glEnableClientState(GL_VERTEX_ARRAY) 
     glDrawArrays(GL_TRIANGLES, 0, no_of_vertices)
     glDisableClientState(GL_VERTEX_ARRAY) 

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

while True:
    body()

这可以通过Vertex BuffersVertex Array Objects 进一步改进。
Pygame OpenGL 3D Cube Lag

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 2011-03-09
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多