pyglet.graphics

Submodules

Details

Low-level graphics rendering and abstractions.

This module provides efficient abstractions over OpenGL objects, such as Shaders and Buffers. It also provides classes for highly performant batched rendering and grouping.

See the Shaders and Rendering for details on how to use this graphics API.

class Batch

Manage a collection of drawables for batched rendering.

Many drawable pyglet objects accept an optional Batch argument in their constructors. By giving a Batch to multiple objects, you can tell pyglet that you expect to draw all of these objects at once, so it can optimise its use of OpenGL. Hence, drawing a Batch is often much faster than drawing each contained drawable separately.

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()

While any drawables can be added to a Batch, only those with the same draw mode, shader program, and group can be optimised together.

Internally, a Batch manages a set of VertexDomains along with information about how the domains are to be drawn. To implement batching on a custom drawable, get your vertex domains from the given batch instead of setting them up yourself.

group_children: dict[Group, list[Group]]
group_map: dict[Group, dict[Tuple[bool, int, int, str], VertexDomain]]
top_groups: list[Group]
class Group

Group of common OpenGL state.

Group provides extra control over how drawables are handled within a Batch. When a batch draws a drawable, it ensures its group’s state is set; this can include binding textures, shaders, or setting any other parameters. It also sorts the groups before drawing.

In the following example, the background sprite is guaranteed to be drawn before the car and the boat:

batch = pyglet.graphics.Batch()
background = pyglet.graphics.Group(order=0)
foreground = pyglet.graphics.Group(order=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()
property batches: tuple[Batch, ...]

Which graphics Batches this Group is a part of.

Read Only.

property order: int

Rendering order of this group compared to others.

Lower numbers are drawn first.

property visible: bool

Visibility of the group in the rendering pipeline.

Determines whether this Group is visible in any of the Batches it is assigned to. If False, objects in this Group will not be rendered.

class ShaderGroup

A group that enables and binds a ShaderProgram.

class TextureGroup

A group that enables and binds a texture.

TextureGroups are equal if their textures’ targets and names are equal.