Data item parameters
As final parameters, several of the procedures and methods in this module accept any number of data parameters. In the formal parameter list, these are denoted as *data in the documentation.
A data argument specifies the format of a vertex attribute and an optional sequence for initialising it. The following are some examples of typical attribute formats:
- "v3f" - Three floats specify the vertex position.
- " c4B " - Four unsigned bytes specify the vertex colour.
- " t2f " - Two floats specify texture coordinates.
The data item is simply the format string when no starting data is provided. The following code, for example, generates a two-element vertex list with position and colour attributes:
vertex_list = pyglet.graphics.vertex_list(2, 'v2f', 'c4B')

You can also try this code with Online Python Compiler
Run Code
Wrap the format string and the starting data in a tuple when initial data is required, for example:
vertex_list = pyglet.graphics.vertex_list(2,'v2f', (0.0, 1.0, 1.0, 0.0)),('c4B', (255, 255, 255, 255) * 2))

You can also try this code with Online Python Compiler
Run Code
Drawing modes
Any value as a parameter in the OpenGL drawing mode enumeration will be accepted by methods in the module that accept a mode parameter.GL_POINTS, GL_LINE_LOOP, GL_LINES, GL_TRIANGLE_STRIP, GL_LINE_STRIP, GL_TRIANGLE_FAN, GL_QUAD_STRIP, GL_TRIANGLES, GL_QUADS, and GL_POLYGON.
pyglet.graphics.draw(1, GL_POINTS, ('v2i',(10,20)))

You can also try this code with Online Python Compiler
Run Code
GL_LINE_LOOP, GL_POLYGON, and GL_TRIANGLE_FAN, on the other hand, cannot be utilised because of the way the graphics API produces multiple primitives with shared state – the results are undefined.
When using GL_TRIANGLE_STRIP, GL_LINE_STRIP, or GL_QUAD_STRIP, it's important to avoid putting degenerate vertices at the start and end of each vertex list. Given the vertex list, for example:
A, B, C, D

You can also try this code with Online Python Compiler
Run Code
The following is the correct vertex list to provide:
A, A, B, C, D, D

