OpenGL:球体减慢帧速率

2024-05-19 02:27:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个类,它根据传入的堆栈计数、扇区计数和半径生成一个球体。问题是,每当我绘制球体时,我的帧速率从57-60 fps下降到20 fps

这是我绘制球体的方式:

    def draw_edges(self):
        """Draws the sphere's edges"""
        glPushMatrix()
        glTranslate(self.position[0], self.position[1], self.position[2])
        glRotate(self.rotation[3],self.rotation[0],self.rotation[1],self.rotation[2])
        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
        glBegin(GL_TRIANGLES)
        for edge in self.edges:
            for vertex in edge:
                glVertex3fv(self.vertices[vertex])
        glEnd()
        glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
        glPopMatrix()

有人知道我怎样才能加快速度吗


Tags: andselfforback绘制position计数front
1条回答
网友
1楼 · 发布于 2024-05-19 02:27:01

尝试摆脱嵌套循环。有关使用Vertex Buffer ObjectVertex Array Object绘制网格的现代方法,请参见Vertex Specification


另一种(不推荐使用的)可能性是使用固定的函数属性

OpenGL 4.6 API Compatibility Profile Specification; 10.3.3 Specifying Arrays for Fixed-Function Attributes; page 402

The commands

void VertexPointer( int size, enum type, sizei stride, const void *pointer );
void NormalPointer( enum type, sizei stride, const void *pointer );
void ColorPointer( int size, enum type, sizei stride, const void *pointer );
[...]

specify the location and organization of arrays to store vertex coordinates, normals, colors, [...] An individual array is enabled or disabled by calling one of

void EnableClientState( enum array );
void DisableClientState( enum array );

with array set to VERTEX_ARRAY, NORMAL_ARRAY, COLOR_ARRAY, [...], for the vertex, normal, color, [...] array, respectively.


使用类的构造函数中的顶点属性数据创建列表:

def __init__(self):
    # [...]
   
    self.vertexArray = []
    for edge in self.edges:
        for vertex in edge:
            self.vertexArray.append(self.vertices[vertex])

使用阵列指定顶点并绘制网格:

def draw_edges(self):
    """Draws the sphere's edges"""
    glPushMatrix()
    glTranslate(self.position[0], self.position[1], self.position[2])
    glRotate(self.rotation[3],self.rotation[0],self.rotation[1],self.rotation[2])
    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
    
    glEnableClientState(GL_VERTEX_ARRAY)
    glVertexPointer(3, GL_FLOAT, 0, self.vertexArray)
    glDrawArrays(GL_TRIANGLES, 0, len(self.vertexArray))
    glDisableClientState(GL_VERTEX_ARRAY)
    
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
    glPopMatrix()

这是使用VBO和VAO实现现代解决方案的第一步(很小)


通过使用Vertex Buffer Object可以进一步提高性能:

def __init__(self):
    # [...]

    self.vertexArray = []
    for edge in self.edges:
        for vertex in edge:
            self.vertexArray += self.verticies[vertex] # < - flat list

    array = (GLfloat * len(self.vertexArray))(*self.vertexArray)
    self.vbo = glGenBuffers(1)        
    glBindBuffer(GL_ARRAY_BUFFER, self.vbo)
    glBufferData(GL_ARRAY_BUFFER, array, GL_STATIC_DRAW)
    glBindBuffer(GL_ARRAY_BUFFER, 0)
def draw_edges(self):
    # [...]

    glBindBuffer(GL_ARRAY_BUFFER, self.vbo)
    glVertexPointer(3, GL_FLOAT, 0, None)
    glBindBuffer(GL_ARRAY_BUFFER, 0)
    
    glEnableClientState(GL_VERTEX_ARRAY)
    glDrawArrays(GL_TRIANGLES, 0, len(self.vertexArray) // 3)
    glDisableClientState(GL_VERTEX_ARRAY)

    # [...]

相关问题 更多 >

    热门问题