Drawing modes of OpenGL

Command-based
  • Immediate mode
  • Display lists
Array-based
  • Vertex array
  • Vertex buffer objects

1. Immediate mode

glBegin(GL_TRIANGLES)
glColor3f(0, 1, 1)
glVertex3f(0, 0, 0)
#...
glEnd()

● Very slow; for each function call:
– argument marshalling
– C function call
– glGetError handling
● ...millions of vertices in a scene...
● Resend even if unchanged

2. Display lists

● Record GL commands off-line:
list = glGenLists(1)
glNewList(list, GL_COMPILE)
glBegin(GL_TRIANGLES)
#...
glEnd()
glEndList()


● Playback when needed:
glCallList(list)

● ...or use a nested display list
nested_list = glGenLists(1)
glNewList(nested_list, GL_COMPILE)
for y in (10, 20, 30):
glLoadIdentity()
glTranslate3f(0, y, 0)
glCallList(list)
glEndList()
glCallList(nested_list)

3. Vertex arry

● Array of vertices
vertices = [(1.0, 1.0, 0.0), (0.0, 0.0, 1.0), ...]
varray = numpy.array(vertices, 'f'))


● Need for semantics
glVertexPointer(3, GL_FLOAT, 12, varray)
glDrawArrays(GL_TRIANGLES, 0, len(varray))


● Multiple objects:
multiple glDrawArrays
bigger array


● Vertices stored application side

4. Vertex buffer objects


● Closer to the metal
● HW impls store vertex array on GPU
vertices = [(1.0, 1.0, 0.0), (0.0, 0.0, 1.0), ...]
vb = vbo.VBO(numpy.array(vertices, 'f'))
● Draw on command (bind-to-use)
vb.bind()
try:
glVertexPointer(3, GL_FLOAT, 12, 0)
glDrawArrays(GL_TRIANGLES, 0, len(vb))
finally:
vb.unbind()
● Same API as VA


posted @ 2011-07-15 18:56  我的小屋子  阅读(137)  评论(0)    收藏  举报