Drawing primitives

The pyglet.graphics module draws the OpenGL primitive objects by a mode denoted by the constants

See the OpenGL Programming Guide for a description of each of mode.

Each primitive is made up of one or more vertices. Each vertex is specified with either 2, 3 or 4 components (for 2D, 3D, or non-homogeneous coordinates). The data type of each component can be either int or float.

Use pyglet.graphics.draw to draw a primitive. The following example draws two points at coordinates (10, 15) and (30, 35):

pyglet.graphics.draw(2, pyglet.gl.GL_POINTS,
    ('v2i', (10, 15, 30, 35))
)

The first and second arguments to the function give the number of vertices to draw and the primitive mode, respectively. The third argument is a "data item", and gives the actual vertex data.

Because vertex data can be supplied in several forms, a "format string" is required. In this case, the format string is "v2i", meaning the vertex position data has two components (2D) and int type.

The following example has the same effect as the previous one, but uses floating point data and 3 components per vertex:

pyglet.graphics.draw(2, pyglet.gl.GL_POINTS,
    ('v3f', (10.0, 15.0, 0.0, 30.0, 35.0, 0.0))
)

Vertices can also be drawn out of order and more than once by using the pyglet.graphics.draw_indexed function. This requires a list of integers giving the indices into the vertex data. The following example draws the same two points as above, but indexes the vertices (sequentially):

pyglet.graphics.draw_indexed(2, pyglet.gl.GL_POINTS,
    [0, 1, 2, 3],
    ('v2i', (10, 15, 30, 35))
)

This second example is more typical; two adjacent triangles are drawn, and the shared vertices are reused with indexing:

pyglet.graphics.draw_indexed(4, pyglet.gl.GL_TRIANGLES,
    [0, 1, 2, 0, 2, 3],
    ('v2i', (100, 100,
             150, 100,
             150, 150,
             100, 150))
)

Note that the first argument gives the number of vertices in the data, not the number of indices (which is implicit on the length of the index list given in the third argument).