如何提高以下代码的性能?
How to improve the performance of the following code?
glBegin(GL_TRIANGLES)
for face in faces:
for vertex_i in face:
glVertex3f(*vertices[vertex_i])
glEnd()
有什么方法可以用更高效的 function/method 替换双 for 循环?
是的,您可以使用固定函数属性并定义一个顶点数据数组glVertexPointer
/ glEnableClientState
. In this case you can draw the triangles by glDrawElements
。
indices
必须是三角形索引数组和 vertexarray
以及坐标分量数组。例如:
初始化时:
vertexarray = [c for v in vertices for c in v]
indices = [i for f in faces for i in f]
每帧:
glVertexPointer(3, GL_FLOAT, 0, vertexarray)
glEnableClientState(GL_VERTEX_ARRAY)
glDrawElements(GL_TRIANGLES, len(indices), GL_UNSIGNED_INT, indices)
glDisableClientState(GL_VERTEX_ARRAY)
或者您可以使用 numpy.array
import numpy
vertexarray = numpy.array(vertices, dtype=numpy.float)
indices = numpy.array(faces, dtype=numpy.uint32)
glVertexPointer(3, GL_FLOAT, 0, vertexarray)
glEnableClientState(GL_VERTEX_ARRAY)
glDrawElements(GL_TRIANGLES, indices.size, GL_UNSIGNED_INT, indices)
glDisableClientState(GL_VERTEX_ARRAY)
glBegin(GL_TRIANGLES)
for face in faces:
for vertex_i in face:
glVertex3f(*vertices[vertex_i])
glEnd()
有什么方法可以用更高效的 function/method 替换双 for 循环?
是的,您可以使用固定函数属性并定义一个顶点数据数组glVertexPointer
/ glEnableClientState
. In this case you can draw the triangles by glDrawElements
。
indices
必须是三角形索引数组和 vertexarray
以及坐标分量数组。例如:
初始化时:
vertexarray = [c for v in vertices for c in v]
indices = [i for f in faces for i in f]
每帧:
glVertexPointer(3, GL_FLOAT, 0, vertexarray)
glEnableClientState(GL_VERTEX_ARRAY)
glDrawElements(GL_TRIANGLES, len(indices), GL_UNSIGNED_INT, indices)
glDisableClientState(GL_VERTEX_ARRAY)
或者您可以使用 numpy.array
import numpy
vertexarray = numpy.array(vertices, dtype=numpy.float)
indices = numpy.array(faces, dtype=numpy.uint32)
glVertexPointer(3, GL_FLOAT, 0, vertexarray)
glEnableClientState(GL_VERTEX_ARRAY)
glDrawElements(GL_TRIANGLES, indices.size, GL_UNSIGNED_INT, indices)
glDisableClientState(GL_VERTEX_ARRAY)