You can also try this code with Online Python Compiler
Run Code
If the NV_primitive_restart extension is provided, it can be used instead. GL_LINE_LOOP, GL_POLYGON, and GL_TRIANGLE_FAN can now be used. Unfortunately, older video drivers do not support the extension, which requires indexed vertex lists.
1. classBatch
For batch rendering, manage a collection of vertex lists. The add and add indexed methods are used to add vertex lists to a Batch. Along with the vertex list, an optional group can be given, which specifies the OpenGL state necessary for rendering. In a single operation, shared mode and group vertex lists are allocated into adjacent memory locations and delivered to the graphics hardware.
In order to remove a vertex list from a batch, use VertexList.delete.
add(count, mode, group, *data)
It is used to add a vertex list to the batch.
Parameters:
- count (int) – The number of vertices present in the list.
- mode (int) – It is the OpenGL drawing mode enumeration; for example, one of GL_LINES, GL_POINTS, GL_TRIANGLES, etc.
- group (Group) – It represents the group of the vertex list, or None if there is no group required.
- data (data items) – It contains the attribute formats and initial data for the vertex list.
Return type:
It returns the VertexList.
add_indexed(count, mode, group, indices, *data)
It is used to add an indexed vertex list.
Parameters:
- count (int) – The number of vertices present in the list.
- mode (int) – It is the OpenGL drawing mode enumeration; for example, one of GL_LINES, GL_POINTS, GL_TRIANGLES, etc.
- group (Group) – It represents the group of the vertex list, or None if there is no group required.
- data (data items) – It contains the attribute formats and initial data for the vertex list.
Return type:
IndexedVertexList
draw()
It is used to draw the batch.
draw_subset(vertex_lists)
It's used to draw only some of the batch's vertex lists.
This method is extremely inefficient. Hence it is strongly discouraged. Typically, an application can be redesigned so that batches can be drawn in their entirety using draw at any time.
The specified vertex lists must be part of this batch; otherwise, the behavior is undefined.
Parameters:
- vertex_lists (IndexedVertexList) – Vertex lists to draw.
invalidate()
The batch is forced to update the draw list. When the order of groups in the batch has changed, this method can be used to compel the batch to re-compute the draw list.
migrate(vertex_list, mode, group, batch)
Transfer a vertex list to a new batch or group. The vertex list to migrate is identified by the combination of vertex list and mode. After migration, group and batch are the new owners of the vertex list. If the mode is incorrect or the vertex_list does not belong to this batch, the results are undefined (they are not checked and will not necessarily throw an exception immediately). If only a group modification is required, the batch can be left intact.
Parameters:
- vertex_list (VertexList) – It contains a vertex list which is currently belonging to this batch.
- mode (int) – It depicts the current GL drawing mode of the vertex list.
- group (Group) – This represents the new group to migrate.
- batch (Batch) – It consists of the batch to migrate to (or the current batch).
2. classGroup(parent=None)
A collection of common OpenGL states. The OpenGL state of a vertex list's group, as well as the states of its ancestors, is set before it is rendered. The default state modification has no effect and just organises vertex lists in the order in which they are drawn.
set_state()
It is used to change the state of OpenGL. The default implementation is completely useless.
set_state_recursive()
It is used to set the ancestry of this group. If you're utilising a group in isolation, call this method: the parent groups will be called in order, with this class's set coming last.
unset_state()
It is used to remove the OpenGL state modification. The default implementation is completely useless.
unset_state_recursive()
It is used to remove this group's ancestors. It is the inverse of set_state_recursive.
batches
visible
3. classNullGroup(parent=None)
When a batch is supplied None, this is the default group class.
This implementation has no impact.
4. classOrderedGroup(order, parent=None)
It is a partially ordered group. Ordered groups with a shared parent are rendered in the order field's ascending order. This is a handy method for rendering several levels of a scene in a single batch.
5. classTextureGroup(texture, parent=None)
A texture enabling and binding group. Texture groups are equal if the targets and names of their textures are the same.
set_state()
It is used to change the state of OpenGL. The default implementation is completely useless.
unset_state()
It is used to remove the OpenGL state modification. The default implementation is completely useless.
6. draw(size, mode, *data)
It is used to draw a primitive immediately.
Parameters:
- size (int) – It represents the number of vertices given
- mode (gl primitive type) – It is the OpenGL drawing mode, for example, GL_TRIANGLES.
- data (data items) – It consists of the attribute formats and data.
7. draw_indexed(size, mode, indices, *data)
Immediately draw a primitive with indexed vertices.
Parameters:
- size (int) – It represents the number of vertices given
- mode (gl primitive type) – It is the OpenGL drawing mode, for example, GL_TRIANGLES.
- indices (sequence of int) – It is the sequence of the integers giving indices into the vertex list.
- data (data items) – It consists of the attribute formats and data.
8. vertex_list(count, *data)
It is used to make a VertexList that isn't linked to a batch, group, or mode.
Parameters:
- count (int) – It consists of the number of vertices in the list.
- data (data items) – It consists of the attribute formats and data.
Return type:
VertexList
vertex_list_indexed(count, indices, *data)
It is used to make an IndexedVertexList that isn't tied to a batch, group, or mode.
Parameters:
- count (int) – It consists of the number of vertices in the list.
- indices (sequence of int) – It is the sequence of the integers giving indices into the vertex list.
- data (data items) – It consists of the attribute formats and data.
Return type:
IndexedVertexList
9. null_group= <pyglet.graphics.NullGroup object>
It is the default group.
Type: Group
Frequently Asked Questions
What is a pyglet?
Pyglet is a Python library that provides an object-oriented application programming interface for the development of games and other multimedia applications.
What are batch and group objects?
Batch and Group objects are used to boost the performance of sprite and text rendering without having to grasp the nuances of how to draw primitives with the graphics API.
What is the use of classBatch?
It is used for batch rendering and managing a collection of vertex lists.
Conclusion
In this article, we have extensively discussed pyglet.graphics. The article explains the details of pyglet.graphics, and details of its different class objects.
We hope that this blog has helped you enhance your knowledge regarding pyglet.graphics and if you would like to learn more, check out our articles on pyglet. You can refer to our guided paths on the Coding Ninjas Studio platform to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc. To practice and improve yourself in the interview, you can also check out Top 100 SQL problems, Interview experience, Coding interview questions, and the Ultimate guide path for interviews.
Do upvote our blog to help other ninjas grow.
Happy Coding!!