Low-level graphics rendering.
This module provides an efficient low-level abstraction over OpenGL. It gives very good performance for rendering OpenGL primitives; far better than the typical immediate-mode usage and, on modern graphics cards, better than using display lists in many cases. The module is used internally by other areas of pyglet.
See the Programming Guide for details on how to use this graphics API.
Without even needing to understand the details on how to draw primitives with the graphics API, developers can make use of Batch and Group objects to improve performance of sprite and text rendering.
The Sprite, Label and TextLayout classes all accept a
group parameter in their constructors. A batch manages a set of objects
that will be drawn all at once, and a group describes the manner in which an
object is drawn.
The following example creates a batch, adds two sprites to the batch, and then draws the entire batch:
batch = pyglet.graphics.Batch() car = pyglet.sprite.Sprite(car_image, batch=batch) boat = pyglet.sprite.Sprite(boat_image, batch=batch) def on_draw() batch.draw()
Drawing a complete batch is much faster than drawing the items in the batch individually, especially when those items belong to a common group.
Groups describe the OpenGL state required for an item. This is for the most part managed by the sprite and text classes, however you can also use groups to ensure items are drawn in a particular order. For example, the following example adds a background sprite which is guaranteed to be drawn before the car and the boat:
batch = pyglet.graphics.Batch() background = pyglet.graphics.OrderedGroup(0) foreground = pyglet.graphics.OrderedGroup(1) background = pyglet.sprite.Sprite(background_image, batch=batch, group=background) car = pyglet.sprite.Sprite(car_image, batch=batch, group=foreground) boat = pyglet.sprite.Sprite(boat_image, batch=batch, group=foreground) def on_draw() batch.draw()
It’s preferable to manage sprites and text objects within as few batches as possible. If the drawing of sprites or text objects need to be interleaved with other drawing that does not use the graphics API, multiple batches will be required.
Many of the functions and methods in this module accept any number of
parameters as their final parameters. In the documentation these are notated
*data in the formal parameter list.
A data parameter describes a vertex attribute format and an optional sequence to initialise that attribute. Examples of common attribute formats are:
See pyglet.graphics.vertexattribute for the complete syntax of the vertex format string.
When no initial data is to be given, the data item is just the format string. For example, the following creates a 2 element vertex list with position and color attributes:
vertex_list = pyglet.graphics.vertex_list(2, 'v2f', 'c4B')
When initial data is required, wrap the format string and the initial data in a tuple, for example:
vertex_list = pyglet.graphics.vertex_list(2, ('v2f', (0.0, 1.0, 1.0, 0.0)), ('c4B', (255, 255, 255, 255) * 2))
Methods in this module that accept a
mode parameter will accept any value
in the OpenGL drawing mode enumeration:
pyglet.graphics.draw(1, GL_POINTS, ('v2i',(10,20)))
However, because of the way the graphics API renders multiple primitives with
be used — the results are undefined.
must be taken to insert degenerate vertices at the beginning and end of each
vertex list. For example, given the vertex list:
A, B, C, D
the correct vertex list to provide the vertex list is:
A, A, B, C, D, D
NV_primitive_restart extension can be used if it is
present. This also permits use of
GL_TRIANGLE_FAN. Unfortunately the extension is not provided by older
video drivers, and requires indexed vertex lists.
Since pyglet 1.1
||Memory allocation algorithm for vertex arrays and buffers.|
||Access byte arrays as arrays of vertex attributes.|
||Byte abstractions of Vertex Buffer Objects and vertex arrays.|
||Manage related vertex attributes within a single vertex domain.|
||Manage a collection of vertex lists for batched rendering.|
||Group of common OpenGL state.|
||The default group class used when
||A group with partial order.|
||A group that enables and binds a texture.|
||Draw a primitive immediately.|
||Draw a primitive with indexed vertices immediately.|
||Create a VertexList not associated with a batch, group or mode.|
||Create an IndexedVertexList not associated with a batch, group or mode.|
str(object=’‘) -> string
Return a nice string representation of the object. If the argument is a string, the return value is the same object.
null_group= <pyglet.graphics.NullGroup object>¶
The default group.