pyglet.graphics.draw
- 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.
- __init__(initial_count: int = 32) None
Initialize a graphics batch.
- Parameters:
initial_count (
int) – The initial vertex count of the buffers created by this batch.
- delete_empty_domains() None
Deletes all empty domains and all of their buffers.
Should not need to be called through normal usage, as this will occur periodically.
- Return type:
- draw_subset(
- vertex_lists: Sequence[VertexList | IndexedVertexList],
Draw only some vertex lists in the batch.
The use of this method is highly discouraged, as it is quite inefficient. Usually an application can be redesigned so that batches can always be drawn in their entirety, using draw.
The given vertex lists must belong to this batch; behaviour is undefined if this condition is not met.
- Parameters:
vertex_lists (
Sequence[VertexList|IndexedVertexList]) – Vertex lists to draw.- Return type:
- get_domain( ) VertexDomain
Get, or create, the vertex domain corresponding to the given arguments.
mode is the render mode such as GL_LINES or GL_TRIANGLES
- Return type:
- invalidate() None
Force the batch to update the draw list.
This method can be used to force the batch to re-compute the draw list when the ordering of groups has changed.
Added in version 1.2.
- Return type:
- migrate(
- vertex_list: VertexList | IndexedVertexList,
- mode: GeometryMode,
- group: Group,
- batch: Batch,
Migrate a vertex list to another batch and/or group.
vertex_list and mode together identify the vertex list to migrate. group and batch are new owners of the vertex list after migration.
The results are undefined if mode is not correct or if vertex_list does not belong to this batch (they are not checked and will not necessarily throw an exception immediately).
batchcan remain unchanged if only a group change is desired.- Parameters:
vertex_list (
VertexList|IndexedVertexList) – A vertex list currently belonging to this batch.mode (
GeometryMode) – The current GL drawing mode of the vertex list.group (
Group) – The new group to migrate to.batch (
Batch) – The batch to migrate to (or the current batch).
- Return type:
- update_shader(
- vertex_list: VertexList | IndexedVertexList,
- mode: GeometryMode,
- group: Group,
- program: ShaderProgram,
Migrate a vertex list to another domain that has the specified shader attributes.
The results are undefined if mode is not correct or if vertex_list does not belong to this batch (they are not checked and will not necessarily throw an exception immediately).
- Parameters:
vertex_list (
VertexList|IndexedVertexList) – A vertex list currently belonging to this batch.mode (
GeometryMode) – The current GL drawing mode of the vertex list.group (
Group) – The new group to migrate to.program (
ShaderProgram) – The new shader program to migrate to.
- Return type:
- Returns:
False if the domain’s no longer match. The caller should handle this scenario.
-
group_map:
dict[Group,dict[_DomainKey,VertexDomain]]
- class Group
Group of common state.
Groupprovides extra control over how drawables are handled within aBatch. 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()
- add_comparison(value)
- set_blend(
- blend_src: BlendFactor,
- blend_dst: BlendFactor,
- blend_op: BlendOp = BlendOp.ADD,
- set_shader_program(program: ShaderProgram)
- set_shader_uniforms(
- program: ShaderProgram,
- uniforms: dict[str, Any],
- set_state_recursive(ctx: SurfaceContext) None
Set this group and its ancestry.
Call this method if you are using a group in isolation: the parent groups will be called in top-down order, with this class’s
setbeing called last.- Return type:
- set_texture( ) None
Set the texture state.
- Parameters:
texture (
GLTexture) – The Texture instance that this draw call uses.texture_unit (
int) – The binding unit this Texture/Sampler is bound to. In OpenGL this is the Active Texture (glActiveTexture). In Vulkan this is the Sampler binding number in the descriptor.set_id (
int) – The set that the sampler belongs to. Only applicable in Vulkan.
- Return type:
- set_viewport(x, y, width, height)
- unset_state_all(ctx: SurfaceContext) None
Calls all unset states of the underlying Group.
- Return type:
- unset_state_recursive(ctx: SurfaceContext) None
Unset this group and its ancestry.
The inverse of
set_state_recursive.- Return type: