I have a class that makes a sphere based on the passed in stack count, sector count and radius. The issue is, whenever I draw the sphere, my frame-rate drops from 57-60 fps to 20 fps.
This is how I draw my sphere:
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()
Does anyone know how I could speed that up?
Try to get rid of the nested loops. See Vertex Specification for a modern way of drawing meshes by the use of a Vertex Buffer Object and Vertex Array Object.
Another (deprecated) possibility is to use fixed function attributes.
OpenGL 4.6 API Compatibility Profile Specification; 10.3.3 Specifying Arrays for Fixed-Function Attributes; page 402
Create a list with the vertex attribute data in the constructor of the class:
Use the array to specify the vertices and to draw the mesh:
This is the first (small) step to a modern solution with VBO and VAO.
A further performance improvement can be gained by the use of a Vertex Buffer Object: